User.php 26 KB

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