Partytest.php 17 KB

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