User.php 27 KB

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