123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- import { models, db, _ } from '../../utils/cloudbase.js'
- Page({
- data: {
- item: {},
- imgList: [], // 本地或云端可预览的图片 URL
- fileIDs: [], // 云端 fileID
- description: '', // 新增描述字段
- },
- onLoad(options) {
- const itemData = decodeURIComponent(options.data);
- const item = JSON.parse(itemData);
- this.setData({ item });
- },
- // 上传图片
- chooseImage() {
- if (this.data.imgList.length >= 6) {
- wx.showToast({ title: '最多上传6张图片', icon: 'none' });
- return;
- }
- wx.chooseImage({
- count: 6 - this.data.imgList.length, // 剩余可上传数
- sizeType: ['original', 'compressed'],
- sourceType: ['album', 'camera'],
- success: (res) => {
- res.tempFilePaths.forEach(filePath => {
- const cloudPath = 'aftersales/' + Date.now() + '-' + Math.floor(Math.random() * 1000) + filePath.match(/\.[^.]+?$/)[0];
- wx.cloud.uploadFile({
- cloudPath,
- filePath,
- success: uploadRes => {
- // 上传成功,更新列表
- this.setData({
- imgList: [...this.data.imgList, uploadRes.fileID],
- fileIDs: [...this.data.fileIDs, uploadRes.fileID]
- });
- },
- fail: () => {
- wx.showToast({ title: '上传失败', icon: 'none' });
- }
- });
- });
- }
- });
- },
- // 删除图片
- deleteImage(e) {
- const index = e.currentTarget.dataset.index;
- const fileID = this.data.imgList[index]; // 直接取字符串
-
- wx.cloud.deleteFile({
- fileList: [fileID]
- }).then(res => {
- console.log('删除结果', res);
-
- // 前端同步移除
- const imgList = [...this.data.imgList];
- imgList.splice(index, 1);
- this.setData({ imgList });
-
- // 如果你还要同步更新 fileIDs,也要处理
- const fileIDs = [...this.data.fileIDs];
- fileIDs.splice(index, 1);
- this.setData({ fileIDs });
- }).catch(err => {
- console.error('删除文件失败', err);
- });
- },
- // 预览图片
- previewImage(e) {
- const currentUrl = e.currentTarget.dataset.url;
- wx.previewImage({
- current: currentUrl,
- urls: this.data.imgList
- });
- },
- // 问题描述
- onDescriptionInput(e) {
- this.setData({ description: e.detail.value });
- },
- // 提交表单
- async submitForm() {
- const { description, fileIDs } = this.data;
-
- if (!description.trim()) {
- wx.showToast({ title: '请输入问题描述', icon: 'none' });
- return;
- }
-
- if (fileIDs.length === 0) {
- wx.showToast({ title: '请先上传图片', icon: 'none' });
- return;
- }
-
- wx.showLoading({ title: '提交中...' });
-
- try {
- // 这里替换成你真实的参数
- const goods_id = this.data.item.merchandise_id || "";
- const school_id = this.data.item.schools_id || "";
- const user_id = this.data.item.user_id || "";
- const order_id = this.data.item.orders_id || "";
- console.log(this.data.item, 'goods_id',);
-
- const { data } = await models.wx_afterservice.create({
- data: {
- goods_id,
- url: fileIDs,
- des: description,
- school_id,
- user_id,
- state: 0,
- order_id,
- },
- envType: "prod",
- });
-
- console.log('创建成功,id:', data.id);
-
- wx.hideLoading();
- wx.showToast({ title: '提交成功', icon: 'success' });
- this.setData({
- imgList: [], // 本地或云端可预览的图片 URL
- fileIDs: [], // 云端 fileID
- description: '', // 新增描述字段
- })
- setTimeout(() => {
- wx.navigateBack();
- }, 1000);
-
- // 提交成功后可以清空表单或者跳转页面
- } catch (error) {
- wx.hideLoading();
- wx.showToast({ title: '提交失败,请重试', icon: 'none' });
- console.error('提交接口失败', error);
- }
- }
- });
|