User.php 11 KB

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