4***@qq.com
4***@qq.com
  • 发布:2024-12-29 20:57
  • 更新:2024-12-29 20:57
  • 阅读:26

schema2code生成vue3页面uni-file-picker有问题,还是我的代码有问题,麻烦帮看看

分类:uniCloud

用以下代码通过schema2code生成add.vue,上传不管是jpg,还是png图片,都显示图片类型无效。是我的schema代码哪里出错了么研究了很久文档也没研究出所以然。
{
"bsonType": "object",
"required": ["tupian"],
"properties": {
"tupian": {
"bsonType": "string",
"title": "图片",
"description": "仅支持上传一张图片,大小不超过500KB",
"componentForEdit": {
"name": "uni-file-picker",
"props": {
"fileType": "image",
"extname": ".jpg,.jpeg,.png,.gif",
"limit": 1,
"maxSize": 512000, // 500KB = 500 * 1024 字节
"autoUpload": true,
"returnFilePath": true
},
"events": {
"success": "handleFileSuccess",
"fail": "handleFileFail"
}
},
"componentForShow": {
"name": "image",
"props": {
"class": "admin-list-image",
"mode": "aspectFill",
":src": "item.tupian"
}
}
}
}
}

以下是add.vue代码
<template>
<view class="uni-container">
<uni-forms ref="form" :model="formData" validateTrigger="bind">
<uni-forms-item name="tupian" label="图片" required>
<uni-file-picker fileType="image" extname=".jpg,.jpeg,.png,.gif" limit="1" maxSize="500 * 1024," autoUpload="true" returnFilePath="true" placeholder="仅支持上传一张图片,大小不超过500KB" v-model="formData.tupian"></uni-file-picker>
</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/atupian.js';

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

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 = {
"tupian": ""
}
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>

2024-12-29 20:57 负责人:无 分享
已邀请:

要回复问题请先登录注册