UserCenter.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * 用户中心
  4. */
  5. namespace app\api\controller\v1;
  6. use app\api\controller\Api;
  7. use app\common\model\Members;
  8. use app\common\model\MembersTwo;
  9. use app\common\model\Game;
  10. use think\Db;
  11. use think\Env;
  12. use think\Session;
  13. class UserCenter extends Api
  14. {
  15. protected $memberModel;
  16. protected $uid;
  17. protected $game_id;
  18. protected $username;
  19. protected $userInfo;
  20. protected $sub_username; //子账号名
  21. public function _initialize()
  22. {
  23. if (!Session::has('username') || !Session::has('game_id')) {
  24. $this->error('请先登录');
  25. }
  26. //检查用户是否冻结
  27. $userInfo = model('Members')->field('id,mobile,email')->where([
  28. 'username' => Session::get('username'),
  29. 'flag' => 0,
  30. ])->find();
  31. if (empty($userInfo)) {
  32. $this->error("用户冻结");
  33. }
  34. $this->userInfo = $userInfo;
  35. $this->username = Session::get('username');
  36. $this->game_id = Session::get('game_id');
  37. $this->sub_username = Session::get('sub_username');
  38. }
  39. public function index()
  40. {
  41. //todo 获取用户平台币
  42. $isVisitor = '';
  43. $membersTwoModel = new MembersTwo();
  44. $membersTwoInfo = $membersTwoModel->where(['userid' => $this->userInfo['id']])->find();
  45. $userInfo = (new Members())->where(['id' => $this->userInfo['id']])->field('mobile,email,channel_id')->find();
  46. $gameInfo = (new Game())->where(['id' => $this->game_id])->find();
  47. // echo $gameInfo['game_auth_type']."---game_auth_type----".$gameInfo['game_bizid']."---game_bizid----<br>";
  48. if ($gameInfo['game_auth_type'] == 1 || $gameInfo['game_auth_type'] == 2) { //中宣部实名认证
  49. $gameAccountInfo = model('Subaccount')->where(['game_id' => $this->game_id, 'member_id' => $this->userInfo['id']])->find();
  50. if ($gameAccountInfo) {
  51. if ($gameAccountInfo['auth_status'] == 3) {
  52. $membersTwoInfo['realname'] = trim($gameAccountInfo['real_name']);
  53. $membersTwoInfo['idcard'] = trim($gameAccountInfo['idcard_num']);
  54. } else if ($gameAccountInfo['auth_status'] == 1) {
  55. vendor('authSdk.Authentication', '.class.php');
  56. $auth = new \Authentication();
  57. $result = $auth->query(trim($gameInfo['game_bizid']), $gameAccountInfo['member_id'] . '-' . $gameAccountInfo['id']);
  58. if ($result['errcode'] == 0) {
  59. $resultData = $result['data']['result'];
  60. if ($resultData['status'] == 0 && isset($resultData['pi']) && trim($resultData['pi']) <> '') {
  61. $updAccountData = array();
  62. $updAccountData['auth_type'] = 1;
  63. $updAccountData['auth_status'] = 3;
  64. $updAccountData['auth_pi'] = $resultData['pi'];
  65. $updAccountData['last_query_time'] = time();
  66. $updResult = model('Subaccount')->where(['id' => $gameAccountInfo['id']])->update($updAccountData);
  67. if ($updResult) {
  68. $membersTwoInfo['realname'] = trim($gameAccountInfo['real_name']);
  69. $membersTwoInfo['idcard'] = trim($gameAccountInfo['idcard_num']);
  70. } else {
  71. $isVisitor = '(认证中)';
  72. }
  73. } else if ($resultData['status'] == 1) {
  74. $updAccountData = array();
  75. $updAccountData['auth_type'] = 1;
  76. $updAccountData['last_query_time'] = time();
  77. $updResult = model('Subaccount')->where(['id' => $gameAccountInfo['id']])->update($updAccountData);
  78. $membersTwoInfo['realname'] = trim($gameAccountInfo['real_name']);
  79. $membersTwoInfo['idcard'] = trim($gameAccountInfo['idcard_num']);
  80. $isVisitor = '(认证中)';
  81. } else if ($resultData['status'] == 2) {
  82. $updAccountData = array();
  83. $updAccountData['auth_type'] = 1;
  84. $updAccountData['auth_status'] = 2;
  85. $updAccountData['last_query_time'] = time();
  86. $updResult = model('Subaccount')->where(['id' => $gameAccountInfo['id']])->update($updAccountData);
  87. $isVisitor = '(游客)';
  88. }
  89. }
  90. } else {
  91. if (empty($userInfo['mobile']) && empty($userInfo['email'])) {
  92. $isVisitor = '(游客)';
  93. }
  94. }
  95. } else {
  96. if (empty($userInfo['mobile']) && empty($userInfo['email'])) {
  97. $isVisitor = '(游客)';
  98. }
  99. }
  100. } else { //平台实名认证
  101. if ((-1 == $membersTwoInfo['realname']) && (-1 == $membersTwoInfo['idcard'])) {
  102. if (empty($userInfo['mobile']) && empty($userInfo['email'])) {
  103. $isVisitor = '(游客)';
  104. }
  105. }
  106. }
  107. $this->assign('username', $this->username);
  108. $this->assign('isVisitor', $isVisitor);
  109. $this->assign('gameInfo', $gameInfo);
  110. return $this->fetch();
  111. }
  112. public function logout()
  113. {
  114. Session::clear();
  115. }
  116. /**
  117. * 绑定中心
  118. * @return mixed
  119. * @throws \app\api\controller\HttpResponseException
  120. */
  121. public function account()
  122. {
  123. $userInfo = (new Members())->where(['id' => $this->userInfo['id']])->field('mobile,email,channel_id')->find();
  124. $gameInfo = (new Game())->where(['id' => $this->game_id])->find();
  125. $share_url = '';
  126. if (in_array($this->game_id, [223,224])) {
  127. $short_link = db('nw_promotion_short_link')->where(['game_id' => $this->game_id, 'channel_id' => $userInfo['channel_id'], 'package_type' => 0])->value('short_link');
  128. if (!empty($short_link)) {
  129. $share_url = API_DOMAIN . '/' . $short_link;
  130. }
  131. }
  132. $this->assign('gameInfo', $gameInfo);
  133. $this->assign('share_url', $share_url);
  134. $this->assign('mobile', mobileObfuscation($this->userInfo['mobile']));
  135. $this->assign('email', mailObfuscation($this->userInfo['email']));
  136. $this->assign('username', $this->username);
  137. return $this->fetch();
  138. }
  139. /**
  140. * 返回json数据到H5
  141. * @param array $data
  142. * @param int $code
  143. * @param string $msg
  144. * @return \think\response\Json
  145. */
  146. public function json($data = [], $code = 0, $msg = '')
  147. {
  148. return json(['data' => $data, 'code' => $code, 'msg' => $msg]);
  149. }
  150. /**
  151. * todo 解绑避免手机邮箱都解绑的判断防止无法找回密码
  152. */
  153. public function checkBind()
  154. {
  155. }
  156. /**
  157. * 修改密码
  158. */
  159. public function editPassword()
  160. {
  161. if ($this->request->isPost()) {
  162. $data = $this->request->post();
  163. //1.验证数据合法性 todo 密码格式加以验证
  164. if (true !== ($res = $this->validate($data, [
  165. 'old_password|旧密码' => 'require|isEmpty',
  166. 'new_password|新密码' => 'require|isEmpty|length:6,15',
  167. 're_password|重复新密码' => 'require|isEmpty|length:6,15|confirm:re_password',
  168. ]))) {
  169. return $this->json('', 0, $res);
  170. }
  171. $this->memberModel = new Members();
  172. //2.验证旧密码是否匹配
  173. $memberInfo = $this->memberModel->field('id,password,username,mobile')->where([
  174. 'username' => $this->username,
  175. 'flag' => 0,
  176. ])->find();
  177. $old_password = auth_code($data['old_password'], 'ENCODE', Env::get('auth_key'));
  178. if ($old_password !== $memberInfo['password']) {
  179. return $this->json([], 0, '旧密码错误');
  180. }
  181. //3.更新新密码
  182. $password = auth_code($data['new_password'], 'ENCODE', Env::get('auth_key'));
  183. if ($this->memberModel->update(['password' => $password], ['id' => $memberInfo['id']])) {
  184. // 插入玩家历史记录
  185. Db::table('cy_member_history')->insert([
  186. 'userid' => $memberInfo['id'],
  187. 'password' => $password,
  188. 'mobile' => $memberInfo['mobile'],
  189. 'ip' => request()->ip(),
  190. 'create_time' => time(),
  191. ]);
  192. // $memberLogic = new MemberLogic;
  193. // //同步用户信息到账户中心
  194. // $openid = $memberLogic->syncMember($memberInfo['username'], $data['password'], $memberInfo['mobile'], time());
  195. return $this->json('', 1, '密码重置成功');
  196. }
  197. }
  198. return $this->fetch('edit_password');
  199. }
  200. }