User.php 29 KB

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