Front-end processing of encoding and decoding of emoji expressions

solution:

The encoding of emoji is in hexadecimal, just transcode the emoji into octal and store it

//把utf16的emoji表情字符进行转码成八进制的字符
function utf16toEntities(str) {
    
    
    var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则  
    return str.replace(patt, function (char) {
    
    
        var H, L, code;
        if (char.length === 2) {
    
    
            H = char.charCodeAt(0); // 取出高位  
            L = char.charCodeAt(1); // 取出低位  
            code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法  
            return "&#" + code + ";";
        } else {
    
    
            return char;
        }
    });
}
 
//将编码后的八进制的emoji表情重新解码成十六进制的表情字符
function entitiesToUtf16(str) {
    
    
    return str.replace(/&#(\d+);/g, function (match, dec) {
    
    
        let H = Math.floor((dec - 0x10000) / 0x400) + 0xD800;
        let L = Math.floor(dec - 0x10000) % 0x400 + 0xDC00;
        return String.fromCharCode(H, L);
    });
}
WeChat example:

1. Create a new custom js file (base64.js)
2. Add the following code to the custom js file (base64.js)
3. Introduce the custom js file (base64.js) to the page that needs to be used

1. Create a new custom js file (base64.js)

Insert picture description here

2. Add the following code in the custom js file (base64.js)

//把utf16的emoji表情字符进行转码成八进制的字符
function utf16toEntities(str) {
    
    
    var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则  
    return str.replace(patt, function(char) {
    
    
        var H, L, code;
        if (char.length === 2) {
    
    
            H = char.charCodeAt(0); // 取出高位  
            L = char.charCodeAt(1); // 取出低位  
            code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法  
            return "&#" + code + ";";
        } else {
    
    
            return char;
        }
    });
}

//将编码后的八进制的emoji表情重新解码成十六进制的表情字符
function entitiesToUtf16(str) {
    
    
    return str.replace(/&#(\d+);/g, function(match, dec) {
    
    
        let H = Math.floor((dec - 0x10000) / 0x400) + 0xD800;
        let L = Math.floor(dec - 0x10000) % 0x400 + 0xDC00;
        return String.fromCharCode(H, L);
    });
}

module.exports = {
    
    
    utf16toEntities: utf16toEntities,
    entitiesToUtf16: entitiesToUtf16
}

3. Introduce a custom js file (base64.js) in the page that needs to be used

import base64Js from '../../utils/base64.js';

Page({
    
    
    /**
     * 页面的初始数据
     */
    data: {
    
    
    	userInfo: {
    
    }	// 用户信息
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
    
    
    	request.get('xxxx', {
    
    xxx}).then(res => {
    
    
    		// 调用 entitiesToUtf16 对 niki 进行解码
            res.userInfo.niki = base64Js.entitiesToUtf16(res.userInfo.niki);
	        ........
	        ........
	    }).catch(e => {
    
    
	        xxxxx
	    })
    },
    saveUserInfo(userInfo){
    
    
    	.......
    	// 调用 utf16toEntities 对 niki 进行编码
    	userInfo.niki = base64Js.utf16toEntities(userInfo.niki);
		.......
    }

Guess you like

Origin blog.csdn.net/qq_25992675/article/details/106384197