User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. namespace app\api\controller;
  3. use liuniu\IdentityCard;
  4. use app\admin\model\user\{UserExt};
  5. use app\admin\model\{User as UserModel};
  6. use app\admin\model\UserRelation;
  7. use app\common\controller\Api;
  8. use app\common\library\Ems;
  9. use app\common\library\Sms;
  10. use fast\Random;
  11. use liuniu\UtilService;
  12. use think\Config;
  13. use think\Request;
  14. use think\Validate;
  15. /**
  16. * 会员接口
  17. */
  18. class User extends Api
  19. {
  20. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. if (!Config::get('fastadmin.usercenter')) {
  26. $this->error(__('User center already closed'));
  27. }
  28. }
  29. /**
  30. * 会员中心
  31. */
  32. public function index()
  33. {
  34. $this->success('', ['welcome' => $this->auth->nickname]);
  35. }
  36. /**
  37. * 会员登录
  38. *
  39. * @ApiMethod (POST)
  40. * @param string $account 账号
  41. * @param string $password 密码
  42. */
  43. public function login()
  44. {
  45. $account = $this->request->post('account');
  46. $password = $this->request->post('password');
  47. if (!$account || !$password) {
  48. $this->error(__('Invalid parameters'));
  49. }
  50. $ret = $this->auth->login($account, $password,$this->cid);
  51. if ($ret) {
  52. $data = ['userinfo' => $this->auth->getUserinfo()];
  53. $this->success(__('Logged in successful'), $data);
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. /**
  59. * 手机验证码登录
  60. *
  61. * @ApiMethod (POST)
  62. * @param string $mobile 手机号
  63. * @param string $captcha 验证码
  64. */
  65. public function mobilelogin()
  66. {
  67. $mobile = $this->request->post('mobile');
  68. $captcha = $this->request->post('captcha');
  69. if (!$mobile || !$captcha) {
  70. $this->error(__('Invalid parameters'));
  71. }
  72. if (!Validate::regex($mobile, "^1\d{10}$")) {
  73. $this->error(__('Mobile is incorrect'));
  74. }
  75. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  76. $this->error(__('Captcha is incorrect'));
  77. }
  78. $user = \app\common\model\User::getByMobile($this->cid,$mobile);
  79. if ($user) {
  80. if ($user->status != 'normal') {
  81. $this->error(__('Account is locked'));
  82. }
  83. //如果已经有账号则直接登录
  84. $ret = $this->auth->direct($user->id);
  85. } else {
  86. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  87. }
  88. if ($ret) {
  89. Sms::flush($mobile, 'mobilelogin');
  90. $data = ['userinfo' => $this->auth->getUserinfo()];
  91. $this->success(__('Logged in successful'), $data);
  92. } else {
  93. $this->error($this->auth->getError());
  94. }
  95. }
  96. /**
  97. * 注册会员
  98. *
  99. * @ApiMethod (POST)
  100. * @param string $username 用户名
  101. * @param string $password 密码
  102. * @param string $email 邮箱
  103. * @param string $mobile 手机号
  104. * @param string $code 验证码
  105. */
  106. public function register()
  107. {
  108. $username = $this->request->post('username');
  109. $password = $this->request->post('password');
  110. $email = $this->request->post('email');
  111. $mobile = $this->request->post('mobile');
  112. $code = $this->request->post('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. /**
  123. $ret = Sms::check($mobile, $code, 'register');
  124. if (!$ret) {
  125. $this->error(__('Captcha is incorrect'));
  126. }**/
  127. $ret = $this->auth->register($username, $password, $email, $mobile, [],$this->cid);
  128. if ($ret) {
  129. $data = ['userinfo' => $this->auth->getUserinfo()];
  130. $this->success(__('Sign up successful'), $data);
  131. } else {
  132. $this->error($this->auth->getError());
  133. }
  134. }
  135. /**
  136. * 退出登录
  137. * @ApiMethod (POST)
  138. */
  139. public function logout()
  140. {
  141. if (!$this->request->isPost()) {
  142. $this->error(__('Invalid parameters'));
  143. }
  144. $this->auth->logout();
  145. $this->success(__('Logout successful'));
  146. }
  147. /**
  148. * 修改会员个人信息
  149. *
  150. * @ApiMethod (POST)
  151. * @param string $avatar 头像地址
  152. * @param string $username 用户名
  153. * @param string $nickname 昵称
  154. * @param string $bio 个人简介
  155. */
  156. public function profile()
  157. {
  158. $user = $this->auth->getUser();
  159. $username = $this->request->post('username');
  160. $nickname = $this->request->post('nickname');
  161. $bio = $this->request->post('bio');
  162. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  163. if ($username) {
  164. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  165. if ($exists) {
  166. $this->error(__('Username already exists'));
  167. }
  168. $user->username = $username;
  169. }
  170. if ($nickname) {
  171. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  172. if ($exists) {
  173. $this->error(__('Nickname already exists'));
  174. }
  175. $user->nickname = $nickname;
  176. }
  177. $user->bio = $bio;
  178. $user->avatar = $avatar;
  179. $user->save();
  180. $this->success();
  181. }
  182. /**
  183. * 修改邮箱
  184. *
  185. * @ApiMethod (POST)
  186. * @param string $email 邮箱
  187. * @param string $captcha 验证码
  188. */
  189. public function changeemail()
  190. {
  191. $user = $this->auth->getUser();
  192. $email = $this->request->post('email');
  193. $captcha = $this->request->post('captcha');
  194. if (!$email || !$captcha) {
  195. $this->error(__('Invalid parameters'));
  196. }
  197. if (!Validate::is($email, "email")) {
  198. $this->error(__('Email is incorrect'));
  199. }
  200. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  201. $this->error(__('Email already exists'));
  202. }
  203. $result = Ems::check($email, $captcha, 'changeemail');
  204. if (!$result) {
  205. $this->error(__('Captcha is incorrect'));
  206. }
  207. $verification = $user->verification;
  208. $verification->email = 1;
  209. $user->verification = $verification;
  210. $user->email = $email;
  211. $user->save();
  212. Ems::flush($email, 'changeemail');
  213. $this->success();
  214. }
  215. /**
  216. * 修改手机号
  217. *
  218. * @ApiMethod (POST)
  219. * @param string $mobile 手机号
  220. * @param string $captcha 验证码
  221. */
  222. public function changemobile()
  223. {
  224. $user = $this->auth->getUser();
  225. $mobile = $this->request->post('mobile');
  226. $captcha = $this->request->post('captcha');
  227. if (!$mobile || !$captcha) {
  228. $this->error(__('Invalid parameters'));
  229. }
  230. if (!Validate::regex($mobile, "^1\d{10}$")) {
  231. $this->error(__('Mobile is incorrect'));
  232. }
  233. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  234. $this->error(__('Mobile already exists'));
  235. }
  236. $result = Sms::check($mobile, $captcha, 'changemobile');
  237. if (!$result) {
  238. $this->error(__('Captcha is incorrect'));
  239. }
  240. $verification = $user->verification;
  241. $verification->mobile = 1;
  242. $user->verification = $verification;
  243. $user->mobile = $mobile;
  244. $user->save();
  245. Sms::flush($mobile, 'changemobile');
  246. $this->success();
  247. }
  248. /**
  249. * 第三方登录
  250. *
  251. * @ApiMethod (POST)
  252. * @param string $platform 平台名称
  253. * @param string $code Code码
  254. */
  255. public function third()
  256. {
  257. $url = url('user/index');
  258. $platform = $this->request->post("platform");
  259. $code = $this->request->post("code");
  260. $config = get_addon_config('third');
  261. if (!$config || !isset($config[$platform])) {
  262. $this->error(__('Invalid parameters'));
  263. }
  264. $app = new \addons\third\library\Application($config);
  265. //通过code换access_token和绑定会员
  266. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  267. if ($result) {
  268. $loginret = \addons\third\library\Service::connect($platform, $result);
  269. if ($loginret) {
  270. $data = [
  271. 'userinfo' => $this->auth->getUserinfo(),
  272. 'thirdinfo' => $result
  273. ];
  274. $this->success(__('Logged in successful'), $data);
  275. }
  276. }
  277. $this->error(__('Operation failed'), $url);
  278. }
  279. /**
  280. * 重置密码
  281. *
  282. * @ApiMethod (POST)
  283. * @param string $mobile 手机号
  284. * @param string $newpassword 新密码
  285. * @param string $captcha 验证码
  286. */
  287. public function resetpwd()
  288. {
  289. $type = $this->request->post("type");
  290. $mobile = $this->request->post("mobile");
  291. $email = $this->request->post("email");
  292. $newpassword = $this->request->post("newpassword");
  293. $captcha = $this->request->post("captcha");
  294. if (!$newpassword || !$captcha) {
  295. $this->error(__('Invalid parameters'));
  296. }
  297. if ($type == 'mobile') {
  298. if (!Validate::regex($mobile, "^1\d{10}$")) {
  299. $this->error(__('Mobile is incorrect'));
  300. }
  301. $user = \app\common\model\User::getByMobile($mobile);
  302. if (!$user) {
  303. $this->error(__('User not found'));
  304. }
  305. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  306. if (!$ret) {
  307. $this->error(__('Captcha is incorrect'));
  308. }
  309. Sms::flush($mobile, 'resetpwd');
  310. } else {
  311. if (!Validate::is($email, "email")) {
  312. $this->error(__('Email is incorrect'));
  313. }
  314. $user = \app\common\model\User::getByEmail($email);
  315. if (!$user) {
  316. $this->error(__('User not found'));
  317. }
  318. $ret = Ems::check($email, $captcha, 'resetpwd');
  319. if (!$ret) {
  320. $this->error(__('Captcha is incorrect'));
  321. }
  322. Ems::flush($email, 'resetpwd');
  323. }
  324. //模拟一次登录
  325. $this->auth->direct($user->id);
  326. $ret = $this->auth->changepwd($newpassword, '', true);
  327. if ($ret) {
  328. $this->success(__('Reset password successful'));
  329. } else {
  330. $this->error($this->auth->getError());
  331. }
  332. }
  333. public function userinfo()
  334. {
  335. $info = $this->auth->getUserinfo();
  336. $ext = UserExt::where('user_id',$info['id'])->find();
  337. if($ext)
  338. $info['ext'] = $ext->toArray();
  339. else
  340. $info['ext'] = null;
  341. $this->success('获取成功',$info);
  342. }
  343. public function setuser(Request $request)
  344. {
  345. $where = UtilService::postMore(
  346. [
  347. ['full_name',''],
  348. ['vocation',''],
  349. ['position',''],
  350. ['education',''],
  351. ['tel',''],
  352. ['address',''],
  353. ['company_name',''],
  354. ['nature',''],
  355. ['legal_person',''],
  356. ['zip_code',''],
  357. ['compnay_address',''],
  358. ['company_email',''],
  359. ['certificateimage',''],
  360. ['id_card',''],
  361. ['nation',''],
  362. ['birthday',''],
  363. ['user_type',0],
  364. ['mobile',''],
  365. ],$request
  366. );
  367. $user = [];$user_ext = [];
  368. $info = UserModel::find($this->auth->getUserinfo()['id']);
  369. if($info['user_type']!=0 && $info['user_type']!=$where['user_type']) $this->error('用户类型不可修改');
  370. if(!preg_match("/^1\d{10}$/", $where['mobile'])) $this->error('手机号不对');
  371. if(IdentityCard::isValid($where['id_card']))$this->error('身份证号不正确!');
  372. foreach ($where as $key=>$v)
  373. {
  374. if($v!='')
  375. {
  376. if(in_array($key,['id_card','birthday','user_type','mobile']))
  377. {
  378. $user[$key] = $v;
  379. }
  380. else
  381. {
  382. $user_ext[$key] =$v;
  383. }
  384. }
  385. }
  386. if(sizeof($user)>0)
  387. {
  388. UserModel::where('id',$this->auth->getUserinfo()['id'])->update($user);
  389. }
  390. if(sizeof($user_ext)>0)
  391. {
  392. UserExt::where('id',$this->auth->getUserinfo()['id'])->update($user_ext);
  393. }
  394. $this->success('修改成功');
  395. }
  396. }