User.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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','introcode',
  263. 'audio_bio','photo_images','video_bio',
  264. 'height','weight','provincename','cityname','bio',
  265. 'marital_id','job_id','wages_id','suqiu_id','tag_ids','hobby_ids',
  266. 'hide_is_finishinfo',
  267. 'open_match_video','open_match_audio','match_video_price','match_audio_price','match_typing_price'
  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. $data[$field] = $newone;
  286. }
  287. //视频,需要审核
  288. $user_audit_switch = config('site.user_audit_switch');
  289. if(isset($data['video_bio']) && !empty($data['video_bio']) && $user_audit_switch == 1){
  290. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','video_bio')->where('status',0)->find();
  291. if(!empty($check_exist)){
  292. $this->error('信息已提交等待审核,请勿重复提交');
  293. }
  294. if($data['video_bio'] != $this->auth->video_bio){
  295. $bio_data = [
  296. 'user_id' => $this->auth->id,
  297. 'type' => 'video_bio',
  298. 'old_data' => $this->auth->video_bio,
  299. 'new_data' => $data['video_bio'],
  300. 'createtime' => time(),
  301. ];
  302. Db::name('user_audit')->insertGetId($bio_data);
  303. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  304. }
  305. unset($data['video_bio']);
  306. }
  307. //相册,需要审核
  308. if(isset($data['photo_images']) && !empty($data['photo_images']) && $user_audit_switch == 1){
  309. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','photo_images')->where('status',0)->find();
  310. if(!empty($check_exist)){
  311. $this->error('信息已提交等待审核,请勿重复提交');
  312. }
  313. if($data['photo_images'] != $this->auth->photo_images){
  314. $bio_data = [
  315. 'user_id' => $this->auth->id,
  316. 'type' => 'photo_images',
  317. 'old_data' => $this->auth->photo_images,
  318. 'new_data' => $data['photo_images'],
  319. 'createtime' => time(),
  320. ];
  321. Db::name('user_audit')->insertGetId($bio_data);
  322. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  323. }
  324. unset($data['photo_images']);
  325. }
  326. //语音,需要审核
  327. if(isset($data['audio_bio']) && !empty($data['audio_bio']) && $user_audit_switch == 1){
  328. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','audio_bio')->where('status',0)->find();
  329. if(!empty($check_exist)){
  330. $this->error('信息已提交等待审核,请勿重复提交');
  331. }
  332. if($data['audio_bio'] != $this->auth->audio_bio){
  333. $bio_data = [
  334. 'user_id' => $this->auth->id,
  335. 'type' => 'audio_bio',
  336. 'old_data' => $this->auth->audio_bio,
  337. 'new_data' => $data['audio_bio'],
  338. 'createtime' => time(),
  339. ];
  340. Db::name('user_audit')->insertGetId($bio_data);
  341. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  342. }
  343. unset($data['audio_bio']);
  344. }
  345. //头像,是否需要审核
  346. if(isset($data['avatar']) && !empty($data['avatar']) && $data['avatar'] != config('avatar_boy') && $data['avatar'] != config('avatar_girl') && $user_audit_switch == 1){
  347. $check_exist = Db::name('user_audit')->where('user_id',$this->auth->id)->where('type','audio_bio')->where('status',0)->find();
  348. if(!empty($check_exist)){
  349. $this->error('信息已提交等待审核,请勿重复提交');
  350. }
  351. if($data['avatar'] != $this->auth->avatar){
  352. $bio_data = [
  353. 'user_id' => $this->auth->id,
  354. 'type' => 'avatar',
  355. 'old_data' => $this->auth->avatar,
  356. 'new_data' => $data['avatar'],
  357. 'createtime' => time(),
  358. ];
  359. Db::name('user_audit')->insertGetId($bio_data);
  360. if(!isset($data['gender'])){
  361. $this->error('信息已提交,审核通过后即可正常展示');//正确不弹出,只能用error
  362. }
  363. }
  364. unset($data['avatar']);
  365. }
  366. //第一次传入性别,头像只能用默认,因为自传头像需要审核
  367. if(isset($data['gender']) && $user_audit_switch == 1){
  368. $data['avatar'] = $data['gender'] == 1 ? config('avatar_boy') : config('avatar_girl');
  369. }
  370. //
  371. if(isset($data['birthday'])){
  372. $data['birthday'] = strtotime($data['birthday']);
  373. }
  374. if(isset($data['tag_ids'])){
  375. $data['tag_ids'] = implode(',',explode(',',$data['tag_ids']));
  376. }
  377. if(isset($data['hobby_ids'])){
  378. $data['hobby_ids'] = implode(',',explode(',',$data['hobby_ids']));
  379. }
  380. //dump($data);
  381. if(empty($data)){
  382. $this->success();
  383. }
  384. Db::startTrans();
  385. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  386. if($update_rs === false){
  387. Db::rollback();
  388. $this->error('修改资料失败');
  389. }
  390. //task任务
  391. if(isset($data['tag_ids']) && isset($data['bio']) && !empty($this->auth->audio_bio)){
  392. //task任务
  393. $task_rs = \app\common\model\TaskLog::tofinish($this->auth->id,4);
  394. if($task_rs === false){
  395. Db::rollback();
  396. $this->error('完成任务失败');
  397. }
  398. }
  399. Db::commit();
  400. $this->success();
  401. }
  402. /**
  403. * 修改会员权限
  404. */
  405. public function setpower()
  406. {
  407. /*$is_vip = $this->is_vip($this->auth->id);
  408. if(!$is_vip){
  409. $this->error('VIP才能设置隐私权限');
  410. }*/
  411. $field_array = ['meili','weizhi','giftwall'];
  412. $data = [];
  413. foreach($field_array as $key => $field){
  414. if(!input('?'.$field)){
  415. continue;
  416. }
  417. $newone = input($field);
  418. $data[$field] = $newone;
  419. }
  420. $update_rs = Db::name('user_power')->where('user_id',$this->auth->id)->update($data);
  421. $this->success();
  422. }
  423. //重新绑定
  424. public function set_intro(){
  425. if(isset($data['introcode']) && !empty($data['introcode'])){
  426. $intro_user = Db::name('user')->where('introcode',$data['introcode'])->value('id');
  427. if(!$intro_user){
  428. $this->error('不存在的邀请人');
  429. }
  430. if(!empty($this->auth->intro_uid)){
  431. $this->error('您已经填写过邀请人');
  432. }
  433. unset($data['introcode']);//别人的邀请码,不能改了自己的
  434. $data['intro_uid'] = $intro_user;
  435. }
  436. }
  437. /*
  438. * 修改用户的坐标
  439. * */
  440. public function change_longlat(){
  441. $longitude = input('longitude',0);
  442. $latitude = input('latitude',0);
  443. $cityname = input('cityname','');
  444. $provincename = input('provincename','');
  445. /*if(empty($longitude) || empty($latitude) || empty($cityname)){
  446. $this->error();
  447. }*/
  448. $data = [];
  449. $longitude && $data['longitude'] = $longitude;
  450. $latitude && $data['latitude'] = $latitude;
  451. $cityname && $data['cityname'] = $cityname;
  452. $provincename && $data['provincename'] = $provincename;
  453. //传入了城市,但是没传入省,直接省名改空
  454. if(isset($data['cityname']) && !isset($data['provincename'])){
  455. $data['provincename'] = '';
  456. }
  457. //没传入城市,但是传入省,直接城市名改空
  458. if(!isset($data['cityname']) && isset($data['provincename'])){
  459. $data['cityname'] = '';
  460. }
  461. if(!empty($data)){
  462. Db::name('user')->where('id',$this->auth->id)->update($data);
  463. }
  464. $this->success();
  465. }
  466. //修改用户设备id
  467. public function change_plat_unique_id(){
  468. $plat_unique_id = input('plat_unique_id','');
  469. $data = [
  470. 'plat_unique_id' => $plat_unique_id,
  471. ];
  472. Db::name('user')->where('id',$this->auth->id)->update($data);
  473. $this->success();
  474. }
  475. /**
  476. * 修改手机号
  477. *
  478. * @ApiMethod (POST)
  479. * @param string $mobile 手机号
  480. * @param string $captcha 验证码
  481. */
  482. public function changemobile()
  483. {
  484. $user = $this->auth->getUser();
  485. $oldcaptcha = $this->request->request('oldcaptcha');
  486. $mobile = $this->request->request('mobile');
  487. $captcha = $this->request->request('captcha');
  488. if (!$oldcaptcha || !$mobile || !$captcha) {
  489. $this->error(__('Invalid parameters'));
  490. }
  491. if (!Validate::regex($mobile, "^1\d{10}$")) {
  492. $this->error(__('Mobile is incorrect'));
  493. }
  494. if($user->mobile == $mobile){
  495. $this->error('新手机号不能与旧手机号相同');
  496. }
  497. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  498. $this->error(__('Mobile already exist'));
  499. }
  500. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  501. if (!$result) {
  502. $this->error(__('Captcha is incorrect'));
  503. }
  504. $result = Sms::check($mobile, $captcha, 'changemobile');
  505. if (!$result) {
  506. $this->error(__('Captcha is incorrect'));
  507. }
  508. Sms::flush($user->mobile, 'changemobile');
  509. Sms::flush($mobile, 'changemobile');
  510. $user->mobile = $mobile;
  511. $user->save();
  512. $this->success();
  513. }
  514. /**
  515. * 苹果注册来的,绑定手机号
  516. *
  517. * @ApiMethod (POST)
  518. * @param string $mobile 手机号
  519. * @param string $captcha 验证码
  520. */
  521. public function applebindmobile()
  522. {
  523. $mobile = $this->request->param('mobile');
  524. $captcha = $this->request->param('captcha');
  525. $iosUserId = $this->request->param('ios_user_id','');
  526. if (!$mobile || !$captcha || !$iosUserId) {
  527. $this->error(__('Invalid parameters'));
  528. }
  529. if (!Validate::regex($mobile, "^1\d{10}$")) {
  530. $this->error(__('Mobile is incorrect'));
  531. }
  532. $result = Sms::check($mobile, $captcha, 'changemobile');
  533. if (!$result) {
  534. $this->error(__('Captcha is incorrect'));
  535. }
  536. //检查ios_user_id绑定的用户
  537. $user = Db::name('user')->where('ios_user_id',$iosUserId)->find();
  538. if ($user) {
  539. if ($user['status'] == -1) {
  540. $this->error('账户已经注销');
  541. }
  542. if ($user['status'] != 1) {
  543. $this->error(__('Account is locked'));
  544. }
  545. //如果已经有账号则直接登录
  546. $ret = $this->auth->direct($user['id']);
  547. $this->success('success',$this->auth->getUserinfo_simple());
  548. }
  549. //新的ios用户
  550. $where = [];
  551. $where['mobile'] = $mobile;
  552. $userData = Db::name('user')->where($where)->find();//老用户
  553. if (!empty($userData)) {
  554. if (empty($userData['ios_user_id'])) {
  555. Db::name('user')->where('id',$userData['id'])->update(['ios_user_id' => $iosUserId]);//老用户更新ios_user_id
  556. } else {
  557. if ($userData['ios_user_id'] != $iosUserId) {
  558. $this->error('该手机号已被其他用户绑定');
  559. }
  560. }
  561. $ret = $this->auth->direct($userData['id']);
  562. } else {
  563. $extend = [
  564. 'ios_user_id' => $iosUserId,
  565. ];
  566. $ret = $this->auth->register('', '','', $mobile, $extend);
  567. }
  568. if (!$ret) {
  569. $this->error($this->auth->getError());
  570. }
  571. $this->success('success',$this->auth->getUserinfo_simple());
  572. }
  573. /**
  574. * 微信注册来的,绑定手机号
  575. *
  576. * @ApiMethod (POST)
  577. * @param string $mobile 手机号
  578. * @param string $captcha 验证码
  579. */
  580. public function bindmobile()
  581. {
  582. $mobile = $this->request->param('mobile');
  583. $captcha = $this->request->param('captcha');
  584. $code = $this->request->param('code');
  585. if (!$mobile || !$captcha || !$code) {
  586. $this->error(__('Invalid parameters'));
  587. }
  588. if (!Validate::regex($mobile, "^1\d{10}$")) {
  589. $this->error(__('Mobile is incorrect'));
  590. }
  591. $result = Sms::check($mobile, $captcha, 'changemobile');
  592. if (!$result) {
  593. $this->error(__('Captcha is incorrect'));
  594. }
  595. $wechatCodeWhere['code'] = $code;
  596. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  597. if (empty($wechatCode)) {
  598. $this->error('请先微信登录');
  599. }
  600. //检查appid绑定的用户
  601. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  602. if ($user) {
  603. if ($user['status'] == -1) {
  604. $this->error('账户已注销');
  605. }
  606. if ($user['status'] != 1) {
  607. $this->error(__('Account is locked'));
  608. }
  609. //如果已经有账号则直接登录
  610. $ret = $this->auth->direct($user['id']);
  611. $this->success('success',$this->auth->getUserinfo_simple());
  612. }
  613. //新的openid用户
  614. $where = [];
  615. $where['mobile'] = $mobile;
  616. $userData = Db::name('user')->where($where)->find();//老用户
  617. if (!empty($userData)) {
  618. if (empty($userData['wechat_openid'])) {
  619. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  620. } else {
  621. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  622. $this->error('该手机号已被其他用户绑定');
  623. }
  624. }
  625. $ret = $this->auth->direct($userData['id']);
  626. } else {
  627. $extend = [
  628. 'wechat_openid' => $wechatCode['openid'],
  629. ];
  630. $ret = $this->auth->register('', '','', $mobile, $extend);
  631. }
  632. if (!$ret) {
  633. $this->error($this->auth->getError());
  634. }
  635. $this->success('success',$this->auth->getUserinfo_simple());
  636. }
  637. /**
  638. * 手机注册来的,绑定微信
  639. *
  640. * @ApiMethod (POST)
  641. * @param string $wechat_openid
  642. */
  643. public function bindopenid()
  644. {
  645. $wechat_openid = input('wechat_openid');
  646. if (!$wechat_openid) {
  647. $this->error(__('Invalid parameters'));
  648. }
  649. if(!empty($this->auth->wechat_openid)){
  650. //$this->error('已经绑定了微信号');
  651. }
  652. $otherUserWhere['wechat_openid'] = $wechat_openid;
  653. $otherUserWhere['id'] = ['neq',$this->auth->id];
  654. if (\app\common\model\User::where($otherUserWhere)->find()) {
  655. $this->error('该微信号已被其他用户绑定');
  656. }
  657. $user = $this->auth->getUser();
  658. $user->wechat_openid = $wechat_openid;
  659. $user->save();
  660. $this->success('绑定成功',$this->auth->getUserinfo_simple());
  661. }
  662. /**
  663. * 重置密码
  664. *
  665. * @ApiMethod (POST)
  666. * @param string $mobile 手机号
  667. * @param string $newpassword 新密码
  668. * @param string $captcha 验证码
  669. */
  670. public function resetpwd()
  671. {
  672. //$type = input("type");
  673. $type = 'mobile';
  674. $mobile = input("mobile");
  675. // $email = input("email");
  676. $newpassword = input("newpassword");
  677. $captcha = input("captcha");
  678. if (!$mobile || !$newpassword || !$captcha) {
  679. $this->error(__('Invalid parameters'));
  680. }
  681. if ($type == 'mobile') {
  682. if (!Validate::regex($mobile, "^1\d{10}$")) {
  683. $this->error(__('Mobile is incorrect'));
  684. }
  685. $user = \app\common\model\User::getByMobile($mobile);
  686. if (!$user) {
  687. $this->error(__('User not found'));
  688. }
  689. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  690. if (!$ret) {
  691. $this->error(__('Captcha is incorrect'));
  692. }
  693. Sms::flush($mobile, 'resetpwd');
  694. }
  695. //模拟一次登录
  696. $this->auth->direct($user->id);
  697. $ret = $this->auth->changepwd($newpassword, '', true);
  698. if ($ret) {
  699. $this->success(__('Reset password successful'));
  700. } else {
  701. $this->error($this->auth->getError());
  702. }
  703. }
  704. /**
  705. * 修改密码
  706. *
  707. * @ApiMethod (POST)
  708. * @param string $newpassword 新密码
  709. * @param string $oldpassword 旧密码
  710. */
  711. public function changepwd(){
  712. $newpassword = input('newpassword');
  713. $oldpassword = input('oldpassword','');
  714. if (!$newpassword) {
  715. $this->error(__('Invalid parameters'));
  716. }
  717. if($this->auth->password && empty($oldpassword)){
  718. $this->error('原密码必填');
  719. }
  720. if(empty($this->auth->password)){
  721. $ret = $this->auth->changepwd($newpassword, '', true);
  722. }else{
  723. $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
  724. }
  725. if ($ret) {
  726. $this->success('设置密码成功');
  727. } else {
  728. $this->error($this->auth->getError());
  729. }
  730. }
  731. //假注销
  732. public function cancleUser(){
  733. $captcha = input('captcha','');
  734. if (!$captcha) {
  735. $this->error(__('Invalid parameters'));
  736. }
  737. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  738. $this->error(__('Captcha is incorrect'));
  739. }
  740. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  741. $this->auth->logout();
  742. $this->success('注销成功');
  743. }
  744. }