当前位置:   article > 正文

PHP微信小程序授权登录_小程序手机号登录 php

小程序手机号登录 php
  1. /**
  2. * 微信授权登录
  3. * @throws \think\db\exception\DataNotFoundException
  4. * @throws \think\db\exception\ModelNotFoundException
  5. * @throws \think\exception\DbException
  6. */
  7. public function wxlogin()
  8. {
  9. //接收参数
  10. $code = $this->request->post('code'); //code码
  11. //验证参数
  12. if (empty($code)){
  13. $this->error('参数不能为空');
  14. }
  15. //自己到微信公众平台获取
  16. $appid = $this->appid;
  17. $secret = $this->secret;
  18. //获取openid
  19. $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
  20. $userInfo = json_decode(file_get_contents($url),true);
  21. if (empty($userInfo['openid']))
  22. $this->error('注册失败');
  23. //根据获取的openid 来查看数据库 有的话更新 session_key 没有的话添加
  24. $user = \app\common\model\User::where('openid',$userInfo['openid'])->find();
  25. if ($user){
  26. $ret = $this->auth->direct($user['id']);
  27. }else {
  28. //获取access_token
  29. $mobile = $this->request->post('mobile'); //获取手机号
  30. $data = [
  31. 'openid' => $userInfo['openid'],
  32. ];
  33. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, $data);
  34. }
  35. if ($ret) {
  36. $info = $this->auth->getUserinfo();
  37. $data = ['userinfo' => $info];
  38. $this->success(__('Logged in successful'), $data);
  39. } else {
  40. $this->error($this->auth->getError());
  41. }
  42. }

PHP微信小程序授权登录  fastadmin可直接使用 需要在user表里增加openid字段 手机号如果不需要的话可以隐藏掉,更改里面的配置即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/132019
推荐阅读
相关标签
  

闽ICP备14008679号