User.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 onlogin\onlogin;
  12. use app\common\library\Keyworld;
  13. use addons\epay\library\Service;
  14. //use addons\epay\library\Wechat;
  15. use app\common\library\Wechat;
  16. /**
  17. * 会员接口,登录,注册,修改资料等
  18. */
  19. class User extends Api
  20. {
  21. protected $noNeedLogin = ['login','h5register', 'mobilelogin','wechatlogin','applelogin','bindmobile','applebindmobile', 'register', 'resetpwd', 'changemobile', 'onlogin','getUserOpenid_gzh','jssdkBuildConfig'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 会员中心
  29. */
  30. public function index()
  31. {
  32. $this->success('', ['welcome' => $this->auth->nickname]);
  33. }
  34. //h5注册
  35. public function h5register()
  36. {
  37. $mobile = input('mobile');
  38. $captcha = input('captcha');
  39. $invite_no = input('invite_no','');
  40. $intro_no = input('intro_no','');
  41. if (!$mobile || !$captcha) {
  42. $this->error(__('Invalid parameters'));
  43. }
  44. if (!Validate::regex($mobile, "^1\d{10}$")) {
  45. $this->error(__('Mobile is incorrect'));
  46. }
  47. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  48. $this->error(__('Captcha is incorrect'));
  49. }
  50. $user = \app\common\model\User::getByMobile($mobile);
  51. if ($user) {
  52. $this->error('该手机号已经注册过了');
  53. } else {
  54. $extend = [];
  55. if($invite_no == 'undefined'){$invite_no = '';}
  56. if($intro_no == 'undefined'){$intro_no = '';}
  57. if(!empty($invite_no)){
  58. $inviteUserInfo = \app\common\model\User::where(["introcode" => $invite_no])->find();
  59. if (!$inviteUserInfo) {
  60. //$this->error("查询不到该邀请码用户信息!");
  61. }else{
  62. $extend['invite_uid'] = $inviteUserInfo['id'];
  63. }
  64. }elseif(!empty($intro_no)){
  65. $introUserInfo = \app\common\model\User::where(["introcode" => $intro_no])->find();
  66. if (!$introUserInfo) {
  67. //$this->error("查询不到该邀请码用户信息!");
  68. }else{
  69. $extend['intro_uid'] = $introUserInfo['id'];
  70. }
  71. }
  72. $ret = $this->auth->register('','','', $mobile, $extend);
  73. }
  74. if ($ret) {
  75. $this->success('注册成功');
  76. } else {
  77. $this->error($this->auth->getError());
  78. }
  79. }
  80. /**
  81. * 手机验证码登录
  82. *
  83. * @ApiMethod (POST)
  84. * @param string $mobile 手机号
  85. * @param string $captcha 验证码
  86. */
  87. public function mobilelogin()
  88. {
  89. $mobile = input('mobile');
  90. $captcha = input('captcha');
  91. if (!$mobile || !$captcha) {
  92. $this->error(__('Invalid parameters'));
  93. }
  94. if (!Validate::regex($mobile, "^1\d{10}$")) {
  95. $this->error(__('Mobile is incorrect'));
  96. }
  97. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  98. $this->error(__('Captcha is incorrect'));
  99. }
  100. $user = \app\common\model\User::getByMobile($mobile);
  101. if ($user) {
  102. if ($user->status == -1) {
  103. $this->error('账户已注销');
  104. }
  105. if ($user->status != 1) {
  106. $this->error(__('Account is locked'));
  107. }
  108. //如果已经有账号则直接登录
  109. $ret = $this->auth->direct($user->id);
  110. } else {
  111. $extend = [
  112. 'plat_unique_id' => input('plat_unique_id',''),
  113. 'plat_from' => input('plat_from',''),
  114. ];
  115. $ret = $this->auth->register('', '', '', $mobile, $extend);
  116. }
  117. if ($ret) {
  118. Sms::flush($mobile, 'mobilelogin');
  119. $data = $this->auth->getUserinfo_simple();
  120. $this->success(__('Logged in successful'), $data);
  121. } else {
  122. $this->error($this->auth->getError());
  123. }
  124. }
  125. /**
  126. * 手机注册来的,绑定微信
  127. *
  128. * @ApiMethod (POST)
  129. * @param string $wechat_openid
  130. */
  131. public function bindopenid()
  132. {
  133. $code = input('code','');
  134. if(!$code){
  135. $this->error(__('Invalid parameters'));
  136. }
  137. //微信
  138. $wechat = new Wechat();
  139. $wxuserinfo = $wechat->getAccessToken($code);
  140. if(!$wxuserinfo){
  141. $this->error('openid获取失败');
  142. }
  143. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  144. $this->error('openid获取失败');
  145. }
  146. $wechat_openid = $wxuserinfo['openid'];
  147. if(!empty($this->auth->wechat_openid)){
  148. //$this->error('已经绑定了微信号'); //让openid能修改,不报错了
  149. }
  150. $otherUserWhere['wechat_openid'] = $wechat_openid;
  151. $otherUserWhere['id'] = ['neq',$this->auth->id];
  152. if (\app\common\model\User::where($otherUserWhere)->find()) {
  153. $this->error('该微信号已被其他用户绑定');
  154. }
  155. $user = $this->auth->getUser();
  156. $user->wechat_openid = $wechat_openid;
  157. $user->save();
  158. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  159. }
  160. //微信登录+注册
  161. public function wechatlogin(){
  162. $code = input('code','');
  163. if(!$code){
  164. $this->error(__('Invalid parameters'));
  165. }
  166. //微信
  167. $wechat = new Wechat();
  168. $wxuserinfo = $wechat->getAccessToken($code);
  169. if(!$wxuserinfo){
  170. $this->error('openid获取失败');
  171. }
  172. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  173. $this->error('openid获取失败');
  174. }
  175. $openid = $wxuserinfo['openid'];
  176. //
  177. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  178. if ($user) {
  179. if ($user['status'] == -1) {
  180. $this->error('账户已注销');
  181. }
  182. if ($user['status'] != 1) {
  183. $this->error(__('Account is locked'));
  184. }
  185. //如果已经有账号则直接登录
  186. $ret = $this->auth->direct($user['id']);
  187. } else {
  188. $extend = [
  189. 'plat_unique_id' => input('plat_unique_id',''),
  190. 'plat_from' => input('plat_from',''),
  191. ];
  192. $ret = $this->auth->openid_register($openid,$extend);
  193. }
  194. if ($ret) {
  195. $data = $this->auth->getUserinfo_simple();
  196. $this->success(__('Logged in successful'), $data);
  197. } else {
  198. $this->error($this->auth->getError());
  199. }
  200. }
  201. /**
  202. * 微信注册来的,绑定手机号
  203. *
  204. * @ApiMethod (POST)
  205. * @param string $mobile 手机号
  206. * @param string $captcha 验证码
  207. */
  208. public function bindmobile()
  209. {
  210. $mobile = input('mobile');
  211. $captcha = input('captcha');
  212. if(!empty($this->auth->mobile)){
  213. $this->error('已经绑定了手机号');
  214. }
  215. if (!$mobile || !$captcha) {
  216. $this->error(__('Invalid parameters'));
  217. }
  218. if (!Validate::regex($mobile, "^1\d{10}$")) {
  219. $this->error(__('Mobile is incorrect'));
  220. }
  221. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  222. $this->error('该手机号已被其他用户绑定');
  223. }
  224. $result = Sms::check($mobile, $captcha, 'changemobile');
  225. if (!$result) {
  226. $this->error(__('Captcha is incorrect'));
  227. }
  228. $user = $this->auth->getUser();
  229. $user->mobile = $mobile;
  230. $user->save();
  231. Sms::flush($mobile, 'changemobile');
  232. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  233. }
  234. //用户详细资料
  235. public function userInfo($type = 1){
  236. $info = $this->auth->getUserinfo();
  237. if($type == 'return'){
  238. return $info;
  239. }
  240. $this->success(__('success'),$info);
  241. }
  242. /**
  243. * 退出登录
  244. * @ApiMethod (POST)
  245. */
  246. public function logout()
  247. {
  248. if (!$this->request->isPost()) {
  249. $this->error(__('Invalid parameters'));
  250. }
  251. //退出im
  252. // $tenIm = new Tenim();
  253. // $tenIm->loginoutim($this->auth->id);
  254. $this->auth->logout();
  255. $this->success(__('Logout successful'));
  256. }
  257. /**
  258. * 修改会员个人信息
  259. *
  260. * @ApiMethod (POST)
  261. * @param string $avatar 头像地址
  262. * @param string $username 用户名
  263. * @param string $nickname 昵称
  264. * @param string $bio 个人简介
  265. */
  266. public function profile()
  267. {
  268. $field_array = [
  269. 'avatar','nickname','birthday','gender',
  270. 'audio_bio','audio_seconds','photo_images','video_bio',
  271. 'height','weight','bio',
  272. 'marital_id','job_id','wages_id','suqiu_id','tag_ids','hobby_ids',
  273. 'hide_is_finishinfo',
  274. 'invite_no',
  275. ];
  276. $data = [];
  277. foreach($field_array as $key => $field){
  278. //前端传不了post,改了
  279. /*if(!request()->has($field,'post')){
  280. continue;
  281. }*/
  282. if(!input('?'.$field)){
  283. continue;
  284. }
  285. $newone = input($field);
  286. if($field == 'avatar'){
  287. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  288. }
  289. if($field == 'photo_images'){
  290. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  291. }
  292. if($field == 'nickname'){
  293. $newone = Keyworld::sensitive($newone);
  294. }
  295. if($field == 'bio'){
  296. $newone = Keyworld::sensitive($newone);
  297. }
  298. $data[$field] = $newone;
  299. }
  300. //视频,需要审核
  301. $user_audit_switch = 1;
  302. if(isset($data['video_bio']) && !empty($data['video_bio']) && $user_audit_switch == 1){
  303. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','video_bio')->where('status',0)->find();
  304. if(!empty($check_exist)){
  305. $this->error('信息已提交等待审核,请勿重复提交');
  306. }
  307. if($data['video_bio'] != $this->auth->video_bio){
  308. $bio_data = [
  309. 'user_id' => $this->auth->id,
  310. 'type' => 'video_bio',
  311. 'old_data' => $this->auth->video_bio,
  312. 'new_data' => $data['video_bio'],
  313. 'createtime' => time(),
  314. ];
  315. Db::name('user_audit')->insertGetId($bio_data);
  316. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  317. }
  318. unset($data['video_bio']);
  319. }
  320. //相册,需要审核
  321. if(isset($data['photo_images']) && !empty($data['photo_images']) && $user_audit_switch == 1){
  322. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','photo_images')->where('status',0)->find();
  323. if(!empty($check_exist)){
  324. $this->error('信息已提交等待审核,请勿重复提交');
  325. }
  326. if($data['photo_images'] != $this->auth->photo_images){
  327. $bio_data = [
  328. 'user_id' => $this->auth->id,
  329. 'type' => 'photo_images',
  330. 'old_data' => $this->auth->photo_images,
  331. 'new_data' => $data['photo_images'],
  332. 'createtime' => time(),
  333. ];
  334. Db::name('user_audit')->insertGetId($bio_data);
  335. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  336. }
  337. unset($data['photo_images']);
  338. }
  339. //语音,需要审核
  340. if(isset($data['audio_bio']) && !empty($data['audio_bio']) && $user_audit_switch == 1){
  341. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','audio_bio')->where('status',0)->find();
  342. if(!empty($check_exist)){
  343. $this->error('信息已提交等待审核,请勿重复提交');
  344. }
  345. if($data['audio_bio'] != $this->auth->audio_bio){
  346. $bio_data = [
  347. 'user_id' => $this->auth->id,
  348. 'type' => 'audio_bio',
  349. 'old_data' => $this->auth->audio_bio,
  350. 'new_data' => $data['audio_bio'],
  351. 'createtime' => time(),
  352. ];
  353. Db::name('user_audit')->insertGetId($bio_data);
  354. //语音秒数
  355. $bio_data = [
  356. 'user_id' => $this->auth->id,
  357. 'type' => 'audio_seconds',
  358. 'old_data' => $this->auth->audio_seconds,
  359. 'new_data' => $data['audio_seconds'],
  360. 'createtime' => time(),
  361. ];
  362. Db::name('user_audit')->insertGetId($bio_data);
  363. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  364. }
  365. unset($data['audio_bio']);
  366. unset($data['audio_seconds']);
  367. }
  368. //头像,是否需要审核
  369. if(isset($data['avatar']) && !empty($data['avatar']) && $data['avatar'] != config('avatar_boy') && $data['avatar'] != config('avatar_girl') && $user_audit_switch == 1){
  370. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','avatar')->where('status',0)->find();
  371. if(!empty($check_exist)){
  372. $this->error('信息已提交等待审核,请勿重复提交');
  373. }
  374. if($data['avatar'] != $this->auth->avatar){
  375. $bio_data = [
  376. 'user_id' => $this->auth->id,
  377. 'type' => 'avatar',
  378. 'old_data' => $this->auth->avatar,
  379. 'new_data' => $data['avatar'],
  380. 'createtime' => time(),
  381. ];
  382. Db::name('user_audit')->insertGetId($bio_data);
  383. //失去真人认证
  384. $data['real_status'] = -1;
  385. Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  386. if(!isset($data['gender'])){
  387. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  388. }
  389. }
  390. unset($data['avatar']);
  391. }
  392. //第一次传入性别,头像只能用默认,因为自传头像需要审核
  393. if(isset($data['gender']) && $user_audit_switch == 1){
  394. $data['avatar'] = $data['gender'] == 1 ? config('avatar_boy') : config('avatar_girl');
  395. }
  396. //初次设置性别为 男
  397. if(isset($data['gender']) && $data['gender'] == 1 && $this->auth->gender == -1){
  398. //男性赠送
  399. $gift_data = [
  400. 'audio_sec' => config('site.man_reg_audio_sec'),
  401. 'video_sec' => config('site.man_reg_video_sec'),
  402. 'typing_times' => config('site.man_reg_typing_times'),
  403. ];
  404. Db::name('user_wallet')->where('user_id',$this->auth->id)->update($gift_data);
  405. //修改财富值等级为1级
  406. $data['wealth_level'] = 1;
  407. }
  408. if(isset($data['invite_no']) && !empty($data['invite_no']) && empty($this->auth->intro_uid) && empty($this->auth->invite_uid)){
  409. $invite_user = Db::name('user')->where('introcode',$data['invite_no'])->value('id');
  410. if($invite_user){
  411. $data['invite_uid'] = $invite_user;
  412. }
  413. unset($data['invite_no']);//别人的邀请码,不能改了自己的
  414. }
  415. //
  416. if(isset($data['birthday'])){
  417. $data['birthday'] = strtotime($data['birthday']);
  418. }
  419. if(isset($data['tag_ids'])){
  420. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  421. }
  422. if(isset($data['hobby_ids'])){
  423. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  424. }
  425. //dump($data);
  426. if(empty($data)){
  427. $this->success();
  428. }
  429. Db::startTrans();
  430. //修改用户
  431. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  432. if($update_rs === false){
  433. Db::rollback();
  434. $this->error('修改资料失败');
  435. }
  436. //task任务
  437. if(isset($data['tag_ids']) || isset($data['bio'])){
  438. $userinfo = Db::name('user')->where('id',$this->auth->id)->find();
  439. if($userinfo['hide_is_finishinfo'] == 0){
  440. if($userinfo['tag_ids'] && $userinfo['bio']){
  441. //task任务
  442. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  443. if($task_rs === false){
  444. Db::rollback();
  445. $this->error('完成任务失败');
  446. }
  447. $finish['hide_is_finishinfo'] = 1;//完善资料了
  448. Db::name('user')->where('id',$this->auth->id)->update($finish);
  449. }
  450. }
  451. }
  452. Db::commit();
  453. $this->success();
  454. }
  455. public function getpower(){
  456. $rs = Db::name('user_power')->where('user_id',$this->auth->id)->find();
  457. $this->success(1,$rs);
  458. }
  459. /**
  460. * 修改会员权限
  461. */
  462. public function setpower()
  463. {
  464. /*$is_vip = $this->is_vip($this->auth->id);
  465. if(!$is_vip){
  466. $this->error('VIP才能设置隐私权限');
  467. }*/
  468. $field_array = ['meili','caifu','weizhi','giftwall'];
  469. $data = [];
  470. foreach($field_array as $key => $field){
  471. if(!input('?'.$field)){
  472. continue;
  473. }
  474. $newone = input($field);
  475. $data[$field] = $newone;
  476. }
  477. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  478. $this->success();
  479. }
  480. //重新绑定
  481. public function set_intro(){
  482. if(isset($data['introcode']) && !empty($data['introcode'])){
  483. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  484. if(!$intro_user){
  485. $this->error('不存在的邀请人');
  486. }
  487. if(!empty($this->auth->intro_uid)){
  488. $this->error('您已经填写过邀请人');
  489. }
  490. unset($data['introcode']);//别人的邀请码,不能改了自己的
  491. $data['intro_uid'] = $intro_user;
  492. }
  493. }
  494. /*
  495. * 修改用户的坐标
  496. * */
  497. public function change_longlat(){
  498. $this->success();
  499. // $longitude = input('longitude',0);
  500. // $latitude = input('latitude',0);
  501. $cityname = input('cityname','');
  502. $provincename = input('provincename','');
  503. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  504. $this->error();
  505. }*/
  506. $data = [];
  507. // $longitude && $data['longitude'] = $longitude;
  508. // $latitude && $data['latitude'] = $latitude;
  509. $cityname && $data['cityname'] = $cityname;
  510. $provincename && $data['provincename'] = $provincename;
  511. //传入了城市,但是没传入省,直接省名改空
  512. if(isset($data['cityname']) && !isset($data['provincename'])){
  513. $data['provincename'] = '';
  514. }
  515. //没传入城市,但是传入省,直接城市名改空
  516. if(!isset($data['cityname']) && isset($data['provincename'])){
  517. $data['cityname'] = '';
  518. }
  519. if(!empty($data)){
  520. Db::name('user')->where('id',$this->auth->id)->update($data);
  521. }
  522. $this->success();
  523. }
  524. //修改用户设备id
  525. public function change_plat_unique_id(){
  526. $plat_unique_id = input('plat_unique_id','');
  527. $plat_from = input('plat_from','');
  528. $data = [
  529. 'plat_unique_id' => $plat_unique_id,
  530. 'plat_from' => $plat_from,
  531. ];
  532. Db::name('user')->where('id',$this->auth->id)->update($data);
  533. $this->success();
  534. }
  535. /**
  536. * 修改手机号
  537. *
  538. * @ApiMethod (POST)
  539. * @param string $mobile 手机号
  540. * @param string $captcha 验证码
  541. */
  542. public function changemobile()
  543. {
  544. $user = $this->auth->getUser();
  545. $oldcaptcha = $this->request->request('oldcaptcha');
  546. $mobile = $this->request->request('mobile');
  547. $captcha = $this->request->request('captcha');
  548. if (!$oldcaptcha || !$mobile || !$captcha) {
  549. $this->error(__('Invalid parameters'));
  550. }
  551. if (!Validate::regex($mobile, "^1\d{10}$")) {
  552. $this->error(__('Mobile is incorrect'));
  553. }
  554. if($user->mobile == $mobile){
  555. $this->error('新手机号不能与旧手机号相同');
  556. }
  557. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  558. $this->error(__('Mobile already exist'));
  559. }
  560. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  561. if (!$result) {
  562. $this->error(__('Captcha is incorrect'));
  563. }
  564. $result = Sms::check($mobile, $captcha, 'changemobile');
  565. if (!$result) {
  566. $this->error(__('Captcha is incorrect'));
  567. }
  568. Sms::flush($user->mobile, 'changemobile');
  569. Sms::flush($mobile, 'changemobile');
  570. $user->mobile = $mobile;
  571. $user->save();
  572. $this->success();
  573. }
  574. //假注销
  575. public function cancleUser(){
  576. $captcha = input('captcha','');
  577. if (!$captcha) {
  578. $this->error(__('Invalid parameters'));
  579. }
  580. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  581. $this->error(__('Captcha is incorrect'));
  582. }
  583. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  584. $this->auth->logout();
  585. $this->success('注销成功');
  586. }
  587. //文字语音视频收费设置
  588. public function chargeconfig() {
  589. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=视频
  590. if (!in_array($type, [0, 1, 2])) {
  591. $this->error('您的网络开小差啦~');
  592. }
  593. $where['type'] = $type;
  594. $where['level'] = ['elt',$this->auth->charm_level];
  595. $list = Db::name('charge_config')->field('id, price, level')->where($where)->order('price asc')->select();
  596. $this->success('success', $list);
  597. }
  598. //文字语音视频收费/隐藏所在位置设置
  599. public function chargeset() {
  600. if($this->auth->gender == 1 && $this->auth->idcard_status != 1){
  601. $this->error('请先完成实名认证');
  602. }
  603. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  604. $this->error('请先完成真人认证');
  605. }
  606. $chat_id = input('chat_id', 0, 'intval'); //文字收费id
  607. $voice_id = input('voice_id', 0, 'intval'); //语音收费id
  608. $video_id = input('video_id', 0, 'intval'); //视频收费id
  609. $open_match_audio = input('open_match_audio', -1, 'intval'); //是否开启语音:1是 0否
  610. $open_match_video = input('open_match_video', -1, 'intval'); //是否开启视频:1是 0否
  611. $data = [];
  612. //查询我的魅力等级
  613. $level = $this->auth->charm_level;
  614. if ($chat_id) {
  615. $charge_config = Db::name('charge_config')->where(['id' => $chat_id, 'type' => 0])->find();
  616. if (!$charge_config) {
  617. $this->error('您的网络开小差啦~');
  618. }
  619. if ($level < $charge_config['level']) {
  620. $this->error('您还未满足条件~');
  621. }
  622. $data['match_typing_price'] = $charge_config['price'];
  623. }
  624. if ($voice_id) {
  625. $charge_config = Db::name('charge_config')->where(['id' => $voice_id, 'type' => 1])->find();
  626. if (!$charge_config) {
  627. $this->error('您的网络开小差啦~');
  628. }
  629. if ($level < $charge_config['level']) {
  630. $this->error('您还未满足条件~');
  631. }
  632. $data['match_audio_price'] = $charge_config['price'];
  633. }
  634. if ($video_id) {
  635. $charge_config = Db::name('charge_config')->where(['id' => $video_id, 'type' => 2])->find();
  636. if (!$charge_config) {
  637. $this->error('您的网络开小差啦~');
  638. }
  639. if ($level < $charge_config['level']) {
  640. $this->error('您还未满足条件~');
  641. }
  642. $data['match_video_price'] = $charge_config['price'];
  643. }
  644. if (in_array($open_match_audio, [1, 0])) {
  645. $data['open_match_audio'] = $open_match_audio;
  646. }
  647. if (in_array($open_match_video, [1, 0])) {
  648. $data['open_match_video'] = $open_match_video;
  649. }
  650. if (!$data) {
  651. $this->error('没有修改信息~');
  652. }
  653. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  654. if ($rs === false) {
  655. $this->error('您的网络开小差啦~');
  656. }
  657. $this->success('设置成功');
  658. }
  659. //////////////////////////////////////////////////////////////////////////
  660. //微信登录,预先假注册
  661. public function wechatlogin_fake(){
  662. $code = $this->request->param('code','');
  663. if(!$code){
  664. $this->error(__('Invalid parameters'));
  665. }
  666. //微信
  667. $wechat = new Wechat();
  668. $wxuserinfo = $wechat->getAccessToken($code);
  669. if(!$wxuserinfo){
  670. $this->error('openid获取失败');
  671. }
  672. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  673. $this->error('openid获取失败');
  674. }
  675. $openid = $wxuserinfo['openid'];
  676. //检查用户
  677. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  678. if ($user) {
  679. if ($user['status'] == -1) {
  680. $this->error('账户已注销');
  681. }
  682. if ($user['status'] != 1) {
  683. $this->error(__('Account is locked'));
  684. }
  685. //如果已经有账号则直接登录
  686. $ret = $this->auth->direct($user['id']);
  687. if ($ret) {
  688. $userInfo = $this->auth->getUserinfo();
  689. $userInfo['is_register'] = 0;
  690. $userInfo['code'] = $code;
  691. $this->success(__('Logged in successful'), $userInfo);
  692. } else {
  693. $this->error($this->auth->getError());
  694. }
  695. } else {
  696. //记录code和openid,绑定手机号的时候更新openid
  697. $wechatCodeData = [
  698. 'code' => $code,
  699. 'openid' => $openid,
  700. 'createtime' => time(),
  701. ];
  702. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  703. if (empty($wechatCode)) {
  704. Db::name('wechat_code')->insertGetId($wechatCodeData);
  705. } else {
  706. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  707. }
  708. //直接返回
  709. $userInfo = [];
  710. $userInfo['is_register'] = 1;
  711. $userInfo['code'] = $code;
  712. $this->success('获取信息成功', $userInfo);
  713. }
  714. }
  715. /**
  716. * 微信注册来的,绑定手机号
  717. *
  718. * @ApiMethod (POST)
  719. * @param string $mobile 手机号
  720. * @param string $captcha 验证码
  721. */
  722. public function bindmobile_fake()
  723. {
  724. $mobile = $this->request->param('mobile');
  725. $captcha = $this->request->param('captcha');
  726. $code = $this->request->param('code');
  727. if (!$mobile || !$captcha || !$code) {
  728. $this->error(__('Invalid parameters'));
  729. }
  730. if (!Validate::regex($mobile, "^1\d{10}$")) {
  731. $this->error(__('Mobile is incorrect'));
  732. }
  733. $result = Sms::check($mobile, $captcha, 'changemobile');
  734. if (!$result) {
  735. $this->error(__('Captcha is incorrect'));
  736. }
  737. $wechatCodeWhere['code'] = $code;
  738. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  739. if (empty($wechatCode)) {
  740. $this->error('请先微信登录');
  741. }
  742. //检查appid绑定的用户
  743. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  744. if ($user) {
  745. if ($user['status'] == -1) {
  746. $this->error('账户已注销');
  747. }
  748. if ($user['status'] != 1) {
  749. $this->error(__('Account is locked'));
  750. }
  751. //如果已经有账号则直接登录
  752. $ret = $this->auth->direct($user['id']);
  753. $this->success('success',$this->auth->getUserinfo_simple());
  754. }
  755. //新的openid用户
  756. $where = [];
  757. $where['mobile'] = $mobile;
  758. $userData = Db::name('user')->where($where)->find();//老用户
  759. if (!empty($userData)) {
  760. if (empty($userData['wechat_openid'])) {
  761. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  762. } else {
  763. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  764. $this->error('该手机号已被其他用户绑定');
  765. }
  766. }
  767. $ret = $this->auth->direct($userData['id']);
  768. } else {
  769. $extend = [
  770. 'wechat_openid' => $wechatCode['openid'],
  771. 'plat_unique_id' => input('plat_unique_id',''),
  772. 'plat_from' => input('plat_from',''),
  773. ];
  774. $ret = $this->auth->register('', '','', $mobile, $extend);
  775. }
  776. if (!$ret) {
  777. $this->error($this->auth->getError());
  778. }
  779. $this->success('success',$this->auth->getUserinfo_simple());
  780. }
  781. /**
  782. * 运营商一键登录
  783. */
  784. public function onLogin()
  785. {
  786. $accessToken = input('accessToken');// 运营商预取号获取到的token
  787. $token = input('tokenT');// 易盾返回的token
  788. if (!$accessToken || !$token) {
  789. $this->error("参数获取失败!");
  790. }
  791. $params = array(
  792. // 运营商预取号获取到的token
  793. "accessToken" => $accessToken,
  794. // 易盾返回的token
  795. "token" => $token
  796. );
  797. // 获取密钥配置
  798. $configInfo = config("onLogin");
  799. $onlogin = new onlogin($configInfo["secretid"], $configInfo["secretkey"], $configInfo["businessid"]);
  800. $onret = $onlogin->check($params);
  801. // $ret = [];
  802. // $ret["code"] = 200;
  803. // $ret["msg"] = "ok";
  804. // $ret["data"] = [
  805. // "phone" => "17574504021",
  806. // "resultCode" => 0
  807. // ];
  808. if ($onret["code"] == 200) {
  809. $mobile = $onret["data"]["phone"];
  810. if (empty($mobile)) {
  811. // 取号失败,建议进行二次验证,例如短信验证码
  812. $this->error("取号登录失败,请用验证码方式登录!");
  813. } else {
  814. // 取号成功, 执行登录等流程
  815. // 用户登录逻辑 === 开始
  816. $user = \app\common\model\User::getByMobile($mobile);
  817. if ($user) {
  818. if ($user->status == -1) {
  819. $this->error('账户已注销');
  820. }
  821. if ($user->status != 1) {
  822. $this->error(__('Account is locked'));
  823. }
  824. //如果已经有账号则直接登录
  825. $ret = $this->auth->direct($user->id);
  826. $is_register = 0;
  827. } else {
  828. $extend = [
  829. ];
  830. $ret = $this->auth->register('', '', '', $mobile, $extend);
  831. $is_register = 1;
  832. }
  833. if ($ret) {
  834. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  835. } else {
  836. $this->error($this->auth->getError());
  837. }
  838. // 用户登录逻辑 === 结束
  839. }
  840. } else {
  841. $this->error("登录失败,请用验证码方式登录!");
  842. }
  843. }
  844. //苹果登录+预注册
  845. public function applelogin(){
  846. $iosUserId = $this->request->param('ios_user_id','');
  847. if(!$iosUserId){
  848. $this->error(__('Invalid parameters'));
  849. }
  850. //检查用户
  851. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  852. if ($user) {
  853. if ($user['status'] == -1) {
  854. $this->error('账户已经注销');
  855. }
  856. if ($user['status'] != 1) {
  857. $this->error(__('Account is locked'));
  858. }
  859. //如果已经有账号则直接登录
  860. $ret = $this->auth->direct($user['id']);
  861. if ($ret) {
  862. $userInfo = $this->auth->getUserinfo_simple();
  863. $userInfo['is_register'] = 0;
  864. $userInfo['ios_user_id'] = $iosUserId;
  865. $this->success(__('Logged in successful'), $userInfo);
  866. } else {
  867. $this->error($this->auth->getError());
  868. }
  869. } else {
  870. //直接返回
  871. $userInfo = [];
  872. $userInfo['is_register'] = 1;
  873. $userInfo['ios_user_id'] = $iosUserId;
  874. $this->success('获取信息成功', $userInfo);
  875. }
  876. }
  877. /**
  878. * 苹果注册来的,绑定手机号
  879. *
  880. * @ApiMethod (POST)
  881. * @param string $mobile 手机号
  882. * @param string $captcha 验证码
  883. */
  884. public function applebindmobile()
  885. {
  886. $mobile = $this->request->param('mobile');
  887. $captcha = $this->request->param('captcha');
  888. $iosUserId = $this->request->param('ios_user_id','');
  889. if (!$mobile || !$captcha || !$iosUserId) {
  890. $this->error(__('Invalid parameters'));
  891. }
  892. if (!Validate::regex($mobile, "^1\d{10}$")) {
  893. $this->error(__('Mobile is incorrect'));
  894. }
  895. $result = Sms::check($mobile, $captcha, 'changemobile');
  896. if (!$result) {
  897. $this->error(__('Captcha is incorrect'));
  898. }
  899. //检查ios_user_id绑定的用户
  900. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  901. if ($user) {
  902. if ($user['status'] == -1) {
  903. $this->error('账户已经注销');
  904. }
  905. if ($user['status'] != 1) {
  906. $this->error(__('Account is locked'));
  907. }
  908. //如果已经有账号则直接登录
  909. $ret = $this->auth->direct($user['id']);
  910. $this->success('success',$this->auth->getUserinfo_simple());
  911. }
  912. //新的ios用户
  913. $where = [];
  914. $where['mobile'] = $mobile;
  915. $userData = Db::name('user')->where($where)->find();//老用户
  916. if (!empty($userData)) {
  917. if (empty($userData['ios_user_id'])) {
  918. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  919. } else {
  920. if ($userData['ios_user_id'] != $iosUserId) {
  921. $this->error('该手机号已被其他用户绑定');
  922. }
  923. }
  924. $ret = $this->auth->direct($userData['id']);
  925. } else {
  926. $extend = [
  927. 'ios_user_id' => $iosUserId,
  928. ];
  929. $ret = $this->auth->register('', '','', $mobile, $extend);
  930. }
  931. if (!$ret) {
  932. $this->error($this->auth->getError());
  933. }
  934. $this->success('success',$this->auth->getUserinfo_simple());
  935. }
  936. /**
  937. * 重置密码
  938. *
  939. * @ApiMethod (POST)
  940. * @param string $mobile 手机号
  941. * @param string $newpassword 新密码
  942. * @param string $captcha 验证码
  943. */
  944. public function resetpwd()
  945. {
  946. //$type = input("type");
  947. $type = 'mobile';
  948. $mobile = input("mobile");
  949. // $email = input("email");
  950. $newpassword = input("newpassword");
  951. $captcha = input("captcha");
  952. if (!$mobile || !$newpassword || !$captcha) {
  953. $this->error(__('Invalid parameters'));
  954. }
  955. if ($type == 'mobile') {
  956. if (!Validate::regex($mobile, "^1\d{10}$")) {
  957. $this->error(__('Mobile is incorrect'));
  958. }
  959. $user = \app\common\model\User::getByMobile($mobile);
  960. if (!$user) {
  961. $this->error(__('User not found'));
  962. }
  963. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  964. if (!$ret) {
  965. $this->error(__('Captcha is incorrect'));
  966. }
  967. Sms::flush($mobile, 'resetpwd');
  968. }
  969. //模拟一次登录
  970. $this->auth->direct($user->id);
  971. $ret = $this->auth->changepwd($newpassword, '', true);
  972. if ($ret) {
  973. $this->success(__('Reset password successful'));
  974. } else {
  975. $this->error($this->auth->getError());
  976. }
  977. }
  978. /**
  979. * 修改密码
  980. *
  981. * @ApiMethod (POST)
  982. * @param string $newpassword 新密码
  983. * @param string $oldpassword 旧密码
  984. */
  985. public function changepwd(){
  986. $newpassword = input('newpassword');
  987. $oldpassword = input('oldpassword','');
  988. if (!$newpassword) {
  989. $this->error(__('Invalid parameters'));
  990. }
  991. if($this->auth->password && empty($oldpassword)){
  992. $this->error('原密码必填');
  993. }
  994. if(empty($this->auth->password)){
  995. $ret = $this->auth->changepwd($newpassword, '', true);
  996. }else{
  997. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  998. }
  999. if ($ret) {
  1000. $this->success('设置密码成功');
  1001. } else {
  1002. $this->error($this->auth->getError());
  1003. }
  1004. }
  1005. }