User.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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','wechatlogin', '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 wechatlogin(){
  133. $nickname = input('nickname','');
  134. $avatar = input('avatar','');
  135. $gender = input('gender',1);
  136. $wechat_openid = input('wechat_openid','');
  137. if (!$wechat_openid) {
  138. $this->error(__('Invalid parameters'));
  139. }
  140. if($gender != 1){
  141. $gender = 0;
  142. }
  143. $user = \app\common\model\User::getByOpenid($wechat_openid);
  144. if ($user) {
  145. if ($user->status != 1) {
  146. $this->error(__('Account is locked'));
  147. }
  148. //如果已经有账号则直接登录
  149. $ret = $this->auth->direct($user->id);
  150. } else {
  151. if (!$nickname || !$avatar) {
  152. $this->error(__('Invalid parameters'));
  153. }
  154. $reg_data = ['nickname'=>$nickname,'avatar'=>$avatar,'gender'=>$gender];
  155. $ret = $this->auth->openid_register($wechat_openid,$reg_data);
  156. }
  157. if ($ret) {
  158. $data = $this->userInfo('return');
  159. $this->success(__('Logged in successful'), $data);
  160. } else {
  161. $this->error($this->auth->getError());
  162. }
  163. }
  164. //用户详细资料
  165. public function userInfo($type = 1){
  166. $info = $this->auth->getUserinfo();
  167. if($type == 'return'){
  168. return $info;
  169. }
  170. $this->success(__('success'),$info);
  171. }
  172. //用户邀请信息
  173. public function userintroinfo(){
  174. $intro_num = Db::name('user')->where('intro_uid',$this->auth->id)->count();
  175. $money_sum = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'log_type'=>63])->sum('change_value');
  176. $user_list = Db::name('user')->field('id,avatar,nickname,createtime')->where('intro_uid',$this->auth->id)->autopage()->select();
  177. $rs = [
  178. 'intro_num' => $intro_num,
  179. 'money_sum' => $money_sum,
  180. 'user_list' => $user_list,
  181. ];
  182. $this->success('success',$rs);
  183. }
  184. public function testhaibao(){
  185. $haibao = $this->haibao($this->auth->id,['introcode'=>$this->auth->introcode]);
  186. dump($haibao);
  187. }
  188. //海报
  189. public function haibao($player_id,$data){
  190. //下载页二维码,没必要保留
  191. $params = [
  192. 'text' => config('site.domain_name').'/index/index/appdownload',
  193. 'size' => 90,
  194. 'logo' => false,
  195. 'label' => false,
  196. 'padding' => 0,
  197. ];
  198. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  199. $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
  200. mk_dir($qrcode_path);
  201. $download_qrcode = $qrcode_path.'/download.png';
  202. $qrCode->writeFile($download_qrcode);
  203. //海报
  204. $haibao = $this->createhaibao($download_qrcode,$player_id,$data);
  205. return $haibao;
  206. }
  207. public function createhaibao($download_qrcode,$player_id,$sub_data){
  208. //背景图
  209. $background = config('site.domain_name').'/assets/img/haibao.png';
  210. //海报图片路径
  211. $new_path = 'uploads/hbplayer/'.date("Ymd").'/';
  212. mk_dir($new_path);
  213. $wap_file_name = $new_path .'wap_player_'. $player_id . '.png';
  214. //二维码
  215. $download_qrcode= config('site.domain_name').'/'.$download_qrcode;
  216. //合成wap图片
  217. $image = new \addons\poster\library\Image2();
  218. $imgurl = $image->createPosterImage($background,$download_qrcode,$sub_data['introcode'],$wap_file_name);
  219. return '/'.$wap_file_name;
  220. }
  221. //申请真人认证
  222. public function apply_real_confirm(){
  223. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  224. if(!$avatar){
  225. $this->error('请上传真人头像');
  226. }
  227. Db::startTrans();
  228. $data = [
  229. 'avatar' => $avatar,
  230. 'real_status' => 1,
  231. ];
  232. $rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  233. if($rs === false){
  234. Db::rollback();
  235. $this->error('认证失败');
  236. }
  237. //tag任务赠送金币
  238. //完成本人基本资料 +15金币《所有资料完善,包括真人认证和实名认证》
  239. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  240. if($task_rs === false){
  241. Db::rollback();
  242. $this->error('完成任务赠送奖励失败');
  243. }
  244. //完成真人头像 +5金币
  245. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,7);
  246. if($task_rs === false){
  247. Db::rollback();
  248. $this->error('完成任务赠送奖励失败');
  249. }
  250. //邀请人拿奖励,男性3元
  251. $intro_money = $this->auth->gender == 1 ? config('site.intro_man_money') : config('site.intro_woman_money');
  252. if($this->auth->idcard_status == 1 && !empty($this->auth->intro_uid) && $intro_money > 0){
  253. $task_rs = model('wallet')->lockChangeAccountRemain($this->auth->intro_uid,'money',$intro_money,63,$remark='');
  254. if($task_rs['status'] === false){
  255. Db::rollback();
  256. $this->error($task_rs['msg']);
  257. }
  258. }
  259. //系统消息
  260. $msg_id = \app\common\model\Message::addMessage($this->auth->id,'真人认证','真人认证已经审核通过');
  261. Db::commit();
  262. $this->success();
  263. }
  264. //实名认证信息
  265. public function idcard_confirm_info(){
  266. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->order('id desc')->find();
  267. $this->success('success',$check);
  268. }
  269. //申请实名认证
  270. public function apply_idcard_confirm(){
  271. $truename = input('truename','');
  272. $idcard = input('idcard','');
  273. $idcard_images = input('idcard_images','');
  274. $alipay_account = input('alipay_account','');
  275. if(empty($truename) || empty($idcard) || empty($idcard_images) || empty($alipay_account)){
  276. $this->error('实名认证信息必填');
  277. }
  278. if($this->auth->idcard_status == 1){
  279. $this->error('您已经完成实名认证');
  280. }
  281. if($this->auth->idcard_status == 0){
  282. $this->error('您已经提交实名认证,请等待审核');
  283. }
  284. Db::startTrans();
  285. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  286. if(!empty($check)){
  287. if($check['status'] == 0){
  288. Db::rollback();
  289. $this->error('您已经提交实名认证,请等待审核');
  290. }
  291. if($check['status'] == 1){
  292. Db::rollback();
  293. $this->error('您已经完成实名认证');
  294. }
  295. }
  296. $data = [
  297. 'user_id' => $this->auth->id,
  298. 'truename' => $truename,
  299. 'idcard' => $idcard,
  300. 'idcard_images' => $idcard_images,
  301. 'alipay_account' => $alipay_account,
  302. 'status' => 0,
  303. 'createtime' => time(),
  304. 'updatetime' => time(),
  305. ];
  306. //更新
  307. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcard_status'=>0]);
  308. if(!empty($check)){
  309. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  310. }else{
  311. $rs = Db::name('user_idconfirm')->insertGetId($data);
  312. }
  313. if(!$rs || !$update_rs){
  314. Db::rollback();
  315. $this->error('提交失败');
  316. }
  317. Db::commit();
  318. $this->success('提交成功,请等待审核');
  319. }
  320. /**
  321. * 退出登录
  322. * @ApiMethod (POST)
  323. */
  324. public function logout()
  325. {
  326. if (!$this->request->isPost()) {
  327. $this->error(__('Invalid parameters'));
  328. }
  329. //退出im
  330. $tenIm = new Tenim();
  331. $tenIm->loginoutim($this->auth->id);
  332. $this->auth->logout();
  333. $this->success(__('Logout successful'));
  334. }
  335. /**
  336. * 修改会员个人信息
  337. *
  338. * @ApiMethod (POST)
  339. * @param string $avatar 头像地址
  340. * @param string $username 用户名
  341. * @param string $nickname 昵称
  342. * @param string $bio 个人简介
  343. */
  344. public function profile()
  345. {
  346. $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','wechat_openid'];
  347. $data = [];
  348. foreach($field_array as $key => $field){
  349. if(!input('?'.$field)){
  350. continue;
  351. }
  352. $newone = input($field);
  353. if($field == 'avatar'){
  354. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  355. }
  356. if($field == 'photo_images'){
  357. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  358. }
  359. $data[$field] = $newone;
  360. }
  361. if(isset($data['birthday'])){
  362. $data['birthday'] = strtotime($data['birthday']);
  363. }
  364. if(isset($data['avatar'])){
  365. $data['real_status'] = -1; //或许应该改成0。性别不能改所以不需要
  366. }
  367. if(isset($data['hobby_ids'])){
  368. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  369. }
  370. if(isset($data['tag_ids'])){
  371. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  372. }
  373. if(isset($data['introcode'])){
  374. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  375. if(!$intro_user){
  376. $this->error('不存在的邀请人');
  377. }
  378. unset($data['introcode']);//别人的邀请码,不能改了自己的
  379. $data['intro_uid'] = $intro_user;
  380. }
  381. //dump($data);
  382. if(empty($data)){
  383. $this->error('没有任何改变');
  384. }
  385. Db::startTrans();
  386. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  387. if($update_rs === false){
  388. Db::rollback();
  389. $this->error('修改资料失败');
  390. }
  391. //tag任务赠送金币
  392. //上传头像加5金币
  393. if(isset($data['avatar'])){
  394. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,1);
  395. if($task_rs === false){
  396. Db::rollback();
  397. $this->error('完成任务赠送奖励失败');
  398. }
  399. }
  400. //上传本人语音介绍 10金币
  401. if(isset($data['audio_bio'])){
  402. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,6);
  403. if($task_rs === false){
  404. Db::rollback();
  405. $this->error('完成任务赠送奖励失败');
  406. }
  407. }
  408. //上传本人五张照片 10金币
  409. if(isset($data['photo_images']) && count(explode(',',$data['photo_images'])) >= 5){
  410. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,8);
  411. if($task_rs === false){
  412. Db::rollback();
  413. $this->error('完成任务赠送奖励失败');
  414. }
  415. }
  416. //所有资料完成
  417. $user_info = Db::name('user')->where('id',$this->auth->id)->find();
  418. if($user_info['avatar'] && $user_info['nickname'] && $user_info['mobile'] && $user_info['audio_bio'] && $user_info['photo_images']){
  419. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,5);
  420. if($task_rs === false){
  421. Db::rollback();
  422. $this->error('完成任务赠送奖励失败');
  423. }
  424. }
  425. Db::commit();
  426. $this->success();
  427. }
  428. public function set_status_switch(){
  429. }
  430. /*
  431. * 修改用户的坐标
  432. * */
  433. public function change_longlat(){
  434. $longitude = input_post('longitude');
  435. $latitude = input_post('latitude');
  436. $cityname = input_post('cityname');
  437. if(empty($longitude) || empty($latitude) || empty($cityname)){
  438. $this->error();
  439. }
  440. $data = [
  441. 'longitude' => $longitude,
  442. 'latitude' => $latitude,
  443. 'cityname' => $cityname,
  444. ];
  445. Db::name('user')->where('id',$this->auth->id)->update($data);
  446. $this->success();
  447. }
  448. /**
  449. * 修改邮箱
  450. *
  451. * @ApiMethod (POST)
  452. * @param string $email 邮箱
  453. * @param string $captcha 验证码
  454. */
  455. /*public function changeemail()
  456. {
  457. $user = $this->auth->getUser();
  458. $email = $this->request->post('email');
  459. $captcha = $this->request->post('captcha');
  460. if (!$email || !$captcha) {
  461. $this->error(__('Invalid parameters'));
  462. }
  463. if (!Validate::is($email, "email")) {
  464. $this->error(__('Email is incorrect'));
  465. }
  466. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  467. $this->error(__('Email already exists'));
  468. }
  469. $result = Ems::check($email, $captcha, 'changeemail');
  470. if (!$result) {
  471. $this->error(__('Captcha is incorrect'));
  472. }
  473. $verification = $user->verification;
  474. $verification->email = 1;
  475. $user->verification = $verification;
  476. $user->email = $email;
  477. $user->save();
  478. Ems::flush($email, 'changeemail');
  479. $this->success();
  480. }*/
  481. /**
  482. * 修改手机号
  483. *
  484. * @ApiMethod (POST)
  485. * @param string $mobile 手机号
  486. * @param string $captcha 验证码
  487. */
  488. public function changemobile()
  489. {
  490. $user = $this->auth->getUser();
  491. $oldcaptcha = $this->request->request('oldcaptcha');
  492. $mobile = $this->request->request('mobile');
  493. $captcha = $this->request->request('captcha');
  494. if (!$oldcaptcha || !$mobile || !$captcha) {
  495. $this->error(__('Invalid parameters'));
  496. }
  497. if (!Validate::regex($mobile, "^1\d{10}$")) {
  498. $this->error(__('Mobile is incorrect'));
  499. }
  500. if($user->mobile == $mobile){
  501. $this->error('新手机号不能与旧手机号相同');
  502. }
  503. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  504. $this->error(__('Mobile already exist'));
  505. }
  506. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  507. if (!$result) {
  508. $this->error(__('Captcha is incorrect'));
  509. }
  510. $result = Sms::check($mobile, $captcha, 'changemobile');
  511. if (!$result) {
  512. $this->error(__('Captcha is incorrect'));
  513. }
  514. $verification = $user->verification;
  515. $verification->mobile = 1;
  516. $user->verification = $verification;
  517. $user->mobile = $mobile;
  518. $user->save();
  519. Sms::flush($user->mobile, 'changemobile');
  520. Sms::flush($mobile, 'changemobile');
  521. $this->success();
  522. }
  523. /**
  524. * 微信注册来的,绑定手机号
  525. *
  526. * @ApiMethod (POST)
  527. * @param string $mobile 手机号
  528. * @param string $captcha 验证码
  529. */
  530. public function bindmobile()
  531. {
  532. $user = $this->auth->getUser();
  533. $mobile = $this->request->request('mobile');
  534. $captcha = $this->request->request('captcha');
  535. if (!$mobile || !$captcha) {
  536. $this->error(__('Invalid parameters'));
  537. }
  538. if (!Validate::regex($mobile, "^1\d{10}$")) {
  539. $this->error(__('Mobile is incorrect'));
  540. }
  541. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  542. $this->error(__('Mobile already exist'));
  543. }
  544. $result = Sms::check($mobile, $captcha, 'changemobile');
  545. if (!$result) {
  546. $this->error(__('Captcha is incorrect'));
  547. }
  548. $verification = $user->verification;
  549. $verification->mobile = 1;
  550. $user->verification = $verification;
  551. $user->mobile = $mobile;
  552. $user->save();
  553. Sms::flush($mobile, 'changemobile');
  554. $this->success();
  555. }
  556. /**
  557. * 第三方登录
  558. *
  559. * @ApiMethod (POST)
  560. * @param string $platform 平台名称
  561. * @param string $code Code码
  562. */
  563. /*public function third()
  564. {
  565. $url = url('user/index');
  566. // $platform = $this->request->post("platform");
  567. $platform = 'wechat';
  568. $code = $this->request->post("code");
  569. $config = get_addon_config('third');
  570. if (!$config || !isset($config[$platform])) {
  571. $this->error(__('Invalid parameters'));
  572. }
  573. $app = new \addons\third\library\Application($config);
  574. //通过code换access_token和绑定会员
  575. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  576. if ($result) {
  577. $loginret = \addons\third\library\Service::connect($platform, $result);
  578. if ($loginret) {
  579. $data = [
  580. 'userinfo' => $this->auth->getUserinfo(),
  581. 'thirdinfo' => $result
  582. ];
  583. $this->success(__('Logged in successful'), $data);
  584. }
  585. }
  586. $this->error(__('Operation failed'), $url);
  587. }*/
  588. /**
  589. * 重置密码
  590. *
  591. * @ApiMethod (POST)
  592. * @param string $mobile 手机号
  593. * @param string $newpassword 新密码
  594. * @param string $captcha 验证码
  595. */
  596. public function resetpwd()
  597. {
  598. //$type = input("type");
  599. $type = 'mobile';
  600. $mobile = input("mobile");
  601. $email = input("email");
  602. $newpassword = input("newpassword");
  603. $captcha = input("captcha");
  604. if (!$newpassword || !$captcha) {
  605. $this->error(__('Invalid parameters'));
  606. }
  607. if ($type == 'mobile') {
  608. if (!Validate::regex($mobile, "^1\d{10}$")) {
  609. $this->error(__('Mobile is incorrect'));
  610. }
  611. $user = \app\common\model\User::getByMobile($mobile);
  612. if (!$user) {
  613. $this->error(__('User not found'));
  614. }
  615. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  616. if (!$ret) {
  617. $this->error(__('Captcha is incorrect'));
  618. }
  619. Sms::flush($mobile, 'resetpwd');
  620. } else {
  621. if (!Validate::is($email, "email")) {
  622. $this->error(__('Email is incorrect'));
  623. }
  624. $user = \app\common\model\User::getByEmail($email);
  625. if (!$user) {
  626. $this->error(__('User not found'));
  627. }
  628. $ret = Ems::check($email, $captcha, 'resetpwd');
  629. if (!$ret) {
  630. $this->error(__('Captcha is incorrect'));
  631. }
  632. Ems::flush($email, 'resetpwd');
  633. }
  634. //模拟一次登录
  635. $this->auth->direct($user->id);
  636. $ret = $this->auth->changepwd($newpassword, '', true);
  637. if ($ret) {
  638. $this->success(__('Reset password successful'));
  639. } else {
  640. $this->error($this->auth->getError());
  641. }
  642. }
  643. /**
  644. * 修改密码
  645. *
  646. * @ApiMethod (POST)
  647. * @param string $newpassword 新密码
  648. * @param string $oldpassword 旧密码
  649. */
  650. public function changepwd(){
  651. $newpassword = input('newpassword');
  652. $oldpassword = input('oldpassword','');
  653. if (!$newpassword) {
  654. $this->error(__('Invalid parameters'));
  655. }
  656. if($this->auth->password && empty($oldpassword)){
  657. $this->error('原密码必填');
  658. }
  659. if(empty($this->auth->password)){
  660. $ret = $this->auth->changepwd($newpassword, '', true);
  661. }else{
  662. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  663. }
  664. if ($ret) {
  665. $this->success(__('Reset password successful'));
  666. } else {
  667. $this->error($this->auth->getError());
  668. }
  669. }
  670. /**
  671. * 记录当前登陆的设备ID,设备信息,IP等
  672. */
  673. public function changeDeviceIp()
  674. {
  675. // 接口防并发
  676. if (!$this->apiLimit(1, 5000)) {
  677. return ;
  678. }
  679. $user = $this->auth->getUser();
  680. $ip = request()->ip();
  681. $deviceId = $this->request->request('device_id','');
  682. $phoneModel = $this->request->request('phone_model','');
  683. $brand = $this->request->request('brand','');
  684. $apiVersion = $this->request->request('api_version','');
  685. $deviceOs = $this->request->request('device_os','');
  686. if ($ip !== $user->loginip){
  687. $update = [];
  688. $update['id'] = $user->id;
  689. $update['loginip'] = $ip;
  690. \app\common\model\User::update($update);
  691. }
  692. $userDeviceInfo = UserDeviceInfo::get(['user_id'=>$user->u_id]);
  693. if (empty($userDeviceInfo)){
  694. $userDeviceInfo = new UserDeviceInfo();
  695. $userDeviceInfo->user_id = $user->u_id;
  696. }
  697. $userDeviceInfo->device_os = $deviceOs;
  698. $userDeviceInfo->device_id = $deviceId;
  699. $userDeviceInfo->phone_model = $phoneModel;
  700. $userDeviceInfo->brand = $brand;
  701. $userDeviceInfo->api_version = $apiVersion;
  702. $userDeviceInfo->save();
  703. //首页接口调用,这里不反回信息
  704. // $this->success("更新成功!");
  705. }
  706. }