User.php 11 KB

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