User.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. //task任务
  459. if(isset($data['tag_ids']) && isset($data['bio'])){
  460. //task任务
  461. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  462. if($task_rs === false){
  463. Db::rollback();
  464. $this->error('完成任务失败');
  465. }
  466. $data['hide_is_finishinfo'] = 1;//完善资料了
  467. }
  468. //修改用户
  469. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  470. if($update_rs === false){
  471. Db::rollback();
  472. $this->error('修改资料失败');
  473. }
  474. Db::commit();
  475. $this->success();
  476. }
  477. public function getpower(){
  478. $rs = Db::name('user_power')->where('user_id',$this->auth->id)->find();
  479. $this->success(1,$rs);
  480. }
  481. /**
  482. * 修改会员权限
  483. */
  484. public function setpower()
  485. {
  486. /*$is_vip = $this->is_vip($this->auth->id);
  487. if(!$is_vip){
  488. $this->error('VIP才能设置隐私权限');
  489. }*/
  490. $field_array = ['meili','caifu','weizhi','giftwall'];
  491. $data = [];
  492. foreach($field_array as $key => $field){
  493. if(!input('?'.$field)){
  494. continue;
  495. }
  496. $newone = input($field);
  497. $data[$field] = $newone;
  498. }
  499. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  500. $this->success();
  501. }
  502. //重新绑定
  503. public function set_intro(){
  504. if(isset($data['introcode']) && !empty($data['introcode'])){
  505. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  506. if(!$intro_user){
  507. $this->error('不存在的邀请人');
  508. }
  509. if(!empty($this->auth->intro_uid)){
  510. $this->error('您已经填写过邀请人');
  511. }
  512. unset($data['introcode']);//别人的邀请码,不能改了自己的
  513. $data['intro_uid'] = $intro_user;
  514. }
  515. }
  516. /*
  517. * 修改用户的坐标
  518. * */
  519. public function change_longlat(){
  520. // $longitude = input('longitude',0);
  521. // $latitude = input('latitude',0);
  522. $cityname = input('cityname','');
  523. $provincename = input('provincename','');
  524. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  525. $this->error();
  526. }*/
  527. $data = [];
  528. // $longitude && $data['longitude'] = $longitude;
  529. // $latitude && $data['latitude'] = $latitude;
  530. $cityname && $data['cityname'] = $cityname;
  531. $provincename && $data['provincename'] = $provincename;
  532. //传入了城市,但是没传入省,直接省名改空
  533. if(isset($data['cityname']) && !isset($data['provincename'])){
  534. $data['provincename'] = '';
  535. }
  536. //没传入城市,但是传入省,直接城市名改空
  537. if(!isset($data['cityname']) && isset($data['provincename'])){
  538. $data['cityname'] = '';
  539. }
  540. if(!empty($data)){
  541. Db::name('user')->where('id',$this->auth->id)->update($data);
  542. }
  543. $this->success();
  544. }
  545. //修改用户设备id
  546. public function change_plat_unique_id(){
  547. $plat_unique_id = input('plat_unique_id','');
  548. $plat_from = input('plat_from','');
  549. $data = [
  550. 'plat_unique_id' => $plat_unique_id,
  551. 'plat_from' => $plat_from,
  552. ];
  553. Db::name('user')->where('id',$this->auth->id)->update($data);
  554. $this->success();
  555. }
  556. /**
  557. * 修改手机号
  558. *
  559. * @ApiMethod (POST)
  560. * @param string $mobile 手机号
  561. * @param string $captcha 验证码
  562. */
  563. public function changemobile()
  564. {
  565. $user = $this->auth->getUser();
  566. $oldcaptcha = $this->request->request('oldcaptcha');
  567. $mobile = $this->request->request('mobile');
  568. $captcha = $this->request->request('captcha');
  569. if (!$oldcaptcha || !$mobile || !$captcha) {
  570. $this->error(__('Invalid parameters'));
  571. }
  572. if (!Validate::regex($mobile, "^1\d{10}$")) {
  573. $this->error(__('Mobile is incorrect'));
  574. }
  575. if($user->mobile == $mobile){
  576. $this->error('新手机号不能与旧手机号相同');
  577. }
  578. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  579. $this->error(__('Mobile already exist'));
  580. }
  581. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  582. if (!$result) {
  583. $this->error(__('Captcha is incorrect'));
  584. }
  585. $result = Sms::check($mobile, $captcha, 'changemobile');
  586. if (!$result) {
  587. $this->error(__('Captcha is incorrect'));
  588. }
  589. Sms::flush($user->mobile, 'changemobile');
  590. Sms::flush($mobile, 'changemobile');
  591. $user->mobile = $mobile;
  592. $user->save();
  593. $this->success();
  594. }
  595. /**
  596. * 苹果注册来的,绑定手机号
  597. *
  598. * @ApiMethod (POST)
  599. * @param string $mobile 手机号
  600. * @param string $captcha 验证码
  601. */
  602. public function applebindmobile()
  603. {
  604. $mobile = $this->request->param('mobile');
  605. $captcha = $this->request->param('captcha');
  606. $iosUserId = $this->request->param('ios_user_id','');
  607. if (!$mobile || !$captcha || !$iosUserId) {
  608. $this->error(__('Invalid parameters'));
  609. }
  610. if (!Validate::regex($mobile, "^1\d{10}$")) {
  611. $this->error(__('Mobile is incorrect'));
  612. }
  613. $result = Sms::check($mobile, $captcha, 'changemobile');
  614. if (!$result) {
  615. $this->error(__('Captcha is incorrect'));
  616. }
  617. //检查ios_user_id绑定的用户
  618. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  619. if ($user) {
  620. if ($user['status'] == -1) {
  621. $this->error('账户已经注销');
  622. }
  623. if ($user['status'] != 1) {
  624. $this->error(__('Account is locked'));
  625. }
  626. //如果已经有账号则直接登录
  627. $ret = $this->auth->direct($user['id']);
  628. $this->success('success',$this->auth->getUserinfo_simple());
  629. }
  630. //新的ios用户
  631. $where = [];
  632. $where['mobile'] = $mobile;
  633. $userData = Db::name('user')->where($where)->find();//老用户
  634. if (!empty($userData)) {
  635. if (empty($userData['ios_user_id'])) {
  636. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  637. } else {
  638. if ($userData['ios_user_id'] != $iosUserId) {
  639. $this->error('该手机号已被其他用户绑定');
  640. }
  641. }
  642. $ret = $this->auth->direct($userData['id']);
  643. } else {
  644. $extend = [
  645. 'ios_user_id' => $iosUserId,
  646. ];
  647. $ret = $this->auth->register('', '','', $mobile, $extend);
  648. }
  649. if (!$ret) {
  650. $this->error($this->auth->getError());
  651. }
  652. $this->success('success',$this->auth->getUserinfo_simple());
  653. }
  654. /**
  655. * 微信注册来的,绑定手机号
  656. *
  657. * @ApiMethod (POST)
  658. * @param string $mobile 手机号
  659. * @param string $captcha 验证码
  660. */
  661. public function bindmobile()
  662. {
  663. $mobile = $this->request->param('mobile');
  664. $captcha = $this->request->param('captcha');
  665. $code = $this->request->param('code');
  666. if (!$mobile || !$captcha || !$code) {
  667. $this->error(__('Invalid parameters'));
  668. }
  669. if (!Validate::regex($mobile, "^1\d{10}$")) {
  670. $this->error(__('Mobile is incorrect'));
  671. }
  672. $result = Sms::check($mobile, $captcha, 'changemobile');
  673. if (!$result) {
  674. $this->error(__('Captcha is incorrect'));
  675. }
  676. $wechatCodeWhere['code'] = $code;
  677. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  678. if (empty($wechatCode)) {
  679. $this->error('请先微信登录');
  680. }
  681. //检查appid绑定的用户
  682. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  683. if ($user) {
  684. if ($user['status'] == -1) {
  685. $this->error('账户已注销');
  686. }
  687. if ($user['status'] != 1) {
  688. $this->error(__('Account is locked'));
  689. }
  690. //如果已经有账号则直接登录
  691. $ret = $this->auth->direct($user['id']);
  692. $this->success('success',$this->auth->getUserinfo_simple());
  693. }
  694. //新的openid用户
  695. $where = [];
  696. $where['mobile'] = $mobile;
  697. $userData = Db::name('user')->where($where)->find();//老用户
  698. if (!empty($userData)) {
  699. if (empty($userData['wechat_openid'])) {
  700. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  701. } else {
  702. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  703. $this->error('该手机号已被其他用户绑定');
  704. }
  705. }
  706. $ret = $this->auth->direct($userData['id']);
  707. } else {
  708. $extend = [
  709. 'wechat_openid' => $wechatCode['openid'],
  710. ];
  711. $ret = $this->auth->register('', '','', $mobile, $extend);
  712. }
  713. if (!$ret) {
  714. $this->error($this->auth->getError());
  715. }
  716. $this->success('success',$this->auth->getUserinfo_simple());
  717. }
  718. /**
  719. * 手机注册来的,绑定微信
  720. *
  721. * @ApiMethod (POST)
  722. * @param string $wechat_openid
  723. */
  724. public function bindopenid()
  725. {
  726. $wechat_openid = input('wechat_openid');
  727. if (!$wechat_openid) {
  728. $this->error(__('Invalid parameters'));
  729. }
  730. if(!empty($this->auth->wechat_openid)){
  731. //$this->error('已经绑定了微信号');
  732. }
  733. $otherUserWhere['wechat_openid'] = $wechat_openid;
  734. $otherUserWhere['id'] = ['neq',$this->auth->id];
  735. if (\app\common\model\User::where($otherUserWhere)->find()) {
  736. $this->error('该微信号已被其他用户绑定');
  737. }
  738. $user = $this->auth->getUser();
  739. $user->wechat_openid = $wechat_openid;
  740. $user->save();
  741. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  742. }
  743. /**
  744. * 重置密码
  745. *
  746. * @ApiMethod (POST)
  747. * @param string $mobile 手机号
  748. * @param string $newpassword 新密码
  749. * @param string $captcha 验证码
  750. */
  751. public function resetpwd()
  752. {
  753. //$type = input("type");
  754. $type = 'mobile';
  755. $mobile = input("mobile");
  756. // $email = input("email");
  757. $newpassword = input("newpassword");
  758. $captcha = input("captcha");
  759. if (!$mobile || !$newpassword || !$captcha) {
  760. $this->error(__('Invalid parameters'));
  761. }
  762. if ($type == 'mobile') {
  763. if (!Validate::regex($mobile, "^1\d{10}$")) {
  764. $this->error(__('Mobile is incorrect'));
  765. }
  766. $user = \app\common\model\User::getByMobile($mobile);
  767. if (!$user) {
  768. $this->error(__('User not found'));
  769. }
  770. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  771. if (!$ret) {
  772. $this->error(__('Captcha is incorrect'));
  773. }
  774. Sms::flush($mobile, 'resetpwd');
  775. }
  776. //模拟一次登录
  777. $this->auth->direct($user->id);
  778. $ret = $this->auth->changepwd($newpassword, '', true);
  779. if ($ret) {
  780. $this->success(__('Reset password successful'));
  781. } else {
  782. $this->error($this->auth->getError());
  783. }
  784. }
  785. /**
  786. * 修改密码
  787. *
  788. * @ApiMethod (POST)
  789. * @param string $newpassword 新密码
  790. * @param string $oldpassword 旧密码
  791. */
  792. public function changepwd(){
  793. $newpassword = input('newpassword');
  794. $oldpassword = input('oldpassword','');
  795. if (!$newpassword) {
  796. $this->error(__('Invalid parameters'));
  797. }
  798. if($this->auth->password && empty($oldpassword)){
  799. $this->error('原密码必填');
  800. }
  801. if(empty($this->auth->password)){
  802. $ret = $this->auth->changepwd($newpassword, '', true);
  803. }else{
  804. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  805. }
  806. if ($ret) {
  807. $this->success('设置密码成功');
  808. } else {
  809. $this->error($this->auth->getError());
  810. }
  811. }
  812. //假注销
  813. public function cancleUser(){
  814. $captcha = input('captcha','');
  815. if (!$captcha) {
  816. $this->error(__('Invalid parameters'));
  817. }
  818. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  819. $this->error(__('Captcha is incorrect'));
  820. }
  821. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  822. $this->auth->logout();
  823. $this->success('注销成功');
  824. }
  825. //文字语音视频收费设置
  826. public function chargeconfig() {
  827. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=视频
  828. if (!in_array($type, [0, 1, 2])) {
  829. $this->error('您的网络开小差啦~');
  830. }
  831. $where['type'] = $type;
  832. $where['level'] = ['elt',$this->auth->charm_level];
  833. $list = Db::name('charge_config')->field('id, price, level')->where($where)->order('price asc')->select();
  834. $this->success('success', $list);
  835. }
  836. //文字语音视频收费/隐藏所在位置设置
  837. public function chargeset() {
  838. if($this->auth->gender == 1 && $this->auth->idcard_status != 1){
  839. $this->error('请先完成实名认证');
  840. }
  841. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  842. $this->error('请先完成真人认证');
  843. }
  844. $chat_id = input('chat_id', 0, 'intval'); //文字收费id
  845. $voice_id = input('voice_id', 0, 'intval'); //语音收费id
  846. $video_id = input('video_id', 0, 'intval'); //视频收费id
  847. $open_match_audio = input('open_match_audio', -1, 'intval'); //是否开启语音:1是 0否
  848. $open_match_video = input('open_match_video', -1, 'intval'); //是否开启视频:1是 0否
  849. $data = [];
  850. //查询我的魅力等级
  851. $level = $this->auth->charm_level;
  852. if ($chat_id) {
  853. $charge_config = Db::name('charge_config')->where(['id' => $chat_id, 'type' => 0])->find();
  854. if (!$charge_config) {
  855. $this->error('您的网络开小差啦~');
  856. }
  857. if ($level < $charge_config['level']) {
  858. $this->error('您还未满足条件~');
  859. }
  860. $data['match_typing_price'] = $charge_config['price'];
  861. }
  862. if ($voice_id) {
  863. $charge_config = Db::name('charge_config')->where(['id' => $voice_id, 'type' => 1])->find();
  864. if (!$charge_config) {
  865. $this->error('您的网络开小差啦~');
  866. }
  867. if ($level < $charge_config['level']) {
  868. $this->error('您还未满足条件~');
  869. }
  870. $data['match_audio_price'] = $charge_config['price'];
  871. }
  872. if ($video_id) {
  873. $charge_config = Db::name('charge_config')->where(['id' => $video_id, 'type' => 2])->find();
  874. if (!$charge_config) {
  875. $this->error('您的网络开小差啦~');
  876. }
  877. if ($level < $charge_config['level']) {
  878. $this->error('您还未满足条件~');
  879. }
  880. $data['match_video_price'] = $charge_config['price'];
  881. }
  882. if (in_array($open_match_audio, [1, 0])) {
  883. $data['open_match_audio'] = $open_match_audio;
  884. }
  885. if (in_array($open_match_video, [1, 0])) {
  886. $data['open_match_video'] = $open_match_video;
  887. }
  888. if (!$data) {
  889. $this->error('没有修改信息~');
  890. }
  891. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  892. if ($rs === false) {
  893. $this->error('您的网络开小差啦~');
  894. }
  895. $this->success('设置成功');
  896. }
  897. }