async mounted () {
await this.getOpenId()
await this.getAuthent()
},
methods: {
getCode () {
window.location.href = `http://localhost:8080/index.html?code=011p8u022Ek39W0eMDZ12zmN022p8u0o`
},
async getOpenId () {
if (!window.localStorage.openId) {
let jsCode = getUrlCode().code
if (!jsCode) {
this.getCode()
jsCode = getUrlCode().code
}
let openId = await this.postRequest('/machine/getOpenId', {
jsCode: `${jsCode}`
})
localStorage.openId = openId.data.data
}
},
// 鑒權
async getAuthent () {
const openId = window.localStorage.openId
try {
const authenticStatus = await this.postRequest('/machine/authentication', {
openId: `${openId}`
})
if (authenticStatus.data.code === 200) {
localStorage.loginType = 'true'
} else {
console.log(authenticStatus.data.code)
localStorage.loginType = 'false'
}
} catch (error) {
this.$router.push('login')
}
if (localStorage.loginType !== 'true') {
this.$router.push('login')
}
}
}
//getUrlCode.js目的:截取url中的code方法
const getUrlCode = () => {
// 截取url中的code方法
let url = location.search
let theRequest = {}
if (url.indexOf('?') !== -1) {
let str = url.substr(1)
let strs = str.split('&')
for (let i = 0; i < strs.length; i++) {
theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
}
}
return theRequest
}
export default getUrlCode
async await:以同步的方法去接收異步方法