afterservice.js 4.8 KB

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