User.php 33 KB

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