提交 604e8cca 编写于 作者: M MicroMilo

测试完成,新增运动管理界面

上级 1f8e06ea
// 表单校验规则由 schema2code 生成,不建议直接修改校验规则,而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema
const validator = {
"start_date": {
"rules": [
{
"format": "string"
}
],
"title": "开始时间",
"label": "开始时间"
},
"duration": {
"rules": [
{
"format": "int"
}
],
"title": "持续时间",
"label": "持续时间"
},
"feelings": {
"rules": [
{
"format": "string"
}
],
"title": "感受",
"label": "感受"
},
"owner_id": {
"rules": [
{
"format": "string"
}
],
"title": "用户id",
"label": "用户id"
},
"distance": {
"rules": [
{
"format": "double"
}
],
"title": "距离",
"label": "距离"
},
"pace": {
"rules": [
{
"format": "double"
}
],
"title": "配速",
"label": "配速"
},
"path_line": {
"rules": [
{
"format": "array"
}
],
"title": "路线",
"label": "路线"
},
"start_point": {
"rules": [
{
"format": "object"
}
],
"title": "起始地点",
"label": "起始地点"
},
"end_point": {
"rules": [
{
"format": "object"
}
],
"title": "结束地点",
"label": "结束地点"
}
}
const enumConverter = {}
function filterToWhere(filter, command) {
let where = {}
for (let field in filter) {
let { type, value } = filter[field]
switch (type) {
case "search":
if (typeof value === 'string' && value.length) {
where[field] = new RegExp(value)
}
break;
case "select":
if (value.length) {
let selectValue = []
for (let s of value) {
selectValue.push(command.eq(s))
}
where[field] = command.or(selectValue)
}
break;
case "range":
if (value.length) {
let gt = value[0]
let lt = value[1]
where[field] = command.and([command.gte(gt), command.lte(lt)])
}
break;
case "date":
if (value.length) {
let [s, e] = value
let startDate = new Date(s)
let endDate = new Date(e)
where[field] = command.and([command.gte(startDate), command.lte(endDate)])
}
break;
case "timestamp":
if (value.length) {
let [startDate, endDate] = value
where[field] = command.and([command.gte(startDate), command.lte(endDate)])
}
break;
}
}
return where
}
export { validator, enumConverter, filterToWhere }
// 表单校验规则由 schema2code 生成,不建议直接修改校验规则,而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema
const validator = {
"start_date": {
"rules": [
{
"format": "string"
}
],
"title": "开始时间",
"label": "开始时间"
},
"duration": {
"rules": [
{
"format": "int"
}
],
"title": "持续时间",
"label": "持续时间"
},
"feelings": {
"rules": [
{
"format": "string"
}
],
"title": "感受",
"label": "感受"
},
"owner_id": {
"rules": [
{
"format": "string"
}
],
"title": "用户id",
"label": "用户id"
},
"distance": {
"rules": [
{
"format": "double"
}
],
"title": "距离",
"label": "距离"
},
"steps": {
"rules": [
{
"format": "int"
}
],
"title": "步数",
"label": "步数"
},
"start_point": {
"rules": [
{
"format": "object"
}
],
"title": "起始地点",
"label": "起始地点"
},
"end_point": {
"rules": [
{
"format": "object"
}
],
"title": "结束地点",
"label": "结束地点"
},
"path_line": {
"rules": [
{
"format": "array"
}
],
"title": "路线",
"label": "路线"
}
}
const enumConverter = {}
function filterToWhere(filter, command) {
let where = {}
for (let field in filter) {
let { type, value } = filter[field]
switch (type) {
case "search":
if (typeof value === 'string' && value.length) {
where[field] = new RegExp(value)
}
break;
case "select":
if (value.length) {
let selectValue = []
for (let s of value) {
selectValue.push(command.eq(s))
}
where[field] = command.or(selectValue)
}
break;
case "range":
if (value.length) {
let gt = value[0]
let lt = value[1]
where[field] = command.and([command.gte(gt), command.lte(lt)])
}
break;
case "date":
if (value.length) {
let [s, e] = value
let startDate = new Date(s)
let endDate = new Date(e)
where[field] = command.and([command.gte(startDate), command.lte(endDate)])
}
break;
case "timestamp":
if (value.length) {
let [startDate, endDate] = value
where[field] = command.and([command.gte(startDate), command.lte(endDate)])
}
break;
}
}
return where
}
export { validator, enumConverter, filterToWhere }
......@@ -280,6 +280,36 @@
"style": {
"navigationBarTitleText": "列表"
}
}, {
"path": "mustgo-record/running/add",
"style": {
"navigationBarTitleText": "新增"
}
}, {
"path": "mustgo-record/running/edit",
"style": {
"navigationBarTitleText": "编辑"
}
}, {
"path": "mustgo-record/running/list",
"style": {
"navigationBarTitleText": "列表"
}
}, {
"path": "mustgo-record/walking/add",
"style": {
"navigationBarTitleText": "新增"
}
}, {
"path": "mustgo-record/walking/edit",
"style": {
"navigationBarTitleText": "编辑"
}
}, {
"path": "mustgo-record/walking/list",
"style": {
"navigationBarTitleText": "列表"
}
}]
},
{
......
<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validateTrigger="bind">
<uni-forms-item name="start_date" label="开始时间">
<uni-easyinput placeholder="运动开始时间" v-model="formData.start_date"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="duration" label="持续时间">
<uni-easyinput placeholder="运动持续时间" type="number" v-model="formData.duration"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="feelings" label="感受">
<uni-easyinput placeholder="运动感受" v-model="formData.feelings"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="owner_id" label="用户id">
<uni-easyinput placeholder="用户id" v-model="formData.owner_id"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="distance" label="距离">
<uni-easyinput placeholder="运动距离" type="number" v-model="formData.distance"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="pace" label="配速">
<uni-easyinput placeholder="运动配速" type="number" v-model="formData.pace"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="path_line" label="路线">
<uni-data-checkbox :multiple="true" v-model="formData.path_line"></uni-data-checkbox>
</uni-forms-item>
<uni-forms-item name="start_point" label="起始地点">
<undefined v-model="formData.start_point"></undefined>
</uni-forms-item>
<uni-forms-item name="end_point" label="结束地点">
<undefined v-model="formData.end_point"></undefined>
</uni-forms-item>
<view class="uni-button-group">
<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
<navigator open-type="navigateBack" style="margin-left: 15px;">
<button class="uni-button" style="width: 100px;">返回</button>
</navigator>
</view>
</uni-forms>
</view>
</template>
<script>
import { validator } from '../../../../js_sdk/validator/mustgo-running-record.js';
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'mustgo-running-record';
function getValidator(fields) {
let result = {}
for (let key in validator) {
if (fields.includes(key)) {
result[key] = validator[key]
}
}
return result
}
export default {
data() {
let formData = {
"start_date": "",
"duration": null,
"feelings": "",
"owner_id": "",
"distance": null,
"pace": null,
"path_line": [],
"start_point": null,
"end_point": null
}
return {
formData,
formOptions: {},
rules: {
...getValidator(Object.keys(formData))
}
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 验证表单并提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.validate().then((res) => {
return this.submitForm(res)
}).catch(() => {
}).finally(() => {
uni.hideLoading()
})
},
/**
* 提交表单
*/
submitForm(value) {
// 使用 clientDB 提交数据
return db.collection(dbCollectionName).add(value).then((res) => {
uni.showToast({
title: '新增成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
})
}
}
}
</script>
<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validateTrigger="bind">
<uni-forms-item name="start_date" label="开始时间">
<uni-easyinput placeholder="运动开始时间" v-model="formData.start_date"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="duration" label="持续时间">
<uni-easyinput placeholder="运动持续时间" type="number" v-model="formData.duration"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="feelings" label="感受">
<uni-easyinput placeholder="运动感受" v-model="formData.feelings"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="owner_id" label="用户id">
<uni-easyinput placeholder="用户id" v-model="formData.owner_id"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="distance" label="距离">
<uni-easyinput placeholder="运动距离" type="number" v-model="formData.distance"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="pace" label="配速">
<uni-easyinput placeholder="运动配速" type="number" v-model="formData.pace"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="path_line" label="路线">
<uni-data-checkbox :multiple="true" v-model="formData.path_line"></uni-data-checkbox>
</uni-forms-item>
<uni-forms-item name="start_point" label="起始地点">
<undefined v-model="formData.start_point"></undefined>
</uni-forms-item>
<uni-forms-item name="end_point" label="结束地点">
<undefined v-model="formData.end_point"></undefined>
</uni-forms-item>
<view class="uni-button-group">
<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
<navigator open-type="navigateBack" style="margin-left: 15px;">
<button class="uni-button" style="width: 100px;">返回</button>
</navigator>
</view>
</uni-forms>
</view>
</template>
<script>
import { validator } from '../../../../js_sdk/validator/mustgo-running-record.js';
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'mustgo-running-record';
function getValidator(fields) {
let result = {}
for (let key in validator) {
if (fields.includes(key)) {
result[key] = validator[key]
}
}
return result
}
export default {
data() {
let formData = {
"start_date": "",
"duration": null,
"feelings": "",
"owner_id": "",
"distance": null,
"pace": null,
"path_line": [],
"start_point": null,
"end_point": null
}
return {
formData,
formOptions: {},
rules: {
...getValidator(Object.keys(formData))
}
}
},
onLoad(e) {
if (e.id) {
const id = e.id
this.formDataId = id
this.getDetail(id)
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 验证表单并提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.validate().then((res) => {
return this.submitForm(res)
}).catch(() => {
}).finally(() => {
uni.hideLoading()
})
},
/**
* 提交表单
*/
submitForm(value) {
// 使用 clientDB 提交数据
return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
uni.showToast({
title: '修改成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
})
},
/**
* 获取表单数据
* @param {Object} id
*/
getDetail(id) {
uni.showLoading({
mask: true
})
db.collection(dbCollectionName).doc(id).field("start_date,duration,feelings,owner_id,distance,pace,path_line,start_point,end_point").get().then((res) => {
const data = res.result.data[0]
if (data) {
this.formData = data
}
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
}).finally(() => {
uni.hideLoading()
})
}
}
}
</script>
<template>
<view>
<view class="uni-header">
<view class="uni-group">
<view class="uni-title"></view>
<view class="uni-sub-title"></view>
</view>
<view class="uni-group">
<input class="uni-search" type="text" v-model="query" @confirm="search" placeholder="请输入搜索内容" />
<button class="uni-button" type="default" size="mini" @click="search">搜索</button>
<button class="uni-button" type="default" size="mini" @click="navigateTo('./add')">新增</button>
<button class="uni-button" type="default" size="mini" :disabled="!selectedIndexs.length" @click="delTable">批量删除</button>
<download-excel class="hide-on-phone" :fields="exportExcel.fields" :data="exportExcelData" :type="exportExcel.type" :name="exportExcel.filename">
<button class="uni-button" type="primary" size="mini">导出 Excel</button>
</download-excel>
</view>
</view>
<view class="uni-container">
<unicloud-db ref="udb" :collection="collectionList" field="start_date,duration,feelings,owner_id,distance,pace,path_line,start_point,end_point" :where="where" page-data="replace"
:orderby="orderby" :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent"
v-slot:default="{data,pagination,loading,error,options}" :options="options" loadtime="manual" @load="onqueryload">
<uni-table ref="table" :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe type="selection" @selection-change="selectionChange">
<uni-tr>
<uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'start_date')" sortable @sort-change="sortChange($event, 'start_date')">开始时间</uni-th>
<uni-th align="center" filter-type="range" @filter-change="filterChange($event, 'duration')" sortable @sort-change="sortChange($event, 'duration')">持续时间(秒)</uni-th>
<uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'feelings')" sortable @sort-change="sortChange($event, 'feelings')">感受</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'owner_id')">用户id</uni-th>
<uni-th align="center" filter-type="range" @filter-change="filterChange($event, 'distance')" sortable @sort-change="sortChange($event, 'distance')">距离</uni-th>
<uni-th align="center" filter-type="range" @filter-change="filterChange($event, 'pace')" sortable @sort-change="sortChange($event, 'pace')">配速</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'path_line')">路线</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'start_point')">起始地点</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'end_point')">结束地点</uni-th>
<uni-th align="center">操作</uni-th>
</uni-tr>
<uni-tr v-for="(item,index) in data" :key="index">
<uni-td align="center">{{item.start_date}}</uni-td>
<uni-td align="center">{{item.duration}}</uni-td>
<uni-td align="center">{{item.feelings}}</uni-td>
<uni-td align="center">{{item.owner_id}}</uni-td>
<uni-td align="center">{{item.distance}}</uni-td>
<uni-td align="center">{{item.pace}}</uni-td>
<uni-td align="center">{{item.path_line}}</uni-td>
<uni-td align="center">{{item.start_point}}</uni-td>
<uni-td align="center">{{item.end_point}}</uni-td>
<uni-td align="center">
<view class="uni-group">
<button @click="navigateTo('./edit?id='+item._id, false)" class="uni-button" size="mini" type="primary">修改</button>
<button @click="confirmDelete(item._id)" class="uni-button" size="mini" type="warn">删除</button>
</view>
</uni-td>
</uni-tr>
</uni-table>
<view class="uni-pagination-box">
<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current" :total="pagination.count" @change="onPageChanged" />
</view>
</unicloud-db>
</view>
</view>
</template>
<script>
import { enumConverter, filterToWhere } from '../../../../js_sdk/validator/mustgo-running-record.js';
const db = uniCloud.database()
// 表查询配置
const dbOrderBy = '' // 排序字段
const dbSearchFields = [] // 模糊搜索字段,支持模糊搜索的字段列表。联表查询格式: 主表字段名.副表字段名,例如用户表关联角色表 role.role_name
// 分页配置
const pageSize = 20
const pageCurrent = 1
const orderByMapping = {
"ascending": "asc",
"descending": "desc"
}
export default {
data() {
return {
collectionList: "mustgo-running-record",
query: '',
where: '',
orderby: dbOrderBy,
orderByFieldName: "",
selectedIndexs: [],
options: {
pageSize,
pageCurrent,
filterData: {},
...enumConverter
},
imageStyles: {
width: 64,
height: 64
},
exportExcel: {
"filename": "mustgo-running-record.xls",
"type": "xls",
"fields": {
"开始时间": "start_date",
"持续时间": "duration",
"感受": "feelings",
"用户id": "owner_id",
"距离": "distance",
"配速": "pace",
"路线": "path_line",
"起始地点": "start_point",
"结束地点": "end_point"
}
},
exportExcelData: []
}
},
onLoad() {
this._filter = {}
},
onReady() {
this.$refs.udb.loadData()
},
methods: {
onqueryload(data) {
this.exportExcelData = data
},
getWhere() {
const query = this.query.trim()
if (!query) {
return ''
}
const queryRe = new RegExp(query, 'i')
return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
},
search() {
const newWhere = this.getWhere()
this.where = newWhere
this.$nextTick(() => {
this.loadData()
})
},
loadData(clear = true) {
this.$refs.udb.loadData({
clear
})
},
onPageChanged(e) {
this.selectedIndexs.length = 0
this.$refs.table.clearSelection()
this.$refs.udb.loadData({
current: e.current
})
},
navigateTo(url, clear) {
// clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
uni.navigateTo({
url,
events: {
refreshData: () => {
this.loadData(clear)
}
}
})
},
// 多选处理
selectedItems() {
var dataList = this.$refs.udb.dataList
return this.selectedIndexs.map(i => dataList[i]._id)
},
// 批量删除
delTable() {
this.$refs.udb.remove(this.selectedItems(), {
success:(res) => {
this.$refs.table.clearSelection()
}
})
},
// 多选
selectionChange(e) {
this.selectedIndexs = e.detail.index
},
confirmDelete(id) {
this.$refs.udb.remove(id, {
success:(res) => {
this.$refs.table.clearSelection()
}
})
},
sortChange(e, name) {
this.orderByFieldName = name;
if (e.order) {
this.orderby = name + ' ' + orderByMapping[e.order]
} else {
this.orderby = ''
}
this.$refs.table.clearSelection()
this.$nextTick(() => {
this.$refs.udb.loadData()
})
},
filterChange(e, name) {
this._filter[name] = {
type: e.filterType,
value: e.filter
}
let newWhere = filterToWhere(this._filter, db.command)
if (Object.keys(newWhere).length) {
this.where = newWhere
} else {
this.where = ''
}
this.$nextTick(() => {
this.$refs.udb.loadData()
})
}
}
}
</script>
<style>
</style>
<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validateTrigger="bind">
<uni-forms-item name="start_date" label="开始时间">
<uni-easyinput placeholder="运动开始时间" v-model="formData.start_date"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="duration" label="持续时间">
<uni-easyinput placeholder="运动持续时间" type="number" v-model="formData.duration"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="feelings" label="感受">
<uni-easyinput placeholder="运动感受" v-model="formData.feelings"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="owner_id" label="用户id">
<uni-easyinput placeholder="用户id" v-model="formData.owner_id"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="distance" label="距离">
<uni-easyinput placeholder="运动距离" type="number" v-model="formData.distance"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="steps" label="步数">
<uni-easyinput placeholder="健走步数" type="number" v-model="formData.steps"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="start_point" label="起始地点">
<undefined v-model="formData.start_point"></undefined>
</uni-forms-item>
<uni-forms-item name="end_point" label="结束地点">
<undefined v-model="formData.end_point"></undefined>
</uni-forms-item>
<uni-forms-item name="path_line" label="路线">
<uni-data-checkbox :multiple="true" v-model="formData.path_line"></uni-data-checkbox>
</uni-forms-item>
<view class="uni-button-group">
<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
<navigator open-type="navigateBack" style="margin-left: 15px;">
<button class="uni-button" style="width: 100px;">返回</button>
</navigator>
</view>
</uni-forms>
</view>
</template>
<script>
import { validator } from '../../../../js_sdk/validator/mustgo-walking-record.js';
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'mustgo-walking-record';
function getValidator(fields) {
let result = {}
for (let key in validator) {
if (fields.includes(key)) {
result[key] = validator[key]
}
}
return result
}
export default {
data() {
let formData = {
"start_date": "",
"duration": null,
"feelings": "",
"owner_id": "",
"distance": null,
"steps": null,
"start_point": null,
"end_point": null,
"path_line": []
}
return {
formData,
formOptions: {},
rules: {
...getValidator(Object.keys(formData))
}
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 验证表单并提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.validate().then((res) => {
return this.submitForm(res)
}).catch(() => {
}).finally(() => {
uni.hideLoading()
})
},
/**
* 提交表单
*/
submitForm(value) {
// 使用 clientDB 提交数据
return db.collection(dbCollectionName).add(value).then((res) => {
uni.showToast({
title: '新增成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
})
}
}
}
</script>
<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validateTrigger="bind">
<uni-forms-item name="start_date" label="开始时间">
<uni-easyinput placeholder="运动开始时间" v-model="formData.start_date"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="duration" label="持续时间">
<uni-easyinput placeholder="运动持续时间" type="number" v-model="formData.duration"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="feelings" label="感受">
<uni-easyinput placeholder="运动感受" v-model="formData.feelings"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="owner_id" label="用户id">
<uni-easyinput placeholder="用户id" v-model="formData.owner_id"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="distance" label="距离">
<uni-easyinput placeholder="运动距离" type="number" v-model="formData.distance"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="steps" label="步数">
<uni-easyinput placeholder="健走步数" type="number" v-model="formData.steps"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="start_point" label="起始地点">
<undefined v-model="formData.start_point"></undefined>
</uni-forms-item>
<uni-forms-item name="end_point" label="结束地点">
<undefined v-model="formData.end_point"></undefined>
</uni-forms-item>
<uni-forms-item name="path_line" label="路线">
<uni-data-checkbox :multiple="true" v-model="formData.path_line"></uni-data-checkbox>
</uni-forms-item>
<view class="uni-button-group">
<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
<navigator open-type="navigateBack" style="margin-left: 15px;">
<button class="uni-button" style="width: 100px;">返回</button>
</navigator>
</view>
</uni-forms>
</view>
</template>
<script>
import { validator } from '../../../../js_sdk/validator/mustgo-walking-record.js';
const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'mustgo-walking-record';
function getValidator(fields) {
let result = {}
for (let key in validator) {
if (fields.includes(key)) {
result[key] = validator[key]
}
}
return result
}
export default {
data() {
let formData = {
"start_date": "",
"duration": null,
"feelings": "",
"owner_id": "",
"distance": null,
"steps": null,
"start_point": null,
"end_point": null,
"path_line": []
}
return {
formData,
formOptions: {},
rules: {
...getValidator(Object.keys(formData))
}
}
},
onLoad(e) {
if (e.id) {
const id = e.id
this.formDataId = id
this.getDetail(id)
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/**
* 验证表单并提交
*/
submit() {
uni.showLoading({
mask: true
})
this.$refs.form.validate().then((res) => {
return this.submitForm(res)
}).catch(() => {
}).finally(() => {
uni.hideLoading()
})
},
/**
* 提交表单
*/
submitForm(value) {
// 使用 clientDB 提交数据
return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
uni.showToast({
title: '修改成功'
})
this.getOpenerEventChannel().emit('refreshData')
setTimeout(() => uni.navigateBack(), 500)
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
})
},
/**
* 获取表单数据
* @param {Object} id
*/
getDetail(id) {
uni.showLoading({
mask: true
})
db.collection(dbCollectionName).doc(id).field("start_date,duration,feelings,owner_id,distance,steps,start_point,end_point,path_line").get().then((res) => {
const data = res.result.data[0]
if (data) {
this.formData = data
}
}).catch((err) => {
uni.showModal({
content: err.message || '请求服务失败',
showCancel: false
})
}).finally(() => {
uni.hideLoading()
})
}
}
}
</script>
<template>
<view>
<view class="uni-header">
<view class="uni-group">
<view class="uni-title"></view>
<view class="uni-sub-title"></view>
</view>
<view class="uni-group">
<input class="uni-search" type="text" v-model="query" @confirm="search" placeholder="请输入搜索内容" />
<button class="uni-button" type="default" size="mini" @click="search">搜索</button>
<button class="uni-button" type="default" size="mini" @click="navigateTo('./add')">新增</button>
<button class="uni-button" type="default" size="mini" :disabled="!selectedIndexs.length" @click="delTable">批量删除</button>
<download-excel class="hide-on-phone" :fields="exportExcel.fields" :data="exportExcelData" :type="exportExcel.type" :name="exportExcel.filename">
<button class="uni-button" type="primary" size="mini">导出 Excel</button>
</download-excel>
</view>
</view>
<view class="uni-container">
<unicloud-db ref="udb" :collection="collectionList" field="start_date,duration,feelings,owner_id,distance,steps,start_point,end_point,path_line" :where="where" page-data="replace"
:orderby="orderby" :getcount="true" :page-size="options.pageSize" :page-current="options.pageCurrent"
v-slot:default="{data,pagination,loading,error,options}" :options="options" loadtime="manual" @load="onqueryload">
<uni-table ref="table" :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe type="selection" @selection-change="selectionChange">
<uni-tr>
<uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'start_date')" sortable @sort-change="sortChange($event, 'start_date')">开始时间</uni-th>
<uni-th align="center" filter-type="range" @filter-change="filterChange($event, 'duration')" sortable @sort-change="sortChange($event, 'duration')">持续时间(秒)</uni-th>
<uni-th align="center" filter-type="search" @filter-change="filterChange($event, 'feelings')" sortable @sort-change="sortChange($event, 'feelings')">感受</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'owner_id')">用户id</uni-th>
<uni-th align="center" filter-type="range" @filter-change="filterChange($event, 'distance')" sortable @sort-change="sortChange($event, 'distance')">距离</uni-th>
<uni-th align="center" filter-type="range" @filter-change="filterChange($event, 'steps')" sortable @sort-change="sortChange($event, 'steps')">步数</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'start_point')">起始地点</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'end_point')">结束地点</uni-th>
<uni-th align="center" sortable @sort-change="sortChange($event, 'path_line')">路线</uni-th>
<uni-th align="center">操作</uni-th>
</uni-tr>
<uni-tr v-for="(item,index) in data" :key="index">
<uni-td align="center">{{item.start_date}}</uni-td>
<uni-td align="center">{{item.duration}}</uni-td>
<uni-td align="center">{{item.feelings}}</uni-td>
<uni-td align="center">{{item.owner_id}}</uni-td>
<uni-td align="center">{{item.distance}}</uni-td>
<uni-td align="center">{{item.steps}}</uni-td>
<uni-td align="center">{{item.start_point}}</uni-td>
<uni-td align="center">{{item.end_point}}</uni-td>
<uni-td align="center">{{item.path_line}}</uni-td>
<uni-td align="center">
<view class="uni-group">
<button @click="navigateTo('./edit?id='+item._id, false)" class="uni-button" size="mini" type="primary">修改</button>
<button @click="confirmDelete(item._id)" class="uni-button" size="mini" type="warn">删除</button>
</view>
</uni-td>
</uni-tr>
</uni-table>
<view class="uni-pagination-box">
<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current" :total="pagination.count" @change="onPageChanged" />
</view>
</unicloud-db>
</view>
</view>
</template>
<script>
import { enumConverter, filterToWhere } from '../../../../js_sdk/validator/mustgo-walking-record.js';
const db = uniCloud.database()
// 表查询配置
const dbOrderBy = '' // 排序字段
const dbSearchFields = [] // 模糊搜索字段,支持模糊搜索的字段列表。联表查询格式: 主表字段名.副表字段名,例如用户表关联角色表 role.role_name
// 分页配置
const pageSize = 20
const pageCurrent = 1
const orderByMapping = {
"ascending": "asc",
"descending": "desc"
}
export default {
data() {
return {
collectionList: "mustgo-walking-record",
query: '',
where: '',
orderby: dbOrderBy,
orderByFieldName: "",
selectedIndexs: [],
options: {
pageSize,
pageCurrent,
filterData: {},
...enumConverter
},
imageStyles: {
width: 64,
height: 64
},
exportExcel: {
"filename": "mustgo-walking-record.xls",
"type": "xls",
"fields": {
"开始时间": "start_date",
"持续时间": "duration",
"感受": "feelings",
"用户id": "owner_id",
"距离": "distance",
"步数": "steps",
"起始地点": "start_point",
"结束地点": "end_point",
"路线": "path_line"
}
},
exportExcelData: []
}
},
onLoad() {
this._filter = {}
},
onReady() {
this.$refs.udb.loadData()
},
methods: {
onqueryload(data) {
this.exportExcelData = data
},
getWhere() {
const query = this.query.trim()
if (!query) {
return ''
}
const queryRe = new RegExp(query, 'i')
return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
},
search() {
const newWhere = this.getWhere()
this.where = newWhere
this.$nextTick(() => {
this.loadData()
})
},
loadData(clear = true) {
this.$refs.udb.loadData({
clear
})
},
onPageChanged(e) {
this.selectedIndexs.length = 0
this.$refs.table.clearSelection()
this.$refs.udb.loadData({
current: e.current
})
},
navigateTo(url, clear) {
// clear 表示刷新列表时是否清除页码,true 表示刷新并回到列表第 1 页,默认为 true
uni.navigateTo({
url,
events: {
refreshData: () => {
this.loadData(clear)
}
}
})
},
// 多选处理
selectedItems() {
var dataList = this.$refs.udb.dataList
return this.selectedIndexs.map(i => dataList[i]._id)
},
// 批量删除
delTable() {
this.$refs.udb.remove(this.selectedItems(), {
success:(res) => {
this.$refs.table.clearSelection()
}
})
},
// 多选
selectionChange(e) {
this.selectedIndexs = e.detail.index
},
confirmDelete(id) {
this.$refs.udb.remove(id, {
success:(res) => {
this.$refs.table.clearSelection()
}
})
},
sortChange(e, name) {
this.orderByFieldName = name;
if (e.order) {
this.orderby = name + ' ' + orderByMapping[e.order]
} else {
this.orderby = ''
}
this.$refs.table.clearSelection()
this.$nextTick(() => {
this.$refs.udb.loadData()
})
},
filterChange(e, name) {
this._filter[name] = {
type: e.filterType,
value: e.filter
}
let newWhere = filterToWhere(this._filter, db.command)
if (Object.keys(newWhere).length) {
this.where = newWhere
} else {
this.where = ''
}
this.$nextTick(() => {
this.$refs.udb.loadData()
})
}
}
}
</script>
<style>
</style>
......@@ -3,7 +3,7 @@
<uni-forms ref="form" :model="formData" validateTrigger="bind">
<uni-forms-item name="icon" label="队标" @click="avatarChoose">
<image :src="formData.icon" style="width: 120px; height: 80px;"></image>
</uni-forms-item>
</uni-forms-item>
<uni-forms-item name="name" label="队名">
<uni-easyinput placeholder="小队名称" v-model="formData.name"></uni-easyinput>
</uni-forms-item>
......
......@@ -88,11 +88,12 @@
filterToWhere
} from '../../../js_sdk/validator/mustgo-user.js';
console.log(Date.now().toString());
uniCloud.callFunction({
name: 'fe-retrievePassword',
data: {
phone: "13950014724",
code: "",
code: "123456",
password: "12345678"
}
})
......
......@@ -18,9 +18,11 @@ exports.main = async (event, context) => {
phone_num: event.phone
}).get()
// console.log(res)
res = await collection.doc(event.userId).update({
password: res.data[0]["password"],
console.log(res)
let userId = res.data[0]["_id"]
res = await collection.doc(userId).update({
password: event.password
})
if (res.updated > 0)
return {
......
{"bsonType":"object","required":[],"permission":{"read":true,"create":true,"update":true,"delete":true},"properties":{"_id":{"title":"id","description":"ID,系统自动生成"},"start_date":{"bsonType":"string","title":"开始时间","description":"运动开始时间"},"duration":{"bsonType":"int","title":"持续时间","description":"运动持续时间"},"feelings":{"bsonType":"string","title":"感受","description":"运动感受"},"owner_id":{"bsonType":"string","title":"用户id","description":"用户id","foreignKey":"mustgo-user._id"},"distance":{"bsonType":"double","title":"距离","description":"运动距离"},"pace":{"bsonType":"double","title":"配速","description":"运动配速"},"start_point":{"bsonType":"object","title":"起始地点","description":"运动起始地点,经纬度"},"end_point":{"bsonType":"object","title":"结束地点","description":"运动结束地点,经纬度"},"path_line":{"bsonType":"array","title":"路线","description":"运动路线"}}}
\ No newline at end of file
{
"bsonType": "object",
"required": [],
"permission": {
"read": true,
"create": true,
"update": true,
"delete": true
},
"properties": {
"_id": {
"title": "id",
"description": "ID,系统自动生成"
},
"start_date": {
"bsonType": "string",
"title": "开始时间",
"description": "运动开始时间"
},
"duration": {
"bsonType": "int",
"title": "持续时间",
"description": "运动持续时间"
},
"feelings": {
"bsonType": "string",
"title": "感受",
"description": "运动感受"
},
"owner_id": {
"bsonType": "string",
"title": "用户id",
"description": "用户id",
"foreignKey": "mustgo-user._id"
},
"distance": {
"bsonType": "double",
"title": "距离",
"description": "运动距离"
},
"pace": {
"bsonType": "double",
"title": "配速",
"description": "运动配速"
},
"start_point": {
"bsonType": "object",
"title": "起始地点",
"description": "运动起始地点,经纬度"
},
"end_point": {
"bsonType": "object",
"title": "结束地点",
"description": "运动结束地点,经纬度"
},
"path_line": {
"bsonType": "array",
"title": "路线",
"description": "运动路线"
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册