User.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace addons\exam\controller;
  3. use addons\exam\library\CacheService;
  4. use addons\exam\library\WechatService;
  5. use addons\exam\model\UserInfoModel;
  6. use addons\exam\model\UserModel;
  7. use app\common\library\Token;
  8. use think\Validate;
  9. /**
  10. * 会员接口
  11. */
  12. class User extends Base
  13. {
  14. protected $noNeedLogin = ['login', 'userLogin', 'register'];
  15. protected $noNeedRight = '*';
  16. protected $visibleFields = ['id', 'avatar', 'gender', 'nickname', 'mobile', 'birthday', 'status', 'createtime', 'logintime'];
  17. /**
  18. * 授权登录
  19. * @ApiMethod (POST)
  20. * @param string $code 授权code
  21. * @param string $userInfo 授权后拿到的用户信息
  22. */
  23. public function login()
  24. {
  25. $user_info = input('userInfo/a', []);
  26. $code = input('code/s', '');
  27. $from_user_id = input('from_user_id/d', 0);
  28. if (!$code) {
  29. fail('缺少小程序参数code');
  30. }
  31. if (!$user_info) {
  32. fail('缺少小程序参数userInfo');
  33. }
  34. $service = new WechatService();
  35. $wechat_user = $service->miniLogin($code);
  36. if (!isset($wechat_user['openid'])) {
  37. fail('获取小程序用户信息失败');
  38. }
  39. $open_id = $wechat_user['openid'];
  40. $session_key = $wechat_user['session_key'] ?? '';
  41. $user = UserModel::get(['username' => $open_id]);
  42. if (empty($user)) {
  43. $user = UserModel::fastRegister($open_id, $user_info['nickName'] ?? '', $user_info['avatarUrl'] ?? '', $user_info['gender'] ?? 0);
  44. if (!$user) {
  45. fail('注册用户失败');
  46. }
  47. } else {
  48. $data = [
  49. // 'nickname' => $user_info['nickName'],
  50. // 'avatar' => $user_info['avatarUrl'],
  51. 'logintime' => time(),
  52. ];
  53. // if (!$user->parent_id) {
  54. // $data['parent_id'] = $from_user_id;
  55. // }
  56. $user->isUpdate(true)->save($data);
  57. }
  58. // 记录session_key,用于后续获取手机号码等功能
  59. CacheService::setWechatUserSessionKey($user->id, $session_key);
  60. // 清除之前的token
  61. Token::clear($user->id);
  62. // 直接登录
  63. $this->auth->direct($user->id);
  64. // 用户扩展信息
  65. $info = UserInfoModel::getUserInfo($user->id);
  66. $this->success('', [
  67. 'token' => $this->auth->getToken(),
  68. 'user' => array_merge($user->only($this->visibleFields), ['info' => $info->toArray()]),
  69. ]);
  70. }
  71. /**
  72. * 用户信息
  73. */
  74. public function info()
  75. {
  76. $user = $this->auth->getUser()->visible($this->visibleFields)->toArray();
  77. $user['info'] = UserInfoModel::getUserInfo($this->auth->id);
  78. $this->success('', $user);
  79. }
  80. /**
  81. * 获取微信绑定的手机号码
  82. */
  83. public function getWechatPhone()
  84. {
  85. $iv = input('iv/s', '');
  86. $encryptedData = input('encryptedData/s', '');
  87. if (!$iv) {
  88. fail('缺少小程序参数iv');
  89. }
  90. if (!$encryptedData) {
  91. fail('缺少小程序参数encryptedData');
  92. }
  93. if (!$session_key = CacheService::getWechatUserSessionKey($this->auth->id)) {
  94. fail('微信sessionKey丢失,请重新登录再试');
  95. }
  96. // try {
  97. $service = new WechatService();
  98. $data = $service->decryptedData($session_key, $iv, $encryptedData);
  99. succ($data);
  100. // } catch (\Exception $exception) {
  101. // fail('sessionKey失效,请重新登录再试:' . $exception->getMessage());
  102. // }
  103. }
  104. /**
  105. * 保存个人信息
  106. */
  107. public function save()
  108. {
  109. $update_fields = ['avatar', 'nickname', 'mobile', 'gender', 'birthday'];
  110. $data = ['updatetime' => time()];
  111. foreach ($update_fields as $field) {
  112. $value = input("{$field}/s", '');
  113. if ($value !== '') {
  114. $data[$field] = $value;
  115. }
  116. }
  117. $user = $this->auth->getUser();
  118. if ($user->save($data)) {
  119. succ(['user' => $user->visible($this->visibleFields)]);
  120. }
  121. fail('保存失败,请重试');
  122. }
  123. /**
  124. * 账号密码注册
  125. */
  126. public function register()
  127. {
  128. if (!$username = input('username/s')) {
  129. fail('请填写登录账号');
  130. }
  131. if (!$password = input('password/s')) {
  132. fail('请填写登录密码');
  133. }
  134. if (!$nickname = input('nickname/s')) {
  135. fail('请填写昵称');
  136. }
  137. if (!$mobile = input('mobile/s')) {
  138. fail('请填写手机号码');
  139. }
  140. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  141. fail(__('Mobile is incorrect'));
  142. }
  143. $gender = input('gender/d', 1);
  144. // 注册
  145. $user = UserModel::fastRegister($username, $nickname, '', $gender, $password, $mobile);
  146. // 用户扩展信息
  147. $info = UserInfoModel::getUserInfo($user->id);
  148. // 接口层登录
  149. $this->auth->direct($user->id);
  150. succ([
  151. 'user' => array_merge($user->only($this->visibleFields), ['info' => $info->toArray()]),
  152. 'token' => $this->auth->getToken()
  153. ]);
  154. }
  155. /**
  156. * 账号密码登录
  157. */
  158. public function userLogin()
  159. {
  160. if (!$username = input('username/s')) {
  161. fail('请填写登录账号');
  162. }
  163. if (!$password = input('password/s')) {
  164. fail('请填写登录密码');
  165. }
  166. if (!$username || !$password) {
  167. $this->error(__('Invalid parameters'));
  168. }
  169. $user = UserModel::get(['username' => $username]);
  170. if (!$user) {
  171. fail('登录失败,账号或密码错误');
  172. }
  173. if ($user->password != $this->auth->getEncryptPassword($password, $user->salt)) {
  174. fail('登录失败,账号或密码错误');
  175. }
  176. if ($user->status != 1) {
  177. fail('登录失败,账号已被禁用登录');
  178. }
  179. // 用户扩展信息
  180. $info = UserInfoModel::getUserInfo($user->id);
  181. // 接口层登录
  182. $this->auth->direct($user->id);
  183. succ([
  184. 'user' => array_merge($user->only($this->visibleFields), ['info' => $info->toArray()]),
  185. 'token' => $this->auth->getToken()
  186. ]);
  187. }
  188. /**
  189. * 保存用户常用题库设置
  190. */
  191. // public function saveMyCate()
  192. // {
  193. // if (!$cate_id = input('cate_id/d', 0)) {
  194. // fail('请选择常用题库类型');
  195. // }
  196. // if (!$cate = CateModel::get($cate_id)) {
  197. // fail('题库类型不存在,请重新选择');
  198. // }
  199. //
  200. // $cate_ids = [$cate['id']];
  201. // $cate_names = [$cate['name']];
  202. //
  203. // // 上级
  204. // if ($cateParent1 = CateModel::where('id', $cate['parent_id'])->find()) {
  205. // array_unshift($cate_ids, $cateParent1['id']);
  206. // array_unshift($cate_names, $cateParent1['name']);
  207. //
  208. // // 上上级
  209. // if ($cateParent2 = CateModel::where('id', $cateParent1['parent_id'])->find()) {
  210. // array_unshift($cate_ids, $cateParent2['id']);
  211. // array_unshift($cate_names, $cateParent2['name']);
  212. // }
  213. // }
  214. //
  215. // $info = UserInfoModel::getUserInfo($this->auth->id);
  216. // $info->default_cate_ids = $cate_ids;
  217. // $info->default_cate_names = $cate_names;
  218. //
  219. // if ($info->save()) {
  220. // succ($info->toArray());
  221. // }
  222. //
  223. // fail('保存失败,请重试');
  224. // }
  225. }