Noble.php 11 KB

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