User.php 26 KB

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