User.php 12 KB

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