refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,42 @@
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
|
||||
const PAGE_PATH = '/pages/API/unicloud/unicloud/cloud-function'
|
||||
|
||||
describe('unicloud-call-function', () => {
|
||||
if (isAndroid) {
|
||||
it('android 部分模拟器会造成自动化测试 socket 断开', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor(500)
|
||||
await page.setData({ data: { isUniTest: true } })
|
||||
})
|
||||
|
||||
it('UniCloudError', async () => {
|
||||
const result = await page.callMethod('jest_UniCloudError')
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
|
||||
it('callFunction', async () => {
|
||||
await page.callMethod('callFunction')
|
||||
const callFunctionResult = await page.data('data.callFunctionResult')
|
||||
const callFunctionError = await page.data('data.callFunctionError')
|
||||
const callFunctionResult_Detail_functionName = await page.data('data.callFunctionResult_Detail_functionName')
|
||||
expect(callFunctionResult['showMessage']).toBe("Hello uniCloud function")
|
||||
expect(callFunctionResult['event']['num']).toBe(1)
|
||||
expect(callFunctionResult['event']['str']).toBe('ABC')
|
||||
expect(callFunctionResult_Detail_functionName).toBe('echo-cf')
|
||||
})
|
||||
|
||||
it('callFunctionWithGeneric', async () => {
|
||||
await page.callMethod('callFunctionWithGeneric')
|
||||
const genericDemoShowMessage = await page.data('data.genericDemoShowMessage')
|
||||
expect(genericDemoShowMessage).toBe("Hello uniCloud function")
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,197 @@
|
||||
<template>
|
||||
<!-- #ifdef APP -->
|
||||
<scroll-view class="page-scroll-view">
|
||||
<!-- #endif -->
|
||||
<view>
|
||||
<page-head :title="title"></page-head>
|
||||
<view class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @click="callFunction">请求云函数</button>
|
||||
<button type="primary" @click="callFunctionWithGeneric">请求云函数传入泛型</button>
|
||||
<!-- #ifdef APP-ANDROID || APP-IOS -->
|
||||
<button type="primary" @click="callEncryptionFunction">请求安全网络加密云函数</button>
|
||||
<button type="primary" @click="callVerifyFunction">请求安全网络客户端校验云函数</button>
|
||||
<view><text class="tips">安全网络相关功能需要打包自定义基座方可正常使用</text></view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #ifdef APP -->
|
||||
</scroll-view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
|
||||
type DataType = {
|
||||
callFunctionResult: UTSJSONObject,
|
||||
callFunctionResult_Detail_functionName: string,
|
||||
callFunctionError: UTSJSONObject,
|
||||
genericDemoShowMessage: string,
|
||||
isUniTest: boolean,
|
||||
}
|
||||
|
||||
const title = ref('请求云函数')
|
||||
const data = reactive({
|
||||
callFunctionResult: {} as UTSJSONObject,
|
||||
callFunctionResult_Detail_functionName: '',
|
||||
callFunctionError: {} as UTSJSONObject,
|
||||
genericDemoShowMessage: '',
|
||||
isUniTest: false,
|
||||
} as DataType)
|
||||
|
||||
function notify(content : string, title : string) {
|
||||
if (!data.isUniTest) {
|
||||
uni.showModal({
|
||||
title,
|
||||
content,
|
||||
showCancel: false
|
||||
})
|
||||
} else {
|
||||
console.log(title, content)
|
||||
}
|
||||
}
|
||||
|
||||
async function callFunctionWithGeneric(): Promise<void> {
|
||||
type EchoCfResult = {
|
||||
showMessage : string
|
||||
}
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
await uniCloud.callFunction<EchoCfResult>({
|
||||
name: 'echo-cf',
|
||||
data: {
|
||||
num: 1,
|
||||
str: 'ABC'
|
||||
}
|
||||
}).then(res => {
|
||||
const result = res.result
|
||||
uni.hideLoading()
|
||||
data.genericDemoShowMessage = result.showMessage
|
||||
notify(result.showMessage, '提示')
|
||||
}).catch((err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
data.callFunctionError = {
|
||||
errCode: error.errCode,
|
||||
errMsg: error.errMsg
|
||||
} as UTSJSONObject
|
||||
uni.hideLoading()
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function callFunction(): Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
await uniCloud.callFunction({
|
||||
name: 'echo-cf',
|
||||
data: {
|
||||
num: 1,
|
||||
str: 'ABC'
|
||||
}
|
||||
}).then(res => {
|
||||
const result = res.result
|
||||
data.callFunctionResult = result
|
||||
const detail = result.get('detail') as UTSJSONObject
|
||||
data.callFunctionResult_Detail_functionName = detail.get('functionName') as string
|
||||
console.log('callFunctionResult_Detail_functionName: ' + data.callFunctionResult_Detail_functionName)
|
||||
uni.hideLoading()
|
||||
notify(result['showMessage'] as string, '提示')
|
||||
}).catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
if(err instanceof UniCloudError) {
|
||||
const error = err as UniCloudError
|
||||
data.callFunctionError = {
|
||||
errCode: error.errCode,
|
||||
errMsg: error.errMsg
|
||||
} as UTSJSONObject
|
||||
notify(error.errMsg, '错误')
|
||||
} else {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function callEncryptionFunction() {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
uniCloud.callFunction({
|
||||
name: 'encryption',
|
||||
data: {},
|
||||
secretType: 'both'
|
||||
}).then(res => {
|
||||
uni.hideLoading()
|
||||
notify(JSON.stringify(res.result), '提示')
|
||||
}).catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
if(err instanceof UniCloudError) {
|
||||
const error = err as UniCloudError
|
||||
data.callFunctionError = {
|
||||
errCode: error.errCode,
|
||||
errMsg: error.errMsg
|
||||
} as UTSJSONObject
|
||||
notify(error.errMsg, '错误')
|
||||
} else {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function callVerifyFunction() {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
uniCloud.callFunction({
|
||||
name: 'verify-client',
|
||||
data: {},
|
||||
secretType: 'both'
|
||||
}).then(res => {
|
||||
uni.hideLoading()
|
||||
notify(JSON.stringify(res.result), '提示')
|
||||
}).catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
if(err instanceof UniCloudError) {
|
||||
const error = err as UniCloudError
|
||||
data.callFunctionError = {
|
||||
errCode: error.errCode,
|
||||
errMsg: error.errMsg
|
||||
} as UTSJSONObject
|
||||
notify(error.errMsg, '错误')
|
||||
} else {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function jest_UniCloudError() {
|
||||
return new Error() instanceof UniCloudError
|
||||
}
|
||||
|
||||
onLoad((options : OnLoadOptions) => {
|
||||
})
|
||||
|
||||
onUnload(() => {
|
||||
if (data.isUniTest) {
|
||||
uni.hideToast()
|
||||
}
|
||||
})
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
callFunction,
|
||||
callFunctionWithGeneric,
|
||||
jest_UniCloudError
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.tips {
|
||||
color: #999999;
|
||||
font-size: 12px;
|
||||
padding: 10px 0px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,40 @@
|
||||
const PAGE_PATH = '/pages/API/unicloud/unicloud/cloud-object'
|
||||
|
||||
describe('unicloud-import-object', () => {
|
||||
let page
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor(500)
|
||||
await page.setData({ data: { isUniTest: true } })
|
||||
})
|
||||
it('importObject', async () => {
|
||||
await page.callMethod('addTodo')
|
||||
await page.callMethod('addTodoWithGeneric')
|
||||
await page.callMethod('fail')
|
||||
await page.callMethod('failWithNumberErrCode')
|
||||
await page.callMethod('success')
|
||||
|
||||
const todoTitle = await page.data('data.todoTitle')
|
||||
const todoContent = await page.data('data.todoContent')
|
||||
const returnTodoTitle = await page.data('data.returnTodoTitle')
|
||||
const returnTodoContent = await page.data('data.returnTodoContent')
|
||||
const genericDemoReturnTodoTitle = await page.data('data.genericDemoReturnTodoTitle')
|
||||
const genericDemoReturnTodoContent = await page.data('data.genericDemoReturnTodoContent')
|
||||
const failErrCode = await page.data('data.failErrCode')
|
||||
const failErrSubject = await page.data('data.failErrSubject')
|
||||
const failErrDetailTips = await page.data('data.failErrDetailTips')
|
||||
const failNumberErrCode = await page.data('data.failNumberErrCode')
|
||||
const successErrCode = await page.data('data.successErrCode')
|
||||
|
||||
expect(returnTodoTitle).toBe(todoTitle)
|
||||
expect(returnTodoContent).toBe(todoContent)
|
||||
expect(genericDemoReturnTodoTitle).toBe(todoTitle)
|
||||
expect(genericDemoReturnTodoContent).toBe(todoContent)
|
||||
expect(failErrCode).toBe('TEST_ERROR_CODE')
|
||||
expect(failErrSubject).toBe('fail')
|
||||
expect(failErrDetailTips).toBe('DO_NOT_TRY_AGAIN')
|
||||
expect(failNumberErrCode).toBe(-1)
|
||||
expect(successErrCode).toBe(0)
|
||||
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,194 @@
|
||||
<template>
|
||||
<!-- #ifdef APP -->
|
||||
<scroll-view class="page-scroll-view">
|
||||
<!-- #endif -->
|
||||
<view>
|
||||
<page-head :title="title"></page-head>
|
||||
<view class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @tap="addTodo">添加Todo</button>
|
||||
</view>
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @tap="addTodoWithGeneric">
|
||||
添加Todo传入泛型
|
||||
</button>
|
||||
</view>
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @tap="randomFail">随机触发失败重试</button>
|
||||
</view>
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @tap="fail">云对象失败调用</button>
|
||||
</view>
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @tap="failWithNumberErrCode">
|
||||
云对象数字错误码
|
||||
</button>
|
||||
</view>
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @tap="success">云对象成功调用</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #ifdef APP -->
|
||||
</scroll-view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
|
||||
type DataType = {
|
||||
todoTitle: string,
|
||||
todoContent: string,
|
||||
returnTodoTitle: string,
|
||||
returnTodoContent: string,
|
||||
genericDemoReturnTodoTitle: string,
|
||||
genericDemoReturnTodoContent: string,
|
||||
failErrCode: string,
|
||||
failErrSubject: string,
|
||||
failErrDetailTips: string,
|
||||
failNumberErrCode: number,
|
||||
successErrCode: number,
|
||||
isUniTest: boolean,
|
||||
}
|
||||
|
||||
// Data
|
||||
const title = ref('请求云对象')
|
||||
const data = reactive({
|
||||
todoTitle: '学习编程',
|
||||
todoContent: '熟悉uts语法',
|
||||
returnTodoTitle: '',
|
||||
returnTodoContent: '',
|
||||
genericDemoReturnTodoTitle: '',
|
||||
genericDemoReturnTodoContent: '',
|
||||
failErrCode: '',
|
||||
failErrSubject: '',
|
||||
failErrDetailTips: '',
|
||||
failNumberErrCode: 0,
|
||||
successErrCode: -1,
|
||||
isUniTest: false,
|
||||
} as DataType)
|
||||
|
||||
// Functions
|
||||
function notify(content : string, title : string) {
|
||||
if (!data.isUniTest) {
|
||||
uni.showModal({
|
||||
title,
|
||||
content,
|
||||
showCancel: false
|
||||
})
|
||||
} else {
|
||||
console.log(title, content)
|
||||
}
|
||||
}
|
||||
|
||||
async function addTodo() : Promise<void> {
|
||||
const todo = uniCloud.importObject('todo', {
|
||||
customUI: data.isUniTest
|
||||
})
|
||||
const title = data.todoTitle
|
||||
const content = data.todoContent
|
||||
await todo.add<UTSJSONObject>(title, content).then((res : UTSJSONObject) => {
|
||||
data.returnTodoTitle = res['title'] as string
|
||||
data.returnTodoContent = res['content'] as string
|
||||
notify(res['showMessage'] as string, '提示')
|
||||
}).catch((err : any | null) => {
|
||||
console.log(err)
|
||||
const error = err as UniCloudError
|
||||
console.error(error)
|
||||
})
|
||||
}
|
||||
|
||||
async function addTodoWithGeneric() : Promise<void> {
|
||||
type AddTodoResult = {
|
||||
title : string,
|
||||
content : string,
|
||||
showMessage : string
|
||||
}
|
||||
const todo = uniCloud.importObject('todo', {
|
||||
customUI: data.isUniTest
|
||||
})
|
||||
const title = data.todoTitle
|
||||
const content = data.todoContent
|
||||
await todo.add<AddTodoResult>(title, content).then((res : AddTodoResult) => {
|
||||
data.genericDemoReturnTodoTitle = res.title
|
||||
data.genericDemoReturnTodoContent = res.content
|
||||
notify(res.showMessage, '提示')
|
||||
}).catch((err : any | null) => {
|
||||
console.log(err)
|
||||
const error = err as UniCloudError
|
||||
console.error(error)
|
||||
})
|
||||
}
|
||||
|
||||
async function randomFail() : Promise<void> {
|
||||
const todoObj = uniCloud.importObject('todo', {
|
||||
errorOptions: {
|
||||
retry: true
|
||||
}
|
||||
})
|
||||
await todoObj.randomFail().then((res : UTSJSONObject) => {
|
||||
notify(res['showMessage'] as string, '提示')
|
||||
}).catch((err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
console.log(error)
|
||||
})
|
||||
}
|
||||
|
||||
async function fail() : Promise<void> {
|
||||
const todo = uniCloud.importObject('todo', {
|
||||
customUI: data.isUniTest
|
||||
})
|
||||
await todo.fail().then((res : UTSJSONObject) => {
|
||||
notify('todo.fail应调用失败,此处错误的触发了成功回调', '错误')
|
||||
console.log('todo.fail: ', res);
|
||||
}).catch((err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
data.failErrCode = error.errCode as string
|
||||
data.failErrSubject = error.errSubject as string
|
||||
const detail = error.detail
|
||||
if (detail != null && detail['tips'] != null) {
|
||||
data.failErrDetailTips = detail['tips'] as string
|
||||
}
|
||||
console.log(error)
|
||||
})
|
||||
}
|
||||
|
||||
async function failWithNumberErrCode() : Promise<void> {
|
||||
const todo = uniCloud.importObject('todo', {
|
||||
customUI: data.isUniTest
|
||||
})
|
||||
await todo.failWithNumberErrCode().then((res : UTSJSONObject) => {
|
||||
notify('todo.fail应调用失败,此处错误的触发了成功回调', '错误')
|
||||
console.log('todo.fail: ', res);
|
||||
}).catch((err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
data.failNumberErrCode = error.errCode as number
|
||||
console.log(error)
|
||||
})
|
||||
}
|
||||
|
||||
async function success() : Promise<void> {
|
||||
const todo = uniCloud.importObject('todo', {
|
||||
customUI: data.isUniTest
|
||||
})
|
||||
await todo.success().then((res : UTSJSONObject) => {
|
||||
data.successErrCode = res['errCode'] as number
|
||||
notify(res['showMessage'] as string, '提示')
|
||||
}).catch((err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
console.error(error)
|
||||
})
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
addTodo,
|
||||
addTodoWithGeneric,
|
||||
fail,
|
||||
failWithNumberErrCode,
|
||||
success
|
||||
})
|
||||
</script>
|
||||
|
||||
<style></style>
|
||||
@@ -0,0 +1,85 @@
|
||||
<template>
|
||||
<!-- #ifdef APP -->
|
||||
<scroll-view class="page-scroll-view">
|
||||
<!-- #endif -->
|
||||
<view>
|
||||
<page-head :title="title"></page-head>
|
||||
<view class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @click="uploadFile">选择文件上传</button>
|
||||
<button type="primary" @click="chooseAndUploadFile">一个接口选择文件并上传</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #ifdef APP -->
|
||||
</scroll-view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
|
||||
// Data
|
||||
const title = ref('云存储')
|
||||
|
||||
// Functions
|
||||
function uploadFile() {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success(res) : void {
|
||||
uni.showLoading({
|
||||
title: '上传中...'
|
||||
})
|
||||
const tempFilePath = res.tempFilePaths[0]
|
||||
uniCloud.uploadFile({
|
||||
filePath: tempFilePath,
|
||||
cloudPath: 'test.jpg'
|
||||
})
|
||||
.then(function (res) {
|
||||
uni.hideLoading()
|
||||
uni.showModal({
|
||||
content: '上传成功',
|
||||
showCancel: false
|
||||
});
|
||||
})
|
||||
.catch(function (err : any | null) {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
uni.showModal({
|
||||
content: '上传失败,' + error.errMsg,
|
||||
showCancel: false
|
||||
});
|
||||
})
|
||||
// .finally((_: number) : void => {
|
||||
// uni.hideLoading()
|
||||
// })
|
||||
},
|
||||
fail(err) : void {
|
||||
console.error('chooseImage fail: ', err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function chooseAndUploadFile() {
|
||||
uniCloud.chooseAndUploadFile({
|
||||
type: 'image'
|
||||
}).then(function (res) {
|
||||
uni.hideLoading()
|
||||
uni.showModal({
|
||||
content: '上传成功',
|
||||
showCancel: false
|
||||
});
|
||||
})
|
||||
.catch(function (err : any | null) {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
uni.showModal({
|
||||
content: '上传失败,' + error.errMsg,
|
||||
showCancel: false
|
||||
});
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
@@ -0,0 +1,65 @@
|
||||
const PAGE_PATH = '/pages/API/unicloud/unicloud/database'
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isSafari = platformInfo.indexOf('safari') > -1
|
||||
const isAndroid = platformInfo.startsWith('android')
|
||||
|
||||
describe('unicloud-database', () => {
|
||||
if (isSafari) {
|
||||
it('web safari 暂时规避', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
if (isAndroid) {
|
||||
it('android 部分模拟器会造成自动化测试 socket 断开', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor(500)
|
||||
await page.setData({ data: { isUniTest: true } })
|
||||
})
|
||||
it('databaseBasic', async () => {
|
||||
await page.callMethod('dbRemove')
|
||||
await page.callMethod('dbAdd')
|
||||
await page.callMethod('dbBatchAdd')
|
||||
await page.callMethod('dbGet')
|
||||
await page.callMethod('dbGetWithCommand')
|
||||
await page.callMethod('dbUpdate')
|
||||
await page.callMethod('dbRemove')
|
||||
await page.callMethod('dbMultiSend')
|
||||
|
||||
const addId = await page.data('data.addId')
|
||||
const batchAddIds = await page.data('data.batchAddIds')
|
||||
const batchAddinserted = await page.data('data.batchAddinserted')
|
||||
const updateUpdated = await page.data('data.updateUpdated')
|
||||
const getData = await page.data('data.getData')
|
||||
const getWithCommandData = await page.data('data.getWithCommandData')
|
||||
const removeDeleted = await page.data('data.removeDeleted')
|
||||
const multiSendSuccessCount = await page.data('data.multiSendSuccessCount')
|
||||
|
||||
expect(addId !== '').toBe(true)
|
||||
expect(batchAddIds.length).toBe(2)
|
||||
expect(batchAddinserted).toBe(2)
|
||||
expect(getData.length).toBe(2)
|
||||
expect(getWithCommandData.length).toBe(1)
|
||||
expect(updateUpdated).toBe(3)
|
||||
expect(removeDeleted).toBe(3)
|
||||
expect(multiSendSuccessCount).toBe(2)
|
||||
|
||||
})
|
||||
|
||||
it('databaseLookup', async () => {
|
||||
await page.callMethod('dbLookupInit')
|
||||
await page.callMethod('dbLookup')
|
||||
|
||||
const lookupData = await page.data('data.lookupData')
|
||||
expect(lookupData.length).toBe(2)
|
||||
expect(lookupData[0]['foreign_id'].length).toBe(1)
|
||||
expect(lookupData[1]['foreign_id'].length).toBe(1)
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,348 @@
|
||||
<template>
|
||||
<!-- #ifdef APP -->
|
||||
<scroll-view class="page-scroll-view">
|
||||
<!-- #endif -->
|
||||
<view>
|
||||
<page-head :title="title"></page-head>
|
||||
<view class="uni-padding-wrap uni-common-mt">
|
||||
<view class="uni-btn-v uni-common-mt">
|
||||
<button type="primary" @click="dbAdd">新增单条数据</button>
|
||||
<button type="primary" @click="dbBatchAdd">新增多条数据</button>
|
||||
<button type="primary" @click="dbUpdate">更新数据</button>
|
||||
<button type="primary" @click="dbGet">where传字符串获取数据</button>
|
||||
<button type="primary" @click="dbGetWithCommand">where传对象获取数据</button>
|
||||
<button type="primary" @click="dbRemove">删除数据</button>
|
||||
<button type="primary" @click="dbLookupInit">初始化联表查询数据</button>
|
||||
<button type="primary" @click="dbLookup">联表查询</button>
|
||||
<button type="primary" @click="dbMultiSend">合并查询查询</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #ifdef APP -->
|
||||
</scroll-view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
|
||||
type DataType = {
|
||||
addId: string,
|
||||
batchAddIds: Array<string>,
|
||||
batchAddinserted: number,
|
||||
updateUpdated: number,
|
||||
getData: Array<UTSJSONObject>,
|
||||
getWithCommandData: Array<UTSJSONObject>,
|
||||
removeDeleted: number,
|
||||
lookupData: Array<UTSJSONObject>,
|
||||
multiSendSuccessCount: number,
|
||||
isUniTest: boolean,
|
||||
}
|
||||
|
||||
// Data
|
||||
const title = ref('ClientDB')
|
||||
const data = reactive({
|
||||
addId: '',
|
||||
batchAddIds: [] as Array<string>,
|
||||
batchAddinserted: 0,
|
||||
updateUpdated: 0,
|
||||
getData: [] as Array<UTSJSONObject>,
|
||||
getWithCommandData: [] as Array<UTSJSONObject>,
|
||||
removeDeleted: 0,
|
||||
lookupData: [] as Array<UTSJSONObject>,
|
||||
multiSendSuccessCount: 0,
|
||||
isUniTest: false,
|
||||
} as DataType)
|
||||
|
||||
// Computed
|
||||
const dataTag = computed<string>(() => {
|
||||
return data.isUniTest ? 'default-tag' + Date.now() : 'default-tag'
|
||||
})
|
||||
|
||||
// Functions
|
||||
function notify(content : string, title : string) {
|
||||
if (!data.isUniTest) {
|
||||
uni.showModal({
|
||||
title,
|
||||
content,
|
||||
showCancel: false
|
||||
})
|
||||
} else {
|
||||
console.log(title, content)
|
||||
}
|
||||
}
|
||||
|
||||
async function dbAdd() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('type')
|
||||
.add({
|
||||
num: 1,
|
||||
tag: dataTag.value,
|
||||
date: new Date()
|
||||
})
|
||||
.then(res => {
|
||||
uni.hideLoading()
|
||||
data.addId = res.id
|
||||
notify(`新增成功,id: ${res.id}`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbBatchAdd() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('type')
|
||||
.add([{
|
||||
num: 2,
|
||||
tag: dataTag.value,
|
||||
}, {
|
||||
num: 3,
|
||||
tag: dataTag.value,
|
||||
}])
|
||||
.then((res) => {
|
||||
uni.hideLoading()
|
||||
data.batchAddIds = res.ids
|
||||
data.batchAddinserted = res.inserted
|
||||
notify(`新增成功条数${res.inserted}, id列表: ${res.ids.join(',')}`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbGet() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('type')
|
||||
.where(
|
||||
`tag == "${dataTag.value}"`
|
||||
)
|
||||
.field('num, tag')
|
||||
.orderBy('num desc')
|
||||
.skip(1)
|
||||
.limit(2)
|
||||
.get()
|
||||
.then(res => {
|
||||
uni.hideLoading()
|
||||
data.getData = res.data
|
||||
notify(`获取成功,取到了${res.data.length}条数据`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbGetWithCommand() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('type')
|
||||
.where({
|
||||
num: db.command.gt(1),
|
||||
tag: dataTag.value
|
||||
})
|
||||
.field('num, tag')
|
||||
.orderBy('num desc')
|
||||
.skip(1)
|
||||
.limit(2)
|
||||
.get()
|
||||
.then(res => {
|
||||
uni.hideLoading()
|
||||
data.getWithCommandData = res.data
|
||||
notify(`获取成功,取到了${res.data.length}条数据`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbUpdate() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('type')
|
||||
.where(
|
||||
`tag == "${dataTag.value}"`
|
||||
)
|
||||
.update({
|
||||
num: 4
|
||||
})
|
||||
.then(res => {
|
||||
uni.hideLoading()
|
||||
data.updateUpdated = res.updated
|
||||
notify(`更新成功,更新了${res.updated}条数据`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbRemove() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('type')
|
||||
.where(
|
||||
`tag == "${dataTag.value}"`
|
||||
)
|
||||
.remove()
|
||||
.then(res => {
|
||||
uni.hideLoading()
|
||||
data.removeDeleted = res.deleted
|
||||
notify(`删除成功,删掉了${res.deleted}条数据`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbLookupInit() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
await db.collection('local')
|
||||
.where(`tag == "${dataTag.value}"`)
|
||||
.remove()
|
||||
.then(() : Promise<UniCloudDBRemoveResult> => {
|
||||
return db.collection('foreign')
|
||||
.where(`tag == "${dataTag.value}"`)
|
||||
.remove()
|
||||
})
|
||||
.then(() : Promise<UniCloudDBBatchAddResult> => {
|
||||
return db.collection('local')
|
||||
.add([{
|
||||
id: "local_1",
|
||||
name: "local_1_name",
|
||||
tag: dataTag.value,
|
||||
foreign_id: "foreign_1"
|
||||
}, {
|
||||
id: "local_2",
|
||||
name: "local_2_name",
|
||||
tag: dataTag.value,
|
||||
foreign_id: "foreign_2"
|
||||
}])
|
||||
})
|
||||
.then(() : Promise<UniCloudDBBatchAddResult> => {
|
||||
return db.collection('foreign')
|
||||
.add([{
|
||||
id: "foreign_1",
|
||||
name: "foreign_1_name",
|
||||
tag: dataTag.value
|
||||
}, {
|
||||
id: "foreign_2",
|
||||
name: "foreign_2_name",
|
||||
tag: dataTag.value
|
||||
}])
|
||||
})
|
||||
.then((_) : void => {
|
||||
uni.hideLoading()
|
||||
notify('数据初始化成功', '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
console.error(err)
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbLookup() : Promise<void> {
|
||||
uni.showLoading({
|
||||
title: '加载中...'
|
||||
})
|
||||
const db = uniCloud.databaseForJQL()
|
||||
const local = db.collection('local')
|
||||
.where(`tag == "${dataTag.value}"`)
|
||||
.getTemp()
|
||||
const foreign = db.collection('foreign')
|
||||
.where(`tag == "${dataTag.value}"`)
|
||||
.getTemp()
|
||||
await db.collection(local, foreign)
|
||||
.get()
|
||||
.then(res => {
|
||||
uni.hideLoading()
|
||||
data.lookupData = res.data
|
||||
notify(`联表查询成功,取到了${res.data.length}条数据`, '提示')
|
||||
})
|
||||
.catch((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
async function dbMultiSend() : Promise<void> {
|
||||
const db = uniCloud.databaseForJQL()
|
||||
const temp1 = db.collection('type')
|
||||
.where(
|
||||
'tag == "default-tag"'
|
||||
).getTemp()
|
||||
const temp2 = db.collection('type')
|
||||
.where(
|
||||
'tag == "default-tag"'
|
||||
).getTemp()
|
||||
await db.multiSend(temp1, temp2)
|
||||
.then<void>(res => {
|
||||
uni.hideLoading()
|
||||
let successCount = 0
|
||||
for (let i = 0; i < res.dataList.length; i++) {
|
||||
const item = res.dataList[i]
|
||||
if (item.errCode == 0) {
|
||||
console.log(`第${i}个请求查询到${item.data!.length}条数据`)
|
||||
successCount++
|
||||
} else {
|
||||
console.error(`第${i}个请求查询失败,错误信息:${item.data!.length}`)
|
||||
}
|
||||
}
|
||||
data.multiSendSuccessCount = successCount
|
||||
notify(`合并查询成功,成功查询的语句条数为:${successCount}`, '提示')
|
||||
})
|
||||
.catch<void>((err : any | null) => {
|
||||
uni.hideLoading()
|
||||
const error = err as UniCloudError
|
||||
console.error(err)
|
||||
notify(error.errMsg, '错误')
|
||||
})
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
dbAdd,
|
||||
dbBatchAdd,
|
||||
dbGet,
|
||||
dbGetWithCommand,
|
||||
dbUpdate,
|
||||
dbRemove,
|
||||
dbLookupInit,
|
||||
dbLookup,
|
||||
dbMultiSend
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
@@ -0,0 +1,24 @@
|
||||
const PAGE_PATH = '/pages/API/unicloud/unicloud/sse-channel'
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
const isWeb = platformInfo.startsWith('web')
|
||||
|
||||
describe('sse-channel', () => {
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
})
|
||||
if(!isMP && !isWeb) {
|
||||
// 需要自定义基座方可使用push
|
||||
it('skip test', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
it('basic', async () => {
|
||||
await page.callMethod('receiveMessage')
|
||||
await page.waitFor(5000)
|
||||
const messages = await page.data('data.messages')
|
||||
expect(messages.length).toBe(2)
|
||||
})
|
||||
});
|
||||
@@ -0,0 +1,66 @@
|
||||
<template>
|
||||
<!-- #ifdef APP -->
|
||||
<scroll-view class="page-scroll-view">
|
||||
<!-- #endif -->
|
||||
<view>
|
||||
<page-head :title="title"></page-head>
|
||||
<button @click="receiveMessage">接收服务端通知</button>
|
||||
<view>
|
||||
<view v-for="item in data.messages">{{item}}</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- #ifdef APP -->
|
||||
</scroll-view>
|
||||
<!-- #endif -->
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
type DataType = {
|
||||
messages: Array<string>,
|
||||
}
|
||||
|
||||
const title = ref('服务端通知通道')
|
||||
const data = reactive({
|
||||
messages: [] as Array<string>,
|
||||
} as DataType)
|
||||
|
||||
async function receiveMessage() {
|
||||
data.messages.splice(0, data.messages.length)
|
||||
const sseChannel = new uniCloud.SSEChannel()
|
||||
sseChannel.on('message', (message?: any | null) => {
|
||||
if(typeof message === 'string') {
|
||||
data.messages.push(message)
|
||||
}
|
||||
})
|
||||
sseChannel.on('end', (message?: any | null) => {
|
||||
if(typeof message === 'string') {
|
||||
data.messages.push(message)
|
||||
}
|
||||
})
|
||||
sseChannel.on('open', () => {
|
||||
console.log('sseChannel open')
|
||||
})
|
||||
sseChannel.on('close', () => {
|
||||
console.log('sseChannel close')
|
||||
})
|
||||
sseChannel.on('error', (error: UniCloudError) => {
|
||||
console.log('sseChannel error: ' + error.message)
|
||||
})
|
||||
await sseChannel.open()
|
||||
const res = await uniCloud.callFunction({
|
||||
name: 'sse',
|
||||
data: {
|
||||
sseChannel
|
||||
}
|
||||
} as UniCloudCallFunctionOptions)
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
data,
|
||||
receiveMessage
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
在新工单中引用
屏蔽一个用户