User.php 12 KB

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