Jelajahi Sumber

微信表情路径改为配置形式

xsx 11 bulan lalu
induk
melakukan
705f005baa
3 mengubah file dengan 17 tambahan dan 14 penghapusan
  1. 8 1
      im-uniapp/.env.js
  2. 7 11
      im-uniapp/common/emotion.js
  3. 2 2
      im-uniapp/pages/chat/chat-box.vue

+ 8 - 1
im-uniapp/.env.js

@@ -1,12 +1,19 @@
 //设置环境(打包前修改此变量)
 const ENV = "DEV";
 const UNI_APP = {}
+
+UNI_APP.EMO_URL = "/static/emoji/";
+// #ifdef MP-WEIXIN
+// 微信小程序的本地表情包经常莫名失效,建议将表情放到服务器中
+// UNI_APP.EMO_URL = "https://www.boxim.online/emoji/";
+// #endif
+
 if(ENV=="DEV"){
 	UNI_APP.BASE_URL = "http://127.0.0.1:8888";
 	UNI_APP.WS_URL = "ws://127.0.0.1:8878/im";
 	// H5 走本地代理解决跨域问题
 	// #ifdef H5
-		UNI_APP.BASE_URL = "/api";
+	UNI_APP.BASE_URL = "/api";
 	// #endif
 }
 if(ENV=="PROD"){

+ 7 - 11
im-uniapp/common/emotion.js

@@ -1,3 +1,6 @@
+import UNI_APP from '@/.env.js'
+
+
 const emoTextList = ['憨笑', '媚眼', '开心', '坏笑', '可怜', '爱心', '笑哭', '拍手', '惊喜', '打气',
 	'大哭', '流泪', '饥饿', '难受', '健身', '示爱', '色色', '眨眼', '暴怒', '惊恐',
 	'思考', '头晕', '大吐', '酷笑', '翻滚', '享受', '鼻涕', '快乐', '雀跃', '微笑',
@@ -13,30 +16,23 @@ let containEmoji = (content) => {
 }
 
 let transform = (content, extClass) => {
-	return content.replace(regex, (emoText) => {
+	return content.replace(regex, (emoText)=>{
 		// 将匹配结果替换表情图片
 		let word = emoText.replace(/\#|\;/gi, '');
 		let idx = emoTextList.indexOf(word);
 		if (idx == -1) {
 			return emoText;
 		}
-		let path = textToPath(emoText, true);
+		let path = textToPath(emoText);
 		let img = `<img src="${path}" class="${extClass}"/>`;
 		return img;
 	});
 }
 
-let textToPath = (emoText, isRichText) => {
+let textToPath = (emoText) => {
 	let word = emoText.replace(/\#|\;/gi, '');
 	let idx = emoTextList.indexOf(word);
-	let path = `/static/emoji/${idx}.gif`;
-	// #ifdef MP-WEIXIN	
-	// 小程序的表情要去掉最前面"/"(但有的时候又不能去掉,十分奇怪)
-	if (isRichText) {
-		path = path.slice(1);
-	}
-	// #endif
-	return path;
+	return UNI_APP.EMO_URL + idx + ".gif";
 }
 
 export default {

+ 2 - 2
im-uniapp/pages/chat/chat-box.vue

@@ -94,7 +94,7 @@
 			<scroll-view v-if="chatTabBox === 'emo'" class="chat-emotion" scroll-y="true"
 				:style="{height: keyboardHeight+'px'}">
 				<view class="emotion-item-list">
-					<image class="emotion-item emoji-large" :title="emoText" :src="$emo.textToPath(emoText,false)"
+					<image class="emotion-item emoji-large" :title="emoText" :src="$emo.textToPath(emoText)"
 						v-for="(emoText, i) in $emo.emoTextList" :key="i" @click="selectEmoji(emoText)" mode="aspectFit"
 						lazy-load="true"></image>
 				</view>
@@ -370,7 +370,7 @@ export default {
 			}
 		},
 		selectEmoji(emoText) {
-			let path = this.$emo.textToPath(emoText, true)
+			let path = this.$emo.textToPath(emoText)
 			// 先把键盘禁用了,否则会重新弹出键盘
 			this.isReadOnly = true;
 			this.isEmpty = false;