1.在公众号里面配置域名
公众号->开发->开发管理->开发设置->业务域名
- 安装插件
{
"dependencies": {
"jweixin-module": "^1.6.0"
}
}
npm install jweixin-module --save
import wx from 'jweixin-module'
Vue.prototype.$wx= wx
<template>
<view class="content">
</view>
</template>
<script>
export default {
data() {
return {
}
},
onLoad() {
},
created() {
uni.showLoading({
title:'加载中...'
})
},
onShow() {
this.$wx.miniProgram.reLaunch({
url: `/mind/userInfo/userInfo?code=${this.geturlparam('code')}` //条转小程序页面
})
},
methods: {
geturlparam(code) {
let that = this
let url = window.location.href
let p = url.split('?')[1]
let params = new URLSearchParams(p);
return params.get(code)
}
}
}
</script>
<style>
</style>
<template>
<!-- 用于获取公众号openid -->
<view>
<web-view @message='message' @onPostMessage='onPostMessage' :src="`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxdda6f3a9ef6c&redirect_uri=https://sysaq.zmu.edu.cn/code&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`"></web-view>
</view>
</template>
<script>
export default {
data() {
return {
}
},
methods: {
// https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxdda1c06c&redirect_uri=https://sysaq.zmu.edu.cn/code&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect
},
onLoad(options) {
uni.showLoading({
title:'授权中...'
})
}
}
</script>
<style>
</style>
onLoad(options){
this.getGshOpenid(options.code);
}
// 获取公众号openid
getGshOpenid(code){
uni.$u.http.post('/api/v1.UserLogin/ouath',{
code:code
}).then(res => {
if(res.data.openid){
this.gzhOpenid = res.data.openid
}else{
uni.showToast({
title:'缺少必要参数,重新注册',
icon:'error'
})
setTimeout(() => {
uni.redirectTo({
url:'/pages/index/index'
})
},1500)
}
})
},
后端代码
/**
* 通过code 获取公众号openid
* @return null
*/
public function ouath()
{
$where = $this->request->getMore([
['code', ''],
]);
if (!$where['code']) return $this->error('缺少必要参数');
$appId = Filesystem::getDiskConfig('gzh', 'app_id');
$secret = Filesystem::getDiskConfig('gzh', 'secret');
$data = Ask::geturl('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appId . '&secret=' . $secret . '&code=' . $where['code'] . '&grant_type=authorization_code');
return $this->success('', $data);
}