User.php 10 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 fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use think\Db;
  10. use miniprogram\wxBizDataCrypt;
  11. /**
  12. * 会员接口
  13. */
  14. class User extends Api
  15. {
  16. protected $noNeedLogin = ['emaillogin', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $this->success('', ['welcome' => $this->auth->nickname]);
  31. }
  32. /**
  33. * 会员登录
  34. *
  35. * @ApiMethod (POST)
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function emaillogin()
  40. {
  41. $account = input('account');
  42. $password = input('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = $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. * @param string $mobile 手机号
  59. * @param string $captcha 验证码
  60. */
  61. public function mobilelogin()
  62. {
  63. $countrycode = input('countrycode',86,'intval');
  64. $mobile = input('mobile');
  65. $captcha = input('captcha');
  66. if (!$countrycode || !$mobile || !$captcha) {
  67. $this->error(__('Invalid parameters'));
  68. }
  69. /*if (!Validate::regex($mobile, "^1\d{10}$")) {
  70. $this->error(__('Mobile is incorrect'));
  71. }*/
  72. $mobile = $countrycode.$mobile;
  73. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  74. $this->error(__('Captcha is incorrect'));
  75. }
  76. $user = \app\common\model\User::getByMobile($mobile);
  77. if ($user) {
  78. if ($user->status != 1) {
  79. $this->error(__('Account is locked'));
  80. }
  81. //如果已经有账号则直接登录
  82. $ret = $this->auth->direct($user->id);
  83. } else {
  84. //$ret = $this->auth->register('', '', '', $mobile, []);
  85. $this->error('Account is incorrect');
  86. }
  87. if ($ret) {
  88. Sms::flush($mobile, 'mobilelogin');
  89. $data = $this->auth->getUserinfo();
  90. $this->success(__('Logged in successful'), $data);
  91. } else {
  92. $this->error($this->auth->getError());
  93. }
  94. }
  95. /**
  96. * 注册会员
  97. *
  98. * @ApiMethod (POST)
  99. * @param string $username 用户名
  100. * @param string $password 密码
  101. * @param string $email 邮箱
  102. * @param string $mobile 手机号
  103. * @param string $code 验证码
  104. */
  105. public function register()
  106. {
  107. $firstname = input('firstname');
  108. $lastname = input('lastname');
  109. $countrycode = input('countrycode',86,'intval');
  110. $mobile = input('mobile');
  111. $captcha = input('captcha');
  112. $email = input('email');
  113. $emailcaptcha = input('emailcaptcha');
  114. if (!$firstname || !$lastname || !$mobile || !$captcha || !$email || !$emailcaptcha ) {
  115. $this->error(__('Invalid parameters'));
  116. }
  117. if ($email && !Validate::is($email, "email")) {
  118. $this->error(__('Email is incorrect'));
  119. }
  120. /*if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  121. $this->error(__('Mobile is incorrect'));
  122. }*/
  123. $password = input('password');
  124. $fullmobile = $countrycode.$mobile;
  125. $ret = Sms::check($fullmobile, $captcha, 'register');
  126. if (!$ret) {
  127. $this->error(__('Mobile Captcha is incorrect'));
  128. }
  129. $ret = Ems::check($email, $emailcaptcha, 'register');
  130. if (!$ret) {
  131. $this->error(__('Email Captcha is incorrect'));
  132. }
  133. $extend = [
  134. 'firstname' => $firstname,
  135. 'lastname' => $lastname,
  136. 'simplemobile' => $mobile,
  137. 'height' => input('height',''),
  138. 'age' => input('age',''),
  139. 'weight' => input('weight',''),
  140. 'address' => input('address',''),
  141. ];
  142. $ret = $this->auth->register('', $password, $email, $fullmobile, $extend);
  143. if ($ret) {
  144. $data = $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. $this->auth->logout();
  160. $this->success(__('Logout successful'));
  161. }
  162. /**
  163. * 修改会员个人信息
  164. *
  165. * @ApiMethod (POST)
  166. * @param string $avatar 头像地址
  167. * @param string $username 用户名
  168. * @param string $nickname 昵称
  169. * @param string $bio 个人简介
  170. */
  171. public function profile()
  172. {
  173. $user = $this->auth->getUser();
  174. $username = input('username');
  175. $nickname = input('nickname');
  176. $bio = input('bio');
  177. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  178. if ($username) {
  179. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  180. if ($exists) {
  181. $this->error(__('Username already exists'));
  182. }
  183. $user->username = $username;
  184. }
  185. if ($nickname) {
  186. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  187. if ($exists) {
  188. $this->error(__('Nickname already exists'));
  189. }
  190. $user->nickname = $nickname;
  191. }
  192. $user->bio = $bio;
  193. $user->avatar = $avatar;
  194. $user->save();
  195. $this->success();
  196. }
  197. /**
  198. * 修改邮箱
  199. *
  200. * @ApiMethod (POST)
  201. * @param string $email 邮箱
  202. * @param string $captcha 验证码
  203. */
  204. public function changeemail()
  205. {
  206. $user = $this->auth->getUser();
  207. $email = input('email');
  208. $captcha = input('captcha');
  209. if (!$email || !$captcha) {
  210. $this->error(__('Invalid parameters'));
  211. }
  212. if (!Validate::is($email, "email")) {
  213. $this->error(__('Email is incorrect'));
  214. }
  215. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  216. $this->error(__('Email already exists'));
  217. }
  218. $result = Ems::check($email, $captcha, 'changeemail');
  219. if (!$result) {
  220. $this->error(__('Captcha is incorrect'));
  221. }
  222. $verification = $user->verification;
  223. $verification->email = 1;
  224. $user->verification = $verification;
  225. $user->email = $email;
  226. $user->save();
  227. Ems::flush($email, 'changeemail');
  228. $this->success();
  229. }
  230. /**
  231. * 修改手机号
  232. *
  233. * @ApiMethod (POST)
  234. * @param string $mobile 手机号
  235. * @param string $captcha 验证码
  236. */
  237. public function changemobile()
  238. {
  239. $user = $this->auth->getUser();
  240. $mobile = input('mobile');
  241. $captcha = input('captcha');
  242. if (!$mobile || !$captcha) {
  243. $this->error(__('Invalid parameters'));
  244. }
  245. if (!Validate::regex($mobile, "^1\d{10}$")) {
  246. $this->error(__('Mobile is incorrect'));
  247. }
  248. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  249. $this->error(__('Mobile already exists'));
  250. }
  251. $result = Sms::check($mobile, $captcha, 'changemobile');
  252. if (!$result) {
  253. $this->error(__('Captcha is incorrect'));
  254. }
  255. $verification = $user->verification;
  256. $verification->mobile = 1;
  257. $user->verification = $verification;
  258. $user->mobile = $mobile;
  259. $user->save();
  260. Sms::flush($mobile, 'changemobile');
  261. $this->success();
  262. }
  263. /**
  264. * 重置密码
  265. *
  266. * @ApiMethod (POST)
  267. * @param string $mobile 手机号
  268. * @param string $newpassword 新密码
  269. * @param string $captcha 验证码
  270. */
  271. public function resetpwd()
  272. {
  273. $type = input("type");
  274. $mobile = input("mobile");
  275. $email = input("email");
  276. $newpassword = input("newpassword");
  277. $captcha = input("captcha");
  278. if (!$newpassword || !$captcha) {
  279. $this->error(__('Invalid parameters'));
  280. }
  281. //验证Token
  282. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  283. $this->error(__('Password must be 6 to 30 characters'));
  284. }
  285. if ($type == 'mobile') {
  286. if (!Validate::regex($mobile, "^1\d{10}$")) {
  287. $this->error(__('Mobile is incorrect'));
  288. }
  289. $user = \app\common\model\User::getByMobile($mobile);
  290. if (!$user) {
  291. $this->error(__('User not found'));
  292. }
  293. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  294. if (!$ret) {
  295. $this->error(__('Captcha is incorrect'));
  296. }
  297. Sms::flush($mobile, 'resetpwd');
  298. } else {
  299. if (!Validate::is($email, "email")) {
  300. $this->error(__('Email is incorrect'));
  301. }
  302. $user = \app\common\model\User::getByEmail($email);
  303. if (!$user) {
  304. $this->error(__('User not found'));
  305. }
  306. $ret = Ems::check($email, $captcha, 'resetpwd');
  307. if (!$ret) {
  308. $this->error(__('Captcha is incorrect'));
  309. }
  310. Ems::flush($email, 'resetpwd');
  311. }
  312. //模拟一次登录
  313. $this->auth->direct($user->id);
  314. $ret = $this->auth->changepwd($newpassword, '', true);
  315. if ($ret) {
  316. $this->success(__('Reset password successful'));
  317. } else {
  318. $this->error($this->auth->getError());
  319. }
  320. }
  321. }