User.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. 'invite_no',
  316. ];
  317. $data = [];
  318. foreach($field_array as $key => $field){
  319. //前端传不了post,改了
  320. /*if(!request()->has($field,'post')){
  321. continue;
  322. }*/
  323. if(!input('?'.$field)){
  324. continue;
  325. }
  326. $newone = input($field);
  327. if($field == 'avatar'){
  328. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  329. }
  330. if($field == 'photo_images'){
  331. $newone = input('photo_images', '', 'trim,strip_tags,htmlspecialchars');
  332. }
  333. if($field == 'nickname'){
  334. $newone = Keyworld::sensitive($newone);
  335. }
  336. if($field == 'bio'){
  337. $newone = Keyworld::sensitive($newone);
  338. }
  339. $data[$field] = $newone;
  340. }
  341. //视频,需要审核
  342. $user_audit_switch = 1;
  343. if(isset($data['video_bio']) && !empty($data['video_bio']) && $user_audit_switch == 1){
  344. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','video_bio')->where('status',0)->find();
  345. if(!empty($check_exist)){
  346. $this->error('信息已提交等待审核,请勿重复提交');
  347. }
  348. if($data['video_bio'] != $this->auth->video_bio){
  349. $bio_data = [
  350. 'user_id' => $this->auth->id,
  351. 'type' => 'video_bio',
  352. 'old_data' => $this->auth->video_bio,
  353. 'new_data' => $data['video_bio'],
  354. 'createtime' => time(),
  355. ];
  356. Db::name('user_audit')->insertGetId($bio_data);
  357. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  358. }
  359. unset($data['video_bio']);
  360. }
  361. //相册,需要审核
  362. if(isset($data['photo_images']) && !empty($data['photo_images']) && $user_audit_switch == 1){
  363. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','photo_images')->where('status',0)->find();
  364. if(!empty($check_exist)){
  365. $this->error('信息已提交等待审核,请勿重复提交');
  366. }
  367. if($data['photo_images'] != $this->auth->photo_images){
  368. $bio_data = [
  369. 'user_id' => $this->auth->id,
  370. 'type' => 'photo_images',
  371. 'old_data' => $this->auth->photo_images,
  372. 'new_data' => $data['photo_images'],
  373. 'createtime' => time(),
  374. ];
  375. Db::name('user_audit')->insertGetId($bio_data);
  376. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  377. }
  378. unset($data['photo_images']);
  379. }
  380. //语音,需要审核
  381. if(isset($data['audio_bio']) && !empty($data['audio_bio']) && $user_audit_switch == 1){
  382. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','audio_bio')->where('status',0)->find();
  383. if(!empty($check_exist)){
  384. $this->error('信息已提交等待审核,请勿重复提交');
  385. }
  386. if($data['audio_bio'] != $this->auth->audio_bio){
  387. $bio_data = [
  388. 'user_id' => $this->auth->id,
  389. 'type' => 'audio_bio',
  390. 'old_data' => $this->auth->audio_bio,
  391. 'new_data' => $data['audio_bio'],
  392. 'createtime' => time(),
  393. ];
  394. Db::name('user_audit')->insertGetId($bio_data);
  395. //语音秒数
  396. $bio_data = [
  397. 'user_id' => $this->auth->id,
  398. 'type' => 'audio_seconds',
  399. 'old_data' => $this->auth->audio_seconds,
  400. 'new_data' => $data['audio_seconds'],
  401. 'createtime' => time(),
  402. ];
  403. Db::name('user_audit')->insertGetId($bio_data);
  404. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  405. }
  406. unset($data['audio_bio']);
  407. unset($data['audio_seconds']);
  408. }
  409. //头像,是否需要审核
  410. if(isset($data['avatar']) && !empty($data['avatar']) && $data['avatar'] != config('avatar_boy') && $data['avatar'] != config('avatar_girl') && $user_audit_switch == 1){
  411. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','avatar')->where('status',0)->find();
  412. if(!empty($check_exist)){
  413. $this->error('信息已提交等待审核,请勿重复提交');
  414. }
  415. if($data['avatar'] != $this->auth->avatar){
  416. $bio_data = [
  417. 'user_id' => $this->auth->id,
  418. 'type' => 'avatar',
  419. 'old_data' => $this->auth->avatar,
  420. 'new_data' => $data['avatar'],
  421. 'createtime' => time(),
  422. ];
  423. Db::name('user_audit')->insertGetId($bio_data);
  424. //失去真人认证
  425. $data['real_status'] = -1;
  426. Db::name('user_auth')->where(['user_id' => $this->auth->id])->delete();
  427. if(!isset($data['gender'])){
  428. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  429. }
  430. }
  431. unset($data['avatar']);
  432. }
  433. //第一次传入性别,头像只能用默认,因为自传头像需要审核
  434. if(isset($data['gender']) && $user_audit_switch == 1){
  435. $data['avatar'] = $data['gender'] == 1 ? config('avatar_boy') : config('avatar_girl');
  436. }
  437. //初次设置性别为 男
  438. if(isset($data['gender']) && $data['gender'] == 1 && $this->auth->gender == -1){
  439. //男性赠送
  440. $gift_data = [
  441. 'audio_sec' => config('site.man_reg_audio_sec'),
  442. 'video_sec' => config('site.man_reg_video_sec'),
  443. 'typing_times' => config('site.man_reg_typing_times'),
  444. ];
  445. Db::name('user_wallet')->where('id',$this->auth->id)->update($gift_data);
  446. //修改财富值等级为1级
  447. $data['wealth_level'] = 1;
  448. }
  449. if(isset($data['invite_no']) && !empty($data['invite_no']) && empty($this->auth->intro_uid) && empty($this->auth->invite_uid)){
  450. $invite_user = Db::name('user')->where('introcode',$data['invite_no'])->value('id');
  451. if($invite_user){
  452. $data['invite_uid'] = $invite_user;
  453. }
  454. unset($data['invite_no']);//别人的邀请码,不能改了自己的
  455. }
  456. //
  457. if(isset($data['birthday'])){
  458. $data['birthday'] = strtotime($data['birthday']);
  459. }
  460. if(isset($data['tag_ids'])){
  461. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  462. }
  463. if(isset($data['hobby_ids'])){
  464. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  465. }
  466. //dump($data);
  467. if(empty($data)){
  468. $this->success();
  469. }
  470. Db::startTrans();
  471. //修改用户
  472. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  473. if($update_rs === false){
  474. Db::rollback();
  475. $this->error('修改资料失败');
  476. }
  477. //task任务
  478. if(isset($data['tag_ids']) || isset($data['bio'])){
  479. $userinfo = Db::name('user')->where('id',$this->auth->id)->find();
  480. if($userinfo['hide_is_finishinfo'] == 0){
  481. if($userinfo['tag_ids'] && $userinfo['bio']){
  482. //task任务
  483. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  484. if($task_rs === false){
  485. Db::rollback();
  486. $this->error('完成任务失败');
  487. }
  488. $finish['hide_is_finishinfo'] = 1;//完善资料了
  489. Db::name('user')->where('id',$this->auth->id)->update($finish);
  490. }
  491. }
  492. }
  493. Db::commit();
  494. $this->success();
  495. }
  496. public function getpower(){
  497. $rs = Db::name('user_power')->where('user_id',$this->auth->id)->find();
  498. $this->success(1,$rs);
  499. }
  500. /**
  501. * 修改会员权限
  502. */
  503. public function setpower()
  504. {
  505. /*$is_vip = $this->is_vip($this->auth->id);
  506. if(!$is_vip){
  507. $this->error('VIP才能设置隐私权限');
  508. }*/
  509. $field_array = ['meili','caifu','weizhi','giftwall'];
  510. $data = [];
  511. foreach($field_array as $key => $field){
  512. if(!input('?'.$field)){
  513. continue;
  514. }
  515. $newone = input($field);
  516. $data[$field] = $newone;
  517. }
  518. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  519. $this->success();
  520. }
  521. //重新绑定
  522. public function set_intro(){
  523. if(isset($data['introcode']) && !empty($data['introcode'])){
  524. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  525. if(!$intro_user){
  526. $this->error('不存在的邀请人');
  527. }
  528. if(!empty($this->auth->intro_uid)){
  529. $this->error('您已经填写过邀请人');
  530. }
  531. unset($data['introcode']);//别人的邀请码,不能改了自己的
  532. $data['intro_uid'] = $intro_user;
  533. }
  534. }
  535. /*
  536. * 修改用户的坐标
  537. * */
  538. public function change_longlat(){
  539. // $longitude = input('longitude',0);
  540. // $latitude = input('latitude',0);
  541. $cityname = input('cityname','');
  542. $provincename = input('provincename','');
  543. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  544. $this->error();
  545. }*/
  546. $data = [];
  547. // $longitude && $data['longitude'] = $longitude;
  548. // $latitude && $data['latitude'] = $latitude;
  549. $cityname && $data['cityname'] = $cityname;
  550. $provincename && $data['provincename'] = $provincename;
  551. //传入了城市,但是没传入省,直接省名改空
  552. if(isset($data['cityname']) && !isset($data['provincename'])){
  553. $data['provincename'] = '';
  554. }
  555. //没传入城市,但是传入省,直接城市名改空
  556. if(!isset($data['cityname']) && isset($data['provincename'])){
  557. $data['cityname'] = '';
  558. }
  559. if(!empty($data)){
  560. Db::name('user')->where('id',$this->auth->id)->update($data);
  561. }
  562. $this->success();
  563. }
  564. //修改用户设备id
  565. public function change_plat_unique_id(){
  566. $plat_unique_id = input('plat_unique_id','');
  567. $plat_from = input('plat_from','');
  568. $data = [
  569. 'plat_unique_id' => $plat_unique_id,
  570. 'plat_from' => $plat_from,
  571. ];
  572. Db::name('user')->where('id',$this->auth->id)->update($data);
  573. $this->success();
  574. }
  575. /**
  576. * 修改手机号
  577. *
  578. * @ApiMethod (POST)
  579. * @param string $mobile 手机号
  580. * @param string $captcha 验证码
  581. */
  582. public function changemobile()
  583. {
  584. $user = $this->auth->getUser();
  585. $oldcaptcha = $this->request->request('oldcaptcha');
  586. $mobile = $this->request->request('mobile');
  587. $captcha = $this->request->request('captcha');
  588. if (!$oldcaptcha || !$mobile || !$captcha) {
  589. $this->error(__('Invalid parameters'));
  590. }
  591. if (!Validate::regex($mobile, "^1\d{10}$")) {
  592. $this->error(__('Mobile is incorrect'));
  593. }
  594. if($user->mobile == $mobile){
  595. $this->error('新手机号不能与旧手机号相同');
  596. }
  597. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  598. $this->error(__('Mobile already exist'));
  599. }
  600. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  601. if (!$result) {
  602. $this->error(__('Captcha is incorrect'));
  603. }
  604. $result = Sms::check($mobile, $captcha, 'changemobile');
  605. if (!$result) {
  606. $this->error(__('Captcha is incorrect'));
  607. }
  608. Sms::flush($user->mobile, 'changemobile');
  609. Sms::flush($mobile, 'changemobile');
  610. $user->mobile = $mobile;
  611. $user->save();
  612. $this->success();
  613. }
  614. /**
  615. * 苹果注册来的,绑定手机号
  616. *
  617. * @ApiMethod (POST)
  618. * @param string $mobile 手机号
  619. * @param string $captcha 验证码
  620. */
  621. public function applebindmobile()
  622. {
  623. $mobile = $this->request->param('mobile');
  624. $captcha = $this->request->param('captcha');
  625. $iosUserId = $this->request->param('ios_user_id','');
  626. if (!$mobile || !$captcha || !$iosUserId) {
  627. $this->error(__('Invalid parameters'));
  628. }
  629. if (!Validate::regex($mobile, "^1\d{10}$")) {
  630. $this->error(__('Mobile is incorrect'));
  631. }
  632. $result = Sms::check($mobile, $captcha, 'changemobile');
  633. if (!$result) {
  634. $this->error(__('Captcha is incorrect'));
  635. }
  636. //检查ios_user_id绑定的用户
  637. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  638. if ($user) {
  639. if ($user['status'] == -1) {
  640. $this->error('账户已经注销');
  641. }
  642. if ($user['status'] != 1) {
  643. $this->error(__('Account is locked'));
  644. }
  645. //如果已经有账号则直接登录
  646. $ret = $this->auth->direct($user['id']);
  647. $this->success('success',$this->auth->getUserinfo_simple());
  648. }
  649. //新的ios用户
  650. $where = [];
  651. $where['mobile'] = $mobile;
  652. $userData = Db::name('user')->where($where)->find();//老用户
  653. if (!empty($userData)) {
  654. if (empty($userData['ios_user_id'])) {
  655. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  656. } else {
  657. if ($userData['ios_user_id'] != $iosUserId) {
  658. $this->error('该手机号已被其他用户绑定');
  659. }
  660. }
  661. $ret = $this->auth->direct($userData['id']);
  662. } else {
  663. $extend = [
  664. 'ios_user_id' => $iosUserId,
  665. ];
  666. $ret = $this->auth->register('', '','', $mobile, $extend);
  667. }
  668. if (!$ret) {
  669. $this->error($this->auth->getError());
  670. }
  671. $this->success('success',$this->auth->getUserinfo_simple());
  672. }
  673. /**
  674. * 微信注册来的,绑定手机号
  675. *
  676. * @ApiMethod (POST)
  677. * @param string $mobile 手机号
  678. * @param string $captcha 验证码
  679. */
  680. public function bindmobile()
  681. {
  682. $mobile = $this->request->param('mobile');
  683. $captcha = $this->request->param('captcha');
  684. $code = $this->request->param('code');
  685. if (!$mobile || !$captcha || !$code) {
  686. $this->error(__('Invalid parameters'));
  687. }
  688. if (!Validate::regex($mobile, "^1\d{10}$")) {
  689. $this->error(__('Mobile is incorrect'));
  690. }
  691. $result = Sms::check($mobile, $captcha, 'changemobile');
  692. if (!$result) {
  693. $this->error(__('Captcha is incorrect'));
  694. }
  695. $wechatCodeWhere['code'] = $code;
  696. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  697. if (empty($wechatCode)) {
  698. $this->error('请先微信登录');
  699. }
  700. //检查appid绑定的用户
  701. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  702. if ($user) {
  703. if ($user['status'] == -1) {
  704. $this->error('账户已注销');
  705. }
  706. if ($user['status'] != 1) {
  707. $this->error(__('Account is locked'));
  708. }
  709. //如果已经有账号则直接登录
  710. $ret = $this->auth->direct($user['id']);
  711. $this->success('success',$this->auth->getUserinfo_simple());
  712. }
  713. //新的openid用户
  714. $where = [];
  715. $where['mobile'] = $mobile;
  716. $userData = Db::name('user')->where($where)->find();//老用户
  717. if (!empty($userData)) {
  718. if (empty($userData['wechat_openid'])) {
  719. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  720. } else {
  721. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  722. $this->error('该手机号已被其他用户绑定');
  723. }
  724. }
  725. $ret = $this->auth->direct($userData['id']);
  726. } else {
  727. $extend = [
  728. 'wechat_openid' => $wechatCode['openid'],
  729. 'plat_unique_id' => input('plat_unique_id',''),
  730. 'plat_from' => input('plat_from',''),
  731. ];
  732. $ret = $this->auth->register('', '','', $mobile, $extend);
  733. }
  734. if (!$ret) {
  735. $this->error($this->auth->getError());
  736. }
  737. $this->success('success',$this->auth->getUserinfo_simple());
  738. }
  739. /**
  740. * 手机注册来的,绑定微信
  741. *
  742. * @ApiMethod (POST)
  743. * @param string $wechat_openid
  744. */
  745. public function bindopenid()
  746. {
  747. $wechat_openid = input('wechat_openid');
  748. if (!$wechat_openid) {
  749. $this->error(__('Invalid parameters'));
  750. }
  751. if(!empty($this->auth->wechat_openid)){
  752. //$this->error('已经绑定了微信号');
  753. }
  754. $otherUserWhere['wechat_openid'] = $wechat_openid;
  755. $otherUserWhere['id'] = ['neq',$this->auth->id];
  756. if (\app\common\model\User::where($otherUserWhere)->find()) {
  757. $this->error('该微信号已被其他用户绑定');
  758. }
  759. $user = $this->auth->getUser();
  760. $user->wechat_openid = $wechat_openid;
  761. $user->save();
  762. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  763. }
  764. /**
  765. * 重置密码
  766. *
  767. * @ApiMethod (POST)
  768. * @param string $mobile 手机号
  769. * @param string $newpassword 新密码
  770. * @param string $captcha 验证码
  771. */
  772. public function resetpwd()
  773. {
  774. //$type = input("type");
  775. $type = 'mobile';
  776. $mobile = input("mobile");
  777. // $email = input("email");
  778. $newpassword = input("newpassword");
  779. $captcha = input("captcha");
  780. if (!$mobile || !$newpassword || !$captcha) {
  781. $this->error(__('Invalid parameters'));
  782. }
  783. if ($type == 'mobile') {
  784. if (!Validate::regex($mobile, "^1\d{10}$")) {
  785. $this->error(__('Mobile is incorrect'));
  786. }
  787. $user = \app\common\model\User::getByMobile($mobile);
  788. if (!$user) {
  789. $this->error(__('User not found'));
  790. }
  791. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  792. if (!$ret) {
  793. $this->error(__('Captcha is incorrect'));
  794. }
  795. Sms::flush($mobile, 'resetpwd');
  796. }
  797. //模拟一次登录
  798. $this->auth->direct($user->id);
  799. $ret = $this->auth->changepwd($newpassword, '', true);
  800. if ($ret) {
  801. $this->success(__('Reset password successful'));
  802. } else {
  803. $this->error($this->auth->getError());
  804. }
  805. }
  806. /**
  807. * 修改密码
  808. *
  809. * @ApiMethod (POST)
  810. * @param string $newpassword 新密码
  811. * @param string $oldpassword 旧密码
  812. */
  813. public function changepwd(){
  814. $newpassword = input('newpassword');
  815. $oldpassword = input('oldpassword','');
  816. if (!$newpassword) {
  817. $this->error(__('Invalid parameters'));
  818. }
  819. if($this->auth->password && empty($oldpassword)){
  820. $this->error('原密码必填');
  821. }
  822. if(empty($this->auth->password)){
  823. $ret = $this->auth->changepwd($newpassword, '', true);
  824. }else{
  825. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  826. }
  827. if ($ret) {
  828. $this->success('设置密码成功');
  829. } else {
  830. $this->error($this->auth->getError());
  831. }
  832. }
  833. //假注销
  834. public function cancleUser(){
  835. $captcha = input('captcha','');
  836. if (!$captcha) {
  837. $this->error(__('Invalid parameters'));
  838. }
  839. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  840. $this->error(__('Captcha is incorrect'));
  841. }
  842. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  843. $this->auth->logout();
  844. $this->success('注销成功');
  845. }
  846. //文字语音视频收费设置
  847. public function chargeconfig() {
  848. $type = input('type', 0, 'intval'); //类型:0=文字,1=语音,2=视频
  849. if (!in_array($type, [0, 1, 2])) {
  850. $this->error('您的网络开小差啦~');
  851. }
  852. $where['type'] = $type;
  853. $where['level'] = ['elt',$this->auth->charm_level];
  854. $list = Db::name('charge_config')->field('id, price, level')->where($where)->order('price asc')->select();
  855. $this->success('success', $list);
  856. }
  857. //文字语音视频收费/隐藏所在位置设置
  858. public function chargeset() {
  859. if($this->auth->gender == 1 && $this->auth->idcard_status != 1){
  860. $this->error('请先完成实名认证');
  861. }
  862. if($this->auth->gender == 0 && $this->auth->real_status != 1){
  863. $this->error('请先完成真人认证');
  864. }
  865. $chat_id = input('chat_id', 0, 'intval'); //文字收费id
  866. $voice_id = input('voice_id', 0, 'intval'); //语音收费id
  867. $video_id = input('video_id', 0, 'intval'); //视频收费id
  868. $open_match_audio = input('open_match_audio', -1, 'intval'); //是否开启语音:1是 0否
  869. $open_match_video = input('open_match_video', -1, 'intval'); //是否开启视频:1是 0否
  870. $data = [];
  871. //查询我的魅力等级
  872. $level = $this->auth->charm_level;
  873. if ($chat_id) {
  874. $charge_config = Db::name('charge_config')->where(['id' => $chat_id, 'type' => 0])->find();
  875. if (!$charge_config) {
  876. $this->error('您的网络开小差啦~');
  877. }
  878. if ($level < $charge_config['level']) {
  879. $this->error('您还未满足条件~');
  880. }
  881. $data['match_typing_price'] = $charge_config['price'];
  882. }
  883. if ($voice_id) {
  884. $charge_config = Db::name('charge_config')->where(['id' => $voice_id, 'type' => 1])->find();
  885. if (!$charge_config) {
  886. $this->error('您的网络开小差啦~');
  887. }
  888. if ($level < $charge_config['level']) {
  889. $this->error('您还未满足条件~');
  890. }
  891. $data['match_audio_price'] = $charge_config['price'];
  892. }
  893. if ($video_id) {
  894. $charge_config = Db::name('charge_config')->where(['id' => $video_id, 'type' => 2])->find();
  895. if (!$charge_config) {
  896. $this->error('您的网络开小差啦~');
  897. }
  898. if ($level < $charge_config['level']) {
  899. $this->error('您还未满足条件~');
  900. }
  901. $data['match_video_price'] = $charge_config['price'];
  902. }
  903. if (in_array($open_match_audio, [1, 0])) {
  904. $data['open_match_audio'] = $open_match_audio;
  905. }
  906. if (in_array($open_match_video, [1, 0])) {
  907. $data['open_match_video'] = $open_match_video;
  908. }
  909. if (!$data) {
  910. $this->error('没有修改信息~');
  911. }
  912. $rs = Db::name('user')->where(['id' => $this->auth->id])->setField($data);
  913. if ($rs === false) {
  914. $this->error('您的网络开小差啦~');
  915. }
  916. $this->success('设置成功');
  917. }
  918. }