afterservice.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. this.getadddizhi()
  14. });
  15. },
  16. async getadddizhi() {
  17. const { data } = await models.adresses.get({
  18. filter: {
  19. where: {
  20. $and: [
  21. {
  22. _id: {
  23. $eq: this.data.item.adresses_id, // 推荐传入_id数据标识进行操作
  24. },
  25. },
  26. ]
  27. }
  28. },
  29. // envType: pre 体验环境, prod 正式环境
  30. envType: "prod",
  31. });
  32. // 返回查询到的数据
  33. this.data.item.phone = data.phone
  34. console.log(this.data.item.phone, '*----------------');
  35. },
  36. // 上传图片
  37. chooseImage() {
  38. if (this.data.imgList.length >= 6) {
  39. wx.showToast({ title: '最多上传6张图片', icon: 'none' });
  40. return;
  41. }
  42. wx.chooseImage({
  43. count: 6 - this.data.imgList.length, // 剩余可上传数
  44. sizeType: ['original', 'compressed'],
  45. sourceType: ['album', 'camera'],
  46. success: (res) => {
  47. res.tempFilePaths.forEach(filePath => {
  48. const cloudPath = 'aftersales/' + Date.now() + '-' + Math.floor(Math.random() * 1000) + filePath.match(/\.[^.]+?$/)[0];
  49. wx.cloud.uploadFile({
  50. cloudPath,
  51. filePath,
  52. success: uploadRes => {
  53. // 上传成功,更新列表
  54. this.setData({
  55. imgList: [...this.data.imgList, uploadRes.fileID],
  56. fileIDs: [...this.data.fileIDs, uploadRes.fileID]
  57. });
  58. },
  59. fail: () => {
  60. wx.showToast({ title: '上传失败', icon: 'none' });
  61. }
  62. });
  63. });
  64. }
  65. });
  66. },
  67. // 删除图片
  68. deleteImage(e) {
  69. const index = e.currentTarget.dataset.index;
  70. const fileID = this.data.imgList[index]; // 直接取字符串
  71. wx.cloud.deleteFile({
  72. fileList: [fileID]
  73. }).then(res => {
  74. console.log('删除结果', res);
  75. // 前端同步移除
  76. const imgList = [...this.data.imgList];
  77. imgList.splice(index, 1);
  78. this.setData({ imgList });
  79. // 如果你还要同步更新 fileIDs,也要处理
  80. const fileIDs = [...this.data.fileIDs];
  81. fileIDs.splice(index, 1);
  82. this.setData({ fileIDs });
  83. }).catch(err => {
  84. console.error('删除文件失败', err);
  85. });
  86. },
  87. // 预览图片
  88. previewImage(e) {
  89. const currentUrl = e.currentTarget.dataset.url;
  90. wx.previewImage({
  91. current: currentUrl,
  92. urls: this.data.imgList
  93. });
  94. },
  95. // 问题描述
  96. onDescriptionInput(e) {
  97. this.setData({ description: e.detail.value });
  98. },
  99. // 提交表单
  100. async submitForm() {
  101. const { description, fileIDs } = this.data;
  102. if (!description.trim()) {
  103. wx.showToast({ title: '请输入问题描述', icon: 'none' });
  104. return;
  105. }
  106. if (fileIDs.length === 0) {
  107. wx.showToast({ title: '请先上传图片', icon: 'none' });
  108. return;
  109. }
  110. wx.showLoading({ title: '提交中...' });
  111. try {
  112. // 这里替换成你真实的参数
  113. const goods_id = this.data.item.merchandise_id || "";
  114. const school_id = this.data.item.schools_id || "";
  115. const user_id = this.data.item.user_id || "";
  116. const order_id = this.data.item.orders_id || "";
  117. const phone = this.data.item.update_phone ? this.data.item.update_phone : this.data.item.phone ? this.data.item.phone : ''
  118. const { data } = await models.wx_afterservice.create({
  119. data: {
  120. goods_id,
  121. url: fileIDs,
  122. des: description,
  123. school_id,
  124. user_id,
  125. state: 0,
  126. order_id,
  127. phone,
  128. },
  129. envType: "prod",
  130. });
  131. console.log('创建成功,id:', data.id);
  132. wx.hideLoading();
  133. wx.showToast({ title: '提交成功', icon: 'success' });
  134. this.setData({
  135. imgList: [], // 本地或云端可预览的图片 URL
  136. fileIDs: [], // 云端 fileID
  137. description: '', // 新增描述字段
  138. })
  139. setTimeout(() => {
  140. wx.navigateBack();
  141. }, 1000);
  142. // 提交成功后可以清空表单或者跳转页面
  143. } catch (error) {
  144. wx.hideLoading();
  145. wx.showToast({ title: '提交失败,请重试', icon: 'none' });
  146. console.error('提交接口失败', error);
  147. }
  148. }
  149. });