User.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use think\Db;
  10. use miniprogram\wxBizDataCrypt;
  11. /**
  12. * 会员接口
  13. */
  14. class User extends Api
  15. {
  16. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getUserOpenid','wxMiniProgramLogin'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $this->success('', ['welcome' => $this->auth->nickname]);
  31. }
  32. /**
  33. * 会员登录
  34. *
  35. * @ApiMethod (POST)
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = input('account');
  42. $password = input('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = ['userinfo' => $this->auth->getUserinfo()];
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @ApiMethod (POST)
  58. * @param string $mobile 手机号
  59. * @param string $captcha 验证码
  60. */
  61. public function mobilelogin()
  62. {
  63. $mobile = input('mobile');
  64. $captcha = input('captcha');
  65. if (!$mobile || !$captcha) {
  66. $this->error(__('Invalid parameters'));
  67. }
  68. if (!Validate::regex($mobile, "^1\d{10}$")) {
  69. $this->error(__('Mobile is incorrect'));
  70. }
  71. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  72. $this->error(__('Captcha is incorrect'));
  73. }
  74. $user = \app\common\model\User::getByMobile($mobile);
  75. if ($user) {
  76. if ($user->status != 1) {
  77. $this->error(__('Account is locked'));
  78. }
  79. //如果已经有账号则直接登录
  80. $ret = $this->auth->direct($user->id);
  81. } else {
  82. $ret = $this->auth->register('', '', '', $mobile, []);
  83. }
  84. if ($ret) {
  85. Sms::flush($mobile, 'mobilelogin');
  86. $data = $this->auth->getUserinfo();
  87. $this->success(__('Logged in successful'), $data);
  88. } else {
  89. $this->error($this->auth->getError());
  90. }
  91. }
  92. /**
  93. * 注册会员
  94. *
  95. * @ApiMethod (POST)
  96. * @param string $username 用户名
  97. * @param string $password 密码
  98. * @param string $email 邮箱
  99. * @param string $mobile 手机号
  100. * @param string $code 验证码
  101. */
  102. public function register()
  103. {
  104. $username = input('username');
  105. $password = input('password');
  106. $email = input('email');
  107. $mobile = input('mobile');
  108. $code = input('code');
  109. if (!$username || !$password) {
  110. $this->error(__('Invalid parameters'));
  111. }
  112. if ($email && !Validate::is($email, "email")) {
  113. $this->error(__('Email is incorrect'));
  114. }
  115. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  116. $this->error(__('Mobile is incorrect'));
  117. }
  118. $ret = Sms::check($mobile, $code, 'register');
  119. if (!$ret) {
  120. $this->error(__('Captcha is incorrect'));
  121. }
  122. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  123. if ($ret) {
  124. $data = ['userinfo' => $this->auth->getUserinfo()];
  125. $this->success(__('Sign up successful'), $data);
  126. } else {
  127. $this->error($this->auth->getError());
  128. }
  129. }
  130. /**
  131. * 退出登录
  132. * @ApiMethod (POST)
  133. */
  134. public function logout()
  135. {
  136. if (!$this->request->isPost()) {
  137. $this->error(__('Invalid parameters'));
  138. }
  139. $this->auth->logout();
  140. $this->success(__('Logout successful'));
  141. }
  142. /**
  143. * 修改会员个人信息
  144. *
  145. * @ApiMethod (POST)
  146. * @param string $avatar 头像地址
  147. * @param string $username 用户名
  148. * @param string $nickname 昵称
  149. * @param string $bio 个人简介
  150. */
  151. public function profile()
  152. {
  153. $user = $this->auth->getUser();
  154. $username = input('username');
  155. $nickname = input('nickname');
  156. $bio = input('bio');
  157. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  158. if ($username) {
  159. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  160. if ($exists) {
  161. $this->error(__('Username already exists'));
  162. }
  163. $user->username = $username;
  164. }
  165. if ($nickname) {
  166. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  167. if ($exists) {
  168. $this->error(__('Nickname already exists'));
  169. }
  170. $user->nickname = $nickname;
  171. }
  172. $user->bio = $bio;
  173. $user->avatar = $avatar;
  174. $user->save();
  175. $this->success();
  176. }
  177. /**
  178. * 修改邮箱
  179. *
  180. * @ApiMethod (POST)
  181. * @param string $email 邮箱
  182. * @param string $captcha 验证码
  183. */
  184. public function changeemail()
  185. {
  186. $user = $this->auth->getUser();
  187. $email = input('email');
  188. $captcha = input('captcha');
  189. if (!$email || !$captcha) {
  190. $this->error(__('Invalid parameters'));
  191. }
  192. if (!Validate::is($email, "email")) {
  193. $this->error(__('Email is incorrect'));
  194. }
  195. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  196. $this->error(__('Email already exists'));
  197. }
  198. $result = Ems::check($email, $captcha, 'changeemail');
  199. if (!$result) {
  200. $this->error(__('Captcha is incorrect'));
  201. }
  202. $verification = $user->verification;
  203. $verification->email = 1;
  204. $user->verification = $verification;
  205. $user->email = $email;
  206. $user->save();
  207. Ems::flush($email, 'changeemail');
  208. $this->success();
  209. }
  210. /**
  211. * 修改手机号
  212. *
  213. * @ApiMethod (POST)
  214. * @param string $mobile 手机号
  215. * @param string $captcha 验证码
  216. */
  217. public function changemobile()
  218. {
  219. $user = $this->auth->getUser();
  220. $mobile = input('mobile');
  221. $captcha = input('captcha');
  222. if (!$mobile || !$captcha) {
  223. $this->error(__('Invalid parameters'));
  224. }
  225. if (!Validate::regex($mobile, "^1\d{10}$")) {
  226. $this->error(__('Mobile is incorrect'));
  227. }
  228. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  229. $this->error(__('Mobile already exists'));
  230. }
  231. $result = Sms::check($mobile, $captcha, 'changemobile');
  232. if (!$result) {
  233. $this->error(__('Captcha is incorrect'));
  234. }
  235. $verification = $user->verification;
  236. $verification->mobile = 1;
  237. $user->verification = $verification;
  238. $user->mobile = $mobile;
  239. $user->save();
  240. Sms::flush($mobile, 'changemobile');
  241. $this->success();
  242. }
  243. /**
  244. * 重置密码
  245. *
  246. * @ApiMethod (POST)
  247. * @param string $mobile 手机号
  248. * @param string $newpassword 新密码
  249. * @param string $captcha 验证码
  250. */
  251. public function resetpwd()
  252. {
  253. $type = input("type");
  254. $mobile = input("mobile");
  255. $email = input("email");
  256. $newpassword = input("newpassword");
  257. $captcha = input("captcha");
  258. if (!$newpassword || !$captcha) {
  259. $this->error(__('Invalid parameters'));
  260. }
  261. //验证Token
  262. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  263. $this->error(__('Password must be 6 to 30 characters'));
  264. }
  265. if ($type == 'mobile') {
  266. if (!Validate::regex($mobile, "^1\d{10}$")) {
  267. $this->error(__('Mobile is incorrect'));
  268. }
  269. $user = \app\common\model\User::getByMobile($mobile);
  270. if (!$user) {
  271. $this->error(__('User not found'));
  272. }
  273. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  274. if (!$ret) {
  275. $this->error(__('Captcha is incorrect'));
  276. }
  277. Sms::flush($mobile, 'resetpwd');
  278. } else {
  279. if (!Validate::is($email, "email")) {
  280. $this->error(__('Email is incorrect'));
  281. }
  282. $user = \app\common\model\User::getByEmail($email);
  283. if (!$user) {
  284. $this->error(__('User not found'));
  285. }
  286. $ret = Ems::check($email, $captcha, 'resetpwd');
  287. if (!$ret) {
  288. $this->error(__('Captcha is incorrect'));
  289. }
  290. Ems::flush($email, 'resetpwd');
  291. }
  292. //模拟一次登录
  293. $this->auth->direct($user->id);
  294. $ret = $this->auth->changepwd($newpassword, '', true);
  295. if ($ret) {
  296. $this->success(__('Reset password successful'));
  297. } else {
  298. $this->error($this->auth->getError());
  299. }
  300. }
  301. }