User.php 11 KB

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