1、小程序前端:
get_openid: function(){
var that = this
wx.login({
success(res) {
if (res.errMsg == 'login:ok') {
wx.request({
url: 'https://www.********.top/openid',
data: {
tar_table: 'get_openid',
appid: app.globalData.appid,
app_secret: app.globalData.AppSecret,
code: res.code,
},
header: {
'content-type': 'application/json',
},
success: function (res) {
app.globalData.openid = res.data.openid
console.log("在home.js中获取openid:",app.globalData.openid)
that.getShopInfo()
},
fail: console.error
})
}
}
})
},
其中,AppSecret去小程序管理控制台通过管理员授权生成。
二、python后台
def get_openid():
v_param = request.args
appid = v_param.get("appid")
app_secret = v_param.get("app_secret")
code = v_param.get("code")
code2Session="https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code"
code_url = code2Session.format(appid,app_secret,code)
response = requests.get(code_url) # 返回的是json数据
json_response = response.json() # 把json数据转换为字典
print("返回结果:",json_response)
if json_response.get('session_key'):
return json_response
else:
return False