User.php 13 KB

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