Commit 103970e5 by mamingqun

添加前端授权

parent 19ac3f47
VUE_APP_BUILD_TYPE = 'production'
\ No newline at end of file
...@@ -4,12 +4,14 @@ ...@@ -4,12 +4,14 @@
"private": true, "private": true,
"scripts": { "scripts": {
"serve": "vue-cli-service serve", "serve": "vue-cli-service serve",
"build-off": "vue-cli-service build", "build-production": "vue-cli-service build --mode prod",
"build": "vue-cli-service build --mode stage", "build": "vue-cli-service build --mode stage",
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },
"dependencies": { "dependencies": {
"axios": "^0.19.0",
"core-js": "^2.6.5", "core-js": "^2.6.5",
"jquery": "^3.4.1",
"vue": "^2.6.10", "vue": "^2.6.10",
"vue-router": "^3.0.3", "vue-router": "^3.0.3",
"vuex": "^3.0.1" "vuex": "^3.0.1"
......
...@@ -11,6 +11,19 @@ ...@@ -11,6 +11,19 @@
</div> </div>
</template> </template>
<script>
import config from '@/config/config.js'
export default {
mounted() {
console.log('config')
console.log(config)
console.log('accessToken');
console.log(localStorage.getItem('accessToken'))
}
}
</script>
<style lang="less"> <style lang="less">
#app { #app {
font-family: "Avenir", Helvetica, Arial, sans-serif; font-family: "Avenir", Helvetica, Arial, sans-serif;
......
var config
let configTest = {
BASEURL: 'https://wanda-message-tapi.bdideal.com' || process.env.BASE_URL,
shareIcon: 'https://wanda-h5.bdideal.com/blessing-test/static/img/v2/shareImg.png',
shareLink: 'https://wanda-h5.bdideal.com/blessing-test/index.html',
shareTitle: ['致敬70年光辉岁月!刚刚,我为她献上了最美的祝福', '今天,我把最美的话送给她', '我是第xxx位为新中国成立70周年送祝福的人'],
sysType: 1
}
let configProduction = {
BASEURL: 'https://wanda-message-api.bdideal.com' || process.env.BASE_URL,
shareIcon: 'https://wanda-h5.bdideal.com/blessing/static/img/v2/shareImg.png',
shareLink: 'https://wanda-h5.bdideal.com/blessing/index.html',
shareTitle: ['致敬70年光辉岁月!刚刚,我为她献上了最美的祝福', '今天,我把最美的话送给她', '我是第xxx位为新中国成立70周年送祝福的人'],
sysType: 1
}
config = configTest
if ((process.env.VUE_APP_BUILD_TYPE && process.env.VUE_APP_BUILD_TYPE === 'test')) {
config = configTest
}
if ((process.env.VUE_APP_BUILD_TYPE && process.env.VUE_APP_BUILD_TYPE === 'production')) {
config = configProduction
}
// export default
export default config
\ No newline at end of file
...@@ -2,6 +2,9 @@ import Vue from 'vue' ...@@ -2,6 +2,9 @@ import Vue from 'vue'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
import store from './store' import store from './store'
import './utils/request'
import wxAuth from './utils/wxAuth'
import config from './config/config';
Vue.config.productionTip = false Vue.config.productionTip = false
...@@ -13,6 +16,20 @@ import vconsole from 'vconsole'; ...@@ -13,6 +16,20 @@ import vconsole from 'vconsole';
if (process.env.VUE_APP_BUILD_TYPE && process.env.VUE_APP_BUILD_TYPE === 'test') { if (process.env.VUE_APP_BUILD_TYPE && process.env.VUE_APP_BUILD_TYPE === 'test') {
new vconsole() new vconsole()
} }
import jquery from 'jquery'
window.$ = jquery
import axios from 'axios'
window.$axios = axios
// 微信授权
if (process.env.NODE_ENV === 'development') {
// 调试开发,替换这里token
let testToken = 'OJBwh2ZvViYyQ1phNZEghwf9D8dyAImCD5twnjl0'
axios.defaults.headers.common['sysType'] = config.sysType
axios.defaults.headers.common['accessToken'] = testToken
localStorage.setItem('accessToken', testToken)
}else{
wxAuth.initWX()
}
......
...@@ -34,13 +34,13 @@ export default new Router({ ...@@ -34,13 +34,13 @@ export default new Router({
name: 'index', name: 'index',
component: Index component: Index
}, },
{ // {
path: '/about', // path: '/about',
name: 'about', // name: 'about',
// route level code-splitting // // route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route // // this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited. // // which is lazy-loaded when the route is visited.
component: () => import( /* webpackChunkName: "about" */ './views/About.vue') // component: () => import( /* webpackChunkName: "about" */ './views/About.vue')
} // }
] ]
}) })
\ No newline at end of file
// vue自定义指令
\ No newline at end of file
// vue自定义过滤器
\ No newline at end of file
import axios from 'axios'
import wxAuth from './wxAuth'
import config from '@/config/config.js'
var wxinfoApi = config.BASEURL + '/weixin/info'
// import { getToken } from '@/utils/auth'
// 创建axios实例
// const service = axios.create({
// // baseURL: process.env.BASE_API, // api的base_url
// timeout: 10000 // 请求超时时间
// })
// console.log(service)
// request拦截器
axios.interceptors.request.use(
config => {
// Do something before request is sent
// console.log(config.url, store.state.token, 'accessToken')
if (localStorage.getItem('accessToken')) {
config.headers['accessToken'] = localStorage.getItem('accessToken') // 让每个请求携带token--['ticket']为自定义key 请根据实际情况自行修改
}
// if (store.state.type) {
// config.headers['type'] = store.state.type // 让每个请求携带token--['ticket']为自定义key 请根据实际情况自行修改
// }
if (!config.headers['Content-Type']) {
if (config.url != '/upload') {
config.headers['Content-Type'] = 'application/json'
}
}
// config.headers['Content-Type'] = 'application/json'
// config.headers['platform'] = 'front'
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
)
// respone拦截器
axios.interceptors.response.use(
response => {
if (response.data.code === '0000' || response.data.code === '4010' || response.data.code == 0 || !response.data.code) {
// 1041 公司存在报错
// console.log(response.data)
return response
} else {
response.status = 400
// Toast({
// message: response.data.msg,
// position: 'center',
// duration: 2 * 1000
// })
if (response.data.code == 100100 || response.data.code == 100101) {
// store.dispatch('getAuth')
wxAuth.init(config.sysType, wxinfoApi)
}
return Promise.reject(new Error({
response
}))
}
},
/**
* 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页
* 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
*/
err => {
if (err && err.response) {
if (err.response.data.code == 100100 || err.response.data.code == 100101) {
wxAuth.init(config.sysType, wxinfoApi)
}
switch (err.response.status) {
case 400:
err.message = `${err.response.data.msg}`
break
case 401:
err.message = `${err.response.data.msg}`
break
case 403:
err.message = `${err.response.data.msg}`
break
case 404:
err.message = `请求地址出错: ${err.response.config.url}`
break
case 405:
err.message = `方法错误: ${err.response.data.msg}`
break
case 408:
err.message = '请求超时'
break
case 500:
err.message = `服务器出了点小问题,请稍后再试。`
break
case 501:
err.message = '服务未实现'
break
case 502:
err.message = '网关错误'
break
case 503:
err.message = '服务不可用'
break
case 504:
err.message = '网关超时'
break
case 505:
err.message = 'HTTP版本不受支持'
break
}
}
// Toast({
// message: err.message,
// position: 'center',
// duration: 3 * 1000
// })
// Toast.fail(err.message)
console.log('❎❎', err.message)
return Promise.reject(err)
}
)
\ No newline at end of file
import config from '@/config/config.js'
import $ from 'jquery'
// 初始化微信用户授权 (获取token)fffxiugai
// var sysType = 1
var wxinfoApi = config.BASEURL + '/weixin/info'
var getAccessTokenByCodeApi = config.BASEURL + '/weixin/callback?code='
var accessToken
function initWX() {
if (localStorage.getItem('accessToken')) { // localstorge has token
accessToken = localStorage.getItem('accessToken')
} else if (getQueryString('code')) { // url has code
accessToken = getAccessTokenByCode(config.sysType, getAccessTokenByCodeApi + getQueryString('code'), wxinfoApi) // 该方法依赖Jq的ajax的同步请求
localStorage.setItem('accessToken', accessToken)
} else {
init(config.sysType, wxinfoApi) // 该方法依赖Jq的ajax的同步请求
}
}
function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
var r = window.location.search.substr(1).match(reg)
if (r != null) {
return unescape(r[2])
}
return null
}
function init(sysType, getwxinfoApi) {
window.localStorage.removeItem('accessToken')
var wxinfo = getwxurl(config.sysType, getwxinfoApi)
location.href = wxinfo.data.url
return 1
}
function getwxurl(sysType, getwxinfoApi, state) {
var res = ''
$.ajax({
url: getwxinfoApi,
type: 'get',
async: false,
data: {
state: state
},
success: function (data) {
res = data
}
})
res.data.url = replaceState(res)
return res
}
// 如果有额外的分享参数,拼到参数中
function replaceState(res) {
if (location.href.includes('shareId')) {
var st = res.data.url.split('&')
var sr = st.map((item) => {
if (item.includes('redirect_uri')) {
// let id = location.search.;
var reg = /(\d+)/
var id = location.search.match(reg)[0]
return item = item + '?shareId=' + id
} else {
return item
}
})
var newUrl = sr.join('&')
return newUrl
} else {
return res.data.url
}
}
function getAccessTokenByCode(sysType, getAccessTokenByCodeApi, getwxinfoApi) {
var res = ''
$.ajax({
url: getAccessTokenByCodeApi,
type: 'get',
async: false,
success: function (data) {
res = data
},
error: function (data) {
init(config.sysType, getwxinfoApi)
}
})
return res.data.accessToken
}
export default {
init,
initWX
}
\ No newline at end of file
...@@ -1445,6 +1445,14 @@ aws4@^1.8.0: ...@@ -1445,6 +1445,14 @@ aws4@^1.8.0:
resolved "https://registry.npm.taobao.org/aws4/download/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f" resolved "https://registry.npm.taobao.org/aws4/download/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha1-8OAD2cqef1nHpQiUXXsu+aBKVC8= integrity sha1-8OAD2cqef1nHpQiUXXsu+aBKVC8=
axios@^0.19.0:
version "0.19.0"
resolved "https://registry.npm.taobao.org/axios/download/axios-0.19.0.tgz#8e09bff3d9122e133f7b8101c8fbdd00ed3d2ab8"
integrity sha1-jgm/89kSLhM/e4EByPvdAO09Krg=
dependencies:
follow-redirects "1.5.10"
is-buffer "^2.0.2"
babel-code-frame@^6.22.0, babel-code-frame@^6.26.0: babel-code-frame@^6.22.0, babel-code-frame@^6.26.0:
version "6.26.0" version "6.26.0"
resolved "https://registry.npm.taobao.org/babel-code-frame/download/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b" resolved "https://registry.npm.taobao.org/babel-code-frame/download/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
...@@ -2573,6 +2581,13 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3: ...@@ -2573,6 +2581,13 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3:
dependencies: dependencies:
ms "2.0.0" ms "2.0.0"
debug@=3.1.0:
version "3.1.0"
resolved "https://registry.npm.taobao.org/debug/download/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha1-W7WgZyYotkFJVmuhaBnmFRjGcmE=
dependencies:
ms "2.0.0"
debug@^3.0.0, debug@^3.1.0, debug@^3.2.5, debug@^3.2.6: debug@^3.0.0, debug@^3.1.0, debug@^3.2.5, debug@^3.2.6:
version "3.2.6" version "3.2.6"
resolved "https://registry.npm.taobao.org/debug/download/debug-3.2.6.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b" resolved "https://registry.npm.taobao.org/debug/download/debug-3.2.6.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
...@@ -3583,6 +3598,13 @@ flush-write-stream@^1.0.0: ...@@ -3583,6 +3598,13 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3" inherits "^2.0.3"
readable-stream "^2.3.6" readable-stream "^2.3.6"
follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha1-e3qfmuov3/NnhqlP9kPtB/T/Xio=
dependencies:
debug "=3.1.0"
follow-redirects@^1.0.0: follow-redirects@^1.0.0:
version "1.9.0" version "1.9.0"
resolved "https://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.9.0.tgz#8d5bcdc65b7108fe1508649c79c12d732dcedb4f" resolved "https://registry.npm.taobao.org/follow-redirects/download/follow-redirects-1.9.0.tgz#8d5bcdc65b7108fe1508649c79c12d732dcedb4f"
...@@ -4361,6 +4383,11 @@ is-buffer@^1.1.5: ...@@ -4361,6 +4383,11 @@ is-buffer@^1.1.5:
resolved "https://registry.npm.taobao.org/is-buffer/download/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" resolved "https://registry.npm.taobao.org/is-buffer/download/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha1-76ouqdqg16suoTqXsritUf776L4= integrity sha1-76ouqdqg16suoTqXsritUf776L4=
is-buffer@^2.0.2:
version "2.0.4"
resolved "https://registry.npm.taobao.org/is-buffer/download/is-buffer-2.0.4.tgz?cache=0&sync_timestamp=1569905349018&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fis-buffer%2Fdownload%2Fis-buffer-2.0.4.tgz#3e572f23c8411a5cfd9557c849e3665e0b290623"
integrity sha1-PlcvI8hBGlz9lVfISeNmXgspBiM=
is-callable@^1.1.4: is-callable@^1.1.4:
version "1.1.4" version "1.1.4"
resolved "https://registry.npm.taobao.org/is-callable/download/is-callable-1.1.4.tgz#1e1adf219e1eeb684d691f9d6a05ff0d30a24d75" resolved "https://registry.npm.taobao.org/is-callable/download/is-callable-1.1.4.tgz#1e1adf219e1eeb684d691f9d6a05ff0d30a24d75"
...@@ -4601,6 +4628,11 @@ javascript-stringify@^1.6.0: ...@@ -4601,6 +4628,11 @@ javascript-stringify@^1.6.0:
resolved "https://registry.npm.taobao.org/javascript-stringify/download/javascript-stringify-1.6.0.tgz#142d111f3a6e3dae8f4a9afd77d45855b5a9cce3" resolved "https://registry.npm.taobao.org/javascript-stringify/download/javascript-stringify-1.6.0.tgz#142d111f3a6e3dae8f4a9afd77d45855b5a9cce3"
integrity sha1-FC0RHzpuPa6PSpr9d9RYVbWpzOM= integrity sha1-FC0RHzpuPa6PSpr9d9RYVbWpzOM=
jquery@^3.4.1:
version "3.4.1"
resolved "https://registry.npm.taobao.org/jquery/download/jquery-3.4.1.tgz#714f1f8d9dde4bdfa55764ba37ef214630d80ef2"
integrity sha1-cU8fjZ3eS9+lV2S6N+8hRjDYDvI=
js-levenshtein@^1.1.3: js-levenshtein@^1.1.3:
version "1.1.6" version "1.1.6"
resolved "https://registry.npm.taobao.org/js-levenshtein/download/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d" resolved "https://registry.npm.taobao.org/js-levenshtein/download/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment