Partytest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\Common;
  4. use app\common\model\PartyJoin;
  5. use fast\Random;
  6. use app\common\controller\RedisLeaderboard;
  7. use Redis;
  8. use think\Db;
  9. use think\Exception;
  10. use think\exception\PDOException;
  11. use think\exception\ValidateException;
  12. use think\Request;
  13. use app\common\service\RoomService;
  14. use app\common\library\GatewayworkerTools;
  15. /**
  16. * 派对信息接口
  17. */
  18. class Party extends Common
  19. {
  20. protected $noNeedLogin = ['updatePartyInfo','getPatyType','addUserPositionToParty', 'clearMoney','clearCharm','getPartyRankList','getDefaultBackground','getPartGifList','getPartHeadgifList','isNotalk','getMusicList','updateTops','handleParty'];
  21. protected $noNeedRight = ['*'];
  22. public function __construct(Request $request = null)
  23. {
  24. $this->roomTypeArr = [1=>"party",2=>"live"];
  25. parent::__construct($request);
  26. }
  27. /**
  28. * 全麦/单独赠送礼物
  29. * 优化之前的送礼物
  30. */
  31. public function giveGiftToYou() {
  32. // 是否背包赠送: 1=是,0=否
  33. $is_back = $this->request->request("is_back",0);
  34. // 接口防并发
  35. if($is_back){
  36. if (!$this->apiLimit(50, 1000)) {
  37. //1000毫秒50次
  38. $this->error(__('Operation frequently'));
  39. }
  40. }else{
  41. if (!$this->apiLimit(10, 1000)) {
  42. //1000毫秒10次
  43. $this->error(__('Operation frequently'));
  44. }
  45. }
  46. //送礼物权限
  47. if($this->auth->power['give_gift'] == 1){
  48. $this->error('您已被限制:赠送礼物,请联系管理员');
  49. }
  50. //接收参数
  51. $user_ids = $this->request->request("user_id");// 赠送对象
  52. $gift_id = $this->request->request("gift_id");// 礼物ID。如果是背包赠送,则是gift_back表的id,这样不合理,但是已经不好改了
  53. $party_id = $this->request->request("party_id",0);// 派对ID
  54. $room_type = $this->request->request('room_type',1); // 房间类型
  55. $number = $this->request->request("number");// 赠送数量
  56. if (!$user_ids || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2]))
  57. {
  58. $this->error(__('Invalid parameters'));
  59. }
  60. if(!$party_id){
  61. $this->error('只有房间内能送礼物');
  62. }
  63. //处理参数
  64. $user_id_arr = explode(",",$user_ids);
  65. $userCount = count($user_id_arr);
  66. $userauthid = $this->auth->id;
  67. $money_to_jewel = config('site.money_to_jewel') ?: 10; //余额兑换钻石
  68. $userModel = new \app\common\model\User();
  69. // 不可以赠送给自己
  70. //if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
  71. $backGiftId = 0; //背包礼物表的 gift_id
  72. $boxgiftInfo = [];
  73. //每个人都能得到的礼物价值
  74. $giftValue = 0;
  75. //每个人都能得到的礼物价值
  76. $getValue = 0;
  77. //热度值
  78. $hotValue = 0;
  79. if($is_back == 1) {
  80. // 获取背包礼物信息
  81. $giftInfo = \app\common\model\GiftBack::get($gift_id);
  82. if(!$giftInfo){
  83. $this->error("背包礼物获取失败");
  84. }
  85. $backGiftId = $giftInfo->gift_id;
  86. // 随机获取一个礼物
  87. $allCount = $number*$userCount;
  88. $giftbackList = \app\common\model\GiftBack::where(["gift_id"=>$backGiftId,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->select();
  89. $giftInfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  90. $giftcount = 0;
  91. $giftList = [];
  92. if(!empty($giftbackList)) {
  93. foreach($giftbackList as $k => $v) {
  94. $giftcount = $giftcount + $v["number"];
  95. $giftList[$k] = $v;
  96. if($giftcount >= $allCount) {
  97. break;
  98. }
  99. }
  100. }
  101. if($giftcount < $allCount) $this->error("背包数量不足");
  102. $giftValue = $giftInfo["value"] * $number;
  103. $getValue = $giftValue;
  104. } else {
  105. // 获取礼物信息
  106. $giftInfo = Db::name('gift')->where('id',$gift_id)->find();
  107. if (!$giftInfo) {
  108. $this->error("请选择礼物!");
  109. }
  110. $giftValue = $giftInfo["value"] * $number;
  111. $getValue = $giftValue;
  112. // 判断当前用户余额,这里不需要锁表,钱包操作会锁
  113. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  114. if($this->auth->jewel < $giftCountValue){
  115. $this->error("您的钻石余额不足!");
  116. }
  117. }
  118. //此时 $giftValue,$getValue,$hotValue是相等的
  119. $hotValue = $getValue;
  120. // 转换声币后 再进行抽点设置
  121. $partyInfo = null;
  122. if(!$party_id) {
  123. $platRate = 10;
  124. $guilderRate = 30;
  125. } else {
  126. $partyInfo = \app\common\model\Party::field("user_id,platRate,guilderRate")->where(["id"=>$party_id])->find();
  127. // 获取系统配置信息
  128. $platRate = $partyInfo->platRate; // 平台抽成百分比
  129. $guilderRate = $partyInfo->guilderRate; // 工会长抽成百分比
  130. }
  131. $platValue = bcmul($platRate/100 ,$getValue,2); //平台抽成
  132. $guilderValue = bcmul($guilderRate/100,$getValue,2);// 工会长抽成
  133. $getValue = bcsub(bcsub($getValue ,$platValue,2),$guilderValue,2);//减去抽成剩余价值
  134. // $gif_image = $is_back==1?$giftInfo["gif_image"]:$giftInfo["special"];
  135. $returnData = [];
  136. Db::startTrans();
  137. try {
  138. $redis = new Redis();
  139. $redisconfig = config("redis");
  140. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  141. if ($redisconfig['redis_pwd']) {
  142. $redis->auth($redisconfig['redis_pwd']);
  143. }
  144. if($redisconfig['redis_selectdb'] > 0){
  145. $redis->select($redisconfig['redis_selectdb']);
  146. }
  147. // 获取当天零点
  148. $day = date("Ymd");
  149. // 获取本周第一天
  150. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  151. // 获取本月第一天
  152. $monthday = date("Ym01");
  153. $allVal = 0;
  154. $i = 0;
  155. foreach($user_id_arr as $user_id) {
  156. // 获取赠送用户信息
  157. $where = [];
  158. $where["id"] = $user_id;
  159. $touserInfo = $userModel->where($where)->find();
  160. if($is_back == 1) {
  161. $b=0;
  162. foreach($giftList as $k => $v) {
  163. for($a=1;$a<=$v["number"];$a++) {
  164. $b++;
  165. $num = $v["number"] - $a;
  166. if($num > 0) {
  167. $res1 = \app\common\model\GiftBack::where(["id"=>$v["id"]])->setDec("number");
  168. } else {
  169. $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  170. }
  171. if($b == $number) break;
  172. }
  173. }
  174. $res2 = true;
  175. } else {
  176. $res1 = true;
  177. $res2 = true;
  178. // 扣除当前用户钻石余额
  179. $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', $this->auth->jewel, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3,'jewel');
  180. if($rs_wallet['status'] == false){
  181. $this->error($rs_wallet['msg']);
  182. Db::rollback();
  183. }
  184. }
  185. $giftuserpartyModel = new \app\common\model\GiftUserParty();
  186. // 添加礼物赠送记录表
  187. $data = [];
  188. $data["user_id"] = $userauthid;
  189. $data["user_to_id"] = $user_id;
  190. $data["party_id"] = $party_id;
  191. if($boxgiftInfo){
  192. $data["gift_id"] = $boxgiftInfo['gift_id'];
  193. $data["gift_give_type"] = 2;
  194. $data["gift_name"] = $boxgiftInfo["gift_name"];
  195. $data["gift_gif_image"] = $boxgiftInfo["image"];
  196. $data["number"] = $number;
  197. $data["price"] = $boxgiftInfo["price"];
  198. $data["value"] = $boxgiftInfo["price"] * $number;
  199. }else{
  200. $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
  201. $data["gift_give_type"] = $is_back ? 1 : 2;
  202. $data["gift_name"] = $giftInfo["name"];
  203. $data["gift_gif_image"] = $giftInfo["image"];
  204. $data["number"] = $number;
  205. $data["price"] = $giftInfo["value"];
  206. $data["value"] = $giftValue;
  207. }
  208. //每个礼物都要计算平台抽成和房主抽成
  209. $data['platvalue'] = bcmul($platRate/100,$data["value"],2);
  210. $data['guildervalue'] = bcmul($guilderRate/100,$data["value"],2);
  211. $data['guildermoney'] = bcdiv($data['guildervalue'],$money_to_jewel,2);
  212. $data["createtime"] = time();
  213. $res5 = $giftuserpartyModel->allowField(true)->save($data);
  214. $this->bigGiftNotice($giftuserpartyModel);
  215. // 添加获赠用户余额
  216. if($getValue > 0){
  217. $getMoney = bcdiv($getValue,$money_to_jewel,2);
  218. if($getMoney > 0){
  219. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$this->auth->nickname}送你{$giftInfo['name']}x{$number}",101,'money');
  220. if($rs_wallet['status'] == false){
  221. $this->error($rs_wallet['msg']);
  222. Db::rollback();
  223. }
  224. }
  225. }
  226. // 增加房主抽成
  227. if ($partyInfo && $guilderValue > 0) {
  228. $guilderMoney = bcdiv($guilderValue,$money_to_jewel,2);
  229. if($guilderMoney > 0){
  230. $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
  231. if($rs_wallet['status'] == false){
  232. $this->error($rs_wallet['msg']);
  233. Db::rollback();
  234. }
  235. }
  236. }
  237. if ($res1 && $res2 && $res5) {
  238. $i++;
  239. if($party_id > 0) {
  240. // 添加redis记录做财富排行榜日榜用
  241. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
  242. // 添加redis记录做财富排行榜周榜用
  243. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
  244. // 添加redis记录做财富排行榜月榜用
  245. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
  246. // 添加redis记录做贡献排行榜日榜用
  247. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  248. // 添加redis记录做贡献排行榜周榜用
  249. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  250. // 添加redis记录做贡献排行榜月榜用
  251. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  252. // tcp 更新用户魅力值
  253. $this->updateUserCharm($party_id, $user_id, $hotValue);
  254. // 如果是主播,则添加魅力值记录做榜单统计
  255. if($room_type == 2) {
  256. $data = [];
  257. $data["user_id"] = $user_id;
  258. $data["party_id"] = $party_id;
  259. $data["charm"] = $hotValue;
  260. $data["createtime"] = time();
  261. \app\common\model\UserCharmRank::insert($data);
  262. }
  263. }
  264. $getempirical = config("site.getempirical");
  265. $getempirical = $getempirical * $hotValue;
  266. // 获取用户贵族信息
  267. $noble = \app\common\model\User::getUserNoble($this->auth->id);
  268. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  269. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  270. }
  271. // 增加用户经验值
  272. $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical);
  273. if ($res){
  274. $this->auth->level = $res->level;
  275. }
  276. //增加被送礼物用户的魅力等级
  277. $res_charm = \app\common\model\User::add_charm_level($user_id,$giftValue);
  278. // +exp
  279. \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number);
  280. //贵族升级处理
  281. //$this->checkBeNoble($redis,$this->auth->id,$giftCountValue);
  282. // +message
  283. \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue ." 钻石");
  284. $allVal = $allVal + $hotValue;
  285. // // 剪掉背包礼物
  286. // if($is_back == 1) {
  287. // \app\common\model\GiftBack::update(["is_use"=>1],["id"=>$gift_id]);
  288. // }
  289. }
  290. }
  291. // 获取用户魅力值
  292. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  293. $u = [];
  294. if($users) {
  295. foreach($users as $k => $v) $u[] = [
  296. "user_id"=>$k,
  297. "charm"=>$this->changeW($v)
  298. ];
  299. }
  300. $userCharm = $u;
  301. // tcp 更新房间热度
  302. $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
  303. // 如果是派对,则添加派对热度值记录做榜单统计
  304. if($room_type == 1) {
  305. $data = [];
  306. $data["party_id"] = $party_id;
  307. $data["hot"] = $allVal;
  308. $data["createtime"] = time();
  309. \app\common\model\PartyHot::insert($data);
  310. }
  311. //增加送礼用户的财富等级
  312. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$allVal);
  313. // tcp 获取房间用户周前三名
  314. $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
  315. if($i == $userCount) {
  316. $returnData["userCharm"] = $userCharm;
  317. $returnData["partyHot"] = $this->changeW($partyHot);
  318. $returnData["partyUserTop"] = $partyUserTop;
  319. if($is_back != 1) {
  320. if($giftInfo->box_type > 0) { // 不是背包,宝箱中赠送
  321. $returnData["box_type"] = $giftInfo->box_type;
  322. $returnData["box_image"] = $giftInfo->image;
  323. $returnData["image"] = $boxgiftInfo["image"];
  324. $returnData["name"] = $boxgiftInfo["gift_name"];
  325. $returnData["gif_image"] = $boxgiftInfo["special"];
  326. } else {
  327. $returnData["image"] = $giftInfo["image"];
  328. $returnData["gif_image"] = $giftInfo["special"];
  329. }
  330. } else {
  331. $returnData["image"] = $giftInfo["image"];
  332. $returnData["gif_image"] = $giftInfo["gif_image"];
  333. }
  334. // 增加抽点记录
  335. $data = [];
  336. $data["user_id"] = $user_ids;
  337. $data["party_id"] = $party_id?$party_id:0;
  338. $data["gift_value"] = $giftValue;
  339. $data["plat_value"] = $platValue;
  340. $data["guilder_value"] = $guilderValue;
  341. $data["createtime"] = time();
  342. \app\common\model\UserProfitLog::insert($data);
  343. Db::commit();
  344. $this->success("赠送成功!",$returnData);
  345. } else {
  346. $this->success("赠送失败!");
  347. }
  348. } catch (ValidateException $e) {
  349. Db::rollback();
  350. $this->error($e->getMessage());
  351. } catch (PDOException $e) {
  352. Db::rollback();
  353. $this->error($e->getMessage());
  354. } catch (Exception $e) {
  355. Db::rollback();
  356. $this->error($e->getMessage());
  357. }
  358. }
  359. }