如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
加入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; }