refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,44 @@
|
||||
const PAGE_PATH = '/pages/component/unicloud-db/unicloud-db'
|
||||
const isDom2 = process.env.UNI_APP_X_DOM2 === "true"
|
||||
|
||||
// 仅测试dom2
|
||||
describe('unicloud-db', () => {
|
||||
if (!isDom2) {
|
||||
it('not support', () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
let page
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor(500)
|
||||
})
|
||||
it('check udb ref methods', async () => {
|
||||
const res = await page.callMethod('checkUdbRefMethods')
|
||||
expect(res).toBe(true)
|
||||
})
|
||||
// it('add/get/update/remove', async () => {
|
||||
// await page.callMethod('add')
|
||||
// await page.waitFor(3000)
|
||||
// const {
|
||||
// $addResult
|
||||
// } = await page.data()
|
||||
// expect($addResult['id'].length > 0).toBe(true)
|
||||
|
||||
// await page.callMethod('update', $addResult['id'])
|
||||
// await page.waitFor(3000)
|
||||
// const {
|
||||
// $updateResult
|
||||
// } = await page.data()
|
||||
// expect($updateResult['updated']).toBe(1)
|
||||
|
||||
// await page.callMethod('remove', $addResult['id'])
|
||||
// await page.waitFor(3000)
|
||||
// const {
|
||||
// $removeResult
|
||||
// } = await page.data()
|
||||
// expect($removeResult['deleted']).toBe(1)
|
||||
// })
|
||||
})
|
||||
@@ -0,0 +1,216 @@
|
||||
<template>
|
||||
<view class="content">
|
||||
<page-intro content="本页演示 unicloud-db 云数据库组件:集合查询、分页与 loadtime manual,列表展示与 Add/Get 操作,可跳转通讯录等 mixin-datacom 示例。"></page-intro>
|
||||
<unicloud-db ref="udbRef" v-slot:default="{data, pagination, loading, error}" :collection="collection" :getcount="true"
|
||||
loadtime="manual">
|
||||
<list-view v-if="data.length>0" ref="listViewRef" class="list" :scroll-y="true" @scrolltolower="loadMore()">
|
||||
<list-item class="list-item" v-for="(item, _) in data" :key="item.getString('_id')">
|
||||
<view class="list-item-fill">
|
||||
<text>{{item}}</text>
|
||||
</view>
|
||||
<view>
|
||||
<text class="list-item-remove" @click="remove(item.getString('_id')!)">❌</text>
|
||||
</view>
|
||||
</list-item>
|
||||
</list-view>
|
||||
<text class="loading" v-if="loading">Loading...</text>
|
||||
<view v-if="error!=null">{{error.errMsg}}</view>
|
||||
<view class="pagination" v-if="data.length>0">
|
||||
<text class="pagination-item">{{data.length}} / {{pagination.count}}</text>
|
||||
</view>
|
||||
</unicloud-db>
|
||||
<view class="btn-group">
|
||||
<button class="btn" @click="add()">Add</button>
|
||||
<button class="btn" @click="get()">Get</button>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
const db = uniCloud.databaseForJQL()
|
||||
|
||||
// Template refs
|
||||
const udbRef = ref<UniCloudDBElement | null>(null)
|
||||
const listViewRef = ref<UniListViewElement | null>(null)
|
||||
|
||||
// Data
|
||||
const collection = ref('unicloud-db-test')
|
||||
const collectionList = ref([
|
||||
db.collection('book').where('name == "水浒传"').getTemp(),
|
||||
] as UTSJSONObject[])
|
||||
const isTesting = ref(false)
|
||||
const addResult = ref({})
|
||||
const updateResult = ref({})
|
||||
const removeResult = ref({})
|
||||
|
||||
// Methods
|
||||
function loadMore() {
|
||||
udbRef.value!.loadMore()
|
||||
}
|
||||
|
||||
function get() {
|
||||
udbRef.value!.loadData({
|
||||
clear: true
|
||||
})
|
||||
}
|
||||
|
||||
function showError(err : any | null) {
|
||||
const error = err as UniCloudError
|
||||
uni.showModal({
|
||||
content: error.errMsg,
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
|
||||
function add() {
|
||||
const value = {
|
||||
title: "title-" + Date.now(),
|
||||
comment: "comment" + Date.now()
|
||||
}
|
||||
udbRef.value!.add(value, {
|
||||
showToast: false,
|
||||
success: (res : UniCloudDBAddResult) => {
|
||||
addResult.value = {
|
||||
id: res.id
|
||||
}
|
||||
get()
|
||||
},
|
||||
fail: (err : any | null) => {
|
||||
showError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function update(id : string) {
|
||||
const value = {
|
||||
title: "title-" + Date.now(),
|
||||
comment: "comment" + Date.now()
|
||||
}
|
||||
udbRef.value!.update(id, value, {
|
||||
showToast: false,
|
||||
needLoading: true,
|
||||
needConfirm: false,
|
||||
loadingTitle: "正在更新...",
|
||||
success: (res : UniCloudDBUpdateResult) => {
|
||||
updateResult.value = {
|
||||
updated: res.updated
|
||||
}
|
||||
},
|
||||
fail: (err : any | null) => {
|
||||
showError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function remove(id : string) {
|
||||
udbRef.value!.remove(id, {
|
||||
showToast: false,
|
||||
needConfirm: false,
|
||||
needLoading: false,
|
||||
success: (res : UniCloudDBRemoveResult) => {
|
||||
removeResult.value = {
|
||||
deleted: res.deleted
|
||||
}
|
||||
get()
|
||||
},
|
||||
fail: (err : any | null) => {
|
||||
showError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function onQueryLoad(data : Array<UTSJSONObject>, ended : boolean, pagination : UTSJSONObject) {
|
||||
console.log(data, ended, pagination)
|
||||
}
|
||||
|
||||
// Lifecycle
|
||||
onReady(() => {
|
||||
get()
|
||||
})
|
||||
|
||||
onPullDownRefresh(() => {
|
||||
udbRef.value!.loadData({
|
||||
clear: true,
|
||||
success: (_ : UniCloudDBGetResult) => {
|
||||
uni.stopPullDownRefresh()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// #ifdef VUE3-VAPOR
|
||||
let checkMethods = false
|
||||
|
||||
function checkUdbRefMethods() : boolean {
|
||||
const udb = udbRef.value
|
||||
if (udb == null) {
|
||||
checkMethods = false
|
||||
return checkMethods
|
||||
}
|
||||
|
||||
const hasLoadData = udb.loadData != null
|
||||
const hasLoadMore = udb.loadMore != null
|
||||
const hasAdd = udb.add != null
|
||||
const hasRemove = udb.remove != null
|
||||
const hasUpdate = udb.update != null
|
||||
|
||||
checkMethods = hasLoadData && hasLoadMore && hasAdd && hasRemove && hasUpdate
|
||||
return checkMethods
|
||||
}
|
||||
|
||||
defineExpose({
|
||||
checkUdbRefMethods
|
||||
})
|
||||
|
||||
// #endif
|
||||
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.content {
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.list {
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.list-item {
|
||||
flex-direction: row;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.list-item-fill {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.list-item-remove {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.loading {
|
||||
padding: 10px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.pagination {
|
||||
flex-direction: row;
|
||||
background-color: #f2f2f2;
|
||||
}
|
||||
|
||||
.pagination-item {
|
||||
margin: auto;
|
||||
padding: 5px 10px;
|
||||
}
|
||||
|
||||
.btn-group {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.btn {
|
||||
flex: 1;
|
||||
margin: 10px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,121 @@
|
||||
<template>
|
||||
<view class="page">
|
||||
<unicloud-db ref="udbRef" :collection="collection" loadtime="manual"></unicloud-db>
|
||||
<form @submit="onFormSubmit">
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">姓名</text>
|
||||
<input class="form-item-input" placeholder="姓名" name="username" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">电话</text>
|
||||
<input class="form-item-input" placeholder="电话" name="mobile" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">邮箱</text>
|
||||
<input class="form-item-input" placeholder="邮箱地址" name="email" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">备注</text>
|
||||
<textarea class="form-item-input-comment" placeholder="备注" name="comment" :maxlength="-1" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">性别</text>
|
||||
<radio-group class="radio-list" name="gender">
|
||||
<view class="radio-item" v-for="(item, _) in genderList" :key="item.value">
|
||||
<radio :value="item.value.toString()" />
|
||||
<text>{{item.text}}</text>
|
||||
</view>
|
||||
</radio-group>
|
||||
</view>
|
||||
<button class="btn-add" type="primary" form-type="submit">保存</button>
|
||||
</form>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
import { COLLECTION_NAME, GenderType, GenderList, UNICLOUD_DB_CONTACTS_ADD } from './types.uts'
|
||||
|
||||
const collection = ref(COLLECTION_NAME)
|
||||
const genderList = ref(GenderList as GenderType[])
|
||||
const udbRef = ref<UniCloudDBElement | null>(null)
|
||||
|
||||
function onFormSubmit(e : UniFormSubmitEvent) {
|
||||
const formData = e.detail.value
|
||||
const genderString = formData['gender'] as string
|
||||
formData['gender'] = (genderString.length > 0) ? parseInt(genderString) : -1
|
||||
console.log('formData', formData)
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.add(formData, {
|
||||
showToast: false,
|
||||
needLoading: true,
|
||||
loadingTitle: "正在保存...",
|
||||
success: (_ : UniCloudDBAddResult) => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$emit(UNICLOUD_DB_CONTACTS_ADD, '')
|
||||
setTimeout(() => {
|
||||
uni.navigateBack()
|
||||
}, 500)
|
||||
},
|
||||
fail: (err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
uni.showModal({
|
||||
content: error.errMsg,
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.page {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.form-item {
|
||||
flex-direction: row;
|
||||
margin-bottom: 15px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.form-item-label {
|
||||
width: 45px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.form-item-input {
|
||||
flex: 1;
|
||||
font-size: 14px;
|
||||
color: #a1a1a1;
|
||||
border: 1px #e5e5e5 solid;
|
||||
border-radius: 5px;
|
||||
height: 34px;
|
||||
padding-left: 8px;
|
||||
padding-right: 8px;
|
||||
}
|
||||
|
||||
.form-item-input-comment {
|
||||
flex: 1;
|
||||
font-size: 14px;
|
||||
color: #a1a1a1;
|
||||
border: 1px #e5e5e5 solid;
|
||||
border-radius: 5px;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.radio-list {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.radio-item {
|
||||
flex-direction: row;
|
||||
margin-right: 30px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.btn-add {
|
||||
margin-top: 50px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,104 @@
|
||||
<template>
|
||||
<scroll-view class="page">
|
||||
<unicloud-db ref="udbRef" v-slot:default="{data, loading, error}" :collection="collection" :where="where"
|
||||
page-data="replace">
|
||||
<view v-if="error!=null" class="error">{{error.errMsg}}</view>
|
||||
<view v-if="loading" class="loading">正在加载...</view>
|
||||
<view v-if="data.length>0">
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">姓名</text>
|
||||
<text class="form-item-input">{{data[0]['username']}}</text>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">电话</text>
|
||||
<text class="form-item-input">{{data[0]['mobile']}}</text>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">邮箱</text>
|
||||
<text class="form-item-input">{{data[0]['email']}}</text>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">备注</text>
|
||||
<text class="form-item-input">{{data[0]['comment']}}</text>
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">性别</text>
|
||||
<text class="form-item-input">{{displayGender(data[0].getNumber('gender'))}}</text>
|
||||
</view>
|
||||
<button type="default" @click="gotoUpdatePage(data[0].getString('_id'))">编辑</button>
|
||||
</view>
|
||||
</unicloud-db>
|
||||
</scroll-view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
import { COLLECTION_NAME, GenderType, GenderList, UNICLOUD_DB_CONTACTS_UPDATE } from './types.uts'
|
||||
|
||||
const collection = ref('')
|
||||
const where = ref('')
|
||||
const whereID = ref('')
|
||||
const udbRef = ref<UniCloudDBElement | null>(null)
|
||||
|
||||
function displayGender(value : number | null) : string {
|
||||
const str = value ?? -1
|
||||
return (GenderList as GenderType[]).find((item : GenderType) : boolean => {
|
||||
return item.value == str
|
||||
})!.text;
|
||||
}
|
||||
function onDataChange(event : string) {
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.loadData()
|
||||
}
|
||||
}
|
||||
function gotoUpdatePage(id : string | null) {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$on(UNICLOUD_DB_CONTACTS_UPDATE, onDataChange);
|
||||
uni.navigateTo({
|
||||
url: './edit?id=' + id
|
||||
})
|
||||
}
|
||||
|
||||
onLoad((options) => {
|
||||
collection.value = COLLECTION_NAME;
|
||||
whereID.value = options['id'] as string;
|
||||
where.value = `_id=='${whereID.value}'`;
|
||||
})
|
||||
onPageShow(() => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, onDataChange);
|
||||
})
|
||||
onUnload(() => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, onDataChange);
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.page {
|
||||
flex: 1;
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.loading {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.form-item {
|
||||
flex-direction: row;
|
||||
margin-bottom: 15px;
|
||||
align-items: center;
|
||||
padding: 8px 0;
|
||||
}
|
||||
|
||||
.form-item-label {
|
||||
width: 45px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.form-item-input {
|
||||
flex: 1;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,178 @@
|
||||
<template>
|
||||
<scroll-view class="scroll-view">
|
||||
<unicloud-db ref="udbRef" v-slot:default="{data, loading, error}" :collection="collection" :where="where"
|
||||
page-data="replace">
|
||||
<view v-if="error!=null" class="error">{{error.errMsg}}</view>
|
||||
<view v-if="loading" class="loading">正在加载...</view>
|
||||
<form v-if="data.length>0" @submit="onFormSubmit">
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">姓名</text>
|
||||
<input class="form-item-input" placeholder="姓名" name="username" :value="data[0].getString('username')" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">电话</text>
|
||||
<input class="form-item-input" placeholder="电话" name="mobile" :value="data[0].getString('mobile')" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">邮箱</text>
|
||||
<input class="form-item-input" placeholder="邮箱地址" name="email" :value="data[0].getString('email')" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">备注</text>
|
||||
<textarea class="form-item-input-comment" placeholder="备注" name="comment" :maxlength="-1"
|
||||
:value="data[0].getString('comment')" />
|
||||
</view>
|
||||
<view class="form-item">
|
||||
<text class="form-item-label">性别</text>
|
||||
<radio-group class="radio-list" name="gender">
|
||||
<view class="radio-item" v-for="(item, _) in genderList" :key="item.value">
|
||||
<radio :value="item.value.toString()" :checked="item.value == data[0].getNumber('gender')" />
|
||||
<text>{{item.text}}</text>
|
||||
</view>
|
||||
</radio-group>
|
||||
</view>
|
||||
<view class="btn-group">
|
||||
<button class="btn-submit" type="primary" form-type="submit">保存</button>
|
||||
<button class="btn-delete" type="warn"
|
||||
@click="remove(data[0].getString('_id'), data[0].getString('username'))">删除联系人</button>
|
||||
</view>
|
||||
</form>
|
||||
</unicloud-db>
|
||||
</scroll-view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
import { COLLECTION_NAME, GenderType, GenderList, UNICLOUD_DB_CONTACTS_UPDATE, UNICLOUD_DB_CONTACTS_DELETE } from './types.uts'
|
||||
|
||||
const collection = ref('')
|
||||
const where = ref('')
|
||||
const genderList = ref(GenderList as GenderType[])
|
||||
const whereID = ref('')
|
||||
const udbRef = ref<UniCloudDBElement | null>(null)
|
||||
|
||||
function onFormSubmit(e : UniFormSubmitEvent) {
|
||||
const formData = e.detail.value
|
||||
const genderString = formData['gender'] as string
|
||||
formData['gender'] = (genderString.length > 0) ? parseInt(genderString) : -1
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.update(whereID.value, formData, {
|
||||
showToast: false,
|
||||
needLoading: true,
|
||||
needConfirm: false,
|
||||
loadingTitle: "正在保存...",
|
||||
success: (_ : UniCloudDBUpdateResult) => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$emit(UNICLOUD_DB_CONTACTS_UPDATE, whereID.value)
|
||||
setTimeout(() => {
|
||||
uni.navigateBack()
|
||||
}, 500)
|
||||
},
|
||||
fail: (err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
uni.showModal({
|
||||
content: error.errMsg,
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
function remove(id : string | null, name : string | null) {
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.remove(id!, {
|
||||
needConfirm: true,
|
||||
needLoading: true,
|
||||
loadingTitle: "正在删除...",
|
||||
confirmTitle: "确定删除?",
|
||||
confirmContent: name,
|
||||
success: (_ : UniCloudDBRemoveResult) => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$emit(UNICLOUD_DB_CONTACTS_DELETE, whereID.value)
|
||||
setTimeout(() => {
|
||||
uni.navigateBack({
|
||||
delta: 2
|
||||
})
|
||||
}, 500)
|
||||
},
|
||||
fail: (err : any | null) => {
|
||||
const error = err as UniCloudError
|
||||
uni.showModal({
|
||||
content: error.errMsg,
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
onLoad((options) => {
|
||||
collection.value = COLLECTION_NAME;
|
||||
whereID.value = options['id'] as string;
|
||||
where.value = `_id=='${whereID.value}'`;
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.page {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.scroll-view {
|
||||
padding: 15px;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.loading {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.form-item {
|
||||
flex-direction: row;
|
||||
margin-bottom: 15px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.form-item-label {
|
||||
width: 45px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
.form-item-input {
|
||||
flex: 1;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
border: 1px #e5e5e5 solid;
|
||||
border-radius: 5px;
|
||||
height: 34px;
|
||||
padding-left: 8px;
|
||||
padding-right: 8px;
|
||||
}
|
||||
|
||||
.form-item-input-comment {
|
||||
flex: 1;
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
border: 1px #e5e5e5 solid;
|
||||
border-radius: 5px;
|
||||
padding: 8px;
|
||||
}
|
||||
|
||||
.radio-list {
|
||||
flex-direction: row;
|
||||
}
|
||||
|
||||
.radio-item {
|
||||
flex-direction: row;
|
||||
margin-right: 30px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.btn-group {
|
||||
margin-top: 30px;
|
||||
}
|
||||
|
||||
.btn-delete {
|
||||
margin-top: 15px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,149 @@
|
||||
<template>
|
||||
<view class="page">
|
||||
<unicloud-db ref="udbRef" class="udb" v-slot:default="{data, pagination, loading, hasMore, error}"
|
||||
:collection="collection" :page-size="15" :getcount="true" loadtime="manual">
|
||||
<view v-if="error!=null" class="error">{{error.errMsg}}</view>
|
||||
<scroll-view ref="listView" class="list-view" @scrolltolower="loadMore()">
|
||||
<view class="list-item" v-for="(item, _) in data" @click="gotoDetailPage(item['_id'] as string)">
|
||||
<view class="list-item-fill">
|
||||
<text class="name">{{item['username']}}</text>
|
||||
<text class="mobile">{{item['mobile']}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</scroll-view>
|
||||
<loading v-if="loading" style="color: #999;margin: 20px;align-self: center;"></loading>
|
||||
<view v-if="data.length>0" class="pagination">
|
||||
<text class="pagination-item">{{data.length}} / {{pagination.count}}</text>
|
||||
</view>
|
||||
</unicloud-db>
|
||||
<view class="btn-plus" @click="gotoAddPage">
|
||||
<text class="btn-plus-text">+</text>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
|
||||
import { COLLECTION_NAME, UNICLOUD_DB_CONTACTS_ADD, UNICLOUD_DB_CONTACTS_UPDATE, UNICLOUD_DB_CONTACTS_DELETE } from './types.uts'
|
||||
|
||||
const collection = ref(COLLECTION_NAME)
|
||||
const udbRef = ref<UniCloudDBElement | null>(null)
|
||||
|
||||
function loadMore() {
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.loadMore()
|
||||
}
|
||||
}
|
||||
function onDataChange(event : string) {
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.loadData({
|
||||
clear: true
|
||||
})
|
||||
}
|
||||
}
|
||||
function gotoAddPage() {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$on(UNICLOUD_DB_CONTACTS_ADD, onDataChange);
|
||||
uni.navigateTo({
|
||||
url: './add'
|
||||
})
|
||||
}
|
||||
function gotoDetailPage(id : string) {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$on(UNICLOUD_DB_CONTACTS_UPDATE, onDataChange);
|
||||
uni.navigateTo({
|
||||
url: './detail?id=' + id
|
||||
})
|
||||
}
|
||||
|
||||
onReady(() => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$on(UNICLOUD_DB_CONTACTS_DELETE, onDataChange);
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.loadData()
|
||||
}
|
||||
})
|
||||
onUnload(() => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_ADD, onDataChange);
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, onDataChange);
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_DELETE, onDataChange);
|
||||
})
|
||||
onPageShow(() => {
|
||||
// TODO 后续通过 EventChannel 实现
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_ADD, onDataChange);
|
||||
uni.$off(UNICLOUD_DB_CONTACTS_UPDATE, onDataChange);
|
||||
})
|
||||
onPullDownRefresh(() => {
|
||||
if (udbRef.value != null) {
|
||||
udbRef.value!.loadData({
|
||||
clear: true,
|
||||
success: (_ : UniCloudDBGetResult) => {
|
||||
uni.stopPullDownRefresh()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.page {
|
||||
/* #ifdef APP */
|
||||
flex: 1;
|
||||
/* #endif */
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.loading {
|
||||
margin: 20px;
|
||||
align-self: center;
|
||||
}
|
||||
|
||||
.udb {
|
||||
flex: 1;
|
||||
/* TODO */
|
||||
/* #ifdef WEB */
|
||||
display: flex;
|
||||
/* #endif */
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.list-view {
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.list-item {
|
||||
flex-direction: row;
|
||||
padding: 10px;
|
||||
background-color: #fff;
|
||||
margin-bottom: 1px;
|
||||
}
|
||||
|
||||
.mobile {
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.btn-plus {
|
||||
position: absolute;
|
||||
width: 64px;
|
||||
height: 64px;
|
||||
right: 20px;
|
||||
bottom: 20px;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-color: #1e90ff;
|
||||
border-radius: 64px;
|
||||
}
|
||||
|
||||
.btn-plus-text {
|
||||
font-size: 30px;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.pagination {
|
||||
align-items: center;
|
||||
background-color: #f8f8f8;
|
||||
padding: 15px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,25 @@
|
||||
export const COLLECTION_NAME = 'opendb-contacts'
|
||||
|
||||
export type GenderType = {
|
||||
text : string,
|
||||
value : number,
|
||||
}
|
||||
|
||||
export const GenderList = [
|
||||
{
|
||||
text: "未知",
|
||||
value: 0
|
||||
},
|
||||
{
|
||||
text: "男",
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
text: "女",
|
||||
value: 2
|
||||
},
|
||||
] as GenderType[]
|
||||
|
||||
export const UNICLOUD_DB_CONTACTS_ADD = 'unicloud-db-contacts-add'
|
||||
export const UNICLOUD_DB_CONTACTS_UPDATE = 'unicloud-db-contacts-update'
|
||||
export const UNICLOUD_DB_CONTACTS_DELETE = 'unicloud-db-contacts-delete'
|
||||
@@ -0,0 +1,66 @@
|
||||
<template>
|
||||
<view>
|
||||
<view v-if="mixinDatacomLoading">Loading...</view>
|
||||
<view v-else-if="mixinDatacomError">
|
||||
请求错误:{{mixinDatacomError}}
|
||||
</view>
|
||||
<view v-else="mixinDatacomResData.length>0">
|
||||
<!-- 需要自行处理数据及相关UI展现 -->
|
||||
<!-- {{mixinDatacomResData}} -->
|
||||
<scroll-view class="list-view">
|
||||
<view class="list-item" v-for="(item, _) in mixinDatacomResData">
|
||||
<text>{{item}}</text>
|
||||
</view>
|
||||
</scroll-view>
|
||||
</view>
|
||||
<view v-if="!mixinDatacomLoading && mixinDatacomResData.length==0">
|
||||
无数据
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script lang="uts">
|
||||
export default {
|
||||
mixins: [uniCloud.mixinDatacom],
|
||||
data() {
|
||||
return {
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.load()
|
||||
},
|
||||
methods: {
|
||||
load() {
|
||||
if (this.mixinDatacomLoading == true) {
|
||||
return
|
||||
}
|
||||
this.mixinDatacomLoading = true
|
||||
|
||||
this.mixinDatacomGet(null).then((res : UniCloudDBGetResult) => {
|
||||
const data = res.data
|
||||
// const count = res.count
|
||||
this.mixinDatacomResData = data
|
||||
}).catch((err : any | null) => {
|
||||
this.mixinDatacomError = err as UniCloudError
|
||||
}).finally(() => {
|
||||
this.mixinDatacomLoading = false
|
||||
})
|
||||
},
|
||||
// 当组件属性发生变化时
|
||||
onMixinDatacomPropsChange(needReset : boolean, changed : Array<string>) {
|
||||
console.log(needReset, changed);
|
||||
// needReset=true 需要重置已加载数据和分页信息,例如 collection,orderby
|
||||
// changed,变化的属性名,类型为 Array,例如 ['collection', 'orderby']
|
||||
// if (needReset) {
|
||||
// // 清空已加载的数据
|
||||
// this.mixinDatacomResData = []
|
||||
|
||||
// // 重置分页数据,如果没有分页不需要处理
|
||||
// this.mixinDatacomPage.size = this.pageSize // 重置分页大小
|
||||
// this.mixinDatacomPage.current = 0 // 重置当前分页
|
||||
// this.mixinDatacomPage.count = 0 // 重置数据总数
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,26 @@
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
const isWeb = platformInfo.startsWith('web')
|
||||
const isDom2 = process.env.UNI_APP_X_DOM2 === "true"
|
||||
|
||||
const PAGE_PATH = '/pages/component/unicloud-db/unicloud-db/mixin-datacom/mixin-datacom'
|
||||
|
||||
describe('mixin-datacom', () => {
|
||||
if (isMP || isWeb || isDom2) {
|
||||
it('dummyTest', async () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
return
|
||||
}
|
||||
let page
|
||||
beforeAll(async () => {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor(500)
|
||||
})
|
||||
it('mixinDatacomGet', async () => {
|
||||
const datacom = await page.$('.datacom')
|
||||
const children = await datacom.$$('.list-item')
|
||||
console.log('children',children)
|
||||
// expect(children.length > 0).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,15 @@
|
||||
<template>
|
||||
<view class="content">
|
||||
<datacom class="datacom" collection="unicloud-db-test"></datacom>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import datacom from './datacom.uvue'
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.content {
|
||||
flex: 1;
|
||||
}
|
||||
</style>
|
||||
在新工单中引用
屏蔽一个用户