const actions = () => {
const functionA = () => {/*Do sth*/}
const functionB = () => {/*Do sth*/}
const functionC = () => {/*Send log*/}
return new Map([
[/^guest_[1-4]$/,functionA],
[/^guest_5$/,functionB],
[/^guest_.*$/,functionC],
//...
])
}
const onButtonClick = (identity,status) => {
let action = [...actions()].filter(([key,value]) => (key.test(`${identity}_${status}`)))
action.forEach(([key,value]) => value.call(this))
}利用async function做请求一些异步数据时,利用try...catch捕获错误,一般情况下错误先行,我们可以根据api接口返回的code进行判断;
错误先行, 先判断code是否符合,符合的进行数据操作, 不符合的直接抛出异常, 然后在catch统一处理
async function Handle() {
try {
let result = await $http.getData(argument)
if (result.code !== 0) throw Error(result.errorMsg) /*Error First*/
/*Do sth*/
} catch(error) {
/*Unified processing error*/
let error = error.message || '默认错误信息'
/*Do sth*/
} finally {
/*Do sth*/
}
}错误处理链接:错误处理机制
了解async函数链接:async 函数
腾讯参考文档:valid-jsdoct
JSDoc参考文档:@use JSDoc
在线生成publicKey:生成publicKey地址
引入jsencrypt库
npm i jsencrypt OR
yarn add jsencrypt在vue的mainJS中混入,将生成的的秘钥放置在服务端上, 以下是演示放置出来
import JsEncrypt from 'jsencrypt' //加密
Vue.mixin({//混入加密
methods: {
RSAencrypt(params){
//实例化jsEncrypt对象
const jse = new JsEncrypt()
jse.setPublicKey(keys.publicKey) //设置公钥
return jse.encrypt(params)
},
RSAdecrypt(params){ //解密
const jse = new JsEncrypt()
jse.setPrivateKey(keys.privateKey) //设置私钥
return jse.decrypt(params)
}
}
})
全局混入使用
let secretParams = this.RSAencrypt(params)局部混入,命名为a.js
import JsEncrypt from 'jsencrypt' //加密
export default {
methods: {
RSAencrypt(params){
//实例化jsEncrypt对象
const jse = new JsEncrypt()
//设置公钥
jse.setPublicKey(keys.publicKey)
return jse.encrypt(params)
},
RSAdecrypt(params){ //解密
const jse = new JsEncrypt()
//设置私钥
jse.setPrivateKey(keys.privateKey)
return jse.decrypt(params)
}
}
}局部使用
<template>
</template>
<script>
import a from 'a.js'
export default {
mixins: [a]
method: {
async login() {
/**
* @params {Object} params 待加密的参数
* @returns {String} secretParams 加密后的参数
*/
let secretParams = this.RSAencrypt(params)
let rep = await $http.login(secretParams)
}
}
}
</script>
<style lang="scss" scoped>
</style>function fn (arr,len) {
arr = arr ? arr : Array.apply(null, Array(len))
let index = arr.indexOf(undefined)
if (index === -1) return arr
let ran = Math.floor(Math.random() * (32 - 2 + 1)) + 2
if (arr.includes(ran)) return fn(arr)
arr.splice(index, 1, ran)
return fn(arr)
}