完成XGL微信授权

This commit is contained in:
Andy Leong
2022-07-28 19:26:05 +08:00
parent 30727f4f65
commit 0be21f0195
12 changed files with 403 additions and 82 deletions

127
src/api/authorize-api.js Normal file
View File

@@ -0,0 +1,127 @@
import axios from 'axios'
import qs from 'qs'
const domain_zs = process.env.VUE_APP_ZS_AUTHORIZE_DOMAIN;
const domain_xgl = process.env.VUE_APP_XGL_AUTHORIZE_DOMAIN;
///////////////////////////
// get || post 请求方法封装
function get(url, params = {}) {
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err)
})
})
// 或者return axios.get();
}
/**
* post方法对应post请求
* @param {String} url [请求的url地址]
* @param {Object} params [请求时携带的参数]
*/
function post(url, params) {
return new Promise((resolve, reject) => {
axios
.post(url, qs.stringify(params))
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err)
})
})
// 或者return axios.post();
}
// 1.公众号网页授权
// 请求方法get请求
// 请求链接:/wxauth/official/account/authorize
// 请求参数:
// redirectUrl 请求链接
// scope 授权类型0用户授权1静默授权
// 返回值:无
// 备注回调页面URL后可能携带参数token、userid、forceauth。
// 先取token如有表示登录成功登录成功userid会有值
// 如token无值取forceauth为1时表示需要进行0-用户授权跳转。
function get_authorize() {
let href = window.location.href
let h_idx = window.location.href.indexOf('#')
let out_href = href.slice(0, h_idx)
window.location.href = `${domain_xgl}/wxauth/official/account/authorize?redirectUrl=${out_href}&scope=${1}"`
}
// 2.获取微信公众号jssdk
// 请求方法get请求
// 请求链接:/wxauth/official/account/jssdk/get
// 请求参数:
// url 请求链接
function get_jssdk(url, params) {
// Bus.debug && (params.openid = Bus.testOpenId)
// return readyGet(domain_zs + "/wxauth/official/account/jssdk/get", params)
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err)
})
})
}
function getOpenId(code) {
return new Promise((reslove, reject) => {
utils.rebuildAjax.post(process.env.VUE_APP_BASE_URL + 'cott-acts/api/wx/oauthInfo', {
code
}).then((res) => {
if (res.ret == 0) {
Vue.prototype.$userInfo = {
openid: res.data.openid,
nickname: res.data.nickname,
headimgurl: res.data.headimgurl
};
reslove();
} else {
location.replace(window.location.protocol + '//' + window.location.host + window.location.pathname)
}
})
})
}
// 获取wx 授权 code
function getCode(activityId, gameId, rankType) {
let redirect_uri = `${window.location.protocol}//${window.location.host + window.location.pathname}?activityId=${activityId}&gameId=${gameId}&rankType=${rankType}`;
utils.rebuildAjax.post(process.env.VUE_APP_BASE_URL + 'cott-acts/api/wx/getOAuthUrl', {
redirect_uri
}).then((res) => {
if (res.ret == 0) {
console.log(res.url)
location.replace(res.url);
}
})
}
export {
get_authorize,
get_jssdk,
}