uni-ai-msg.vue 6.9 KB
Newer Older
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<template>
	<view class="msg-item">
		<view class="create_time-box">
			<uni-dateformat class="create_time" :date="msg.create_time" format="MM/dd hh:mm:ss"></uni-dateformat>
		</view>
		<view :class="{reverse:!msg.isAi}">
			<view class="userInfo">
				<image class="avatar" :src="msg.isAi?'../../static/uni-ai.png':'../../static/avatar.png'" mode="widthFix"></image>
			</view>
			<view class="content">
				
				<view class="rich-text-box" :class="{'show-cursor':showCursor}" ref="rich-text-box">
					<rich-text v-if="nodes&&nodes.length" space="nbsp" :nodes="nodes"></rich-text>
				
					<!-- #ifdef H5 -->
					<view class="copy-box" :style="{left,top}">
						<text class="copy" @click="copy">复制</text>
					</view>
					<!-- #endif -->
				</view>
				
				
				<view v-if="msgIndex == msgIndexList.length-1 && adpid && msg.insufficientScore">
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
24 25 26 27 28
					<view style="flex-direction: column;color: red;">
						<view>默认不启用广告组件(被注释),如需使用,请"去掉注释"</view>
						<view>位置:/components/uni-ai-msg/uni-ai-msg.vue 第28行,或全局搜索 uni-ad-rewarded-video</view>
					</view>
					<!-- <uni-ad-rewarded-video :adpid="adpid" @onAdClose="onAdClose"></uni-ad-rewarded-video> -->
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
29 30 31 32 33 34 35 36
				</view>
			</view>
			<uni-icons v-if="msgIndex == msgIndexList.length-1 && !msg.isAi && msg.state != 100 && msgStateIcon(msg)"
				@click="msg.state == -100 ? retriesSendMsg() : ''" :color="msg.state===0?'#999':'#d22'"
				:type="msgStateIcon(msg)" class="msgStateIcon">
			</uni-icons>
		</view>
	</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
37 38
</template>

DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
39
<script>
DCloud_JSON's avatar
1.0.14  
DCloud_JSON 已提交
40 41 42
	// 引入配置文件
	import config from '@/config.js';
	
DCloud_JSON's avatar
1.0.13  
DCloud_JSON 已提交
43 44 45 46 47
	// 获取广告id
	const {
		adpid
	} = config
	
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
48 49
	import {msgList} from '@/pages/chat/msgList.js';
	
DCloud_JSON's avatar
DCloud_JSON 已提交
50 51 52 53 54 55 56 57
	// 引入markdown-it库
	import MarkdownIt from '@/lib/markdown-it.min.js';

	// hljs是由 Highlight.js 经兼容性修改后的文件,请勿直接升级。否则会造成uni-app-vue3-Android下有兼容问题
	import hljs from "@/lib/highlight/highlight-uni.min.js";

	// 引入html-parser.js库
	import parseHtml from '@/lib/html-parser.js'; 
58 59 60

	// console.log('hljs--',hljs);
	// console.log('hljs--',hljs.getLanguage('js'));
61

DCloud_JSON's avatar
DCloud_JSON 已提交
62
	// 初始化 MarkdownIt库
63
	const markdownIt = MarkdownIt({
DCloud_JSON's avatar
DCloud_JSON 已提交
64
		// 在源码中启用 HTML 标签
65
		html: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
66
		// 如果结果以 <pre ... 开头内部包装器则会跳过
DCloud_JSON's avatar
DCloud_JSON 已提交
67
		highlight: function(str, lang) {
68 69 70 71 72 73 74 75 76 77 78 79 80 81
			// if (lang && hljs.getLanguage(lang)) {
			// 	console.error('lang', lang)
			// 	try {
			// 		return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
			// 			hljs.highlight('lang', str, true).value +
			// 			'</code></pre>';
			// 	} catch (__) {}
			// }

			try {
				return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
					hljs.highlightAuto(str).value +
					'</code></pre>';
			} catch (__) {}
82

83 84
			return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;"><code>' +
				markdownIt.utils.escapeHtml(str) + '</code></pre>';
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
85 86 87
		}
	})
	
DCloud_JSON's avatar
DCloud_JSON 已提交
88 89 90
	export default {
		name: "msg",
		data() {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
91
			return {
DCloud_JSON's avatar
DCloud_JSON 已提交
92
				// 悬浮的复制按钮的左边距
93
				left: "-100px",
DCloud_JSON's avatar
DCloud_JSON 已提交
94
				// 悬浮的复制按钮的上边距
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
95 96 97 98
				top: "-100px",
				msg:{
					content:""
				},
DCloud_JSON's avatar
1.0.13  
DCloud_JSON 已提交
99 100
				msgIndexList:0,
				adpid
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
101
			};
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
102
		},
103 104 105 106 107
		mounted() {
			// #ifdef H5
			// web端限制不选中文字时出现系统右键菜单
			let richTextBox = this.$refs['rich-text-box']
			if (richTextBox) {
DCloud_JSON's avatar
DCloud_JSON 已提交
108
				// 监听鼠标右键事件
109
				richTextBox.$el.addEventListener('contextmenu', (e) => {
DCloud_JSON's avatar
DCloud_JSON 已提交
110
					// 判断是否选中了文字内容,如果没有则限制系统默认行为(禁止弹出右键菜单)
111
					if (!document.getSelection().toString()) {
112
						// console.log(e);
DCloud_JSON's avatar
DCloud_JSON 已提交
113
						// 设置悬浮的复制按钮的坐标值
114 115 116
						this.top = e.y + 'px'
						this.left = e.x + 'px'

117 118
						// console.log(e.x);
						// console.log(e.y);
DCloud_JSON's avatar
DCloud_JSON 已提交
119
						// 禁止系统默认行为(禁止弹出右键菜单)
120 121 122 123
						e.preventDefault()
					}
				})
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
124
			// 监听全局点击事件,隐藏悬浮的复制按钮的坐标
125 126 127 128
			document.addEventListener('click', () => {
				this.left = "-100px"
			})
			// #endif
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
129 130 131
		},
		created() {
			this.msg = msgList[this.msgIndex]
DCloud_JSON's avatar
DCloud_JSON 已提交
132 133
		},
		props: {
DCloud_JSON's avatar
DCloud_JSON 已提交
134
			// 是否显示鼠标闪烁的效果
135 136 137 138 139
			showCursor: {
				type: [Boolean, Number],
				default () {
					return false
				}
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
140 141 142 143 144 145
			},
			msgIndex:{
				type: Number,
				default () {
					return false
				}
146
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
147
		},
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
148 149 150 151
		computed: {
			md(){
				return this.msg.content
			},
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
152 153 154 155
			nodes() {
				let htmlString = ''
				// 修改转换结果的htmlString值 用于正确给界面增加鼠标闪烁的效果
				// 判断markdown中代码块标识符的数量是否为偶数
156
				if(this.md.split("```").length%2){
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
157 158 159
					htmlString = markdownIt.render(this.md + ' \n <span class="cursor">|</span>');
				}else{
					htmlString = markdownIt.render(this.md) + ' \n <span class="cursor">|</span>';
160 161
				}
				
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
162 163
				// #ifndef APP-NVUE
				return htmlString
164 165
				// #endif
				
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
166 167 168 169 170
				// nvue模式下将htmlString转成htmlArray,其他情况rich-text内部转
				// 注:本示例项目还没使用nvue编译
				
				// #ifdef APP-NVUE
				return parseHtml(htmlString)
171
				// #endif
172 173
			}
		},
DCloud_JSON's avatar
1.0.11  
DCloud_JSON 已提交
174
		methods: {
175
			// #ifdef H5
DCloud_JSON's avatar
DCloud_JSON 已提交
176
			// 复制文本内容到系统剪切板
177 178 179 180 181
			copy() {
				uni.setClipboardData({
					data: this.md,
					showToast: false,
				})
DCloud_JSON's avatar
DCloud_JSON 已提交
182
				// 设置悬浮的复制按钮的坐标值,使其隐藏
183 184 185
				this.left = "-100px"
			}
			// #endif
186
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
187 188 189
	}
</script>

DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
<style lang="scss">
	/* #ifndef APP-NVUE */
	view,
	textarea,
	button,
	.page {
		display: flex;
		box-sizing: border-box;
	}
	/* #endif */
	
	.userInfo {
		flex-direction: column;
	}
	
	.msg-item {
		position: relative;
		width: 750rpx;
		flex-direction: column;
		padding: 0 15px;
		padding-bottom: 15px;
	}
	
	.msgStateIcon {
		position: relative;
		top: 5px;
		right: 5px;
		align-self: center;
	}
	
	.avatar {
		width: 40px;
		height: 40px;
		border-radius: 2px;
	}
	
	.create_time {
		font-size: 12px;
		padding: 5px 0;
		padding-top: 0;
		color: #aaa;
		text-align: center;
		width: 750rpx;
		/* #ifdef MP */
		display: flex;
		/* #endif */
		justify-content: center;
	}
	
	.content {
		/* #ifndef APP-NVUE */
		max-width: 550rpx;
		/* #endif */
		background-color: #FFF;
		border-radius: 5px;
		padding: 12px 10px;
		margin-left: 10px;
		/* #ifndef APP-NVUE */
		word-break: break-all;
		user-select: text;
		cursor: text;
		/* #endif */
	}
	
	/* #ifndef APP-NVUE */
	.content {
		display: inline;
	}
	
	.content ::v-deep rich-text {
		max-width: 550rpx;
		overflow: auto;
	}
	
	/* #endif */
	
	/* #ifdef H5 */
	.content * {
		display: inline;
	}
	
	/* #endif */
	
	.reverse {
		flex-direction: row-reverse;
	}
	
	.reverse .content {
		margin-left: 0;
		margin-right: 10px;
	}
	
	.reverse-align {
		align-items: flex-end;
	}
DCloud_JSON's avatar
1.0.16  
DCloud_JSON 已提交
285 286 287 288 289
	
	.create_time-box {
		margin-top: 15px;
		justify-content: center;
	}
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
290 291

	
DCloud_JSON's avatar
DCloud_JSON 已提交
292 293
	/* #ifndef VUE3 && APP-PLUS */
	@import "@/components/uni-ai-msg/uni-ai-msg.scss";
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
294
	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
295
</style>