url.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <view :class="{'self':self}">
  3. <block v-if="messages.length==1 && messages[0].type=='url'">
  4. <view v-if="showinfo" class="des" @tap="click_url(messages[0].content)">
  5. <view class="des_title">{{title}}</view>
  6. <view class="des_content" v-if="description!=''">{{description}}</view>
  7. <view class="des_url">
  8. <uni-icons type='chatboxes' class="icon"></uni-icons>
  9. {{messages[0].content}}
  10. </view>
  11. </view>
  12. <view class="url" v-else @tap="click_url(messages[0].content)">
  13. {{messages[0].content}}
  14. </view>
  15. </block>
  16. <block v-else>
  17. <block v-for="(m,index) in messages" :key="index" >
  18. <view v-if="m.type=='url'" class="url" @tap="click_url(m.content)">
  19. {{m.content}}
  20. </view>
  21. <block v-else >
  22. <rich-text :nodes="m.content" class="text"></rich-text>
  23. </block>
  24. </block>
  25. </block>
  26. </view>
  27. </template>
  28. <script>
  29. import uniIcons from './uni-icons/uni-icons.vue'
  30. export default {
  31. components: {
  32. uniIcons
  33. },
  34. props: {
  35. content: {
  36. required: true,
  37. },
  38. self:{
  39. default:0
  40. }
  41. },
  42. data() {
  43. return {
  44. res: {},
  45. messages:[],
  46. showinfo:false,
  47. title:'',
  48. description:'',
  49. }
  50. },
  51. methods: {
  52. showdata(){
  53. for(var i=0;i<this.content.urls.length;i++){
  54. var url=this.content.urls[i];
  55. try{
  56. var content=this.content.content.replace(url,'^'+url+"^") ;
  57. }catch(e){
  58. //TODO handle the exception
  59. // console.log(this.content.content.content)
  60. var content=this.content.content.content.replace(url,'^'+url) ;
  61. }
  62. }
  63. var arr=content.split('^');
  64. var reg=/(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9\/][-a-zA-Z0-9\/]{0,62})+(:\d+)*(\/\w+\.\w+)*$/g
  65. // console.log(arr)
  66. for(var i=0;i<arr.length;i++){
  67. var isin=0;
  68. for(var j=0;j<this.content.urls.length;j++){
  69. // console.log(arr[i],this.content.urls[j])
  70. if(arr[i]==this.content.urls[j]) isin=1;
  71. }
  72. if(arr[i]!=''){
  73. if(isin==1){
  74. var data={type:'url',content:arr[i]}
  75. }
  76. else{
  77. var data={type:'text',content:arr[i]}
  78. }
  79. this.messages.push(data);
  80. }
  81. }
  82. // #ifdef APP-PLUS
  83. if(this.messages.length==1 && this.messages[0].type=='url'){
  84. if(this.messages[0].content.indexOf('http')<=-1) this.messages[0].content='http://'+this.messages[0].content;
  85. var url=this.messages[0].content;
  86. // console.log(url);
  87. uni.request({
  88. url:url,
  89. method:'GET',
  90. success: (res) => {
  91. this.gethtmlinfo(res.data);
  92. }
  93. })
  94. }
  95. // #endif
  96. // console.log( this.messages);
  97. //this.res=this.content.content;
  98. },
  99. gethtmlinfo(data){
  100. var reg = /<title(.*)>(.*)<\/title/;
  101. var match=reg.exec(data);
  102. var title=match[2];
  103. if(title!=undefined && title.length>1){
  104. this.showinfo=true;
  105. this.title=title;
  106. this.description="";
  107. }
  108. var reg = /<meta(\s)name=[\'\"]description[\'\"](\s)content=[\'\"]?([^\'\"]*)[\'\"]?/i;
  109. var match=reg.exec(data);
  110. try{
  111. var content=match[3];
  112. if(title!=undefined && title.length>1 && content!=undefined && content.length>1 ){
  113. this.showinfo=true;
  114. this.title=title;
  115. this.description=content;
  116. }
  117. }catch(e){
  118. //TODO handle the exception
  119. //console.log(title);
  120. if(title!=undefined && title.length>1){
  121. this.showinfo=true;
  122. this.title=title;
  123. this.description="";
  124. }
  125. }
  126. },
  127. click_url(url){
  128. this.$emit('clickurl',url);
  129. //console.log(url)
  130. },
  131. },
  132. created() {
  133. //console.log(this.content)
  134. this.showdata();
  135. },
  136. onLoad() {
  137. }
  138. }
  139. </script>
  140. <style>
  141. .text ,.url {
  142. display:inline-block ;
  143. }
  144. .url{
  145. color: #2319DC;
  146. text-decoration: underline;
  147. }
  148. .des{}
  149. .des_content{
  150. font-size: 12px;
  151. line-height:18px;
  152. overflow : hidden;
  153. text-overflow: ellipsis;
  154. display: -webkit-box;
  155. -webkit-line-clamp: 3;
  156. -webkit-box-orient: vertical;
  157. }
  158. .des_url{
  159. font-size: 12px;
  160. color: #666;
  161. }
  162. .icon{
  163. font-size: 12px !important;
  164. color: #666 !important;
  165. }
  166. .self .des_url{
  167. color: #eee;
  168. }
  169. .self .icon{
  170. color: #eee !important;
  171. }
  172. </style>