User.php 27 KB

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