与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
7.3 KiB

  1. import http from '../../../utils/api'
  2. const baseUrl = require('../../../utils/baseUrl')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. diagnosisData: {},
  9. replies: [],
  10. baseUrl: baseUrl,
  11. refreshing: false
  12. },
  13. /**
  14. * 生命周期函数--监听页面加载
  15. */
  16. onLoad(options) {
  17. if (options.data) {
  18. // 根据id加载兽医回复数据
  19. const data = JSON.parse(decodeURIComponent(options.data));
  20. // 处理用户图片 - 确保是数组格式
  21. if (data.images) {
  22. if (typeof data.images === 'string') {
  23. data.images = data.images.split(',')
  24. }
  25. console.log('用户图片:', data.images);
  26. } else {
  27. data.images = [];
  28. }
  29. this.setData({
  30. diagnosisData: data,
  31. })
  32. this.loadDiagnosisData(data.formId);
  33. }
  34. },
  35. /**
  36. * 加载问诊数据兽医回复
  37. * @param {string} id - 问诊ID
  38. */
  39. loadDiagnosisData(id) {
  40. console.log('加载问诊数据:', id);
  41. http.wzdxq({
  42. data: {
  43. consultationId: id
  44. },
  45. success: res => {
  46. // 处理兽医回复的图片
  47. let replies = [];
  48. if (res && res.rows) {
  49. replies = res.rows || [];
  50. // 遍历每个回复,处理图片字段
  51. for (let i = 0; i < replies.length; i++) {
  52. const item = replies[i];
  53. // 处理图片 - 确保是数组格式
  54. if (item.images) {
  55. if (typeof item.images === 'string') {
  56. // 如果是空字符串,设为空数组
  57. if (item.images.trim() === '') {
  58. item.images = [];
  59. } else {
  60. // 按逗号分割字符串
  61. item.images = item.images.split(',');
  62. }
  63. } else if (Array.isArray(item.images)) {
  64. // 已经是数组,但需要过滤空值
  65. item.images = item.images.filter(img => img && img.trim() !== '');
  66. } else {
  67. item.images = [];
  68. }
  69. } else {
  70. item.images = [];
  71. }
  72. console.log(`兽医回复[${i}]图片:`, item.images);
  73. }
  74. }
  75. console.log('兽医回复数据:', replies);
  76. this.setData({
  77. replies: replies
  78. })
  79. },
  80. fail: err => {
  81. console.error('加载回复失败:', err);
  82. wx.showToast({
  83. title: '加载失败',
  84. icon: 'none'
  85. });
  86. }
  87. })
  88. },
  89. /**
  90. * 返回上一页
  91. */
  92. goBack() {
  93. wx.navigateBack();
  94. },
  95. /**
  96. * 分享
  97. */
  98. onShare() {
  99. wx.showActionSheet({
  100. itemList: ['分享给好友', '保存到相册'],
  101. success: (res) => {
  102. if (res.tapIndex === 0) {
  103. wx.showToast({
  104. title: '已分享',
  105. icon: 'success'
  106. });
  107. }
  108. }
  109. });
  110. },
  111. /**
  112. * 下拉刷新
  113. */
  114. onRefresh() {
  115. this.setData({
  116. refreshing: true
  117. });
  118. if (this.data.diagnosisData && this.data.diagnosisData.formId) {
  119. this.loadDiagnosisData(this.data.diagnosisData.formId)
  120. }
  121. setTimeout(() => {
  122. this.setData({
  123. refreshing: false
  124. });
  125. wx.showToast({
  126. title: '刷新成功',
  127. icon: 'success'
  128. });
  129. }, 1000);
  130. },
  131. /**
  132. * 预览用户图片
  133. */
  134. previewImage(e) {
  135. const dataset = e.currentTarget.dataset;
  136. const current = dataset.url;
  137. const urls = dataset.urls || [];
  138. // 构建完整URL数组
  139. const urlsArray = urls.map(item => {
  140. // 如果已经是完整URL,直接返回
  141. if (typeof item === 'string' && (item.startsWith('http://') || item.startsWith('https://'))) {
  142. return item;
  143. }
  144. // 否则拼接baseUrl
  145. return baseUrl + item;
  146. });
  147. console.log('预览用户图片:', urlsArray);
  148. wx.previewImage({
  149. current: current,
  150. urls: urlsArray
  151. });
  152. },
  153. /**
  154. * 预览兽医回复图片 - 修复版
  155. */
  156. previewReplyImage(e) {
  157. const dataset = e.currentTarget.dataset;
  158. // 获取数据 - 使用三种可能的来源
  159. const currentFileName = dataset.url; // 当前点击的图片文件名
  160. const currentFullUrl = dataset.fullurl; // 当前点击的图片完整URL
  161. const urlsArray = dataset.urls || []; // 该回复的所有图片文件名数组
  162. const currentIndex = dataset.currentIndex || 0; // 当前点击图片的索引
  163. const replyIndex = dataset.replyIndex; // 回复在列表中的索引
  164. console.log('预览兽医图片 - 数据集:', dataset);
  165. // 方法1: 从replies数据中获取当前回复的图片列表
  166. let targetUrls = [];
  167. let targetCurrentUrl = '';
  168. if (replyIndex !== undefined && this.data.replies[replyIndex]) {
  169. // 从replies数据中获取完整的图片列表
  170. const reply = this.data.replies[replyIndex];
  171. if (reply.images && reply.images.length > 0) {
  172. targetUrls = reply.images;
  173. // 构建完整URL数组
  174. const fullUrls = targetUrls.map(img => {
  175. if (typeof img === 'string' && (img.startsWith('http://') || img.startsWith('https://'))) {
  176. return img;
  177. }
  178. return baseUrl + img;
  179. });
  180. // 确定当前图片的完整URL
  181. if (currentIndex < fullUrls.length) {
  182. targetCurrentUrl = fullUrls[currentIndex];
  183. } else if (currentFullUrl) {
  184. targetCurrentUrl = currentFullUrl;
  185. } else if (currentFileName) {
  186. targetCurrentUrl = baseUrl + currentFileName;
  187. }
  188. console.log('预览兽医回复图片(从replies数据):', fullUrls, '当前:', targetCurrentUrl);
  189. wx.previewImage({
  190. current: targetCurrentUrl,
  191. urls: fullUrls
  192. });
  193. return;
  194. }
  195. }
  196. // 方法2: 使用dataset中的数据
  197. if (urlsArray && urlsArray.length > 0) {
  198. // 构建完整URL数组
  199. const fullUrls = urlsArray.map(item => {
  200. if (typeof item === 'string' && (item.startsWith('http://') || item.startsWith('https://'))) {
  201. return item;
  202. }
  203. return baseUrl + item;
  204. });
  205. // 确定当前图片的完整URL
  206. let currentUrl = '';
  207. if (currentFullUrl) {
  208. currentUrl = currentFullUrl;
  209. } else if (currentIndex < fullUrls.length) {
  210. currentUrl = fullUrls[currentIndex];
  211. } else if (currentFileName) {
  212. currentUrl = baseUrl + currentFileName;
  213. }
  214. console.log('预览兽医回复图片(从dataset):', fullUrls, '当前:', currentUrl);
  215. wx.previewImage({
  216. current: currentUrl,
  217. urls: fullUrls
  218. });
  219. return;
  220. }
  221. // 方法3: 只预览单张图片
  222. if (currentFullUrl) {
  223. console.log('预览单张兽医图片:', [currentFullUrl]);
  224. wx.previewImage({
  225. current: currentFullUrl,
  226. urls: [currentFullUrl]
  227. });
  228. } else if (currentFileName) {
  229. const fullUrl = baseUrl + currentFileName;
  230. console.log('预览单张兽医图片(拼接后):', [fullUrl]);
  231. wx.previewImage({
  232. current: fullUrl,
  233. urls: [fullUrl]
  234. });
  235. } else {
  236. wx.showToast({
  237. title: '图片预览失败',
  238. icon: 'none'
  239. });
  240. }
  241. }
  242. });