User.php 35 KB

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