User.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 = input('account');
  43. $password = input('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 = input('mobile');
  65. $captcha = input('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 userintroinfo(){
  141. $intro_num = Db::name('user')->where('intro_uid',$this->auth->id)->count();
  142. $money_sum = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63])->sum('change_value');
  143. $user_list = Db::name('user')->field('id,avatar,nickname,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
  144. $rs = [
  145. 'intro_num' => $intro_num,
  146. 'money_sum' => $money_sum,
  147. 'user_list' => $user_list,
  148. ];
  149. $this->success('success',$rs);
  150. }
  151. //申请真人认证
  152. public function apply_real_confirm(){
  153. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  154. if(!$avatar){
  155. $this->error('请上传真人头像');
  156. }
  157. Db::startTrans();
  158. $data = [
  159. 'avatar' => $avatar,
  160. 'real_status' => 1,
  161. ];
  162. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  163. //tag任务赠送金币
  164. if($rs){
  165. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  166. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  167. if($task_rs === false){
  168. Db::rollback();
  169. $this->error('完成任务赠送奖励失败');
  170. }
  171. //完成真人头像 +5金币
  172. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  173. if($task_rs === false){
  174. Db::rollback();
  175. $this->error('完成任务赠送奖励失败');
  176. }
  177. //邀请人拿奖励,男性3元
  178. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  179. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  180. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  181. if($task_rs['status'] === false){
  182. Db::rollback();
  183. $this->error($task_rs['msg']);
  184. }
  185. }
  186. }
  187. Db::commit();
  188. $this->success();
  189. }
  190. //实名认证信息
  191. public function idcard_confirm_info(){
  192. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  193. $this->success('success',$check);
  194. }
  195. //申请实名认证
  196. public function apply_idcard_confirm(){
  197. $truename = input('truename','');
  198. $idcard = input('idcard','');
  199. $idcard_images = input('idcard_images','');
  200. $alipay_account = input('alipay_account','');
  201. if(empty($truename) || empty($idcard) || empty($idcard_images) || empty($alipay_account)){
  202. $this->error('实名认证信息必填');
  203. }
  204. if($this->auth->idcard_status == 1){
  205. $this->error('您已经完成实名认证');
  206. }
  207. if($this->auth->idcard_status == 0){
  208. $this->error('您已经提交实名认证,请等待审核');
  209. }
  210. Db::startTrans();
  211. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  212. if(!empty($check)){
  213. if($check['status'] == 0){
  214. Db::rollback();
  215. $this->error('您已经提交实名认证,请等待审核');
  216. }
  217. if($check['status'] == 1){
  218. Db::rollback();
  219. $this->error('您已经完成实名认证');
  220. }
  221. }
  222. $data = [
  223. 'user_id' => $this->auth->id,
  224. 'truename' => $truename,
  225. 'idcard' => $idcard,
  226. 'idcard_images' => $idcard_images,
  227. 'alipay_account' => $alipay_account,
  228. 'status' => 0,
  229. 'createtime' => time(),
  230. 'updatetime' => time(),
  231. ];
  232. //更新
  233. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  234. if(!empty($check)){
  235. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  236. }else{
  237. $rs = Db::name('user_idconfirm')->insertGetId($data);
  238. }
  239. if(!$rs || !$update_rs){
  240. Db::rollback();
  241. $this->error('提交失败');
  242. }
  243. Db::commit();
  244. $this->success('提交成功,请等待审核');
  245. }
  246. /**
  247. * 退出登录
  248. * @ApiMethod (POST)
  249. */
  250. public function logout()
  251. {
  252. if (!$this->request->isPost()) {
  253. $this->error(__('Invalid parameters'));
  254. }
  255. //退出im
  256. $tenIm = new Tenim();
  257. $tenIm->loginoutim($this->auth->id);
  258. $this->auth->logout();
  259. $this->success(__('Logout successful'));
  260. }
  261. /**
  262. * 修改会员个人信息
  263. *
  264. * @ApiMethod (POST)
  265. * @param string $avatar 头像地址
  266. * @param string $username 用户名
  267. * @param string $nickname 昵称
  268. * @param string $bio 个人简介
  269. */
  270. public function profile()
  271. {
  272. $field_array = ['nickname','introcode','gender','birthday','height','weight','bio','audio_bio','avatar','photo_images','education_id','hobby_ids','job_id','marital_id','tag_ids','wages_id','hometown_cityid','hide_is_finishinfo'];
  273. $data = [];
  274. foreach($field_array as $key => $field){
  275. if(!input('?'.$field)){
  276. continue;
  277. }
  278. $newone = input($field);
  279. if($field == 'avatar'){
  280. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  281. }
  282. if($field == 'photo_images'){
  283. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  284. }
  285. $data[$field] = $newone;
  286. }
  287. if(isset($data['birthday'])){
  288. $data['birthday'] = strtotime($data['birthday']);
  289. }
  290. if(isset($data['avatar'])){
  291. $data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  292. }
  293. if(isset($data['hobby_ids'])){
  294. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  295. }
  296. if(isset($data['tag_ids'])){
  297. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  298. }
  299. if(isset($data['introcode'])){
  300. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  301. if(!$intro_user){
  302. $this->error('不存在的邀请人');
  303. }
  304. $data['intro_uid'] = $intro_user;
  305. }
  306. //dump($data);
  307. if(empty($data)){
  308. $this->error('没有任何改变');
  309. }
  310. Db::startTrans();
  311. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  312. if($update_rs === false){
  313. Db::rollback();
  314. $this->error('修改资料失败');
  315. }
  316. //tag任务赠送金币
  317. //上传头像加5金币
  318. if(isset($data['avatar'])){
  319. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  320. if($task_rs === false){
  321. Db::rollback();
  322. $this->error('完成任务赠送奖励失败');
  323. }
  324. }
  325. //上传本人语音介绍 10金币
  326. if(isset($data['audio_bio'])){
  327. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  328. if($task_rs === false){
  329. Db::rollback();
  330. $this->error('完成任务赠送奖励失败');
  331. }
  332. }
  333. //上传本人五张照片 10金币
  334. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  335. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  336. if($task_rs === false){
  337. Db::rollback();
  338. $this->error('完成任务赠送奖励失败');
  339. }
  340. }
  341. //所有资料完成
  342. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  343. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  344. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  345. if($task_rs === false){
  346. Db::rollback();
  347. $this->error('完成任务赠送奖励失败');
  348. }
  349. }
  350. Db::commit();
  351. $this->success();
  352. }
  353. public function set_status_switch(){
  354. }
  355. /*
  356. * 修改用户的坐标
  357. * */
  358. public function change_longlat(){
  359. $longitude = input_post('longitude');
  360. $latitude = input_post('latitude');
  361. $cityname = input_post('cityname');
  362. if(empty($longitude) || empty($latitude) || empty($cityname)){
  363. $this->error();
  364. }
  365. $data = [
  366. 'longitude' => $longitude,
  367. 'latitude' => $latitude,
  368. 'cityname' => $cityname,
  369. ];
  370. Db::name('user')->where('id',$this->auth->id)->update($data);
  371. $this->success();
  372. }
  373. /**
  374. * 修改邮箱
  375. *
  376. * @ApiMethod (POST)
  377. * @param string $email 邮箱
  378. * @param string $captcha 验证码
  379. */
  380. /*public function changeemail()
  381. {
  382. $user = $this->auth->getUser();
  383. $email = $this->request->post('email');
  384. $captcha = $this->request->post('captcha');
  385. if (!$email || !$captcha) {
  386. $this->error(__('Invalid parameters'));
  387. }
  388. if (!Validate::is($email, "email")) {
  389. $this->error(__('Email is incorrect'));
  390. }
  391. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  392. $this->error(__('Email already exists'));
  393. }
  394. $result = Ems::check($email, $captcha, 'changeemail');
  395. if (!$result) {
  396. $this->error(__('Captcha is incorrect'));
  397. }
  398. $verification = $user->verification;
  399. $verification->email = 1;
  400. $user->verification = $verification;
  401. $user->email = $email;
  402. $user->save();
  403. Ems::flush($email, 'changeemail');
  404. $this->success();
  405. }*/
  406. /**
  407. * 修改手机号
  408. *
  409. * @ApiMethod (POST)
  410. * @param string $mobile 手机号
  411. * @param string $captcha 验证码
  412. */
  413. public function changemobile()
  414. {
  415. $user = $this->auth->getUser();
  416. $oldcaptcha = $this->request->request('oldcaptcha');
  417. $mobile = $this->request->request('mobile');
  418. $captcha = $this->request->request('captcha');
  419. if (!$oldcaptcha || !$mobile || !$captcha) {
  420. $this->error(__('Invalid parameters'));
  421. }
  422. if (!Validate::regex($mobile, "^1\d{10}$")) {
  423. $this->error(__('Mobile is incorrect'));
  424. }
  425. if($user->mobile == $mobile){
  426. $this->error('新手机号不能与旧手机号相同');
  427. }
  428. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  429. $this->error(__('Mobile already exist'));
  430. }
  431. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  432. if (!$result) {
  433. $this->error(__('Captcha is incorrect'));
  434. }
  435. $result = Sms::check($mobile, $captcha, 'changemobile');
  436. if (!$result) {
  437. $this->error(__('Captcha is incorrect'));
  438. }
  439. $verification = $user->verification;
  440. $verification->mobile = 1;
  441. $user->verification = $verification;
  442. $user->mobile = $mobile;
  443. $user->save();
  444. Sms::flush($user->mobile, 'changemobile');
  445. Sms::flush($mobile, 'changemobile');
  446. $this->success();
  447. }
  448. /**
  449. * 第三方登录
  450. *
  451. * @ApiMethod (POST)
  452. * @param string $platform 平台名称
  453. * @param string $code Code码
  454. */
  455. /*public function third()
  456. {
  457. $url = url('user/index');
  458. $platform = $this->request->post("platform");
  459. $code = $this->request->post("code");
  460. $config = get_addon_config('third');
  461. if (!$config || !isset($config[$platform])) {
  462. $this->error(__('Invalid parameters'));
  463. }
  464. $app = new \addons\third\library\Application($config);
  465. //通过code换access_token和绑定会员
  466. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  467. if ($result) {
  468. $loginret = \addons\third\library\Service::connect($platform, $result);
  469. if ($loginret) {
  470. $data = [
  471. 'userinfo' => $this->auth->getUserinfo(),
  472. 'thirdinfo' => $result
  473. ];
  474. $this->success(__('Logged in successful'), $data);
  475. }
  476. }
  477. $this->error(__('Operation failed'), $url);
  478. }*/
  479. /**
  480. * 重置密码
  481. *
  482. * @ApiMethod (POST)
  483. * @param string $mobile 手机号
  484. * @param string $newpassword 新密码
  485. * @param string $captcha 验证码
  486. */
  487. public function resetpwd()
  488. {
  489. //$type = input("type");
  490. $type = 'mobile';
  491. $mobile = input("mobile");
  492. $email = input("email");
  493. $newpassword = input("newpassword");
  494. $captcha = input("captcha");
  495. if (!$newpassword || !$captcha) {
  496. $this->error(__('Invalid parameters'));
  497. }
  498. if ($type == 'mobile') {
  499. if (!Validate::regex($mobile, "^1\d{10}$")) {
  500. $this->error(__('Mobile is incorrect'));
  501. }
  502. $user = \app\common\model\User::getByMobile($mobile);
  503. if (!$user) {
  504. $this->error(__('User not found'));
  505. }
  506. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  507. if (!$ret) {
  508. $this->error(__('Captcha is incorrect'));
  509. }
  510. Sms::flush($mobile, 'resetpwd');
  511. } else {
  512. if (!Validate::is($email, "email")) {
  513. $this->error(__('Email is incorrect'));
  514. }
  515. $user = \app\common\model\User::getByEmail($email);
  516. if (!$user) {
  517. $this->error(__('User not found'));
  518. }
  519. $ret = Ems::check($email, $captcha, 'resetpwd');
  520. if (!$ret) {
  521. $this->error(__('Captcha is incorrect'));
  522. }
  523. Ems::flush($email, 'resetpwd');
  524. }
  525. //模拟一次登录
  526. $this->auth->direct($user->id);
  527. $ret = $this->auth->changepwd($newpassword, '', true);
  528. if ($ret) {
  529. $this->success(__('Reset password successful'));
  530. } else {
  531. $this->error($this->auth->getError());
  532. }
  533. }
  534. /**
  535. * 修改密码
  536. *
  537. * @ApiMethod (POST)
  538. * @param string $newpassword 新密码
  539. * @param string $oldpassword 旧密码
  540. */
  541. public function changepwd(){
  542. $newpassword = input('newpassword');
  543. $oldpassword = input('oldpassword','');
  544. if (!$newpassword) {
  545. $this->error(__('Invalid parameters'));
  546. }
  547. if($this->auth->password && empty($oldpassword)){
  548. $this->error('原密码必填');
  549. }
  550. if(empty($this->auth->password)){
  551. $ret = $this->auth->changepwd($newpassword, '', true);
  552. }else{
  553. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  554. }
  555. if ($ret) {
  556. $this->success(__('Reset password successful'));
  557. } else {
  558. $this->error($this->auth->getError());
  559. }
  560. }
  561. /**
  562. * 记录当前登陆的设备ID,设备信息,IP等
  563. */
  564. public function changeDeviceIp()
  565. {
  566. // 接口防并发
  567. if (!$this->apiLimit(1, 5000)) {
  568. return ;
  569. }
  570. $user = $this->auth->getUser();
  571. $ip = request()->ip();
  572. $deviceId = $this->request->request('device_id','');
  573. $phoneModel = $this->request->request('phone_model','');
  574. $brand = $this->request->request('brand','');
  575. $apiVersion = $this->request->request('api_version','');
  576. $deviceOs = $this->request->request('device_os','');
  577. if ($ip !== $user->loginip){
  578. $update = [];
  579. $update['id'] = $user->id;
  580. $update['loginip'] = $ip;
  581. \app\common\model\User::update($update);
  582. }
  583. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  584. if (empty($userDeviceInfo)){
  585. $userDeviceInfo = new UserDeviceInfo();
  586. $userDeviceInfo->user_id = $user->u_id;
  587. }
  588. $userDeviceInfo->device_os = $deviceOs;
  589. $userDeviceInfo->device_id = $deviceId;
  590. $userDeviceInfo->phone_model = $phoneModel;
  591. $userDeviceInfo->brand = $brand;
  592. $userDeviceInfo->api_version = $apiVersion;
  593. $userDeviceInfo->save();
  594. //首页接口调用,这里不反回信息
  595. // $this->success("更新成功!");
  596. }
  597. }