User.php 33 KB

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