Skip to content

Commit

Permalink
feat: 支持保存和使用云函数调试令牌
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 4, 2021
1 parent 2b33cc4 commit ac237ef
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
7 changes: 5 additions & 2 deletions packages/devops-admin/src/api/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import request from '@/utils/request'
*/
export function launchFunction(functionName, data, debug = false) {
return request({
url: `/app/func/invoke/${functionName}?debug=${debug}`,
url: `/app/func/invoke/${functionName}`,
method: 'post',
data: data
data: data,
headers: {
'debug-token': debug
}
})
}

Expand Down
6 changes: 5 additions & 1 deletion packages/devops-admin/src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { login, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getToken, setToken, removeToken, setDebugToken, removeDebugToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
Expand Down Expand Up @@ -41,6 +41,9 @@ const actions = {
const { data } = response
commit('SET_TOKEN', data.access_token)
setToken(data.access_token, data.expire)
if (data.debug_token) {
setDebugToken(data.debug_token)
}
resolve()
}).catch(error => {
reject(error)
Expand Down Expand Up @@ -88,6 +91,7 @@ const actions = {
commit('SET_PERMISSIONS', [])

removeToken()
removeDebugToken()
resetRouter()

// reset visited views and cached views
Expand Down
17 changes: 17 additions & 0 deletions packages/devops-admin/src/utils/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function getToken() {

if (!expire || expire <= Date.now() / 1000) {
removeToken()
removeDebugToken()
}

return token
Expand All @@ -22,3 +23,19 @@ export function removeToken() {
localStorage.removeItem(kExpire)
return localStorage.removeItem(kToken)
}

/**
* 获取调试云函数的 token
*/
export function getDebugToken() {
const token = localStorage.getItem('debug_token')
return token
}

export function setDebugToken(token) {
return localStorage.setItem('debug_token', token)
}

export function removeDebugToken() {
return localStorage.removeItem('debug_token')
}
3 changes: 2 additions & 1 deletion packages/devops-admin/src/views/database/policy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div class="main-row">
<div class="collection-list">
<div class="label">选择集合</div>
<el-radio-group v-model="collection_name">
<el-radio-group v-model="collection_name" style="width: 100%;">
<el-radio v-for="item in collections" :key="item" class="collection-radio" border size="medium" :label="item">
{{ item }}
</el-radio>
Expand Down Expand Up @@ -316,6 +316,7 @@ export default {
.collection-list {
width: 200px;
border-radius: 5px;
box-sizing: border-box;
.label {
font-size: 14px;
Expand Down
10 changes: 6 additions & 4 deletions packages/devops-admin/src/views/development/function.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@
import FunctionLogDetail from './components/FunctionLogDetail'
import FunctionEditor from '@/components/FunctionEditor'
import jsonEditor from '@/components/JsonEditor/param'
import { db, dbm_cloud } from '@/api/cloud'
import { launchFunction } from '@/api/func'
import { publishFunctions } from '@/api/publish'
import { db, dbm_cloud } from '../../api/cloud'
import { launchFunction } from '../../api/func'
import { publishFunctions } from '../../api/publish'
import { getDebugToken } from '../../utils/auth'
const defaultParamValue = {
code: 'laf'
Expand Down Expand Up @@ -249,6 +250,7 @@ export default {
* 运行函数代码
*/
async launch() {
const debug_token = getDebugToken()
await this.updateFunc(false)
if (this.loading) {
return
Expand All @@ -258,7 +260,7 @@ export default {
const param = this.parseInvokeParam(this.invokeParams)
const res = await launchFunction(this.func.name, param, true)
const res = await launchFunction(this.func.name, param, debug_token)
.catch(err => {
console.error(err)
this.$message.warning('运行失败: ' + err)
Expand Down
8 changes: 4 additions & 4 deletions packages/devops-admin/src/views/development/functions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,16 @@ export default {
async handleDelete(row, index) {
await this.$confirm('确认要删除此数据?', '删除确认')
if(row.status) {
if (row.status) {
return this.$message.error('请先停用函数,再删除!')
}
const $ = db.command
// 执行删除请求
const r = await db.collection('__functions')
.where({
_id: row._id,
status: 0,
.where({
_id: row._id,
status: 0,
reserved: $.neq(true)
})
.remove()
Expand Down

0 comments on commit ac237ef

Please sign in to comment.