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