This commit is contained in:
tcubic 2022-08-01 15:54:21 +08:00
parent 6fe4e3a4f8
commit 874d34f6df
4 changed files with 161 additions and 95 deletions

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,
}

View File

@ -1,46 +1,10 @@
import axios from 'axios' import Bus from "../bus.js";
import qs from 'qs' import {readyPost,readyGet} from "./base.js"
const domain_zs = process.env.VUE_APP_ZS_AUTHORIZE_DOMAIN; const domain_zs = process.env.VUE_APP_ZS_AUTHORIZE_DOMAIN ;
const domain_xgl = process.env.VUE_APP_XGL_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.公众号网页授权 // 1.公众号网页授权
// 请求方法get请求 // 请求方法get请求
@ -55,9 +19,9 @@ function post(url, params) {
function get_authorize() { function get_authorize() {
let href = window.location.href let href = window.location.href
let h_idx = window.location.href.indexOf('#') let h_idx = window.location.href.indexOf('#')
let out_href = href.slice(0, h_idx) let out_href = href.slice(0,h_idx)
window.location.href = `${domain_xgl}/wxauth/official/account/authorize?redirectUrl=${out_href}&scope=${1}"` window.location.href = `${domain_zs}/wxauth/official/account/authorize?redirectUrl=${out_href}&scope=${1}"`
} }
@ -66,55 +30,9 @@ function get_authorize() {
// 请求链接:/wxauth/official/account/jssdk/get // 请求链接:/wxauth/official/account/jssdk/get
// 请求参数: // 请求参数:
// url 请求链接 // url 请求链接
function get_jssdk(url, params) { function get_jssdk(params) {
// Bus.debug && (params.openid = Bus.testOpenId) // Bus.debug && (params.openid = Bus.testOpenId)
// return readyGet(domain_zs + "/wxauth/official/account/jssdk/get", params) 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);
}
})
} }

View File

@ -77,7 +77,7 @@ import gsap from "gsap";
import axios from 'axios' import axios from 'axios'
import { Toast, Dialog } from 'vant'; import { Toast, Dialog } from 'vant';
import { useStore } from "vuex"; import { useStore } from "vuex";
import { get_authorize, get_jssdk } from '@/api/authorize-api' // import { get_authorize, get_jssdk } from '@/api/authorize-api'
import service from "@/api/httpServe" import service from "@/api/httpServe"
import qs from 'qs' import qs from 'qs'
import h5plugin from '@/utils/plugin' import h5plugin from '@/utils/plugin'
@ -180,6 +180,8 @@ const btn = () => {
// //
const drawBtn = ()=>{ const drawBtn = ()=>{
console.log("抽奖:"); console.log("抽奖:");
console.log("授权:",getAuthorize());
} }
@ -216,6 +218,23 @@ const hideServicePop = () => {
} }
//
const getAuthorize = ()=> {
// let href = window.location.href
// let h_idx = window.location.href.indexOf('#')
// let out_href = href.slice(0,h_idx)
// window.location.href = `${process.env.VUE_APP_ZS_DOMAIN}/wxauth/official/account/authorize?redirectUrl=${out_href}&scope=${1}"`
let href = window.location.href;
let h_idx = window.location.href.indexOf("#");
let out_href = href.slice(0, h_idx);
return `${process.env.VUE_APP_ZS_DOMAIN}/wxauth/official/account/authorize?redirectUrl=${out_href}&scope=${1}"`;
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -13,7 +13,7 @@ import gsap from "gsap";
import axios from 'axios' import axios from 'axios'
import { Toast, Dialog } from 'vant'; import { Toast, Dialog } from 'vant';
import { useStore } from "vuex"; import { useStore } from "vuex";
import { get_authorize, get_jssdk } from '@/api/authorize-api' // import { get_authorize, get_jssdk } from '@/api/authorize-api'
import service from "@/api/httpServe" import service from "@/api/httpServe"
import qs from 'qs' import qs from 'qs'
import h5plugin from '@/utils/plugin' import h5plugin from '@/utils/plugin'
@ -51,10 +51,12 @@ onBeforeMount(() => {
// } // }
}); });
// //
onMounted(() => { }); onMounted(() => { });