User.php 27 KB

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