uni-ai-msg.vue 9.3 KB
Newer Older
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
1 2 3 4 5 6 7
<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">
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
8
				<image class="avatar" :src="msg.isAi?'../../static/uni-ai.png':'../../static/avatar.png'" mode="widthFix"></image>
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
9 10
			</view>
			<view class="content">
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
11
				<view v-if="msg.isAi" class="rich-text-box" :class="{'show-cursor':showCursor}" ref="rich-text-box">
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
12
					<rich-text v-if="nodes&&nodes.length" space="nbsp" :nodes="nodes" @itemclick="trOnclick"></rich-text>
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
13
				</view>
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
14 15 16 17
				<view v-else>
					{{msgContent}}
				</view>
				
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
				<view v-if="isLastMsg && adpid && msg.insufficientScore">
					<text style="color: red;">
						默认不启用广告组件(被注释),如需使用,请"去掉注释"(“重新运行”后生效)
						位置:/components/uni-ai-msg/uni-ai-msg.vue 第28行,或全局搜索 uni-ad-rewarded-video
					</text>
					<!-- <uni-ad-rewarded-video :adpid="adpid" @onAdClose="onAdClose"></uni-ad-rewarded-video> -->
				</view>
				<view v-if="msg.isAi" class="controller">
					<text v-if="isLastMsg" title="换一个答案" @click="changeAnswer" class="retry-icon"></text>
					<view @click="copy" title="复制" class="copy-icon">
						<view class="copy-icon-a"></view>
						<view class="copy-icon-b"></view>
					</view>
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
31 32 33 34 35 36 37 38
				</view>
			</view>
			<uni-icons v-if="isLastMsg && !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 已提交
39 40
</template>

DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53
<script>
	// 引入配置文件
	import config from '@/config.js';

	// 获取广告id
	const {
		adpid
	} = config

	import {
		msgList
	} from '@/pages/chat/msgList.js';

DCloud_JSON's avatar
DCloud_JSON 已提交
54 55 56 57 58 59 60
	// 引入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库
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
61
	import parseHtml from '@/lib/html-parser.js';
62 63 64

	// console.log('hljs--',hljs);
	// console.log('hljs--',hljs.getLanguage('js'));
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
65 66 67 68
	
	// 为复制代码功能保留代码内容
	let codeDataList = []
	
DCloud_JSON's avatar
DCloud_JSON 已提交
69
	// 初始化 MarkdownIt库
70
	const markdownIt = MarkdownIt({
DCloud_JSON's avatar
DCloud_JSON 已提交
71
		// 在源码中启用 HTML 标签
72
		html: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
73
		// 如果结果以 <pre ... 开头内部包装器则会跳过
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
74
		highlight: function(str, lang) {
75 76 77
			// if (lang && hljs.getLanguage(lang)) {
			// 	console.error('lang', lang)
			// 	try {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
78
			// 		return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;display: block;"><code>' +
79 80 81 82
			// 			hljs.highlight('lang', str, true).value +
			// 			'</code></pre>';
			// 	} catch (__) {}
			// }
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
83 84
			// 经过highlight.js处理后的html
			let preCode = ""
85
			try {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
86 87 88 89 90
				preCode = hljs.highlightAuto(str).value
			} catch (err) {
				// console.log('err',err);
				preCode = markdownIt.utils.escapeHtml(str);
			}
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
91
			
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
92 93 94 95 96 97 98 99 100 101 102
			
			// 以换行进行分割
			const lines = preCode.split(/\n/).slice(0, -1)
			// 添加自定义行号
			let html = lines.map((item, index) => {
				// 去掉空行
				if( item == ''){
					return ''
				}
				return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item +'</li>'
			}).join('')
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
103
			html = '<ol style="padding: 0px 30px;">' + html + '</ol>'
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
104
			
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
105 106
			// 代码复制功能
			codeDataList.push(str)
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
107 108 109 110 111 112 113 114 115
			let htmlCode = `<div style="background:#0d1117;margin-top: 5px;color: #888;padding:5px 0;border-radius: 5px;">`
				// #ifndef MP-WEIXIN
					htmlCode += `<div style="text-align: end;font-size: 12px;">`
						htmlCode += `${lang}<a class="copy-btn" code-data-index="${codeDataList.length - 1}" style="cursor: pointer;"> 复制代码 </a>`
					htmlCode += `</div>`
				// #endif
					htmlCode += `<pre class="hljs" style="padding:0 8px;margin-bottom:5px;overflow: auto;display: block;border-radius: 5px;"><code>${html}</code></pre>`;
				htmlCode += '</div>'
			return htmlCode
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
116 117
		}
	})
118

DCloud_JSON's avatar
DCloud_JSON 已提交
119 120 121
	export default {
		name: "msg",
		data() {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
122
			return {
DCloud_JSON's avatar
DCloud_JSON 已提交
123
				// 悬浮的复制按钮的左边距
124
				left: "-100px",
DCloud_JSON's avatar
DCloud_JSON 已提交
125
				// 悬浮的复制按钮的上边距
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
126 127 128 129 130
				top: "-100px",
				msg: {
					content: ""
				},
				msgIndexList: 0,
DCloud_JSON's avatar
1.0.13  
DCloud_JSON 已提交
131
				adpid
DCloud_JSON's avatar
1.0.0  
DCloud_JSON 已提交
132
			};
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
133
		},
134
		mounted() {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
135 136 137
		},
		created() {
			this.msg = msgList[this.msgIndex]
DCloud_JSON's avatar
DCloud_JSON 已提交
138 139
		},
		props: {
DCloud_JSON's avatar
DCloud_JSON 已提交
140
			// 是否显示鼠标闪烁的效果
141 142 143 144 145
			showCursor: {
				type: [Boolean, Number],
				default () {
					return false
				}
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
146 147
			},
			msgIndex: {
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
148 149 150 151
				type: Number,
				default () {
					return false
				}
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
152 153 154 155 156 157
			},
			isLastMsg: {
				type: Boolean,
				default () {
					return false
				}
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
158
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
159
		},
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
160
		computed: {
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
161
			msgContent() {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
162
				return this.msg.content
DCloud_JSON's avatar
1.0.12  
DCloud_JSON 已提交
163
			},
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
164 165 166 167
			nodes() {
				let htmlString = ''
				// 修改转换结果的htmlString值 用于正确给界面增加鼠标闪烁的效果
				// 判断markdown中代码块标识符的数量是否为偶数
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
168 169
				if (this.msgContent.split("```").length % 2) {
					htmlString = markdownIt.render(this.msgContent + '  <span class="cursor">|</span>');
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
170
				} else {
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
171
					htmlString = markdownIt.render(this.msgContent) + ' \n <span class="cursor">|</span>';
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
172 173 174 175 176 177 178 179 180 181 182 183
				}

				// #ifndef APP-NVUE
				return htmlString
				// #endif

				// nvue模式下将htmlString转成htmlArray,其他情况rich-text内部转
				// 注:本示例项目还没使用nvue编译

				// #ifdef APP-NVUE
				return parseHtml(htmlString)
				// #endif
184 185
			}
		},
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
186
		methods: {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
187 188 189 190 191
			// 根据消息状态返回对应的图标
			msgStateIcon(msg) {
				switch (msg.state) {
					case 0:
						//	发送中
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
192
						return ''
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
						break;
					case -100:
						//	发送失败
						return 'refresh-filled'
						break;
					case -200:
						//	禁止发送(内容不合法)
						return 'info-filled'
						break;
					default:
						// 默认不返回任何图标
						return false
						break;
				}
			},
			trOnclick(e){
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
209
				console.log(e);
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
210
				let {attrs} = e.detail.node
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
211
				console.log({attrs});
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
212 213
				let {"code-data-index":codeDataIndex,"class":className} = attrs
				if(className == 'copy-btn'){
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
214
					console.log('codeDataList[codeDataIndex]',codeDataList[codeDataIndex]);
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
215 216 217 218 219 220 221 222 223 224 225
					uni.setClipboardData({
						data:codeDataList[codeDataIndex],
						showToast:false,
						success() {
							uni.showToast({
								title: '复制成功',
								icon: 'none'
							});
						}
					})
				}
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
226 227 228 229 230 231
			},
			changeAnswer(){
				this.$emit('changeAnswer')
			},
			retriesSendMsg(){
				this.$emit('retriesSendMsg')
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
232
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
233
			// 复制文本内容到系统剪切板
234 235
			copy() {
				uni.setClipboardData({
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
236
					data: this.msgContent,
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
237 238 239 240 241 242 243
					showToast: false,
					success() {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						});
					}
244 245
				})
			}
246
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
247 248 249
	}
</script>

DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262
<style lang="scss">
	/* #ifndef APP-NVUE */
	view,
	textarea,
	button,
	.page {
		display: flex;
		box-sizing: border-box;
	}

	/* #endif */

	.userInfo {
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
263
		flex-direction: column;
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	}

	.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;
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
285 286
	}

DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
	.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;
	}

DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
300 301
	.content {
		position: relative;
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
302
		/* #ifndef APP-NVUE */
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
303
		max-width: calc(85% - 15px);
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
304 305 306 307 308 309 310 311 312 313
		/* #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 */
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	}
	
	.controller {
		position: absolute;
		right: -25px;
		bottom: 0;
		width: 20px;
		flex-direction: column;
		height: 40px;
		justify-content: flex-end;
	}
	
	.retry-icon {
		font-size: 26px;
		color: #d4d4d4;
		height: 25px;
		line-height: 15px;
		margin-bottom: 5px;
	}
	.retry-icon:hover {
		color: #BBB;
	}
	
	.retry-icon,.copy-icon {
		cursor: pointer;
	}
	
	.copy-icon {
		position: relative;
		height: 25px;
	}
	
	.copy-icon-a,
	.copy-icon-b {
		position: absolute;
		border: 1.5px solid #d4d4d4;
		width: 10px;
		height: 12px;
		background-color: #FFF;
		left: 2px;
		top: 2px;
		border-radius: 3px;
	}
	.copy-icon:hover .copy-icon-a,
	.copy-icon:hover .copy-icon-b,{
		border-color:#bbb;
	}
	.copy-icon-b {
		top: 5px;
		left: 5px;
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	}

	/* #ifndef APP-NVUE */
	.content ::v-deep rich-text {
		max-width: 100%;
		overflow: auto;
	}
	code .l:before {
		color: #516363;
		position: absolute;
		left: 15px;
		content: counter(step);
		counter-increment: step;
	}
	/* #endif */


	.reverse {
		flex-direction: row-reverse;
	}

	.reverse .content {
		margin-left: 0;
		margin-right: 10px;
	}

	.reverse-align {
		align-items: flex-end;
	}

	.create_time-box {
		margin-top: 15px;
		justify-content: center;
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
397 398 399 400 401 402 403
	}
	
	/* #ifdef H5 */
	a.copy-btn:hover {
	    color: #259939;
	}
	/* #endif */
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
404 405


DCloud_JSON's avatar
DCloud_JSON 已提交
406 407
	/* #ifndef VUE3 && APP-PLUS */
	@import "@/components/uni-ai-msg/uni-ai-msg.scss";
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
408
	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
409
</style>