Partytest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. $hotValue = $getValue;
  118. // 转换声币后 再进行抽点设置
  119. $partyInfo = null;
  120. if(!$party_id) {
  121. $platRate = 10;
  122. $guilderRate = 30;
  123. } else {
  124. $partyInfo = \app\common\model\Party::field("user_id,platRate,guilderRate")->where(["id"=>$party_id])->find();
  125. // 获取系统配置信息
  126. $platRate = $partyInfo->platRate; // 平台抽成百分比
  127. $guilderRate = $partyInfo->guilderRate; // 工会长抽成百分比
  128. }
  129. $platValue = bcmul($platRate/100,$getValue,2); //平台抽成
  130. $guilderValue = bcmul($guilderRate/100,$getValue,2);// 工会长抽成
  131. $getValue = bcsub(bcsub($getValue,$platValue,2),$guilderValue,2);//减去抽成剩余价值
  132. // $gif_image = $is_back==1?$giftInfo["gif_image"]:$giftInfo["special"];
  133. $returnData = [];
  134. Db::startTrans();
  135. try {
  136. $redis = new Redis();
  137. $redisconfig = config("redis");
  138. $redis->connect($redisconfig["host"], $redisconfig["port"], 86400 * 31);
  139. if ($redisconfig['redis_pwd']) {
  140. $redis->auth($redisconfig['redis_pwd']);
  141. }
  142. if($redisconfig['redis_selectdb'] > 0){
  143. $redis->select($redisconfig['redis_selectdb']);
  144. }
  145. // 获取当天零点
  146. $day = date("Ymd");
  147. // 获取本周第一天
  148. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  149. // 获取本月第一天
  150. $monthday = date("Ym01");
  151. $allVal = 0;
  152. $i = 0;
  153. foreach($user_id_arr as $user_id) {
  154. // 获取赠送用户信息
  155. $where = [];
  156. $where["id"] = $user_id;
  157. $touserInfo = $userModel->where($where)->find();
  158. if($is_back == 1) {
  159. $b=0;
  160. foreach($giftList as $k => $v) {
  161. for($a=1;$a<=$v["number"];$a++) {
  162. $b++;
  163. $num = $v["number"] - $a;
  164. if($num > 0) {
  165. $res1 = \app\common\model\GiftBack::where(["id"=>$v["id"]])->setDec("number");
  166. } else {
  167. $res1 = \app\common\model\GiftBack::update(["is_use"=>1,"use_time"=>time()],["id"=>$v["id"]]);
  168. }
  169. if($b == $number) break;
  170. }
  171. }
  172. $res2 = true;
  173. } else {
  174. $res1 = true;
  175. $res2 = true;
  176. // 扣除当前用户钻石余额
  177. $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3,'jewel');
  178. if($rs_wallet['status'] == false){
  179. $this->error($rs_wallet['msg']);
  180. Db::rollback();
  181. }
  182. }
  183. $giftuserpartyModel = new \app\common\model\GiftUserParty();
  184. // 添加礼物赠送记录表
  185. $data = [];
  186. $data["user_id"] = $userauthid;
  187. $data["user_to_id"] = $user_id;
  188. $data["party_id"] = $party_id;
  189. if($boxgiftInfo){
  190. $data["gift_id"] = $boxgiftInfo['gift_id'];
  191. $data["gift_give_type"] = 2;
  192. $data["gift_name"] = $boxgiftInfo["gift_name"];
  193. $data["gift_gif_image"] = $boxgiftInfo["image"];
  194. $data["number"] = $number;
  195. $data["price"] = $boxgiftInfo["price"];
  196. $data["value"] = $boxgiftInfo["price"] * $number;
  197. }else{
  198. $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
  199. $data["gift_give_type"] = $is_back ? 1 : 2;
  200. $data["gift_name"] = $giftInfo["name"];
  201. $data["gift_gif_image"] = $giftInfo["image"];
  202. $data["number"] = $number;
  203. $data["price"] = $giftInfo["value"];
  204. $data["value"] = $giftValue;
  205. }
  206. //每个礼物都要计算平台抽成和房主抽成
  207. $data['platvalue'] = bcmul($platRate/100,$data["value"],2);
  208. $data['guildervalue'] = bcmul($guilderRate/100,$data["value"],2);
  209. $data['guildermoney'] = bcdiv($data['guildervalue'],$money_to_jewel,2);
  210. $data["createtime"] = time();
  211. $res5 = $giftuserpartyModel->allowField(true)->save($data);
  212. $this->bigGiftNotice($giftuserpartyModel);
  213. // 添加获赠用户余额
  214. if($getValue > 0){
  215. $getMoney = bcdiv($getValue,$money_to_jewel,2);
  216. if($getMoney > 0){
  217. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$this->auth->nickname}送你{$giftInfo['name']}x{$number}",101,'money');
  218. if($rs_wallet['status'] == false){
  219. $this->error($rs_wallet['msg']);
  220. Db::rollback();
  221. }
  222. }
  223. }
  224. // 增加房主抽成
  225. if ($partyInfo && $guilderValue > 0) {
  226. $guilderMoney = bcdiv($guilderValue,$money_to_jewel,2);
  227. if($guilderMoney > 0){
  228. $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
  229. if($rs_wallet['status'] == false){
  230. $this->error($rs_wallet['msg']);
  231. Db::rollback();
  232. }
  233. }
  234. }
  235. if ($res1 && $res2 && $res5) {
  236. $i++;
  237. if($party_id > 0) {
  238. // 添加redis记录做财富排行榜日榜用
  239. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
  240. // 添加redis记录做财富排行榜周榜用
  241. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
  242. // 添加redis记录做财富排行榜月榜用
  243. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
  244. // 添加redis记录做贡献排行榜日榜用
  245. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  246. // 添加redis记录做贡献排行榜周榜用
  247. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  248. // 添加redis记录做贡献排行榜月榜用
  249. $redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  250. // tcp 更新用户魅力值
  251. $this->updateUserCharm($party_id, $user_id, $hotValue);
  252. // 如果是主播,则添加魅力值记录做榜单统计
  253. if($room_type == 2) {
  254. $data = [];
  255. $data["user_id"] = $user_id;
  256. $data["party_id"] = $party_id;
  257. $data["charm"] = $hotValue;
  258. $data["createtime"] = time();
  259. \app\common\model\UserCharmRank::insert($data);
  260. }
  261. }
  262. $getempirical = config("site.getempirical");
  263. $getempirical = $getempirical * $hotValue;
  264. // 获取用户贵族信息
  265. $noble = \app\common\model\User::getUserNoble($this->auth->id);
  266. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  267. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  268. }
  269. // 增加用户经验值
  270. $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical);
  271. if ($res){
  272. $this->auth->level = $res->level;
  273. }
  274. //增加被送礼物用户的魅力等级
  275. $res_charm = \app\common\model\User::add_charm_level($user_id,$giftValue);
  276. // +exp
  277. \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number);
  278. //贵族升级处理
  279. //$this->checkBeNoble($redis,$this->auth->id,$giftCountValue);
  280. // +message
  281. \app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue ." 钻石");
  282. $allVal = $allVal + $hotValue;
  283. // // 剪掉背包礼物
  284. // if($is_back == 1) {
  285. // \app\common\model\GiftBack::update(["is_use"=>1],["id"=>$gift_id]);
  286. // }
  287. }
  288. }
  289. // 获取用户魅力值
  290. $users = $redis->zRange("hourCharm_".$party_id,0,-1,true);
  291. $u = [];
  292. if($users) {
  293. foreach($users as $k => $v) $u[] = [
  294. "user_id"=>$k,
  295. "charm"=>$this->changeW($v)
  296. ];
  297. }
  298. $userCharm = $u;
  299. // tcp 更新房间热度
  300. $partyHot = $this->updatePartyHot($party_id, $allVal, $room_type);
  301. // 如果是派对,则添加派对热度值记录做榜单统计
  302. if($room_type == 1) {
  303. $data = [];
  304. $data["party_id"] = $party_id;
  305. $data["hot"] = $allVal;
  306. $data["createtime"] = time();
  307. \app\common\model\PartyHot::insert($data);
  308. }
  309. //增加送礼用户的财富等级
  310. $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$allVal);
  311. // tcp 获取房间用户周前三名
  312. $partyUserTop = $this->getPartyUserTop($party_id, $room_type);
  313. if($i == $userCount) {
  314. $returnData["userCharm"] = $userCharm;
  315. $returnData["partyHot"] = $this->changeW($partyHot);
  316. $returnData["partyUserTop"] = $partyUserTop;
  317. if($is_back != 1) {
  318. if($giftInfo->box_type > 0) { // 不是背包,宝箱中赠送
  319. $returnData["box_type"] = $giftInfo->box_type;
  320. $returnData["box_image"] = $giftInfo->image;
  321. $returnData["image"] = $boxgiftInfo["image"];
  322. $returnData["name"] = $boxgiftInfo["gift_name"];
  323. $returnData["gif_image"] = $boxgiftInfo["special"];
  324. } else {
  325. $returnData["image"] = $giftInfo["image"];
  326. $returnData["gif_image"] = $giftInfo["special"];
  327. }
  328. } else {
  329. $returnData["image"] = $giftInfo["image"];
  330. $returnData["gif_image"] = $giftInfo["gif_image"];
  331. }
  332. // 增加抽点记录
  333. $data = [];
  334. $data["user_id"] = $user_ids;
  335. $data["party_id"] = $party_id?$party_id:0;
  336. $data["gift_value"] = $giftValue;
  337. $data["plat_value"] = $platValue;
  338. $data["guilder_value"] = $guilderValue;
  339. $data["createtime"] = time();
  340. \app\common\model\UserProfitLog::insert($data);
  341. Db::commit();
  342. $this->success("赠送成功!",$returnData);
  343. } else {
  344. $this->success("赠送失败!");
  345. }
  346. } catch (ValidateException $e) {
  347. Db::rollback();
  348. $this->error($e->getMessage());
  349. } catch (PDOException $e) {
  350. Db::rollback();
  351. $this->error($e->getMessage());
  352. } catch (Exception $e) {
  353. Db::rollback();
  354. $this->error($e->getMessage());
  355. }
  356. }
  357. }