ChannelRebind.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * 换绑控制器
  4. *
  5. */
  6. namespace app\guildapi\controller;
  7. use app\guildapi\controller\Guild;
  8. use think\Db;
  9. use think\Config;
  10. use app\common\model\Setting;
  11. use think\Exception;
  12. class ChannelRebind extends Guild {
  13. protected $nowTime;
  14. protected $payRequestLimit = 5; //重复下单的限制时间
  15. protected $redis; //redis的句柄对象
  16. /**
  17. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  18. */
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->nowTime = NOW_TIMESTAMP;
  23. $this->redis = \think\Cache::store('default')->handler();
  24. }
  25. /**
  26. * 换绑列表
  27. * @return [type] [description]
  28. */
  29. public function index()
  30. {
  31. //判断当前账号是否有此功能
  32. if(!in_array($this->_channelLevel,[1])){
  33. $this->jsonResult('', 0, '您无权限使用该功能');
  34. }
  35. $list_rows = $this->input('list_rows',10);
  36. $page = $this->input('page',1);
  37. $account = $this->input('account','','trim');
  38. $cusername = $this->input('cusername','','trim');
  39. $begin_time = $this->input('begin_time','','trim');
  40. $end_time = $this->input('end_time','','trim');
  41. $condition = [];
  42. $condition['admin_type'] = 2;
  43. $condition['admin_id'] = $this->_channelId;
  44. if(input('account')){
  45. $condition['username'] = $account;
  46. }
  47. //申请开始时间和结束时间不为空时
  48. if ($begin_time != '' && $end_time != '') {
  49. $condition['create_time'] = [
  50. ['>=', strtotime($begin_time)],
  51. ['<=', strtotime($end_time . ' 23:59:59')],
  52. ];
  53. } //开始时间不为空时
  54. elseif ($begin_time != '') {
  55. $condition['create_time'] = ['>=', strtotime($begin_time)];
  56. } //结束时间不为空时
  57. elseif ($end_time != '') {
  58. $condition['create_time'] = ['<=', strtotime($end_time . ' 23:59:59')];
  59. }
  60. $whereRaw = '';
  61. if($this->input('cusername')<>''){
  62. $whereRaw .= ' origin_cusername="'.$cusername.'" or cusername="'.$cusername.'" ';
  63. }
  64. if($whereRaw){
  65. $rebindList = model('PlayerRebind')
  66. ->where($condition)
  67. ->whereRaw($whereRaw)
  68. ->field("id,userid,username,game_id,game_name,origin_cuserid,origin_cusername,cuserid,cusername,create_time,cmmt")
  69. ->order("id desc")
  70. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  71. ->toArray();
  72. }
  73. else{
  74. $rebindList = model('PlayerRebind')
  75. ->where($condition)
  76. ->field("id,userid,username,game_id,game_name,origin_cuserid,origin_cusername,cuserid,cusername,create_time,cmmt")
  77. ->order("id desc")
  78. ->paginate(['list_rows'=>$list_rows,'page'=>$page])
  79. ->toArray();
  80. }
  81. // echo model('PlayerRebind')->getLastSql();
  82. $this->jsonResult($rebindList, 20000, '获取列表成功');
  83. }
  84. /**
  85. * 换绑页面
  86. */
  87. public function rebind()
  88. {
  89. if ($this->request->isPost()) {
  90. //判断当前账号是否有充值权限
  91. if(!in_array($this->_channelLevel,[1])){
  92. $this->jsonResult('', 0, '您无权限进行玩家换绑操作');
  93. }
  94. else{
  95. $channelInfo = model("Channel")->field("id,name,level,parent_id,js_amount")->where(['id'=>$this->_channelId])->find();
  96. if(!$channelInfo){
  97. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  98. }
  99. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  100. if(!$adminInfo){
  101. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  102. }
  103. else if(!$adminInfo['secondary_password']){
  104. $this->jsonResult('', 0, '请设置二级密码后才能进行换绑');
  105. }
  106. $this->jsonResult($channelInfo, 20000, '获取提现信息成功');
  107. }
  108. exit;
  109. }
  110. else{
  111. $this->jsonResult('', 0, '非法请求');
  112. exit;
  113. }
  114. }
  115. /**
  116. * 换绑处理
  117. */
  118. public function doRebind()
  119. {
  120. if ($this->request->isPost()) {
  121. $game_id = $this->input('game_id',0,'intval'); //游戏ID
  122. $accounts = $this->input('accounts','','trim'); //玩家账号,多个玩家账号用换行分隔
  123. $cusername = $this->input('cusername','','trim'); //新C账号
  124. $cmmt = $this->input('cmmt','','trim'); //备注
  125. //判断当前账号是否有玩家换绑权限
  126. if(!in_array($this->_channelLevel,[1])){
  127. $this->jsonResult('', 0, '您无权限进行玩家换绑操作');
  128. }
  129. if(!$game_id){
  130. $this->jsonResult('', 0, '请选择要换绑的游戏!');
  131. }
  132. $gameInfo = model("Game")->where(['id'=>$game_id])->find();
  133. if(!$gameInfo){
  134. $this->jsonResult('', 0, '您选择换绑的游戏不存在');
  135. }
  136. if(!$cusername){
  137. $this->jsonResult('', 0, '请输入新的推广员账号!');
  138. }
  139. $promoteChannelInfo = model("Channel")->field("id,name,level,parent_id")->where(['name'=>$cusername])->find();
  140. if(!$promoteChannelInfo){
  141. $this->jsonResult('', 0, '推广员账号不存在或非您所属推广员账号');
  142. }
  143. else if($promoteChannelInfo['level']<>3){
  144. $this->jsonResult('', 0, '请输入推广员账号');
  145. }
  146. else if(get_top_channel($promoteChannelInfo['id'])['id'] <> $this->_channelId){
  147. $this->jsonResult('', 0, '推广员账号不存在或非您所属推广员账号');
  148. }
  149. $adminInfo = model("ChannelAdmin")->field("id,username,channel_id,pay_password,secondary_password")->where(['id'=>session('guild_info')['id'],'channel_id'=>$this->_channelId])->find();
  150. if(!$adminInfo){
  151. $this->jsonResult('', 0, '账号异常,请重新登录后再试');
  152. }
  153. else if(!$adminInfo['secondary_password']){
  154. $this->jsonResult('', 0, '请设置二级密码后才能进行玩家换绑');
  155. }
  156. if(!$accounts){
  157. $this->jsonResult('', 0, '请填写要换绑的玩家账号!');
  158. }
  159. else{
  160. $accountArrs = array();
  161. $accountArrs= preg_split('/\n/', $accounts);
  162. // var_dump($accountArrs);
  163. $errorMsg = "";
  164. if ($accountArrs){
  165. $userRebindArrs = array();
  166. foreach ($accountArrs as $account){
  167. $memberInfo = model('Members')->where(['username' => $account])->field('id,username,flag')->find();
  168. if(!$memberInfo){
  169. $errorMsg .= "'".$account."' 该玩家账号不存在;";
  170. }
  171. else{
  172. $memberGameChannelInfo = model('MemberChannelGame')->where(['member_id' => $memberInfo['id'], 'game_id' => $game_id])->find();
  173. if(!$memberGameChannelInfo){
  174. $errorMsg .= "'".$account."' 该玩家在游戏'".$gameInfo['name']."'中尚未绑定推广员,您不能对其进行绑定;";
  175. }
  176. else if(get_top_channel($memberGameChannelInfo['channel_id'])['id'] <> $this->_channelId){
  177. $errorMsg .= "'".$account."' 该玩家在游戏'".$gameInfo['name']."'中所属推广员不在您的公会下,您不能对其进行换绑;";
  178. }
  179. else if($memberGameChannelInfo['channel_id'] == $promoteChannelInfo['id']){
  180. $errorMsg .= "'".$account."' 该玩家在游戏'".$gameInfo['name']."'中已是属于推广员'".$promoteChannelInfo['name']."',您无需再对其进行换绑;";
  181. }
  182. else if(model('PlayerRebindApply')->where(['userid' => $memberInfo['id'],'game_id' => $game_id,'status' => 0])->count()){
  183. $errorMsg .= "'".$account."' 该玩家在游戏'".$gameInfo['name']."'中已存在换绑申请待处理,您暂时不能对其进行换绑;";
  184. }
  185. else{
  186. $userRebind = array();
  187. $userRebind['userid'] = $memberInfo['id'];
  188. $userRebind['username'] = $memberInfo['username'];
  189. $userRebind['game_id'] = $gameInfo['id'];
  190. $userRebind['game_name'] = $gameInfo['name'];
  191. $userRebind['origin_cuserid'] = $memberGameChannelInfo['channel_id'];
  192. $userRebind['origin_cusername'] = get_channel_name($memberGameChannelInfo['channel_id']);
  193. $userRebind['cuserid'] = $promoteChannelInfo['id'];
  194. $userRebind['cusername'] = $promoteChannelInfo['name'];
  195. $userRebindArrs[] = $userRebind;
  196. }
  197. }
  198. }
  199. }
  200. else{
  201. $this->jsonResult('', 0, '请填写要换绑的玩家账号!');
  202. }
  203. }
  204. $result = ''; //返回值
  205. if($userRebindArrs){
  206. // 启动事务
  207. Db::startTrans();
  208. try{
  209. foreach ($userRebindArrs as $userRebind){
  210. $updData = array();
  211. $updData['channel_id'] = $userRebind['cuserid'];
  212. $updResult = model('MemberChannelGame')->where(['member_id' => $userRebind['userid'], 'game_id' => $userRebind['game_id']])->update($updData);
  213. if (!$updResult) {
  214. throw new Exception("更新推广员失败");
  215. }
  216. $updGameServerData = array();
  217. $updGameServerData['channel_id'] = $userRebind['cuserid'];
  218. $updGameServerResult = model('MemberGameServer')->where(['member_id' => $userRebind['userid'], 'game_id' => $userRebind['game_id']])->update($updGameServerData);
  219. /*
  220. $updGameServerImeilData = array();
  221. $updGameServerImeilData['channel_id'] = $userRebind['cuserid'];
  222. $updGameServerResult = model('GameServerImeil')->where(['member_id' => $userRebind['userid'], 'game_id' => $userRebind['game_id']])->update($updGameServerImeilData);
  223. $updGameServerIpData = array();
  224. $updGameServerIpData['channel_id'] = $userRebind['cuserid'];
  225. $updGameServerResult = model('GameServerIp')->where(['member_id' => $userRebind['userid'], 'game_id' => $userRebind['game_id']])->update($updGameServerIpData);
  226. */
  227. // 插入玩家历史记录
  228. $insertHistoryId = Db::table('cy_member_history')->insert([
  229. 'userid' => $userRebind['userid'],
  230. 'channel' => $userRebind['game_name'] . "," .$userRebind['cusername'],
  231. 'ip' => request()->ip(),
  232. 'create_time' => time(),
  233. 'admin_type' => 2,
  234. 'admin_id' => $this->_channelId
  235. ]);
  236. if(!$insertHistoryId){
  237. throw new Exception("创建玩家历史记录失败! ");
  238. }
  239. $rebindData = array();
  240. $rebindData['userid'] = $userRebind['userid'];
  241. $rebindData['username'] = $userRebind['username'];
  242. $rebindData['game_id'] = $userRebind['game_id'];
  243. $rebindData['game_name'] = $userRebind['game_name'];
  244. $rebindData['origin_cuserid'] = $userRebind['origin_cuserid'];
  245. $rebindData['origin_cusername'] = $userRebind['origin_cusername'];
  246. $rebindData['cuserid'] = $userRebind['cuserid'];
  247. $rebindData['cusername'] = $userRebind['cusername'];
  248. $rebindData['create_time'] = time();
  249. $rebindData['cmmt'] = $cmmt;
  250. $rebindData['admin_type'] = 2;
  251. $rebindData['admin_id'] = $this->_channelId;
  252. $rebindData['admin_name'] = $this->_channelName;
  253. $insertRebindId = model('PlayerRebind')->insert($rebindData);
  254. if(!$insertRebindId){
  255. throw new Exception("创建玩家换绑记录失败! ");
  256. }
  257. $rebindPayFromTime = strtotime(date('Y-m-d',(time()-((date('w',time())==0?7:date('w',time()))-1)*24*3600)));
  258. $updPayData = array();
  259. $updPayData['channel_id'] = $userRebind['cuserid'];
  260. $updPayResult = model('Pay')->where(['userid' => $userRebind['userid'],'gameid' => $userRebind['game_id'],'channel_id' => $userRebind['origin_cuserid'],'create_time'=> ['egt', $rebindPayFromTime]])->update($updPayData);
  261. // echo $updPayResult."----updPayResult-----------<br>";
  262. if ($updPayResult) {
  263. $errorMsg .= "'".$account."' 该玩家在游戏'".$gameInfo['name']."'中已成功换绑,新推广员为'".$promoteChannelInfo['name']."';该玩家本周后消费订单也已做换绑。";
  264. }
  265. else{
  266. $errorMsg .= "'".$account."' 该玩家在游戏'".$gameInfo['name']."'中已成功换绑,新推广员为'".$promoteChannelInfo['name']."';";
  267. }
  268. }
  269. // 提交事务
  270. Db::commit();
  271. } catch (\Exception $e) {
  272. // 回滚事务
  273. Db::rollback();
  274. $this->jsonResult('', 0, '玩家换绑操作失败'.$e->getMessage());
  275. exit;
  276. }
  277. $result = $errorMsg;
  278. $this->jsonResult($result, 20000, $errorMsg);
  279. exit;
  280. }
  281. else{
  282. $result = $errorMsg;
  283. $this->jsonResult($result, 20000, "无相关数据");
  284. exit;
  285. }
  286. }
  287. else{
  288. $this->jsonResult('', 0, '非法请求');
  289. exit;
  290. }
  291. }
  292. }