Plantask.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use Redis;
  5. use think\Db;
  6. use app\common\library\Easemob;
  7. class Plantask extends Controller
  8. {
  9. //关于本文件里的计划任务
  10. //只有 public 方法,auto_开头的才是计划任务,其他private 方法都是工具方法
  11. ////////////////////////////////////////下面都是计划任务方法///////////////////////////////////////////////////////////////
  12. //送礼物(api/party/giveGiftToYou)拆分出来的异步用户升级方法,
  13. //已作废
  14. public function auto_user_level_up_old(){
  15. Db::startTrans();
  16. $tasklist = Db::name('gift_user_party')->where('task_status',0)->limit(20)->lock(true)->select();
  17. if(empty($tasklist)){
  18. Db::rollback();
  19. echo 'empty';
  20. exit;
  21. }
  22. try {
  23. //redis
  24. $redis = new Redis();
  25. $redisconfig = config("redis");
  26. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  27. if ($redisconfig['redis_pwd']) {
  28. $redis->auth($redisconfig['redis_pwd']);
  29. }
  30. if($redisconfig['redis_selectdb'] > 0){
  31. $redis->select($redisconfig['redis_selectdb']);
  32. }
  33. //$money_to_jewel = config('site.money_to_jewel') ?: 10000; //余额兑换金币
  34. // $getempirical_conf = config("site.getempirical");
  35. foreach($tasklist as $key => $giftuserparty){
  36. //echo $giftuserparty['id'].'<br>';
  37. $giftValue = $giftuserparty['value'];
  38. $hotValue = $giftValue;
  39. //用户信息
  40. //$authuser_info = Db::name('user')->field('nickname')->where('id',$giftuserparty['user_id'])->find();
  41. //$touserInfo = Db::name('user')->field('nickname')->where('id',$giftuserparty['user_to_id'])->find();
  42. //添加获赠用户余额
  43. //$user_id = $giftuserparty['user_to_id'];
  44. /*if($giftuserparty['getvalue'] > 0){
  45. $getMoney = bcdiv($giftuserparty['getvalue'],$money_to_jewel,2);
  46. if($getMoney > 0){
  47. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$authuser_info['nickname']}送你{$giftuserparty['gift_name']}x{$giftuserparty['number']}",101,'money');
  48. if($rs_wallet['status'] == false){
  49. Db::rollback();
  50. $this->error($rs_wallet['msg']);
  51. }
  52. }
  53. }*/
  54. $partyInfo = \app\common\model\Party::field("id,room_type,party_name,party_hot,user_id,platRate,guilderRate,easemob_room_id")->where(["id"=>$giftuserparty['party_id']])->find();
  55. //增加房主抽成
  56. /*if ($partyInfo && $giftuserparty['guildervalue'] > 0) {
  57. $guilderMoney = bcdiv($giftuserparty['guildervalue'],$money_to_jewel,2);
  58. if($guilderMoney > 0){
  59. $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$authuser_info['nickname']}送礼物{$giftuserparty['gift_name']}x{$giftuserparty['number']}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
  60. if($rs_wallet['status'] == false){
  61. Db::rollback();
  62. $this->error($rs_wallet['msg']);
  63. }
  64. }
  65. }*/
  66. //更新麦位魅力值
  67. $this->updateSeatCharm($partyInfo['easemob_room_id'],$giftuserparty['seat_num'],$hotValue,$partyInfo,$redis);
  68. // 如果是主播,则添加魅力值记录做榜单统计,这个表和送礼物日志表重复了,无意义
  69. /*if($partyInfo) {
  70. $data = [];
  71. $data["user_id"] = $giftuserparty['user_to_id'];
  72. $data["party_id"] = $party_id;
  73. $data["charm"] = $hotValue;
  74. $data["createtime"] = time();
  75. \app\common\model\UserCharmRank::insert($data);
  76. }*/
  77. //用户经验升级
  78. //$getempirical = $getempirical_conf * $hotValue;
  79. // 获取用户贵族信息
  80. /*$noble = \app\common\model\User::getUserNoble($giftuserparty['user_id']);
  81. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  82. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  83. }*/
  84. // 增加用户经验值
  85. //$res = \app\common\model\User::addEmpirical($giftuserparty['user_id'],$getempirical);
  86. /*if ($res){
  87. $this->auth->level = $res->level;
  88. }*/
  89. //增加被送礼物用户的魅力等级
  90. //$res_charm = \app\common\model\User::add_charm_level($giftuserparty['user_to_id'],$giftValue);
  91. // +exp
  92. // \app\common\model\TaskLog::tofinish($giftuserparty['user_id'],"OBHqCX4g",$giftuserparty['number']);
  93. // +message
  94. //\app\common\model\Message::addMessage($giftuserparty['user_to_id'],"礼物通知","收到 ".$authuser_info['nickname']." 赠送的".$giftuserparty['gift_name']." x".$giftuserparty['number']." 价值 ".$giftValue ." 金币");
  95. //增加送礼用户的财富等级
  96. //$res_wealth = \app\common\model\User::add_wealth_level($giftuserparty['user_id'],$giftValue);
  97. //结束
  98. $rs_up = Db::name('gift_user_party')->where('id',$giftuserparty['id'])->update(['task_status'=>1]);
  99. if($rs_up === false){
  100. Db::rollback();
  101. echo '更新错误'.$giftuserparty['id'];
  102. exit;
  103. }
  104. }
  105. Db::commit();
  106. } catch (ValidateException $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. } catch (PDOException $e) {
  110. Db::rollback();
  111. $this->error($e->getMessage());
  112. } catch (Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. }
  117. //最新方法
  118. public function auto_user_level_up(){
  119. Db::startTrans();
  120. $tasklist = Db::name('gift_user_party')->where('task_status',0)->order('id asc')->field('party_id')->select();
  121. if(empty($tasklist)){
  122. Db::rollback();
  123. echo 'empty';
  124. exit;
  125. }
  126. try {
  127. //redis
  128. $redis = new Redis();
  129. $redisconfig = config("redis");
  130. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  131. if ($redisconfig['redis_pwd']) {
  132. $redis->auth($redisconfig['redis_pwd']);
  133. }
  134. if($redisconfig['redis_selectdb'] > 0){
  135. $redis->select($redisconfig['redis_selectdb']);
  136. }
  137. //开始
  138. $party_ids = array_column($tasklist,'party_id');
  139. $party_ids = array_flip($party_ids);
  140. $party_ids = array_flip($party_ids);
  141. //dump($party_ids);
  142. $partyInfo_arr = Db::name('party')->where('id','IN',$party_ids)->column('id,user_id,easemob_room_id');
  143. //dump($partyInfo_arr);exit;
  144. foreach($partyInfo_arr as $party_id => $partyInfo){
  145. //更新麦位魅力值
  146. $this->updateSeatCharm($partyInfo['easemob_room_id'],$partyInfo,$redis);
  147. }
  148. //结束
  149. $rs_up = Db::name('gift_user_party')->where('party_id','IN',$party_ids)->update(['task_status'=>1]);
  150. Db::commit();
  151. } catch (ValidateException $e) {
  152. Db::rollback();
  153. $this->error($e->getMessage());
  154. } catch (PDOException $e) {
  155. Db::rollback();
  156. $this->error($e->getMessage());
  157. } catch (Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. }
  162. /////////////////////////////////////////下面都是工具方法////////////////////////////////////////////////
  163. /**
  164. * 用户赠送礼物后房间内麦位魅力值增加,更新到麦位自定义信息
  165. * $seatnum 座位数字 1,不是键名 seat1
  166. */
  167. private function updateSeatCharm($easemob_room_id,$party_info,$redis) {
  168. //获取已有信息
  169. $easemob = new Easemob();
  170. $seatlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['seat0','seat1','seat2','seat3','seat4','seat5','seat6','seat7']);
  171. if(empty($seatlist)){
  172. return true;
  173. }
  174. $matedata = [];
  175. // 获取用户魅力值
  176. $users = $redis->zRevRange("hourCharm_".$party_info['id'],0,-1,true);
  177. //轮询麦位
  178. foreach($seatlist as $seatnum => $seatdata){
  179. $seatdata = json_decode($seatdata,true);
  180. if($users) {
  181. foreach($users as $user_id => $charm){
  182. if(isset($seatdata['userNo']) && $seatdata['userNo'] == $user_id){
  183. $seatdata['charm'] = changeW($charm);
  184. }
  185. }
  186. }
  187. $seatdata = json_encode($seatdata);
  188. $matedata[$seatnum] = $seatdata;
  189. }
  190. //重新设置麦位小红心
  191. //获取房间用户周前三名 getPartyUserTop里面摘出来的一部分
  192. //[环信]更新财富榜前3。
  193. $avatarArr = $redis->hGet("user_jewel_top3",$party_info['id']);
  194. $matedata['wealth_top3_userlist'] = $avatarArr;
  195. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  196. return true;
  197. }
  198. }