afterservice.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. item: {},
  5. imgList: [], // 本地或云端可预览的图片 URL
  6. fileIDs: [], // 云端 fileID
  7. description: '', // 新增描述字段
  8. },
  9. onLoad(options) {
  10. const itemData = decodeURIComponent(options.data);
  11. const item = JSON.parse(itemData);
  12. this.setData({ item });
  13. },
  14. // 上传图片
  15. chooseImage() {
  16. if (this.data.imgList.length >= 6) {
  17. wx.showToast({ title: '最多上传6张图片', icon: 'none' });
  18. return;
  19. }
  20. wx.chooseImage({
  21. count: 6 - this.data.imgList.length, // 剩余可上传数
  22. sizeType: ['original', 'compressed'],
  23. sourceType: ['album', 'camera'],
  24. success: (res) => {
  25. res.tempFilePaths.forEach(filePath => {
  26. const cloudPath = 'aftersales/' + Date.now() + '-' + Math.floor(Math.random() * 1000) + filePath.match(/\.[^.]+?$/)[0];
  27. wx.cloud.uploadFile({
  28. cloudPath,
  29. filePath,
  30. success: uploadRes => {
  31. // 上传成功,更新列表
  32. this.setData({
  33. imgList: [...this.data.imgList, uploadRes.fileID],
  34. fileIDs: [...this.data.fileIDs, uploadRes.fileID]
  35. });
  36. },
  37. fail: () => {
  38. wx.showToast({ title: '上传失败', icon: 'none' });
  39. }
  40. });
  41. });
  42. }
  43. });
  44. },
  45. // 删除图片
  46. deleteImage(e) {
  47. const index = e.currentTarget.dataset.index;
  48. const fileID = this.data.imgList[index]; // 直接取字符串
  49. wx.cloud.deleteFile({
  50. fileList: [fileID]
  51. }).then(res => {
  52. console.log('删除结果', res);
  53. // 前端同步移除
  54. const imgList = [...this.data.imgList];
  55. imgList.splice(index, 1);
  56. this.setData({ imgList });
  57. // 如果你还要同步更新 fileIDs,也要处理
  58. const fileIDs = [...this.data.fileIDs];
  59. fileIDs.splice(index, 1);
  60. this.setData({ fileIDs });
  61. }).catch(err => {
  62. console.error('删除文件失败', err);
  63. });
  64. },
  65. // 预览图片
  66. previewImage(e) {
  67. const currentUrl = e.currentTarget.dataset.url;
  68. wx.previewImage({
  69. current: currentUrl,
  70. urls: this.data.imgList
  71. });
  72. },
  73. // 问题描述
  74. onDescriptionInput(e) {
  75. this.setData({ description: e.detail.value });
  76. },
  77. // 提交表单
  78. async submitForm() {
  79. const { description, fileIDs } = this.data;
  80. if (!description.trim()) {
  81. wx.showToast({ title: '请输入问题描述', icon: 'none' });
  82. return;
  83. }
  84. if (fileIDs.length === 0) {
  85. wx.showToast({ title: '请先上传图片', icon: 'none' });
  86. return;
  87. }
  88. wx.showLoading({ title: '提交中...' });
  89. try {
  90. // 这里替换成你真实的参数
  91. const goods_id = this.data.item.merchandise_id || "";
  92. const school_id = this.data.item.schools_id || "";
  93. const user_id = this.data.item.user_id || "";
  94. const order_id = this.data.item.orders_id || "";
  95. console.log(this.data.item, 'goods_id',);
  96. const { data } = await models.wx_afterservice.create({
  97. data: {
  98. goods_id,
  99. url: fileIDs,
  100. des: description,
  101. school_id,
  102. user_id,
  103. state: 0,
  104. order_id,
  105. },
  106. envType: "prod",
  107. });
  108. console.log('创建成功,id:', data.id);
  109. wx.hideLoading();
  110. wx.showToast({ title: '提交成功', icon: 'success' });
  111. this.setData({
  112. imgList: [], // 本地或云端可预览的图片 URL
  113. fileIDs: [], // 云端 fileID
  114. description: '', // 新增描述字段
  115. })
  116. setTimeout(() => {
  117. wx.navigateBack();
  118. }, 1000);
  119. // 提交成功后可以清空表单或者跳转页面
  120. } catch (error) {
  121. wx.hideLoading();
  122. wx.showToast({ title: '提交失败,请重试', icon: 'none' });
  123. console.error('提交接口失败', error);
  124. }
  125. }
  126. });