User.php 11 KB

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