User.php 27 KB

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