User.php 27 KB

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