User.php 34 KB

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