User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 app\common\library\Token;
  10. use think\Db;
  11. use app\common\model\UserDeviceInfo;
  12. /**
  13. * 会员接口,登录,注册,修改资料等
  14. */
  15. class User extends Api
  16. {
  17. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. if (!Config::get('fastadmin.usercenter')) {
  23. $this->error(__('User center already closed'));
  24. }
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 会员登录
  35. *
  36. * @ApiMethod (POST)
  37. * @param string $account 账号
  38. * @param string $password 密码
  39. */
  40. public function login()
  41. {
  42. $account = $this->request->post('account');
  43. $password = $this->request->post('password');
  44. if (!$account || !$password) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. $ret = $this->auth->login($account, $password);
  48. if ($ret) {
  49. $data = $this->userInfo('return');
  50. $this->success(__('Logged in successful'), $data);
  51. } else {
  52. $this->error($this->auth->getError());
  53. }
  54. }
  55. /**
  56. * 手机验证码登录
  57. *
  58. * @ApiMethod (POST)
  59. * @param string $mobile 手机号
  60. * @param string $captcha 验证码
  61. */
  62. public function mobilelogin()
  63. {
  64. $mobile = $this->request->post('mobile');
  65. $captcha = $this->request->post('captcha');
  66. if (!$mobile || !$captcha) {
  67. $this->error(__('Invalid parameters'));
  68. }
  69. if (!Validate::regex($mobile, "^1\d{10}$")) {
  70. $this->error(__('Mobile is incorrect'));
  71. }
  72. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  73. $this->error(__('Captcha is incorrect'));
  74. }
  75. $user = \app\common\model\User::getByMobile($mobile);
  76. if ($user) {
  77. if ($user->status != 1) {
  78. $this->error(__('Account is locked'));
  79. }
  80. //如果已经有账号则直接登录
  81. $ret = $this->auth->direct($user->id);
  82. } else {
  83. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  84. }
  85. if ($ret) {
  86. Sms::flush($mobile, 'mobilelogin');
  87. $data = $this->userInfo('return');
  88. $this->success(__('Logged in successful'), $data);
  89. } else {
  90. $this->error($this->auth->getError());
  91. }
  92. }
  93. /**
  94. * 注册会员
  95. *
  96. * @ApiMethod (POST)
  97. * @param string $username 用户名
  98. * @param string $password 密码
  99. * @param string $email 邮箱
  100. * @param string $mobile 手机号
  101. * @param string $code 验证码
  102. */
  103. /*public function register()
  104. {
  105. $username = $this->request->post('username');
  106. $password = $this->request->post('password');
  107. $email = $this->request->post('email');
  108. $mobile = $this->request->post('mobile');
  109. $code = $this->request->post('code');
  110. if (!$username || !$password) {
  111. $this->error(__('Invalid parameters'));
  112. }
  113. if ($email && !Validate::is($email, "email")) {
  114. $this->error(__('Email is incorrect'));
  115. }
  116. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  117. $this->error(__('Mobile is incorrect'));
  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, []);
  124. if ($ret) {
  125. $data = $this->userInfo('return');
  126. $this->success(__('Sign up successful'), $data);
  127. } else {
  128. $this->error($this->auth->getError());
  129. }
  130. }*/
  131. //用户详细资料
  132. public function userInfo($type = 1){
  133. $info = $this->auth->getUserinfo();
  134. if($type == 'return'){
  135. return $info;
  136. }
  137. $this->success(__('success'),$info);
  138. }
  139. //申请真人认证
  140. public function apply_real_confirm(){
  141. Db::name('user')->where('id',$this->auth->id)->update(['real_status'=>1]);
  142. $this->success();
  143. }
  144. //实名认证信息
  145. public function idcard_confirm_info(){
  146. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  147. $this->success('success',$check);
  148. }
  149. //申请实名认证
  150. public function apply_idcard_confirm(){
  151. $truename = input('truename','');
  152. $idcard = input('idcard','');
  153. $idcard_images = input('idcard_images','');
  154. $alipay_account = input('alipay_account','');
  155. if(empty($truename) || empty($idcard) || empty($idcard_images) || empty($alipay_account)){
  156. $this->error('实名认证信息必填');
  157. }
  158. if($this->auth->idcard_status == 1){
  159. $this->error('您已经完成实名认证');
  160. }
  161. if($this->auth->idcard_status == 0){
  162. $this->error('您已经提交实名认证,请等待审核');
  163. }
  164. Db::startTrans();
  165. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  166. if(!empty($check)){
  167. if($check['status'] == 0){
  168. Db::rollback();
  169. $this->error('您已经提交实名认证,请等待审核');
  170. }
  171. if($check['status'] == 1){
  172. Db::rollback();
  173. $this->error('您已经完成实名认证');
  174. }
  175. }
  176. $data = [
  177. 'user_id' => $this->auth->id,
  178. 'truename' => $truename,
  179. 'idcard' => $idcard,
  180. 'idcard_images' => $idcard_images,
  181. 'alipay_account' => $alipay_account,
  182. 'status' => 0,
  183. 'createtime' => time(),
  184. 'updatetime' => time(),
  185. ];
  186. //更新
  187. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  188. if(!empty($check)){
  189. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  190. }else{
  191. $rs = Db::name('user_idconfirm')->insertGetId($data);
  192. }
  193. if(!$rs || !$update_rs){
  194. Db::rollback();
  195. $this->error('提交失败');
  196. }
  197. Db::commit();
  198. $this->success('提交成功,请等待审核');
  199. }
  200. /**
  201. * 退出登录
  202. * @ApiMethod (POST)
  203. */
  204. public function logout()
  205. {
  206. if (!$this->request->isPost()) {
  207. $this->error(__('Invalid parameters'));
  208. }
  209. $this->auth->logout();
  210. $this->success(__('Logout successful'));
  211. }
  212. /**
  213. * 修改会员个人信息
  214. *
  215. * @ApiMethod (POST)
  216. * @param string $avatar 头像地址
  217. * @param string $username 用户名
  218. * @param string $nickname 昵称
  219. * @param string $bio 个人简介
  220. */
  221. public function profile()
  222. {
  223. $field_array = ['nickname','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid'];
  224. $data = [];
  225. foreach($field_array as $key => $field){
  226. $newone = input_post($field);
  227. if($field == 'avatar'){
  228. $newone = input_post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  229. }
  230. if($field == 'photo_images'){
  231. $newone = input_post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  232. }
  233. if(!empty($newone)){
  234. $data[$field] = $newone;
  235. }
  236. }
  237. if(isset($data['birthday'])){
  238. $data['birthday'] = strtotime($data['birthday']);
  239. }
  240. if(isset($data['avatar'])){
  241. $data['real_status'] = -1; //或许应该改成0
  242. }
  243. if(isset($data['hobby_ids'])){
  244. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids'])); //或许应该改成0
  245. }
  246. if(isset($data['tag_ids'])){
  247. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids'])); //或许应该改成0
  248. }
  249. //dump($data);
  250. if(empty($data)){
  251. $this->error('没有任何改变');
  252. }
  253. Db::name('user')->where('id',$this->auth->id)->update($data);
  254. $this->success();
  255. }
  256. public function set_status_switch(){
  257. }
  258. /*
  259. * 修改用户的坐标
  260. * */
  261. public function change_longlat(){
  262. $longitude = input_post('longitude');
  263. $latitude = input_post('latitude');
  264. $cityname = input_post('cityname');
  265. if(empty($longitude) || empty($latitude) || empty($cityname)){
  266. $this->error();
  267. }
  268. $data = [
  269. 'longitude' => $longitude,
  270. 'latitude' => $latitude,
  271. 'cityname' => $cityname,
  272. ];
  273. Db::name('user')->where('id',$this->auth->id)->update($data);
  274. $this->success();
  275. }
  276. /**
  277. * 修改邮箱
  278. *
  279. * @ApiMethod (POST)
  280. * @param string $email 邮箱
  281. * @param string $captcha 验证码
  282. */
  283. /*public function changeemail()
  284. {
  285. $user = $this->auth->getUser();
  286. $email = $this->request->post('email');
  287. $captcha = $this->request->post('captcha');
  288. if (!$email || !$captcha) {
  289. $this->error(__('Invalid parameters'));
  290. }
  291. if (!Validate::is($email, "email")) {
  292. $this->error(__('Email is incorrect'));
  293. }
  294. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  295. $this->error(__('Email already exists'));
  296. }
  297. $result = Ems::check($email, $captcha, 'changeemail');
  298. if (!$result) {
  299. $this->error(__('Captcha is incorrect'));
  300. }
  301. $verification = $user->verification;
  302. $verification->email = 1;
  303. $user->verification = $verification;
  304. $user->email = $email;
  305. $user->save();
  306. Ems::flush($email, 'changeemail');
  307. $this->success();
  308. }*/
  309. /**
  310. * 修改手机号
  311. *
  312. * @ApiMethod (POST)
  313. * @param string $mobile 手机号
  314. * @param string $captcha 验证码
  315. */
  316. public function changemobile()
  317. {
  318. $user = $this->auth->getUser();
  319. $oldcaptcha = $this->request->request('oldcaptcha');
  320. $mobile = $this->request->request('mobile');
  321. $captcha = $this->request->request('captcha');
  322. if (!$oldcaptcha || !$mobile || !$captcha) {
  323. $this->error(__('Invalid parameters'));
  324. }
  325. if (!Validate::regex($mobile, "^1\d{10}$")) {
  326. $this->error(__('Mobile is incorrect'));
  327. }
  328. if($user->mobile == $mobile){
  329. $this->error('新手机号不能与旧手机号相同');
  330. }
  331. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  332. $this->error(__('Mobile already exist'));
  333. }
  334. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  335. if (!$result) {
  336. $this->error(__('Captcha is incorrect'));
  337. }
  338. $result = Sms::check($mobile, $captcha, 'changemobile');
  339. if (!$result) {
  340. $this->error(__('Captcha is incorrect'));
  341. }
  342. $verification = $user->verification;
  343. $verification->mobile = 1;
  344. $user->verification = $verification;
  345. $user->mobile = $mobile;
  346. $user->save();
  347. Sms::flush($user->mobile, 'changemobile');
  348. Sms::flush($mobile, 'changemobile');
  349. $this->success();
  350. }
  351. /**
  352. * 第三方登录
  353. *
  354. * @ApiMethod (POST)
  355. * @param string $platform 平台名称
  356. * @param string $code Code码
  357. */
  358. /*public function third()
  359. {
  360. $url = url('user/index');
  361. $platform = $this->request->post("platform");
  362. $code = $this->request->post("code");
  363. $config = get_addon_config('third');
  364. if (!$config || !isset($config[$platform])) {
  365. $this->error(__('Invalid parameters'));
  366. }
  367. $app = new \addons\third\library\Application($config);
  368. //通过code换access_token和绑定会员
  369. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  370. if ($result) {
  371. $loginret = \addons\third\library\Service::connect($platform, $result);
  372. if ($loginret) {
  373. $data = [
  374. 'userinfo' => $this->auth->getUserinfo(),
  375. 'thirdinfo' => $result
  376. ];
  377. $this->success(__('Logged in successful'), $data);
  378. }
  379. }
  380. $this->error(__('Operation failed'), $url);
  381. }*/
  382. /**
  383. * 重置密码
  384. *
  385. * @ApiMethod (POST)
  386. * @param string $mobile 手机号
  387. * @param string $newpassword 新密码
  388. * @param string $captcha 验证码
  389. */
  390. public function resetpwd()
  391. {
  392. //$type = $this->request->post("type");
  393. $type = 'mobile';
  394. $mobile = $this->request->post("mobile");
  395. $email = $this->request->post("email");
  396. $newpassword = $this->request->post("newpassword");
  397. $captcha = $this->request->post("captcha");
  398. if (!$newpassword || !$captcha) {
  399. $this->error(__('Invalid parameters'));
  400. }
  401. if ($type == 'mobile') {
  402. if (!Validate::regex($mobile, "^1\d{10}$")) {
  403. $this->error(__('Mobile is incorrect'));
  404. }
  405. $user = \app\common\model\User::getByMobile($mobile);
  406. if (!$user) {
  407. $this->error(__('User not found'));
  408. }
  409. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  410. if (!$ret) {
  411. $this->error(__('Captcha is incorrect'));
  412. }
  413. Sms::flush($mobile, 'resetpwd');
  414. } else {
  415. if (!Validate::is($email, "email")) {
  416. $this->error(__('Email is incorrect'));
  417. }
  418. $user = \app\common\model\User::getByEmail($email);
  419. if (!$user) {
  420. $this->error(__('User not found'));
  421. }
  422. $ret = Ems::check($email, $captcha, 'resetpwd');
  423. if (!$ret) {
  424. $this->error(__('Captcha is incorrect'));
  425. }
  426. Ems::flush($email, 'resetpwd');
  427. }
  428. //模拟一次登录
  429. $this->auth->direct($user->id);
  430. $ret = $this->auth->changepwd($newpassword, '', true);
  431. if ($ret) {
  432. $this->success(__('Reset password successful'));
  433. } else {
  434. $this->error($this->auth->getError());
  435. }
  436. }
  437. /**
  438. * 修改密码
  439. *
  440. * @ApiMethod (POST)
  441. * @param string $newpassword 新密码
  442. * @param string $oldpassword 旧密码
  443. */
  444. public function changepwd(){
  445. $newpassword = input('post.newpassword');
  446. $oldpassword = input('post.oldpassword','');
  447. if (!$newpassword) {
  448. $this->error(__('Invalid parameters'));
  449. }
  450. if($this->auth->password && empty($oldpassword)){
  451. $this->error('原密码必填');
  452. }
  453. if(empty($this->auth->password)){
  454. $ret = $this->auth->changepwd($newpassword, '', true);
  455. }else{
  456. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  457. }
  458. if ($ret) {
  459. $this->success(__('Reset password successful'));
  460. } else {
  461. $this->error($this->auth->getError());
  462. }
  463. }
  464. /**
  465. * 记录当前登陆的设备ID,设备信息,IP等
  466. */
  467. public function changeDeviceIp()
  468. {
  469. // 接口防并发
  470. if (!$this->apiLimit(1, 5000)) {
  471. return ;
  472. }
  473. $user = $this->auth->getUser();
  474. $ip = request()->ip();
  475. $deviceId = $this->request->request('device_id','');
  476. $phoneModel = $this->request->request('phone_model','');
  477. $brand = $this->request->request('brand','');
  478. $apiVersion = $this->request->request('api_version','');
  479. $deviceOs = $this->request->request('device_os','');
  480. if ($ip !== $user->loginip){
  481. $update = [];
  482. $update['id'] = $user->id;
  483. $update['loginip'] = $ip;
  484. \app\common\model\User::update($update);
  485. }
  486. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  487. if (empty($userDeviceInfo)){
  488. $userDeviceInfo = new UserDeviceInfo();
  489. $userDeviceInfo->user_id = $user->u_id;
  490. }
  491. $userDeviceInfo->device_os = $deviceOs;
  492. $userDeviceInfo->device_id = $deviceId;
  493. $userDeviceInfo->phone_model = $phoneModel;
  494. $userDeviceInfo->brand = $brand;
  495. $userDeviceInfo->api_version = $apiVersion;
  496. $userDeviceInfo->save();
  497. //首页接口调用,这里不反回信息
  498. // $this->success("更新成功!");
  499. }
  500. }