uni-ai-msg.vue 9.8 KB
Newer Older
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
2
	<view class="msg-item" v-if="!msg.isDelete">
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
3 4 5 6 7
		<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.2.2  
DCloud_JSON 已提交
14
				<view v-else>{{msgContent}}</view>
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
15
				<view class="menu-box" :class='{"menu-box-ai":msg.isAi}'>
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
16
          <uni-icons v-if="isLastMsg && msg.isAi" title="换一个答案" @click="changeAnswer" color="#ccc" class="pointer change-answer" type="reload" size="16"></uni-icons>
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
17
					<view @click="showMoreMenu = !showMoreMenu" class="more-icon-box">
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
18
            <uni-icons class="more-icon pointer" size="12px" type="more-filled" color="#ccc"></uni-icons>
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
19
					</view>
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
20 21 22 23 24 25
					<template v-if="showMoreMenu">
						<view class="more-menu">
							<view @click="copy" title="复制" class="copy-icon pointer">
								<view class="copy-icon-a"></view>
								<view class="copy-icon-b"></view>
							</view>
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
26
							<uni-icons class="remove-msg pointer" @click="removeMsg" type="trash" size="20" color="#ccc"></uni-icons>
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
27 28 29
						</view>
						<view class="more-menu-mask" @click="showMoreMenu = false"></view>
					</template>
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
30 31 32 33
				</view>
			</view>
		</view>
	</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
34 35
</template>

DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
36 37 38 39 40 41 42 43 44
<script>
	// 引入配置文件
	import config from '@/config.js';

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

DCloud_JSON's avatar
DCloud_JSON 已提交
45 46 47 48 49 50 51
	// 引入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 已提交
52
	import parseHtml from '@/lib/html-parser.js';
53 54 55

	// console.log('hljs--',hljs);
	// console.log('hljs--',hljs.getLanguage('js'));
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
56 57 58 59
	
	// 为复制代码功能保留代码内容
	let codeDataList = []
	
DCloud_JSON's avatar
DCloud_JSON 已提交
60
	// 初始化 MarkdownIt库
61
	const markdownIt = MarkdownIt({
DCloud_JSON's avatar
DCloud_JSON 已提交
62
		// 在源码中启用 HTML 标签
63
		html: true,
DCloud_JSON's avatar
DCloud_JSON 已提交
64
		// 如果结果以 <pre ... 开头内部包装器则会跳过
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
65
		highlight: function(str, lang) {
66 67 68
			// if (lang && hljs.getLanguage(lang)) {
			// 	console.error('lang', lang)
			// 	try {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
69
			// 		return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;display: block;"><code>' +
70 71 72 73
			// 			hljs.highlight('lang', str, true).value +
			// 			'</code></pre>';
			// 	} catch (__) {}
			// }
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
74 75
			// 经过highlight.js处理后的html
			let preCode = ""
76
			try {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
77 78 79 80 81
				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 已提交
82
			
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
83 84 85 86 87 88 89 90 91 92 93
			
			// 以换行进行分割
			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 已提交
94
			html = '<ol style="padding: 0px 30px;">' + html + '</ol>'
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
95
			
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
96 97
			// 代码复制功能
			codeDataList.push(str)
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
98 99 100 101 102 103 104 105 106
			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 已提交
107 108
		}
	})
109

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

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

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

				// #ifdef APP-NVUE
				return parseHtml(htmlString)
				// #endif
182 183
			}
		},
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
184
		methods: {
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
185
			trOnclick(e){
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
186
				console.log(e);
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
187
				let {attrs} = e.detail.node
DCloud_JSON's avatar
1.0.20  
DCloud_JSON 已提交
188
				console.log({attrs});
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
189 190
				let {"code-data-index":codeDataIndex,"class":className} = attrs
				if(className == 'copy-btn'){
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
191
					// console.log('codeDataList[codeDataIndex]',codeDataList[codeDataIndex]);
DCloud_JSON's avatar
1.0.18  
DCloud_JSON 已提交
192 193 194 195 196 197 198 199 200 201 202
					uni.setClipboardData({
						data:codeDataList[codeDataIndex],
						showToast:false,
						success() {
							uni.showToast({
								title: '复制成功',
								icon: 'none'
							});
						}
					})
				}
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
203 204 205 206
			},
			changeAnswer(){
				this.$emit('changeAnswer')
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
207
			// 复制文本内容到系统剪切板
208 209
			copy() {
				uni.setClipboardData({
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
210
					data: this.msgContent,
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
211 212 213 214 215 216 217
					showToast: false,
					success() {
						uni.showToast({
							title: '复制成功',
							icon: 'none'
						});
					}
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
218 219 220 221 222
				})
				this.showMoreMenu = false
			},
			// 删除消息
			removeMsg(){
DCloud_JSON's avatar
1.2.0  
DCloud_JSON 已提交
223
				this.$emit('removeMsg')
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
224
				this.showMoreMenu = false
DCloud_JSON's avatar
1.2.2  
DCloud_JSON 已提交
225
			}
226
		}
DCloud_JSON's avatar
DCloud_JSON 已提交
227 228 229
	}
</script>

DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
230 231 232 233 234 235 236 237 238 239 240 241
<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 已提交
242
		flex-direction: column;
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
	}

	.msg-item {
		position: relative;
		width: 750rpx;
		flex-direction: column;
		padding: 0 15px;
		padding-bottom: 15px;
	}
	.avatar {
		width: 40px;
		height: 40px;
		border-radius: 2px;
DCloud_JSON's avatar
1.0.21  
DCloud_JSON 已提交
256 257
	}

DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270
	.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 已提交
271 272
	.content {
		position: relative;
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
273
		/* #ifndef APP-NVUE */
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
274
		max-width: calc(85% - 45px);
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
275 276 277 278 279 280 281 282 283
		/* #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;
DCloud_JSON's avatar
1.2.0  
DCloud_JSON 已提交
284 285
		/* #endif */
		flex-direction: column;
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
286 287
	}
	
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
288
	.menu-box {
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
289
		position: absolute;
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
290
		left: -18px;
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
291 292 293 294 295
		bottom: 0;
		width: 20px;
		flex-direction: column;
		height: 40px;
		justify-content: flex-end;
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
296 297
    /* #ifndef APP-NVUE */
    /* #endif */
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
298 299
	}
	
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
300 301 302 303 304 305
	.menu-box-ai {
		left: auto;
		right: -20px;
	}
	
	.change-answer {
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
306
		margin-bottom: 5px;
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
307
		position: relative;
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
308
    transform: rotate(90deg);
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
309
	}
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
310 311 312 313 314 315 316 317 318
  
  /* #ifdef H5 */
  .uni-icons {
    opacity: 0.6;
  }
  .uni-icons:hover{
    opacity: 1;
  }
  /* #endif */
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
319 320 321 322 323 324
	
	.pointer {
		cursor: pointer;
	}
	
	.pointer:hover {
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
325 326 327
		color: #BBB;
	}
	
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
328 329 330
	.more-icon-box {
		justify-content: center;
		overflow: hidden;
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
331 332
	}
	
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
333 334
	.more-icon {
		color: #d4d4d4;
DCloud_JSON's avatar
1.2.8  
DCloud_JSON 已提交
335 336
		transform: rotate(90deg);
    padding-top: 2px;
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
337
		position: relative;
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
		font-size: 16px;
		z-index: 999;
	}
	
	.more-menu {
		position: absolute;
		bottom:-10px;
		left:-30px;
		flex-direction: column;
		justify-content: space-around;
		width: 30px;
		padding: 2px 5px;
		height: 60px;
		z-index: 999;
		background-color: #FFF;
		box-shadow: 0 0 20px #eee;
		border-radius: 3px;
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
355 356
	}
	
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	.more-menu-mask {
		position: fixed;
		width: 100vw;
		height: 100vh;
		top: 0;
		left: 0;
		z-index: 998;
	}
	
	.menu-box-ai .more-menu {
		left:auto;
		right: -32px;
	}
	
	.more-menu .pointer{
		width: 20px;
	}
	
	.copy-icon{
		position: relative;
		height: 25px;
		width: 20px;
	}
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
380 381 382 383 384 385 386
	.copy-icon-a,
	.copy-icon-b {
		position: absolute;
		border: 1.5px solid #d4d4d4;
		width: 10px;
		height: 12px;
		background-color: #FFF;
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
387 388
		left: 3px;
		top: 4px;
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
389 390
		border-radius: 3px;
	}
DCloud_JSON's avatar
1.0.23  
DCloud_JSON 已提交
391 392 393 394
	.copy-icon-b {
		top: 8px;
		left: 6px;
	}
DCloud_JSON's avatar
1.0.19  
DCloud_JSON 已提交
395 396 397 398
	.copy-icon:hover .copy-icon-a,
	.copy-icon:hover .copy-icon-b,{
		border-color:#bbb;
	}
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

	/* #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 已提交
431 432 433 434 435 436 437
	}
	
	/* #ifdef H5 */
	a.copy-btn:hover {
	    color: #259939;
	}
	/* #endif */
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
438 439


DCloud_JSON's avatar
DCloud_JSON 已提交
440 441
	/* #ifndef VUE3 && APP-PLUS */
	@import "@/components/uni-ai-msg/uni-ai-msg.scss";
DCloud_JSON's avatar
1.0.17  
DCloud_JSON 已提交
442
	/* #endif */
DCloud_JSON's avatar
DCloud_JSON 已提交
443
</style>