User.php 14 KB

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