User.php 14 KB

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