User.php 32 KB

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