金诚优选前端代码
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.

193 lines
4.1 KiB

  1. <!--**
  2. * forked fromhttps://github.com/F-loat/mpvue-wxParse
  3. *
  4. * github地址: https://github.com/dcloudio/uParse
  5. *
  6. * for: uni-app框架下 富文本解析
  7. *
  8. * 优化 by gaoyia@qq.com https://github.com/gaoyia/parse
  9. */-->
  10. <template>
  11. <!--基础元素-->
  12. <div class="wxParse" :class="className" :style="'user-select:' + userSelect">
  13. <block v-for="(node, index) of nodes" :key="index" v-if="!loading" ><wxParseTemplate :node="node" /></block>
  14. </div>
  15. </template>
  16. <script>
  17. import HtmlToJson from './libs/html2json';
  18. import wxParseTemplate from './components/wxParseTemplate0';
  19. export default {
  20. name: 'wxParse',
  21. props: {
  22. // user-select:none;
  23. userSelect:{
  24. type:String,
  25. default:'text'//none |text| all | element
  26. },
  27. imgOptions:{
  28. type:[Object,Boolean],
  29. default:function(){
  30. return {
  31. loop: false,
  32. indicator:'number',
  33. longPressActions:false
  34. // longPressActions: {
  35. // itemList: ['发送给朋友', '保存图片', '收藏'],
  36. // success: function (res) {
  37. // console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
  38. // },
  39. // fail: function (res) {
  40. // console.log(res.errMsg);
  41. // }
  42. // }
  43. // }
  44. }
  45. }
  46. },
  47. loading: {
  48. type: Boolean,
  49. default: false
  50. },
  51. className: {
  52. type: String,
  53. default: ''
  54. },
  55. content: {
  56. type: String,
  57. default: ''
  58. },
  59. noData: {
  60. type: String,
  61. default: '<div style="color: red;">数据不能为空</div>'
  62. },
  63. startHandler: {
  64. type: Function,
  65. default() {
  66. return node => {
  67. node.attr.class = null;
  68. node.attr.style = null;
  69. };
  70. }
  71. },
  72. endHandler: {
  73. type: Function,
  74. default: null
  75. },
  76. charsHandler: {
  77. type: Function,
  78. default: null
  79. },
  80. imageProp: {
  81. type: Object,
  82. default() {
  83. return {
  84. mode: 'aspectFit',
  85. padding: 0,
  86. lazyLoad: false,
  87. domain: ''
  88. };
  89. }
  90. }
  91. },
  92. components: {
  93. wxParseTemplate
  94. },
  95. data() {
  96. return {
  97. nodes:{},
  98. imageUrls: [],
  99. wxParseWidth:{
  100. value:0
  101. }
  102. };
  103. },
  104. computed: {
  105. },
  106. mounted() {
  107. let that = this
  108. this.getWidth().then(function(data){
  109. that.wxParseWidth.value = data;
  110. })
  111. this.setHtml()
  112. },
  113. methods: {
  114. setHtml(){
  115. let { content, noData, imageProp, startHandler, endHandler, charsHandler } = this;
  116. let parseData = content || noData;
  117. let customHandler = {
  118. start: startHandler,
  119. end: endHandler,
  120. chars: charsHandler
  121. };
  122. let results = HtmlToJson(parseData, customHandler, imageProp, this);
  123. this.imageUrls = results.imageUrls;
  124. this.nodes = results.nodes;
  125. },
  126. getWidth() {
  127. return new Promise((res, rej) => {
  128. // #ifndef MP-ALIPAY || MP-BAIDU
  129. uni.createSelectorQuery()
  130. .in(this)
  131. .select('.wxParse')
  132. .fields(
  133. {
  134. size: true,
  135. scrollOffset: true
  136. },
  137. data => {
  138. res(data.width);
  139. }
  140. ).exec();
  141. // #endif
  142. // #ifdef MP-BAIDU
  143. swan.createSelectorQuery().select('.wxParse').boundingClientRect(function(rect){
  144. rect[0].width
  145. }).exec()
  146. // #endif
  147. // #ifdef MP-ALIPAY
  148. my.createSelectorQuery()
  149. .select('.wxParse')
  150. .boundingClientRect().exec((ret) => {
  151. res(ret[0].width);
  152. });
  153. // #endif
  154. });
  155. },
  156. navigate(href, $event) {
  157. this.$emit('navigate', href, $event);
  158. },
  159. preview(src, $event) {
  160. if (!this.imageUrls.length||typeof this.imgOptions==='boolean'){
  161. }else{
  162. uni.previewImage({
  163. current: src,
  164. urls: this.imageUrls,
  165. loop: this.imgOptions.loop,
  166. indicator: this.imgOptions.indicator,
  167. longPressActions: this.imgOptions.longPressActions
  168. });
  169. }
  170. this.$emit('preview', src, $event);
  171. },
  172. removeImageUrl(src) {
  173. const { imageUrls } = this;
  174. imageUrls.splice(imageUrls.indexOf(src), 1);
  175. }
  176. },
  177. // 父组件中提供
  178. provide() {
  179. return {
  180. parseWidth: this.wxParseWidth
  181. // 提示:provide 和 inject 绑定并不是可响应的。这是刻意为之的。然而,如果你传入了一个可监听的对象,那么其对象的属性还是可响应的。
  182. };
  183. },
  184. watch: {
  185. content() {
  186. this.setHtml()
  187. }
  188. }
  189. };
  190. </script>