随便取个昵称
随便取个昵称
  • 发布:2024-01-01 22:40
  • 更新:2024-01-01 22:40
  • 阅读:242

【报Bug】在后台页面输入数字在最新版HBuilderX上竟然给我变成了string类型?

分类:HBuilderX

产品分类: HbuilderX

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: Windows 10 专业版

HBuilderX版本号: 3.99

示例代码:

<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validateTrigger="bind" labelWidth="90px">
<uni-forms-item name="title" label="商品标题" required>
<uni-easyinput placeholder="商品标题" v-model="formData.title" trim="both"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="primaryImage" label="商品主图" required>
<uni-file-picker file-mediatype="image" file-extname="jpg,png" return-type="object" v-model="formData.primaryImage"></uni-file-picker>
</uni-forms-item>
<uni-forms-item name="images" label="商品轮播图" required>
<uni-file-picker file-mediatype="image" file-extname="jpg,png" :limit="6" return-type="array" v-model="formData.images"></uni-file-picker>
</uni-forms-item>
<uni-forms-item name="classify" label="商品类别" required>
<uni-data-picker v-model="formData.classify" collection="lingshi_classify" field="name as value, name as text"></uni-data-picker>
</uni-forms-item>
<uni-forms-item name="price" label="商品价格" required>
<uni-easyinput placeholder="商品价格,单位为分" type="number" v-model="formData.price"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="stockQuantity" label="商品库存">
<uni-easyinput placeholder="商品库存" type="number" v-model="formData.stockQuantity"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="soldNum" label="已售数量">
<uni-easyinput placeholder="已售数量" type="number" v-model="formData.soldNum"></uni-easyinput>
</uni-forms-item>
<uni-forms-item name="tagList" label="商品标签">
<uni-easyinput placeholder="商品标签,以中文逗号分隔" v-model="formData.tagList"></uni-easyinput>
</uni-forms-item>
<uni-card title="商品详情" :image-styles="{'height': '500rpx','width' : '300rpx'}" margin="0">
<uni-forms-item name="describes" label="商品信息" required>
<textarea :maxlength="-1" auto-height placeholder="标题:内容,(符号全以中文书写,多个信息以逗号分隔)"
@input="binddata('describes', $event.detail.value)" class="uni-textarea-border"
v-model="formData.describes">
</textarea>
</uni-forms-item>
<uni-forms-item name="detaiLimages" label="商品详情图" required>
<uni-file-picker file-mediatype="image" file-extname="jpg,png" :limit="6" return-type="array" v-model="formData.detaiLimages"></uni-file-picker>
</uni-forms-item>
</uni-card>
<!-- <uni-forms-item name="create_date" label="创建时间">
<uni-dateformat return-type="timestamp" v-model="formData.create_date"></uni-dateformat>
</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/lingshi_goods_details.js';

const db = uniCloud.database();
const dbCmd = db.command;
const dbCollectionName = 'lingshi_goods_details';

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 = {
// "skuId": null,
"title": "",
"primaryImage": null,
"images": [],
"classify": "",
"price": 999,
"stockQuantity": 100,
"soldNum": 50,
// "tagList": [],
// "describes": [],
"tagList": null,
"describes": null,
"detaiLimages": null,
// "create_date": null
}
return {
formData,
formOptions: {},
// rules: {
// ...getValidator(Object.keys(formData))
// }
}
},
onReady() {
this.$refs.form.setRules(this.rules)
},
methods: {
/
生成skuId唯一标识方法
作者:泽南Zn
链接:https://juejin.cn/post/7184359234060943421
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/
// 利用时间戳
getSkuId () {
const time = Date.now()
return String(time)
},

  /**  
   * 验证表单并提交  
   */  
  submit() {  
    uni.showLoading({  
      mask: true  
    })  
    this.$refs.form.validate().then((res) => {  
        if(res.tagList && res.tagList.trim()){  
            let tagList = []  
            res.tagList.split(',').forEach((item) => {  
                if(item.trim()) tagList.push(item.trim())  
            })  
            if(tagList.length>0) {  
                res.tagList = tagList  
            } else {res.tagList = null}   
        }else{res.tagList = null}  
        if(res.price){  
            const cent_price = res.price*100  
            res.price = cent_price  
        }  
        if(res.stockQuantity){  
            const stockQuantity = res.stockQuantity  
            console.log(typeof(stockQuantity))  
            if(typeof(stockQuantity)=='string') res.stockQuantity = parseInt(stockQuantity)  
        }  
        if(res.soldNum){  
            const soldNum = res.soldNum  
            console.log(typeof(soldNum))  
            if(typeof(soldNum)=='string') res.soldNum = parseInt(soldNum)  
        }  
        if(res.describes && res.describes.trim()){  
            const desc_list = res.describes.split(',')  
            if (desc_list.length>0){  
                let desc_obj = {}  
                let describes = []  
                desc_list.forEach((item) => {  
                    if(item.trim()){  
                        try {  
                            const desc_obj_str = item.split(':')  
                            const title = desc_obj_str[0].trim()  
                            const content = desc_obj_str[1].trim()  
                            if (!title || !content) {  
                                uni.showToast({  
                                    icon: 'error',  
                                    title: '商品信息格式错误'  
                                })  
                                throw '商品信息格式错误';  
                            }  
                            desc_obj['title'] = title  
                            desc_obj['content'] = content  
                            describes.push(desc_obj)  
                            desc_obj = {}  
                        }catch (e) {  
                            uni.showToast({  
                                icon: 'error',  
                                title: '商品信息格式错误'  
                            })  
                            throw e;  
                        }  
                    }  
                })   
                if(describes.length>0){  
                    res.describes = describes  
                }else{throw '商品信息格式错误';}  
            }  
            res['skuId'] = this.getSkuId()  
        }else{  
            uni.showToast({  
            icon: 'error',  
            title: '商品信息格式错误'  
            })  
            throw '商品信息格式错误';  
        }  
        console.log(res.describes)  
        return this.submitForm(res)  
    }).catch((e) => {  
        console.log(e)  
    }).finally(() => {  
        uni.hideLoading()  
    })  
  },  

  // 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>

操作步骤:

// 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema
{
"bsonType": "object", // 固定节点
"description": "商品详情", //该表的描述
"required": ["title","primaryImage","images","classify","price","desc","describes","detaiLimages"], // 必填字段列表
"permission": {
"read": false, // 前端非admin的读取记录权限控制。默认值是false,即可以不写。可以简单的true/false,也可以写表达式
"create": false, // 前端非admin的新增记录权限控制。默认值是false,即可以不写。可以简单的true/false,也可以写表达式
"update": false, // 前端非admin的更新记录权限控制。默认值是false,即可以不写。可以简单的true/false,也可以写表达式
"delete": false, // 前端非admin的删除记录权限控制。默认值是false,即可以不写。可以简单的true/false,也可以写表达式
"count": true // 前端非admin的求数权限控制。默认值是true,即可以不写。可以简单的true/false,也可以写表达式
},
"properties": { // 该表的字段清单
"_id": { // 字段名称,每个表都会带有_id字段
"description": "ID,系统自动生成"
},
"skuId": {
"description": "商品唯一标识id,提交表单时自动生成"
},
"title": {
"description": "商品标题",
"bsonType": "string",
"title": "商品标题",
"trim": "both", //trim 去除空白字符,支持 none|both|start|end,默认none,仅bsonType="string"时有效
"minLength": 3,
"maxLength": 30
},
"primaryImage": {
"description": "商品主图", //在主页展示商品列表时用的那个图
"bsonType": "file",
"title": "商品主图",
"fileMediaType": "image", // 可选值 all|image|video 默认值为all,表示所有文件,image表示图片类型文件,video表示视频类型文件
"fileExtName": "jpg,png" // 扩展名过滤,多个用 , 分割
},
"images": {
"description": "详情页商品轮播图", //在商品详情页的商品轮播图数组
"bsonType": "array",
"arrayType": "file", //这个数组里每个数组项目的bsonType为file
"title": "商品轮播图",
"multiple": true, // 允许选择多张图片,schema2code生效
"fileMediaType": "image", // 可选值 all|image|video 默认值为all,表示所有文件,image表示图片类型文件,video表示视频类型文件
"fileExtName": "jpg,png", // 扩展名过滤,多个用 , 分割
"minLength": 0,
"maxLength": 6 // 限制最大数量
},
"classify": {
"bsonType": "string",
"description": "商品类别名称, 参考lingshi_classify 表",
"title": "商品类别",
"foreignKey": "lingshi_classify.name",
"enum": {
"collection": "lingshi_classify",
"field": "name as value, name as text"
}
},
"price": {
"description": "商品价格,单位为元",
"bsonType": "double", //double类型慎重,由于js不能精准处理浮点运算,0.1+0.2=0.30000000000000004。所以涉及金额时,建议使用int而不是double,以分为单位而不是以元为单位存储。
"title": "商品价格", //便于编辑人员输入价格,后台把元转为分再存入库
"minimum": 0.5,
"maximum": 100000
},
"stockQuantity": {
"description": "商品库存",
"bsonType": "int",
"title": "商品库存"
},
"soldNum": {
"description": "已售数量",
"bsonType": "int",
"title": "已售数量"
},
"tagList": {
"description": "商品标签,以中文逗号分隔",
"bsonType": "array",
"arrayType": "string",
"title": "商品标签"
},
"describes": {
"bsonType": "array",
"arrayType": "object", //这个数组里每个数组项目的bsonType为object
"description": "商品信息",
"title": "商品信息" ,
"properties": {
"title": {
"bsonType": "string",
"title": "标题",
"trim": "both"
},
"content": {
"bsonType": "string",
"title": "内容",
"trim": "both"
}
}
},
"detaiLimages": {
"description": "商品详情图",
"bsonType": "array",
"arrayType": "file", //这个数组里每个数组项目的bsonType为file
"title": "商品详情图",
"multiple": true, // 允许选择多张图片,schema2code生效
"fileMediaType": "image", // 可选值 all|image|video 默认值为all,表示所有文件,image表示图片类型文件,video表示视频类型文件
"fileExtName": "jpg,png", // 扩展名过滤,多个用 , 分割
"minLength": 0,
"maxLength": 6 // 限制最大数量
},
"create_date": {
"bsonType": "timestamp",
"label": "服务器端创建时间",
"forceDefaultValue": {
"$env": "now"
},
"componentForEdit": {
"name": "uni-dateformat"
}
},
"update_date": {
"bsonType": "timestamp",
"label": "用户端更新时间"
}
}
}

预期结果:

如标题

实际结果:

如标题

bug描述:

本地schema设置字段为int类型,页面输入也自动做了判断,只能输入数字,提交时却自动变成了string类型,奶奶的,以前版本都没这种问题。项目是uni-admin

"stockQuantity": {
"description": "商品库存",
"bsonType": "int",
"title": "商品库存"
},
"soldNum": {
"description": "已售数量",
"bsonType": "int",
"title": "已售数量"
},

2024-01-01 22:40 负责人:无 分享
已邀请:

要回复问题请先登录注册