User.php 31 KB

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