User.php 35 KB

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