Commit a6768fec by liwk

commit

1 parent 28471dd7
import axios from '@/plugins/axios'
import sign from './sign'
import util from '@/utils/util'
class Request {
post(params, returnAll = false) {
return this.request('POST', params, returnAll)
}
get(params, returnAll = false) {
return this.request('GET', params, returnAll)
}
request(method, params, returnAll) {
let noSignAttr = []
params.timestamp = new Date().format('yyyy-MM-dd hh:mm:ss')
let sessionId = util.getLocalStorage('dj_scrm_session_id')
if (sessionId) params.sessionId = sessionId
if (params.noSignAttr) {
noSignAttr = params.noSignAttr
delete params.noSignAttr
}
params = {
...window.$sign,
...params
}
let obj = {
method,
returnAll
}
if (method == 'POST') obj.data = sign(params, noSignAttr)
else obj.params = sign(params, noSignAttr)
return axios(obj)
}
}
export default new Request()
\ No newline at end of file
import sha1 from 'sha1'
import qs from 'qs'
const sign = (params, noSignAttr = []) => {
delete params.sign
var objStr = qs.stringify(params, { arrayFormat: 'indices', encode: false, allowDots: true })
var objVal = objStr.split('&')
var realObj = {}
objVal.forEach(item => {
if (item.split('=')[0] == 'scene') {
realObj.scene = params.scene
} else {
realObj[item.split('=')[0]] = item.split('=')[1]
}
})
const keys = Object.keys(realObj).sort()
var str = []
var obj = null
for (const key in realObj) {
obj = realObj[key]
if (obj === null) {
obj = ''
} else if (typeof obj === 'object') {
obj = JSON.stringify(obj)
}
realObj[key] = obj
}
keys.forEach(e => {
!noSignAttr.includes(e) && str.push(e + realObj[e])
})
const secret = window.$secret
realObj.sign = sha1(secret + str.join('') + secret).toUpperCase()
return realObj
}
export default sign
\ No newline at end of file
import api from './request'
const { http } = window.$HTTP
export default {
/**OAuth2授权登陆 */
login(data) {
return api.post({
method: 'scrm.api.sso.ww.oauth.login',
...data,
}, true)
data.returnAll = true
data.method = 'scrm.api.sso.ww.oauth.login'
return http({ data })
},
/** 选择登录账号 */
selectAccount(data) {
return api.post({
method: 'scrm.api.sso.user.login.select',
...data,
}, true)
data.returnAll = true
data.method = 'scrm.api.sso.user.login.select'
return http({ data })
}
}
\ No newline at end of file
......@@ -2,5 +2,5 @@
.icon-coupon { width: 80px; height: 80px; background-position: 0px 0px;}
.icon-discount { width: 80px; height: 80px; background-position: -90px 0px;}
.icon-exchange { width: 80px; height: 80px; background-position: 0px -90px;}
.icon-vip { width: 80px; height: 80px; background-position: -90px -90px;}
.icon-share { width: 20px; height: 20px; background-position: -180px 0px;}
.icon-vip { width: 80px; height: 80px; background-position: -90px -90px;}
\ No newline at end of file
......@@ -22,6 +22,7 @@ import { bus, tools, store as local } from './utils'
import './utils/util'
import 'components'
import { components } from '@/plugins/vant'
import { HTTP } from 'glsx-common'
components.forEach(component => Vue.use(component))
Vue.prototype.api = api
Vue.prototype.bus = bus
......@@ -43,16 +44,11 @@ Vue.config.productionTip = false
// const vConsole = new VConsole()
const isMock = true
fetchConfig(!isMock).then(({ request, sign, secret, cdn }) => {
fetchConfig(!isMock).then(res => {
// 从配置服务器获取动态配置
// 设置 js中可以访问 $cdn
Vue.prototype.$cdn = cdn
Vue.use(cfg)
if (isMock) {
window.$sign = sign
window.$secret = secret
}
axsinit(request)
window.$Http = new HTTP({...res })
window.vm = new Vue({
el: '#app',
router,
......
/*
* @Descripttion:
* @version: 1.0.0
* @Author: limin
* @Date: 2021-06-07 17:24:19
* @LastEditors: limin
* @LastEditTime: 2021-06-29 16:08:08
* @FilePath: \gh-hall\src\plugins\axios\index.js
*/
import axios from 'axios'
import store from '@/store'
import Qs from 'qs'
import { Notify, Dialog, Toast } from 'vant'
const service = axios.create({
// baseURL: baseApi, // url = base api url + request url
withCredentials: true, // send cookies when cross-domain requests
// timeout: 5000 // request timeout
transformRequest: [function(data) {
return Qs.stringify(data)
}]
})
// request拦截器 request interceptor
service.interceptors.request.use(
config => {
// 开启loading
if (config.loading) {
Toast.loading({
overlay: true,
duration: 0,
forbidClick: true
})
}
return config
},
error => {
Toast.clear()
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
// respone拦截器
service.interceptors.response.use(
response => {
Toast.clear()
console.log(response)
// const { config:{params,data} } = response
let { status, data, config: { returnAll } } = response
if (status != 200) {
Notify({ message })
} else {
if (data.code == 20 || data.code == 21) {
Dialog.alert({
title: '温馨提示',
message: '登录过期,请重新登录。',
confirmButtonColor: '#FF7336'
}).then(() => {
store.dispatch('login')
})
} else {
if (returnAll) {
return Promise.resolve(data)
} else {
if (data.code == '0') {
return Promise.resolve(data.data)
} else {
Notify({ message: data.message || '系统异常' })
}
}
}
}
},
error => {
Toast.clear()
Notify({ message: 'err:' + JSON.stringify(error) })
return Promise.reject(error)
}
)
export const axsinit = function(options = {}) {
Object.assign(service.defaults, options)
}
export default service
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!