Noble.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use GatewayClient\Gateway;
  6. /**
  7. * 贵族接口
  8. */
  9. class Noble extends Api
  10. {
  11. protected $noNeedLogin = ['getNobleLevelList','getNobleLevelPrivilegeInfo','onNoble'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 获取贵族等级列表
  15. */
  16. public function getNobleLevelList() {
  17. $noblelevelModel = new \app\common\model\NobleLevel();
  18. $where = [];
  19. $where["is_show"] = 1;
  20. $noblelevelList = $noblelevelModel->where($where)->select();
  21. return $this->success("获取成功!",$noblelevelList);
  22. }
  23. /**
  24. * 根据贵族等级ID获取贵族等级特权
  25. */
  26. public function getNobleLevelPrivilegeInfo() {
  27. $level_id = $this->request->request("level_id");
  28. if (!$level_id) {
  29. $this->error(__('Invalid parameters'));
  30. }
  31. $nplModel = new \app\common\model\NobleLevelPrivilege();
  32. $where = [];
  33. $where["level_id"] = $level_id;
  34. $where["is_show"] = 1;
  35. $nplInfo = $nplModel->where($where)->select();
  36. if($nplInfo) {
  37. return $this->success("获取成功!",$nplInfo);
  38. } else {
  39. return $this->success("数据为空!",[]);
  40. }
  41. }
  42. /**
  43. * 获取贵族等级以及到期时间
  44. */
  45. public function getNobleDuetime() {
  46. $level_id = $this->request->request("level_id");
  47. if (!$level_id) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $userModel = new \app\common\model\User();
  51. $info = $userModel->getUserNobleInfo($level_id,$this->auth->id);
  52. return $this->success("获取成功!",$info);
  53. }
  54. /**
  55. * 获取贵族等级所需金额
  56. */
  57. public function getNobleLogInfo() {
  58. $level_id = $this->request->request("level_id");
  59. if (!$level_id) {
  60. $this->error(__('Invalid parameters'));
  61. }
  62. $nldlModel = new \app\common\model\NobleDredgeLog();
  63. $noblelevelModel = new \app\common\model\NobleLevel();
  64. $noblelevelInfo = $noblelevelModel->where(["id"=>$level_id])->find();
  65. // 获取贵族等级开通信息
  66. $where = [];
  67. $where["user_id"] = $this->auth->id;
  68. $where["level_no"] = $noblelevelInfo["level_no"];
  69. $nldlCount = 0;
  70. $nldlInfo = $nldlModel->where($where)->limit(1)->order("dredge_num","desc")->select();
  71. $nldlInfo && $nldlInfo = $nldlInfo[0];
  72. // 判断是否是首月开通
  73. if($nldlInfo) {
  74. $nldlCount = $nldlInfo["dredge_num"];
  75. }
  76. $money = $nldlCount>0?$noblelevelInfo["renew"]:$noblelevelInfo["first"];
  77. $data["money"] = $money;
  78. return $this->success("获取成功!",$data);
  79. }
  80. /**
  81. * 开通贵族
  82. */
  83. public function beNoble() {
  84. $level_id = $this->request->request("level_id"); // 贵族等级ID
  85. if (!$level_id) {
  86. $this->error(__('Invalid parameters'));
  87. }
  88. $time = time();
  89. // 获取贵族信息
  90. $noblelevelModel = new \app\common\model\NobleLevel();
  91. $nldlModel = new \app\common\model\NobleDredgeLog();
  92. $noblelevelInfo = $noblelevelModel->where(["id"=>$level_id])->find();
  93. // 获取贵族等级开通信息
  94. $where = [];
  95. $where["user_id"] = $this->auth->id;
  96. $where["level_no"] = $noblelevelInfo["level_no"];
  97. $nldlCount = 0;
  98. $nldlInfo = $nldlModel->where($where)->limit(1)->order("dredge_num","desc")->select();
  99. $nldlInfo && $nldlInfo = $nldlInfo[0];
  100. // 判断是否是首月开通
  101. if($nldlInfo) {
  102. $nldlCount = $nldlInfo["dredge_num"];
  103. // if(time() - $nldlInfo["createtime"] < 60) { // 同一个人,同一等级,一分钟之内重复开通,鉴定为系统bug
  104. // $this->error("您刚刚开通过此贵族特权");
  105. // }
  106. }
  107. $money = $nldlCount>0?$noblelevelInfo["renew"]:$noblelevelInfo["first"];
  108. if(!$noblelevelInfo || $money <= 0) {
  109. $this->error(__('贵族等级信息有误!'));
  110. }
  111. $userModel = new \app\common\model\User();
  112. $userjewellogModel = new \app\common\model\UserJewelLog();
  113. $where = [];
  114. $where["id"] = $this->auth->id;
  115. $userInfo = $userModel->where($where)->find();
  116. // 判断用户余额
  117. if($userInfo["jewel"]-$money < 0) {
  118. $this->error("您的钻石余额不足,请先充值");
  119. }
  120. // 获取当前用户贵族等级
  121. if($userInfo["noble"] == $level_id) {
  122. $duetime = $userInfo["noble_duetime"];
  123. } else {
  124. $duetime = $time;
  125. }
  126. Db::startTrans();
  127. try{
  128. // 扣除当前用户钻石余额 更新用户贵族信息
  129. $where = [];
  130. $where["id"] = $this->auth->id;
  131. $duetime = strtotime("+1 month",$duetime);
  132. $nextMonth = strtotime("+1 month",$time);
  133. $data = [];
  134. $data["noble"] = $level_id;
  135. $data["noble_duetime"] = $duetime;
  136. $submoney = bcsub($userInfo["jewel"],$money);
  137. // 开通返现
  138. $return = $nldlCount>0?$noblelevelInfo["give"]:$noblelevelInfo["firstgive"];
  139. $data["jewel"] = bcadd($submoney,$return);
  140. $res1 = $userModel->update($data,$where);
  141. // 添加当前用户钻石流水记录
  142. $res2 = $userjewellogModel->addUserJewelLog($this->auth->id, $money, "-", $userInfo["jewel"], "开通贵族:'" . $noblelevelInfo["name"] . "',扣除" . $money . "钻石!", 5);
  143. // 添加贵族等级开通明细
  144. $data = [];
  145. $data["user_id"] = $this->auth->id;
  146. $data["level_no"] = $noblelevelInfo["level_no"];
  147. $data["dredge_num"] = $nldlCount+1;
  148. $data["createtime"] = $time;
  149. $res3 = $nldlModel->insertGetId($data);
  150. // 添加当前用户钻石流水记录
  151. $res4 = $userjewellogModel->addUserJewelLog($this->auth->id, $return, "+", $userInfo["jewel"] - $money, "开通贵族:'" . $noblelevelInfo["name"] . "',赠送" . $money . "钻石!", 5);
  152. if($res1 && $res2 && $res3 && $res4) {
  153. Db::commit();
  154. $this->onNobleEffect($noblelevelInfo,$this->auth->id,$this->auth->nickname);
  155. $this->onNobleAttire($noblelevelInfo,$this->auth->id,$nextMonth,$time);
  156. // +exp
  157. $nobleArr = ['1'=>'entymIFE3J'];
  158. isset($nobleArr[$level_id]) && \app\common\model\TaskLog::tofinish($this->auth->id,$nobleArr[$level_id],1);
  159. // +message
  160. \app\common\model\Message::addMessage($this->auth->id,"贵族开通通知","恭喜,您已成功开通!".$noblelevelInfo["name"]." 贵族特权!");
  161. // 赠送消息尾灯
  162. \app\common\model\AttireBack::addToMyBack(15,$this->auth->id);
  163. // 赠送聊天气泡
  164. if($noblelevelInfo->tqgq) {
  165. $params_from = $this->request->request("params_from"); // 贵族等级ID
  166. $attireInfo = \app\common\model\Attire::where(["use"=>1,"use_id"=>$level_id])->find();
  167. $attireInfo && \app\common\model\AttireBack::addToMyBack($attireInfo->id,$this->auth->id,$params_from);
  168. }
  169. $this->success("开通成功!");
  170. }
  171. }catch (ValidateException $e) {
  172. Db::rollback();
  173. $this->error($e->getMessage());
  174. } catch (PDOException $e) {
  175. Db::rollback();
  176. $this->error($e->getMessage());
  177. } catch (Exception $e) {
  178. Db::rollback();
  179. $this->error($e->getMessage());
  180. }
  181. }
  182. /**
  183. * 开通贵族后效果
  184. */
  185. private function onNobleEffect($noblelevelInfo,$user_id,$user_nickname) {
  186. $serviceNotice = "";
  187. $serviceSvga = "";
  188. $partyNotice = "";
  189. // 全服通知
  190. if($noblelevelInfo["qftz"] == 1) {
  191. $serviceNotice = "恭喜,玩家:".$user_nickname." 成功开通:".$noblelevelInfo["name"]."贵族特权!";
  192. }
  193. //全服动画
  194. if($noblelevelInfo["qfdh"]) {
  195. $serviceSvga = $noblelevelInfo["qfdh"];
  196. }
  197. // 公屏通知
  198. if($noblelevelInfo["gptz"] == 1) {
  199. $partyNotice = "恭喜,玩家:".$user_nickname." 成功开通:".$noblelevelInfo["name"]."贵族特权!";
  200. }
  201. $tcpArr = [];
  202. $tcpArr['type'] = "userBeNoble";
  203. $tcpArr['data'] = [
  204. 'user_id' => $user_id,
  205. 'nickname' => $user_nickname,
  206. 'serviceNotice' => $serviceNotice,
  207. 'serviceSvga' => $serviceSvga,
  208. 'partyNotice' => $partyNotice,
  209. ];
  210. $tcpJson = json_encode($tcpArr);
  211. Gateway::sendToAll($tcpJson);
  212. }
  213. /**
  214. * 开通贵族后获得专属座驾
  215. */
  216. private function onNobleAttire($noblelevelInfo,$user_id,$nextMonth,$time) {
  217. // 获取贵族专属座驾信息
  218. $nobleAttireInfo = \app\common\model\NobleAttire::where(["noble_id"=>$noblelevelInfo["id"]])->find();
  219. if($nobleAttireInfo) {
  220. Db::startTrans();
  221. try{
  222. // 更新当前用户背包使用状态
  223. $res1 = \app\common\model\AttireBack::update(["is_using"=>0],["user_id"=>$user_id]);
  224. // 保存用户背包信息
  225. $data = [];
  226. $data["user_id"] = $user_id;
  227. $data["attire_id"] = 0;
  228. $data["type"] = 1;
  229. $data["attire_name"] = $nobleAttireInfo["attire_name"];
  230. $data["price"] = $nobleAttireInfo["price"];
  231. $data["file_image"] = $nobleAttireInfo["file_image"];
  232. $data["gif_image"] = $nobleAttireInfo["gif_image"];
  233. $data["limit_day"] = round(($nextMonth-$time)/86400);
  234. $data["duetime"] = $nextMonth;
  235. $data["is_use"] = 1;
  236. $data["is_using"] = 1;
  237. $data["createtime"] = time();
  238. $res2 = \app\common\model\AttireBack::insert($data);
  239. if($res1 && $res2) {
  240. Db::commit();
  241. }
  242. }catch (ValidateException $e) {
  243. Db::rollback();
  244. $this->error($e->getMessage());
  245. } catch (PDOException $e) {
  246. Db::rollback();
  247. $this->error($e->getMessage());
  248. } catch (Exception $e) {
  249. Db::rollback();
  250. $this->error($e->getMessage());
  251. }
  252. }
  253. }
  254. public function aa() {
  255. Gateway::sendToAll("aaaaaa");
  256. }
  257. }