thinkphp 微信小程序登陆

 //校验openid
    public function getOpenid(){
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $appid = config('wxpay.appid');
        $secret = config('wxpay.secret');
        $js_code = input('code','');
        $url = 'https://api.weixin.qq.com/sns/jscode2session';
        $data = array('appid' => $appid,'secret' => $secret,'js_code' => $js_code,'grant_type' => 'authorization_code',);
        $res = httpRequest($url, 'POST', $data);
        $arr = json_decode($res, true);
        if (isset($arr['errcode']) && !empty($arr['errcode'])) {
            $this->error($arr['errmsg']);
        }
        $this->success('获取成功',$arr);
    }

    public function wxLogin(){
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $openid = input('openid','');
        $avatar = input('avatar','');
        $nickname = input('nickname','');
        if(empty($openid) || empty($avatar) || empty($nickname)){
            $this->error('openid或用户信息为空','');
        }
        $user = db('user')->where('openid', $openid)->find();
        if ($user) {
            unset($user['openid']);
            unset($user['password']);
            $this->success('登录成功',$user);
        } else {
            $data_user = ['openid'=>$openid,'avatar'=>$avatar,'nickname'=>$nickname,'status'=>'normal','jointime'=>time()];
            $id = db('user')->insertGetId($data_user);
            if($id){
                $user = db('user')->where('openid', $openid)->find();
                unset($user['openid']);
                unset($user['password']);
                $this->success('登录成功',$user);
            }else{
                $this->error('登录失败','');
            }
        }
    }
 <button open-type="getUserInfo" bindgetuserinfo="register" class='login-btn'>授权登录</button>

/**
 * 注册
 */
register: function (e) {
  console.log(e.detail.userInfo)
  var that = app

  wx.login({
    success: res => {
      var that = this
      if (res.code) {
        var url = 'api/User/getOpenid'
        var params = { 
          code: res.code,
        }
        util.wxRequest(url,params,"POST",data => {
          wx.setStorageSync('openid',data.data.openid) 
         }, data => { }, data => { })
      } else {
        that.globalData.login = false
        console.log('获取用户code失败')
      }
    }
  }) 
   
  if (e.detail.userInfo !== undefined){
      var param = {
        "openid":wx.getStorageSync('openid'),
        "avatar":e.detail.userInfo.avatarUrl,
        "nickname":e.detail.userInfo.nickName,
      }

      util.wxRequest('api/User/wxLogin',param,"POST",data => {
       console.log(data)
       }, data => { }, data => { })
    }
  },
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1、App/Common/Conf/db.php 数据库连接参数修改; 2、App/Api/Conf/config.php 微信小程序的appid、secret、mchid、key、notify_url,SELF_ROOT的参数修改; 3、ThinkPHP\Library\Vendor\wxpay\lib\WxPay.Config.php 微信小程序的appid、appsecret、mchid、key参数修改; 4、ThinkPHP\Library\Vendor\WeiXinpay\lib\WxPay.Config.php 微信小程序的appid、appsecret、mchid、key、notify_url参数修改; 5、App/Api/Controller/WxPayController.class.php 50行修改链接 6、后台地址:https://xxxx.xxxx.com/index.php/Admin/Login/index.html 后台登录的用户名是admin,密码是123456 这里有几个必要的条件必须得满足: 1、网站支持https; 2、小程序是用企业信息申请的小程序; 3、微信公众号已经开通微信支付; 4、小程序已经绑定微信支付; 在满足以上几个条件之后,其中就有几个非常关键的参数,任何一个出错程序运行起来都是各种问题,这几个参数分别是APPID、MCHID、KEY、APPSECRET。 1、先针对ThinkPHP\Library\Vendor\wxpay\lib\WxPay.Config.php和ThinkPHP\Library\Vendor\WeiXinpay\lib\WxPay.Config.php这两个文件里的这四个参数进行说明,首先这两个库是微信提供的,这几个参数只有填写完全正确,才可以进行正常的微信支付。 AppID是小程序ID,微信小程序后台—>设置—>开发设置便可看到; MCHID是商户号,微信支付开通的时候,会有一封来自微信的邮件,里面就有商户号; APPSECRET是APPID对应的接口密码,用于获取接口调用凭证时使用,在微信小程序后台—>设置—>开发设置便可看到; KEY是商户支付密钥(API密钥)的信息,登录微信支付商户平台,账户中心—>API安全—>API密钥 查询即可; 2、App/Api/Conf/config.php文件的参数, define(‘SELF_ROOT’,’https://xxx.xxx.com/’),修改成你自己的后台域名即可, ‘notify_url’=>’https://xxxx.xxxx.com/index.php/Api/Wxpay/notify’, xxxx也是改成你自己的后台域名, 其他appid、secret、mchid、key四个参数按以上的参数设置即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值