User.php 29 KB

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