1公众号登录:
get_code() {
let that = this
const code = this.getUrlParam('code') // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
const local = window.location.href
let redirect_uri = encodeURIComponent(local) //回调的地址要编码
if (code == null || code === '') {
var appid = that.appid;
window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid +
'&redirect_uri=' + redirect_uri +
'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
} else {
that.code = code
that.httppApi('/owner/api.owner/get_openid', {
code: that.code
}, 1, function(res) {
if (res.code == 1) {
that.openid = res.data.openid
uni.setStorageSync('openId', res.data.openid);
}
}, 'get')
}
},
getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
var r = window.location.search.substr(1).match(reg)
if (r != null) return unescape(r[2])
return null
},
2小程序登录
<view class="top_dis_flex padding_top_20" v-if="user_info == ''" @tap="getUserProfile">请登录</view>
getUserProfile() {
var that = this
var api = that.httpUrl + "/service/member/login"
const muid = uni.getStorageSync('muid');
uni.getUserProfile({
desc: '登录',
success: (info) => {
console.log(info.userInfo)
//这里取到的是用户的信息,自己安排自己的业务
uni.login({
provider: 'weixin',
success: (res) => {
uni.request({
url: api,
data: {
avatarUrl: info.userInfo.avatarUrl,
nickName: info.userInfo.nickName,
code: res.code,
muid: muid
},
success: function(res3) {
uni.setStorageSync('storage_user_info', res3.data.data);
that.msg("登录成功")
}
})
}
})
},
fail: (err) => {}
})
},