PublicController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: Dean <zxxjjforever@163.com>
  8. // +----------------------------------------------------------------------
  9. namespace api\user\controller;
  10. use think\Db;
  11. use think\facade\Validate;
  12. use cmf\controller\RestBaseController;
  13. class PublicController extends RestBaseController
  14. {
  15. /**
  16. * 用户注册
  17. * @throws \think\Exception
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. * @throws \think\exception\PDOException
  22. */
  23. public function register()
  24. {
  25. $validate = new \think\Validate([
  26. 'username' => 'require',
  27. 'password' => 'require',
  28. ]);
  29. $validate->message([
  30. 'username.require' => '请输入用户名!',
  31. 'password.require' => '请输入您的密码!',
  32. ]);
  33. $data = $this->request->param();
  34. if (!$validate->check($data)) {
  35. $this->error($validate->getError());
  36. }
  37. $findUserCount = Db::name("user")->where('user_login', $data['username'])->count();
  38. if ($findUserCount > 0) {
  39. $this->error("此账号已存在!");
  40. }
  41. $user = [];
  42. $user['user_login'] = $data['username'];
  43. $user['create_time'] = time();
  44. $user['user_status'] = 1;
  45. $user['user_type'] = 2;
  46. $user['user_pass'] = cmf_password($data['password']);
  47. $result = Db::name("user")->insert($user);
  48. if (empty($result)) {
  49. $this->error("注册失败,请重试!");
  50. }
  51. $this->success("注册并激活成功,请登录!");
  52. }
  53. /**
  54. * 用户登录
  55. * @throws \think\Exception
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws \think\exception\DbException
  59. * @throws \think\exception\PDOException
  60. */
  61. // TODO 增加最后登录信息记录,如 ip
  62. public function login()
  63. {
  64. $validate = new \think\Validate([
  65. 'username' => 'require',
  66. 'password' => 'require',
  67. ]);
  68. $validate->message([
  69. 'username.require' => '请输入用户名!',
  70. 'password.require' => '请输入您的密码!',
  71. ]);
  72. $data = $this->request->param();
  73. if (!$validate->check($data)) {
  74. $this->error($validate->getError());
  75. }
  76. $findUserWhere = [];
  77. $findUserWhere['user_login'] = $data['username'];
  78. $findUser = Db::name("user")->where($findUserWhere)->find();
  79. if (empty($findUser)) {
  80. $this->error("用户不存在!");
  81. } else {
  82. switch ($findUser['user_status']) {
  83. case 0:
  84. $this->error('您已被拉黑!');
  85. case 2:
  86. $this->error('账户还没有验证成功!');
  87. }
  88. if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
  89. $this->error("密码不正确!");
  90. }
  91. }
  92. $allowedDeviceTypes = $this->allowedDeviceTypes;
  93. if (empty($this->deviceType) && (empty($data['device_type']) || !in_array($data['device_type'], $this->allowedDeviceTypes))) {
  94. $this->error("请求错误,未知设备!");
  95. } else if (!empty($data['device_type'])) {
  96. $this->deviceType = $data['device_type'];
  97. }
  98. // Db::name("user_token")
  99. // ->where('user_id', $findUser['id'])
  100. // ->where('device_type', $data['device_type']);
  101. $findUserToken = Db::name("user_token")
  102. ->where('user_id', $findUser['id'])
  103. ->where('device_type', $this->deviceType)
  104. ->find();
  105. $currentTime = time();
  106. $expireTime = $currentTime + 24 * 3600 * 180;
  107. $token = md5(uniqid()) . md5(uniqid());
  108. if (empty($findUserToken)) {
  109. $result = Db::name("user_token")->insert([
  110. 'token' => $token,
  111. 'user_id' => $findUser['id'],
  112. 'expire_time' => $expireTime,
  113. 'create_time' => $currentTime,
  114. 'device_type' => $this->deviceType,
  115. ]);
  116. } else {
  117. $result = Db::name("user_token")
  118. ->where('user_id', $findUser['id'])
  119. ->where('device_type', $this->deviceType)
  120. ->update([
  121. 'token' => $token,
  122. 'expire_time' => $expireTime,
  123. 'create_time' => $currentTime,
  124. ]);
  125. }
  126. if (empty($result)) {
  127. $this->error("登录失败!");
  128. }
  129. $this->success("登录成功!", ['token' => $token, 'user' => $findUser]);
  130. }
  131. /**
  132. * 用户退出
  133. * @throws \think\Exception
  134. * @throws \think\exception\PDOException
  135. */
  136. public function logout()
  137. {
  138. $userId = $this->getUserId();
  139. Db::name('user_token')->where([
  140. 'token' => $this->token,
  141. 'user_id' => $userId,
  142. 'device_type' => $this->deviceType,
  143. ])->update(['token' => '']);
  144. $this->success("退出成功!");
  145. }
  146. /**
  147. * 用户密码重置
  148. * @throws \think\Exception
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. * @throws \think\exception\DbException
  152. * @throws \think\exception\PDOException
  153. */
  154. public function passwordReset()
  155. {
  156. $validate = new \think\Validate([
  157. 'username' => 'require',
  158. 'password' => 'require',
  159. 'verification_code' => 'require',
  160. ]);
  161. $validate->message([
  162. 'username.require' => '请输入手机号,邮箱!',
  163. 'password.require' => '请输入您的密码!',
  164. 'verification_code.require' => '请输入数字验证码!',
  165. ]);
  166. $data = $this->request->param();
  167. if (!$validate->check($data)) {
  168. $this->error($validate->getError());
  169. }
  170. $userWhere = [];
  171. if (Validate::is($data['username'], 'email')) {
  172. $userWhere['user_email'] = $data['username'];
  173. } else if (cmf_check_mobile($data['username'])) {
  174. $userWhere['mobile'] = $data['username'];
  175. } else {
  176. $this->error("请输入正确的手机或者邮箱格式!");
  177. }
  178. $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
  179. if (!empty($errMsg)) {
  180. $this->error($errMsg);
  181. }
  182. $userPass = cmf_password($data['password']);
  183. Db::name("user")->where($userWhere)->update(['user_pass' => $userPass]);
  184. $this->success("密码重置成功,请使用新密码登录!");
  185. }
  186. }