ShareController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. namespace app\wxshare\controller;
  3. use cmf\controller\HomeBaseController;
  4. use think\facade\Db;
  5. if (!session_status()) {
  6. session_start();
  7. }
  8. class ShareController extends HomebaseController {
  9. public function index(){
  10. $list=Db::name('user_live')->field("uid,title,city,stream,pull,thumb")->where(["islive"=>1])->order("starttime desc")->limit(0,20)->select()->toArray();
  11. foreach($list as $k=>$v){
  12. $userinfo=getUserInfo($v['uid']);
  13. $v['avatar']=$userinfo['avatar'];
  14. $v['avatar_thumb']=$userinfo['avatar_thumb'];
  15. $v['user_nickname']=$userinfo['user_nickname'];
  16. $v['thumb']=get_upload_path($v['thumb']);
  17. if(!$v['thumb']){
  18. $v['thumb']=$v['avatar'];
  19. }
  20. $list[$k]=$v;
  21. }
  22. $this->assign('list',$list);
  23. /* session('uid',null);
  24. session('token',null);
  25. session('openid',null);
  26. session('unionid',null);
  27. session('userinfo',null); */
  28. return $this->fetch();
  29. }
  30. public function show(){
  31. $roomnum = $this->request->param('roomnum', 0, 'intval');
  32. $liveinfo=array();
  33. $configpri=getConfigPri();
  34. $this->assign('configpri',$configpri);
  35. $config=getConfigPub();
  36. $this->assign('config',$config);
  37. $anchor=getUserInfo($roomnum);
  38. $liveinfo=Db::name("user_live")->field("uid,islive,stream,pull,isvideo")->where(['uid'=>$roomnum,'islive'=>1])->find();
  39. if(!$liveinfo){
  40. $liveinfo['uid']=$roomnum;
  41. $liveinfo['type']=0;
  42. $liveinfo['islive']='0';
  43. $liveinfo['pull']='';
  44. $liveinfo['isvideo']=1;
  45. $liveinfo['stream']='';
  46. }
  47. if($liveinfo['islive']==0){
  48. $liveinfo['type']=0;
  49. $liveinfo['pull']='';
  50. }
  51. $liveinfo['user_nickname']=$anchor['user_nickname'];
  52. $liveinfo['avatar']=$anchor['avatar'];
  53. $liveinfo['avatar_thumb']=$anchor['avatar_thumb'];
  54. if($liveinfo['isvideo']==1){
  55. $hls=$liveinfo['pull'] ;
  56. }else{
  57. $hls='';
  58. if($liveinfo['islive']){
  59. if($configpri['cdn_switch']==5){
  60. $hls=$liveinfo['pull'] ;
  61. }else{
  62. $hls=PrivateKeyA('http',$liveinfo['stream'].'.m3u8',0);
  63. }
  64. }
  65. }
  66. $this->assign('hls',$hls);
  67. $this->assign('liveinfo',$liveinfo);
  68. $isattention=0;
  69. // session("uid",'18576');
  70. // session("token",'06adca42219ef55f6d2e705603168720');
  71. $uid=(int)session("uid");
  72. //判断用户是否存在
  73. $isexist=checkUser([['id','=',$uid],['user_type','=','2']]);
  74. if(!$isexist){
  75. session('uid',null);
  76. $uid=0;
  77. }
  78. if($uid==$anchor['id']){
  79. $this->assign('reason','不能进入自己的直播间');
  80. return $this->fetch('error');
  81. }
  82. $userinfo=[];
  83. //$uid=18576;
  84. if($uid){
  85. $res=$this->checkShut($uid,$anchor['id']);
  86. if($res==0){
  87. $this->assign('reason','您已被踢出房间');
  88. return $this->fetch('error');
  89. }
  90. $userinfo=getUserInfo($uid);
  91. $isexist=Db::name('user_attention')->where(['uid'=>$uid,'touid'=>$liveinfo['uid']])->find();
  92. if($isexist){
  93. $isattention=1;
  94. }
  95. }
  96. $this->assign('isattention',$isattention);
  97. $this->assign('userinfo',$userinfo);
  98. if($userinfo){
  99. $this->assign('userinfoj',json_encode($userinfo));
  100. }else{
  101. $this->assign('userinfoj','null');
  102. }
  103. $sensitive_words=str_replace(array("\r\n", "\r", "\n"), "", $configpri['sensitive_words']);
  104. $words_a=explode(',',$sensitive_words);
  105. $this->assign("words_j",json_encode($words_a) );
  106. return $this->fetch();
  107. }
  108. public function wxLogin(){
  109. $roomnum = $this->request->param('roomnum', 0, 'intval');
  110. $configpri=getConfigPri();
  111. $AppID = $configpri['login_wx_appid'];
  112. $callback = get_upload_path('/wxshare/Share/wxLoginCallback?roomnum='.$roomnum); //回调地址
  113. //微信登录
  114. if (!session_id()){
  115. session_start();
  116. }
  117. //-------生成唯一随机串防CSRF攻击
  118. $state = md5(uniqid(rand(), TRUE));
  119. $_SESSION["wx_state"] = $state; //存到SESSION
  120. $callback = urlencode($callback);
  121. //snsapi_base 静默 snsapi_userinfo 授权
  122. $wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$AppID}&redirect_uri={$callback}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect ";
  123. header("Location: $wxurl");
  124. }
  125. public function wxLoginCallback(){
  126. $roomnum = $this->request->param('roomnum', 0, 'intval');
  127. $code = $this->request->param('code');
  128. if($code){
  129. $configpri=getConfigPri();
  130. $AppID = $configpri['login_wx_appid'];
  131. $AppSecret = $configpri['login_wx_appsecret'];
  132. /* 获取token */
  133. $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid={$AppID}&secret={$AppSecret}&code={$code}&grant_type=authorization_code";
  134. $ch = curl_init();
  135. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  136. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  137. curl_setopt($ch, CURLOPT_URL, $url);
  138. $json = curl_exec($ch);
  139. curl_close($ch);
  140. $arr=json_decode($json,1);
  141. if(isset($arr['errcode'])){
  142. $this->assign('reason',$arr['errmsg']);
  143. return $this->fetch('error');
  144. }
  145. /* 刷新token 有效期为30天 */
  146. $url="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$AppID}&grant_type=refresh_token&refresh_token={$arr['refresh_token']}";
  147. $ch = curl_init();
  148. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  149. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  150. curl_setopt($ch, CURLOPT_URL, $url);
  151. $json = curl_exec($ch);
  152. curl_close($ch);
  153. $url="https://api.weixin.qq.com/sns/userinfo?access_token={$arr['access_token']}&openid={$arr['openid']}&lang=zh_CN";
  154. $ch = curl_init();
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  156. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  157. curl_setopt($ch, CURLOPT_URL, $url);
  158. $json = curl_exec($ch);
  159. curl_close($ch);
  160. $wxuser=json_decode($json,1);
  161. /* 公众号绑定到 开放平台 才有 unionid 否则 用 openid */
  162. $openid=$wxuser['unionid'];
  163. if(!$openid){
  164. $this->assign('reason','公众号未绑定到开放平台');
  165. return $this->fetch('error');
  166. }
  167. $type='wx';
  168. $userinfo=DB::name("user")->field("id,user_nickname,avatar,avatar_thumb,sex,signature,coin,consumption,votestotal,province,city,birthday,user_status,login_type,last_login_time")->where("openid!='' and openid='{$openid}' and login_type='{$type}'")->find();
  169. $nowtime=time();
  170. if(empty($userinfo)){
  171. if($openid==""){
  172. $this->assign('reason','登录错误');
  173. return $this->fetch('error');
  174. }
  175. $user_login=$type.'_'.time().rand(100,999);
  176. $user_pass=cmf_password('123456');
  177. $reg_reward=$configpri['reg_reward'];
  178. $code = $this->createCode();
  179. $data=array(
  180. 'openid' =>$openid,
  181. 'user_login' => $user_login,
  182. 'user_pass' =>$user_pass,
  183. 'user_nickname' => $wxuser['nickname'],
  184. 'sex' => $wxuser['sex'],
  185. 'avatar' => $wxuser['headimgurl'],
  186. 'avatar_thumb' => $wxuser['headimgurl'],
  187. 'login_type' => $type,
  188. 'last_login_ip' =>get_client_ip(0,true),
  189. 'create_time' => $nowtime,
  190. 'last_login_time' => $nowtime,
  191. 'coin' => $reg_reward,
  192. 'user_status' => 1,
  193. "user_type" =>2,//会员
  194. 'signature' =>'这家伙很懒,什么都没留下',
  195. 'code' =>$code,
  196. 'source' =>'h5'
  197. );
  198. $userid=DB::name("user")->insertGetId($data);
  199. $userinfo=DB::name("user")->field("id,user_nickname,avatar,avatar_thumb,sex,signature,coin,consumption,votestotal,province,city,birthday,user_status,login_type,last_login_time")->where(['id'=>$userid])->find();
  200. }
  201. if(!$userinfo){
  202. $this->assign('reason','登录错误');
  203. return $this->fetch('error');
  204. }
  205. $token=md5(md5($userinfo['id'].time()));
  206. $expiretime=time()+60*60*24*300;
  207. $isok=DB::name("user_token")
  208. ->where("user_id={$userinfo['id']}")
  209. ->update(array("token"=>$token, "expire_time"=>$expiretime , "create_time"=>$nowtime ));
  210. if(!$isok){
  211. DB::name("user_token")
  212. ->insert(array("user_id"=>$userinfo['id'],"token"=>$token, "expire_time"=>$expiretime , "create_time"=>$nowtime ));
  213. }
  214. if($userinfo['birthday']){
  215. $userinfo['birthday']=date('Y-m-d',$userinfo['birthday']);
  216. }else{
  217. $userinfo['birthday']='';
  218. }
  219. $userinfo['token']=$token;
  220. delcache("token_".$userinfo['id']);
  221. session('uid',$userinfo['id']);
  222. session('token',$userinfo['token']);
  223. session('openid',$wxuser['openid']);
  224. session('unionid',$wxuser['unionid']);
  225. session('userinfo',$userinfo);
  226. $href='/wxshare/Share/show?roomnum='.$roomnum;
  227. header("Location: $href");
  228. }else{
  229. }
  230. }
  231. /* 手机验证码 */
  232. public function getCode(){
  233. $config=getConfigPri();
  234. $mobile = $this->request->param('mobile');
  235. /* $where="user_login='{$mobile}'";
  236. $checkuser = checkUser($where);
  237. if($checkuser){
  238. $rs['errno']=1006;
  239. $rs['errmsg']='该手机号已注册,请登录';
  240. echo json_encode($rs);
  241. return;
  242. } */
  243. if(isset($_SESSION['mobile']) && $_SESSION['mobile']==$mobile && isset($_SESSION['mobile_expiretime']) && $_SESSION['mobile_expiretime']> time() ){
  244. $rs['errno']=1007;
  245. $rs['errmsg']='验证码5分钟有效,勿多发';
  246. echo json_encode($rs);
  247. return;
  248. }
  249. $limit = ip_limit();
  250. if( $limit == 1){
  251. $rs['errno']=1003;
  252. $rs['errmsg']='您已当日发送次数过多';
  253. echo json_encode($rs);
  254. return;
  255. }
  256. $mobile_code = random(6,1);
  257. $result = sendCode($mobile,$mobile_code);
  258. if($result['code']==0){
  259. $_SESSION['mobile'] = $mobile;
  260. $_SESSION['mobile_code'] = $mobile_code;
  261. $_SESSION['mobile_expiretime'] = time() +60*5;
  262. }else if($result['code']==667){
  263. $_SESSION['mobile'] = $mobile;
  264. $_SESSION['mobile_code'] = $result['msg'];
  265. $_SESSION['mobile_expiretime'] = time() +60*5;
  266. $rs['errno']=0;
  267. $rs['errmsg']="验证码为:{$result['msg']}";
  268. echo json_encode($rs);
  269. return;
  270. }else{
  271. $rs['errno']=1004;
  272. $rs['errmsg']=$result['msg'];
  273. echo json_encode($rs);
  274. return;
  275. }
  276. $rs=array(
  277. 'errno'=>0,
  278. 'data'=>array(),
  279. 'errmsg'=>'验证码已送',
  280. );
  281. echo json_encode($rs);
  282. }
  283. /* 登录 */
  284. /* $user_login!=$_SESSION['mobile'] */
  285. public function userLogin(){
  286. $mobile = $this->request->param('mobile');
  287. $code = $this->request->param('code');
  288. $user_login=checkNull($mobile);
  289. $code=checkNull($code);
  290. $rs=array('errno'=>0,'data'=>array(),'errmsg'=>'');
  291. /*var_dump($_SESSION['mobile']);
  292. var_dump($_SESSION['mobile_code']);
  293. die;*/
  294. if( !isset($_SESSION['mobile']) || !isset($_SESSION['mobile_code']) ){
  295. $rs['errno']=1120;
  296. $rs['errmsg']='请先获取验证码';
  297. echo json_encode($rs);
  298. return;
  299. }
  300. if($user_login!=$_SESSION['mobile']){
  301. $rs['errno']=1120;
  302. $rs['errmsg']='手机号码不一致';
  303. echo json_encode($rs);
  304. return;
  305. }
  306. if($code!=$_SESSION['mobile_code']){
  307. $rs['errno']=1120;
  308. $rs['errmsg']='验证码错误';
  309. echo json_encode($rs);
  310. return;
  311. }
  312. $nowtime=time();
  313. $userinfo=Db::name("user")->field("id,user_login,user_nickname,avatar,avatar_thumb,sex,signature,consumption,votestotal,province,city,coin,votes,birthday,issuper,user_status")->where("user_login='{$user_login}' and user_type='2'")->find();
  314. if(!$userinfo){
  315. $pass='yunbaokj';
  316. $user_pass=cmf_password($pass);
  317. $configpri=getConfigPri();
  318. $reg_reward=$configpri['reg_reward'];
  319. $code = $this->createCode();
  320. /* 无信息 进行注册 */
  321. $data=array(
  322. 'user_login' => $user_login,
  323. 'user_email' => '',
  324. 'mobile' =>$user_login,
  325. 'user_nickname' =>'请设置昵称',
  326. 'user_pass' =>$user_pass,
  327. 'signature' =>'这家伙很懒,什么都没留下',
  328. 'avatar' =>'/default.jpg',
  329. 'avatar_thumb' =>'/default_thumb.jpg',
  330. 'last_login_ip' =>get_client_ip(0,true),
  331. 'create_time' => $nowtime,
  332. 'last_login_time' => $nowtime,
  333. 'coin' => $reg_reward,
  334. 'user_status' => 1,
  335. 'user_type' =>2,//会员
  336. 'code' =>$code,
  337. 'source' =>'h5'
  338. );
  339. $userid=Db::name("user")->insertGetId($data);
  340. $userinfo=array(
  341. 'id' => $userid,
  342. 'user_login' => $data['user_login'],
  343. 'user_nickname' => $data['user_nickname'],
  344. 'avatar' => $data['avatar'],
  345. 'avatar_thumb' => $data['avatar_thumb'],
  346. 'sex' => '2',
  347. 'signature' => $data['signature'],
  348. 'consumption' => 0,
  349. 'votestotal' => 0,
  350. 'province' => '',
  351. 'city' => '',
  352. 'coin' => $reg_reward,
  353. 'votes' => 0,
  354. 'birthday' => '',
  355. 'issuper' => 0,
  356. 'user_status' => 1,
  357. );
  358. }
  359. if($userinfo['user_status']==0){
  360. $rs['errno']=1002;
  361. $rs['errmsg']='账号已被禁用';
  362. echo json_encode($rs);
  363. return;
  364. }
  365. $token=md5(md5($userinfo['id'].$userinfo['user_login'].time()));
  366. $expiretime=time()+60*60*24*300;
  367. $isok=DB::name("user_token")
  368. ->where("user_id={$userinfo['id']}")
  369. ->update(array("token"=>$token, "expire_time"=>$expiretime , "create_time"=>$nowtime ));
  370. if(!$isok){
  371. DB::name("user_token")
  372. ->insert(array("user_id"=>$userinfo['id'],"token"=>$token, "expire_time"=>$expiretime , "create_time"=>$nowtime ));
  373. }
  374. $userinfo['token']=$token;
  375. delcache("token_".$userinfo['id']);
  376. session('uid',$userinfo['id']);
  377. session('token',$userinfo['token']);
  378. session('user',$userinfo);
  379. echo json_encode($rs);
  380. }
  381. /* 用户进入 写缓存 */
  382. public function setNodeInfo() {
  383. /* 当前用户信息 */
  384. $uid=(int)session("uid");
  385. $token=session("token");
  386. $liveuid = $this->request->param('liveuid', 0, 'intval');
  387. if($uid>0){
  388. $info=getUserInfo($uid);
  389. $info['liveuid']=$liveuid;
  390. $info['token']=$token;
  391. $info['contribution']='0';
  392. $info['usertype']=getIsAdmin($uid,$liveuid);
  393. $info['level']='0';
  394. //获取用户的守护类型
  395. $guard_info=Db::name("guard_user")->where("uid={$uid} and liveuid={$liveuid}")->find();
  396. if($guard_info){
  397. $now=time();
  398. if($guard_info['endtime']<=$now){
  399. Db::name("guard_user")->where("id={$guard_info['id']}")->delete();
  400. $info['guard_type']='0';
  401. }else{
  402. $info['guard_type']=(string)$guard_info['type'];
  403. }
  404. }else{
  405. $info['guard_type']='0';
  406. }
  407. /* 等级+100 保证等级位置位数相同,最后拼接1 防止末尾出现0 */
  408. $info['sign']=$info['contribution'].'.'.($info['level']+100).'1';
  409. }else{
  410. /* 游客 */
  411. $sign= mt_rand(1000,9999);
  412. $info['id'] = '-'.$sign;
  413. $info['user_nickname'] = '游客'.$sign;
  414. $info['avatar'] = '';
  415. $info['avatar_thumb'] = '';
  416. $info['sex'] = '0';
  417. $info['signature'] = '0';
  418. $info['consumption'] = '0';
  419. $info['votestotal'] = '0';
  420. $info['province'] = '';
  421. $info['city'] = '';
  422. $info['token']=md5($liveuid.'_'.$sign);
  423. $info['liveuid']=$liveuid;
  424. $info['usertype']=30;
  425. $info['contribution']='0';
  426. $info['vip']=array('type'=>'0');
  427. $info['level']='0';
  428. $info['guard_type']='0';
  429. /* 等级+100 保证等级位置位数相同,最后拼接1 防止末尾出现0 */
  430. $info['sign']=$info['contribution'].'.'.($info['level']+100).'1';
  431. $token =$info['token'] ;
  432. }
  433. setcaches($token,$info);
  434. $data=array(
  435. 'error'=>0,
  436. 'userinfo'=>$info,
  437. );
  438. echo json_encode($data);
  439. }
  440. protected function checkShut($uid,$liveuid){
  441. $where=[];
  442. $where['uid']=$uid;
  443. $where['liveuid']=$liveuid;
  444. $isexist=Db::name('user_live_kick')
  445. ->field("id")
  446. ->where($where)
  447. ->find();
  448. if($isexist){
  449. return 0;
  450. }
  451. $isexist=Db::name('user_live_shut')
  452. ->where($where)
  453. ->find();
  454. if($isexist){
  455. hSet($liveuid . 'shutup',$uid,1);
  456. }else{
  457. hDel($liveuid . 'shutup',$uid);
  458. }
  459. return 1;
  460. }
  461. public function getGift(){
  462. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  463. $gift=DB::name('gift')->field("id,type,giftname,needcoin,gifticon,swftime")->where('type!=2')->order("orderno asc")->select();
  464. foreach($gift as $k=>$v){
  465. $v['gifticon']=get_upload_path($v['gifticon']);
  466. $gift[$k]=$v;
  467. }
  468. $rs['info']=$gift;
  469. echo json_encode($rs);
  470. }
  471. /* 关注 */
  472. public function follow(){
  473. $uid=(int)session("uid");
  474. $touid = $this->request->param('touid', 0, 'intval');
  475. $rs = array('code' => 0, 'msg' => '', 'info' => array());
  476. if($uid<1 || $touid<1){
  477. $rs = array(
  478. 'code' => 1001,
  479. 'msg' => '关注失败',
  480. 'info' => array()
  481. );
  482. echo json_encode($rs);
  483. return;
  484. }
  485. $data=array(
  486. "uid"=>$uid,
  487. "touid"=>$touid,
  488. );
  489. $result=DB::name('user_attention')->insert($data);
  490. if(!$result){
  491. $rs = array(
  492. 'code' => 1001,
  493. 'msg' => '关注失败',
  494. 'info' => array()
  495. );
  496. }
  497. echo json_encode($rs);
  498. }
  499. /* 送礼物 */
  500. public function sendGift(){
  501. $uid=(int)session("uid");
  502. $token = $this->request->param('token');
  503. $touid = $this->request->param('touid', 0, 'intval');
  504. $stream = $this->request->param('stream');
  505. $giftid = $this->request->param('giftid', 0, 'intval');
  506. $token=checkNull($token);
  507. $touid=checkNull($touid);
  508. $stream=checkNull($stream);
  509. $giftid=checkNull($giftid);
  510. $giftcount=1;
  511. //礼物信息
  512. $giftinfo=Db::name("gift")->field("giftname,gifticon,needcoin,type,swftype,swf,swftime")->where("id='{$giftid}'")->find();
  513. if(!$giftinfo){
  514. echo '{"errno":"1001","data":"","msg":"礼物信息错误"}';
  515. return;
  516. }
  517. //判断是否是手绘礼物
  518. if($giftinfo['type']==2){
  519. echo '{"errno":"1001","data":"","msg":"此礼物为手绘礼物,请前去下载app体验~"}';
  520. return;
  521. }
  522. $total= $giftinfo['needcoin']*$giftcount;
  523. $addtime=time();
  524. $where3['uid']=$touid;
  525. $liveinfo=Db::name("user_live")->where("islive=1")->where($where3)->find();
  526. $showid=0;
  527. if($liveinfo){
  528. $showid=$liveinfo['starttime'];
  529. }
  530. //更新用户余额 消费
  531. $ifok=Db::name("user")
  532. ->where([['id','=',$uid],['coin','>=',$total]])
  533. ->dec('coin',$total)
  534. ->inc('consumption',$total)
  535. ->update();
  536. if(!$ifok){
  537. /* 余额不足 */
  538. echo '{"errno":"1001","data":"","msg":"余额不足"}';
  539. return;
  540. }
  541. $anthor_total=$total;
  542. // 更新直播 映票 累计映票
  543. Db::name("user")
  544. ->where([['id','=',$touid]])
  545. ->inc('votes',$anthor_total)
  546. ->inc('votestotal',$total)
  547. ->update();
  548. if($anthor_total){
  549. $insert_votes=[
  550. 'action'=>'5',
  551. 'uid'=>$touid,
  552. 'votes'=>$anthor_total,
  553. 'addtime'=>$addtime,
  554. ];
  555. Db::name('votes_record')->insert($insert_votes);
  556. }
  557. $action='sendgift';
  558. //写入消费记录
  559. $data=array(
  560. 'type'=>'expend',
  561. 'action'=>$action,
  562. 'uid'=>$uid,
  563. 'touid'=>$touid,
  564. 'giftid'=>$giftid,
  565. 'giftcount'=>$giftcount,
  566. 'totalcoin'=>$total,
  567. 'showid'=>$showid,
  568. 'addtime'=>$addtime
  569. );
  570. Db::name("user_coinrecord")->insert($data);
  571. /* 清除缓存 */
  572. delCache("userinfo_".$uid);
  573. delCache("userinfo_".$touid);
  574. $userinfo3=Db::name('user')->field("votestotal,user_nickname")->where("id='{$touid}'")->find();
  575. $gifttoken=md5(md5($action.$uid.$touid.$giftid.$giftcount.$total.$showid.$addtime.rand(100,999)));
  576. $swf=$giftinfo['swf'] ? get_upload_path($giftinfo['swf']):'';
  577. $userinfo2=Db::name('user')->field("consumption,coin,votestotal")->where("id='{$uid}'")->find();
  578. $result=array(
  579. 'uid'=>(int)$uid,
  580. 'giftid'=>(int)$giftid,
  581. 'type'=>$giftinfo['type'],
  582. 'giftcount'=>(int)$giftcount,
  583. 'totalcoin'=>$total,
  584. 'giftname'=>$giftinfo['giftname'],
  585. 'gifticon'=>get_upload_path($giftinfo['gifticon']),
  586. 'swftime'=>$giftinfo['swftime'],
  587. 'swftype'=>$giftinfo['swftype'],
  588. 'swf'=>$swf,
  589. 'coin'=>$userinfo2['coin'],
  590. 'votestotal'=>$userinfo3['votestotal'],
  591. );
  592. setcaches($gifttoken,$result);
  593. if($liveinfo){
  594. zIncrBy('user_'.$liveinfo['stream'],$total,$uid);
  595. }
  596. echo '{"errno":"0","uid":"'.$uid.'","type":"'.$giftinfo['type'].'","coin":"'.$userinfo2['coin'].'","gifttoken":"'.$gifttoken.'","livename":"'.$userinfo3['user_nickname'].'","msg":"赠送成功"}';
  597. }
  598. /* 支付页面 */
  599. public function pay(){
  600. $uid=(int)session("uid");
  601. $userinfo=Db::name("user")->field("id,user_nickname,avatar_thumb,coin")->where("id='{$uid}'")->find();
  602. $this->assign('userinfo',$userinfo);
  603. $chargelist=Db::name('charge_rules')->field('id,coin,money,coin_ios,product_id,give')->order('orderno asc')->select();
  604. $this->assign('chargelist',$chargelist);
  605. return $this->fetch();
  606. }
  607. /* 获取订单号 */
  608. public function getOrderId(){
  609. $uid=(int)session("uid");
  610. $chargeid = $this->request->param('chargeid', 0, 'intval');
  611. $rs=array(
  612. 'code'=>0,
  613. 'data'=>array(),
  614. 'msg'=>'',
  615. );
  616. $charge=Db::name('charge_rules')->where("id={$chargeid}")->find();
  617. if(!$charge){
  618. $rs['code']=1002;
  619. $rs['msg']='订单信息错误';
  620. echo json_encode($rs);
  621. return;
  622. }
  623. $orderid=$uid.'_'.date('YmdHis').rand(100,999);
  624. $orderinfo=array(
  625. "uid"=>$uid,
  626. "touid"=>$uid,
  627. "money"=>$charge['money'],
  628. "coin"=>$charge['coin'],
  629. "coin_give"=>$charge['give'],
  630. "orderno"=>$orderid,
  631. "type"=>'2',
  632. "ambient"=>'1',
  633. "status"=>0,
  634. "addtime"=>time()
  635. );
  636. $result=Db::name('charge_user')->insert($orderinfo);
  637. if(!$result){
  638. $rs['code']=1001;
  639. $rs['msg']='订单生成失败';
  640. echo json_encode($rs);
  641. return;
  642. }
  643. $rs['data']['uid']=$uid;
  644. $rs['data']['money']=$charge['money'];
  645. $rs['data']['orderid']=$orderid;
  646. echo json_encode($rs);
  647. }
  648. /**
  649. * @desc 生成邀请码
  650. * @param $len 长度
  651. * @param $format 格式化类型
  652. * @return string
  653. */
  654. function createCode($len=6){
  655. $is_abc = $is_numer = 0;
  656. $password = $tmp ='';
  657. $chars='ABCDEFGHJKLMNPQRSTUVWXYZ0123456789';
  658. while(strlen($password)<$len){
  659. $tmp =substr($chars,(mt_rand()%strlen($chars)),1);
  660. if(($is_numer <> 1 && is_numeric($tmp) && $tmp > 0 )){
  661. $is_numer = 1;
  662. }
  663. if(($is_abc <> 1 && preg_match('/[a-zA-Z]/',$tmp))){
  664. $is_abc = 1;
  665. }
  666. $password.= $tmp;
  667. }
  668. if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
  669. $password = $this->createCode($len);
  670. }
  671. if($password!=''){
  672. $oneinfo=Db::name("user")
  673. ->field("id")
  674. ->where("code='{$password}'")
  675. ->find();
  676. if(!$oneinfo){
  677. return $password;
  678. }
  679. }
  680. $password = $this->createCode($len);
  681. return $password;
  682. }
  683. }