限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >网站框架 >小程序

小程序 中不能使用https://api.weixin.qq.com

分类:小程序时间:2018-11-28浏览:4088
加入https://api.weixin.qq.com请求时,会报错 解决方案: 将获取用户openid,这部分放到后端获取,然后返回给小程序端
//app.js
import { config } from './config.js'

App({
  onLaunch: function () {
    var that = this;
    wx.login({
      success: res => {
        wx.request({
          url: config.api_base_url + 'Xcx/get_openid', 
          // url: that.globalData.wx_url_1 + res.code + that.globalData.wx_url_2,
          data:{
            code: res.code
          },
          success: res => {
            that.globalData.openid = res.data.msg;
          }
        })
      }
    });

  },
  globalData: {
    

  }
})
php
public function get_openid()
    {
        $code = input("code");
        $appid = 'wx355*****e6ab4';
        $appsecret = '07696b0662f*******286c1672922';
        $weixin =  file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
        $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
        $array = get_object_vars($jsondecode);//转换成数组
        return  jssuccess($array['openid']);//输出openid
        // dump($array);die;
    }
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.