index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <scroll-view scroll-y="true" class="scroll-view">
  3. <view class="list-scroll-view">
  4. <view class="content">
  5. <view class="list" v-for="(item, index) in infoFields" :key="index">
  6. <view v-if="item.is_file" @tap.stop="toFile(data[item.model])" class="title">
  7. <view v-if="data[item.model].length>0">
  8. <text>{{item.label}}:</text>{{data[item.model][0].name||'暂无'}}
  9. </view>
  10. </view>
  11. <view v-else-if="item.is_image" @tap.stop="toView(0,data[item.model])" class="title">
  12. <view v-if="data[item.model].length>0">
  13. <text>{{item.label}}:</text>
  14. <image class="image" :src="data[item.model][0].url"></image>
  15. </view>
  16. </view>
  17. <view v-else-if="item.is_editor" class="title">
  18. <text>{{item.label}}:</text>
  19. <rich-text :nodes="formatRichText(data[item.model])"></rich-text>
  20. </view>
  21. <view v-else-if="item.is_show" class="title">
  22. <text>{{item.label}}:</text>
  23. {{getDict(data[item.model]||'暂无')}}
  24. </view>
  25. <view v-else class="title"><text>{{item.label}}:</text>{{data[item.model]||'暂无'}}</view>
  26. </view>
  27. </view>
  28. </view>
  29. </scroll-view>
  30. </template>
  31. <script>
  32. export default {
  33. props: {
  34. infoFields: {
  35. type: Array,
  36. default: []
  37. },
  38. data: {
  39. type: Object,
  40. default: {}
  41. },
  42. showList: {
  43. type: Array,
  44. default: []
  45. },
  46. },
  47. data() {
  48. return {};
  49. },
  50. methods: {
  51. // 查询字典表
  52. getDict(e) {
  53. const that = this;
  54. if (!e) return '暂无'
  55. let data = that.showList.find((i) => i.dict_value == e);
  56. if (data) return data.dict_label;
  57. else return e;
  58. },
  59. // 处理富文本
  60. formatRichText(html) {
  61. if (html) {
  62. // 富文本内容格式化
  63. return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
  64. // 查找所有的 img 元素
  65. return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
  66. '')
  67. // 删除找到的所有 img 元素中的 style 属性
  68. }).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性,并设置宽度为 100%
  69. }
  70. },
  71. // 查看文件
  72. toFile(item) {
  73. const that = this;
  74. if (!item) return;
  75. let serverUrl = that.$config.serverFile;
  76. let url = item[0].url
  77. // 判断前面是否有http
  78. const http = url.lastIndexOf("http");
  79. if (http == -1) url = serverUrl + url
  80. //获取最后一个.的位置
  81. const arr = url.lastIndexOf(".");
  82. //获取后缀
  83. const typeName = url.substr(arr + 1);
  84. //支持预览的文件类型
  85. //微信小程序
  86. let fileType = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'];
  87. uni.showLoading({
  88. title: '加载中',
  89. mask: true
  90. })
  91. //下载文件资源到本地
  92. uni.downloadFile({
  93. url: url,
  94. success: function(res) {
  95. uni.hideLoading();
  96. var filePath = res.tempFilePath;
  97. if (!fileType.includes(typeName)) {
  98. return false;
  99. }
  100. uni.showLoading({
  101. title: '正在打开',
  102. mask: true
  103. })
  104. // 新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
  105. uni.openDocument({
  106. filePath: filePath,
  107. fileType: typeName, // 文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
  108. success: res => {
  109. uni.hideLoading();
  110. console.log('打开文档成功', res);
  111. },
  112. fail: openError => {
  113. uni.hideLoading();
  114. console.log('fail:' + JSON.stringify(openError));
  115. }
  116. });
  117. },
  118. fail: function(err) {
  119. uni.hideLoading();
  120. console.log('fail:' + JSON.stringify(err));
  121. }
  122. });
  123. },
  124. // 图片预览
  125. toView(index, e) {
  126. const that = this;
  127. if (!e) return;
  128. uni.previewImage({
  129. current: index,
  130. urls: [e[0].url]
  131. })
  132. }
  133. }
  134. }
  135. </script>
  136. <style lang="scss">
  137. .content {
  138. max-height: 30vh;
  139. .list {
  140. padding: 1vw 2vw;
  141. .title {
  142. font-size: var(--font14Size);
  143. text {
  144. font-size: var(--font12Size);
  145. color: var(--f85Color);
  146. }
  147. .image {
  148. width: 20vw;
  149. height: 20vw;
  150. }
  151. }
  152. }
  153. }
  154. </style>