前端
// 分享页
onShareAppMessage(res) {
return {
path: `/pages/index/index?uid=${uni.getStorageSync('id')}&msg=分享****`,
}
},
// APP页面
async onLaunch(options) {
console.log('App Launch', options)
wx.login({
success: res => {
console.log('res1111111111:', res)
if (res.code && options.query && options.query.uid) {
uni.getGroupEnterInfo({
success: async res2 =>{
console.log('res2:', res2)
let params = {
uid: options.query && options.query.uid,
msg: options.query && options.query.msg,
code: res.code,
iv: res2.iv,
encryptedData: res2.encryptedData
}
await this.$u.post('/api/user/shareAddIntegral', params)
}
});
}
},
fail: (err)=>{
console.log('拒绝授权', err)
}
})
}
后端
$config = [
'app_id' => Config::get('ask.wxappid'),
'secret' => Config::get('ask.wxappsecret'),
];
$app = Factory::miniProgram($config);
$res = $app->auth->session($code);
$session = $res['session_key'];
// 使用EasyWeChat解密数据
// 群id就在 $decryptedData['opengid']
$decryptedData = $app->encryptor->decryptData($session, $iv, $encryptedData);
$decryptedData['openid'] = $res['openid'];
return $decryptedData;