User.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // | Original reference: https://gitee.com/karson/fastadmin
  12. // +----------------------------------------------------------------------
  13. // +----------------------------------------------------------------------
  14. // | 会员接口
  15. // +----------------------------------------------------------------------
  16. namespace app\api\controller;
  17. use app\common\controller\Api;
  18. use app\common\library\Ems;
  19. use app\common\library\Sms;
  20. use think\exception\ValidateException;
  21. use think\facade\Config;
  22. use think\facade\Validate;
  23. class User extends Api
  24. {
  25. protected $noNeedLogin = ['login', 'register'];
  26. protected $noNeedRight = '*';
  27. public function initialize()
  28. {
  29. parent::initialize();
  30. if (!Config::get('yzn.user_center')) {
  31. $this->error('会员中心已经关闭');
  32. }
  33. }
  34. /**
  35. * 会员中心
  36. */
  37. public function index()
  38. {
  39. $this->success('', ['welcome' => $this->auth->nickname]);
  40. }
  41. /**
  42. * 会员登录
  43. */
  44. public function login()
  45. {
  46. $account = $this->request->param('account');
  47. $password = $this->request->param('password');
  48. $rule = [
  49. 'account|账户' => 'require|length:3,30',
  50. 'password|密码' => 'require|length:3,30',
  51. ];
  52. $data = [
  53. 'account' => $account,
  54. 'password' => $password,
  55. ];
  56. try {
  57. $this->validate($data, $rule);
  58. } catch (ValidateException $e) {
  59. // 验证失败 输出错误信息
  60. $this->error($e->getError());
  61. }
  62. $ret = $this->auth->login($account, $password);
  63. if ($ret) {
  64. $data = ['userinfo' => $this->auth->getUserinfo()];
  65. $this->success('登录成功!', $data);
  66. } else {
  67. $this->error($this->auth->getError());
  68. }
  69. }
  70. /**
  71. * 注册会员
  72. */
  73. public function register()
  74. {
  75. $extend = [];
  76. $data = $this->request->post();
  77. $rule = [
  78. 'username|用户名' => 'unique:user|require|alphaDash|length:3,20',
  79. 'nickname|昵称' => 'unique:user|length:3,20',
  80. 'mobile|手机' => 'require|unique:user|mobile',
  81. 'password|密码' => 'require|length:3,20',
  82. 'email|邮箱' => 'unique:user|require|email',
  83. ];
  84. if (config::get('yzn.user_mobile_verify')) {
  85. $rule['captcha_mobile|手机验证码'] = "require";
  86. }
  87. if (config::get('yzn.user_email_verify')) {
  88. $rule['captcha_email|邮箱验证码'] = "require";
  89. }
  90. try {
  91. $this->validate($data, $rule);
  92. } catch (ValidateException $e) {
  93. // 验证失败 输出错误信息
  94. $this->error($e->getError());
  95. }
  96. if (config::get('yzn.user_mobile_verify')) {
  97. $result = Sms::check($data['mobile'], $data['captcha_mobile'], 'register');
  98. if (!$result) {
  99. $this->error('手机验证码错误!');
  100. }
  101. $extend['ischeck_mobile'] = 1;
  102. }
  103. if (config::get('yzn.user_email_verify')) {
  104. $result = Ems::check($data['email'], $data['captcha_email'], 'register');
  105. if (!$result) {
  106. $this->error('邮箱验证码错误!');
  107. }
  108. $extend['ischeck_email'] = 1;
  109. }
  110. $ret = $this->auth->register($data['username'], $data['password'], $data['email'], $data['mobile'], $extend);
  111. if ($ret) {
  112. $data = ['userinfo' => $this->auth->getUserinfo()];
  113. $this->success('会员注册成功!', $data);
  114. } else {
  115. $this->error($this->auth->getError());
  116. }
  117. }
  118. //手动退出登录
  119. public function logout()
  120. {
  121. if (!$this->request->isPost()) {
  122. $this->error('参数不正确');
  123. }
  124. $this->auth->logout();
  125. $this->success('注销成功!', url("index/login"));
  126. }
  127. /**
  128. * 修改会员个人信息
  129. */
  130. public function profile()
  131. {
  132. $user = $this->auth->getUser();
  133. $data = $this->request->only(['avatar', 'username', 'nickname', 'gender', 'birthday', 'motto']);
  134. $data['id'] = $user->id;
  135. //验证数据合法性
  136. $rule = [
  137. 'username|用户名' => 'unique:user|alphaDash|length:3,20',
  138. 'nickname|昵称' => 'unique:user|length:3,20',
  139. 'birthday|生日' => 'date',
  140. ];
  141. try {
  142. $this->validate($data, $rule);
  143. } catch (ValidateException $e) {
  144. // 验证失败 输出错误信息
  145. $this->error($e->getError());
  146. }
  147. $user->save($data);
  148. $this->success("基本信息修改成功!");
  149. }
  150. /**
  151. * 修改邮箱
  152. */
  153. public function changeemail()
  154. {
  155. $user = $this->auth->getUser();
  156. $email = $this->request->param('email');
  157. $captcha = $this->request->param('captcha');
  158. if (!$email || !$captcha) {
  159. $this->error('参数不得为空!');
  160. }
  161. if (!Validate::is($email, "email")) {
  162. $this->error('邮箱格式不正确!');
  163. }
  164. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  165. $this->error('邮箱已占用');
  166. }
  167. $result = Ems::check($email, $captcha, 'changeemail');
  168. if (!$result) {
  169. $this->error('验证码错误!');
  170. }
  171. //只修改邮箱
  172. $user->ischeck_email = 1;
  173. $user->email = $email;
  174. $user->save();
  175. Ems::flush($email, 'changeemail');
  176. $this->success();
  177. }
  178. /**
  179. * 修改手机号
  180. */
  181. public function changemobile()
  182. {
  183. $user = $this->auth->getUser();
  184. $mobile = $this->request->param('mobile');
  185. $captcha = $this->request->param('captcha');
  186. if (!$mobile || !$captcha) {
  187. $this->error('参数不得为空!');
  188. }
  189. if (!Validate::isMobile($mobile)) {
  190. $this->error('手机号格式不正确!');
  191. }
  192. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  193. $this->error('手机号已占用');
  194. }
  195. $result = Sms::check($mobile, $captcha, 'changemobile');
  196. if (!$result) {
  197. $this->error('验证码错误!');
  198. }
  199. //只修改手机号
  200. $user->mobile = $mobile;
  201. $user->ischeck_mobile = 1;
  202. $user->save();
  203. Sms::flush($mobile, 'changemobile');
  204. $this->success();
  205. }
  206. /**
  207. * 激活邮箱
  208. */
  209. public function actemail()
  210. {
  211. $user = $this->auth->getUser();
  212. $captcha = $this->request->param('captcha');
  213. if (!$captcha) {
  214. $this->error('参数不得为空!');
  215. }
  216. $result = Ems::check($user->email, $captcha, 'actemail');
  217. if (!$result) {
  218. $this->error('验证码错误!');
  219. }
  220. $user->ischeck_email = 1;
  221. $user->save();
  222. Ems::flush($user->email, 'actemail');
  223. $this->success('激活成功!');
  224. }
  225. /**
  226. * 激活手机号
  227. */
  228. public function actmobile()
  229. {
  230. $user = $this->auth->getUser();
  231. $captcha = $this->request->param('captcha');
  232. if (!$captcha) {
  233. $this->error('参数不得为空!');
  234. }
  235. $result = Sms::check($user->mobile, $captcha, 'actmobile');
  236. if (!$result) {
  237. $this->error('验证码错误!');
  238. }
  239. $user->ischeck_mobile = 1;
  240. $user->save();
  241. Sms::flush($user->mobile, 'actmobile');
  242. $this->success('激活成功!');
  243. }
  244. }