公众号开发需要注意的几个小问题

背景:
上一篇博客回忆了下公众号开发的基本流程,这篇博客记录下遇到的几个小问题吧。ps:实际开发当然不止这点问题。。


1.通过code获取openid时报错

{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WiCbHFyFe-A5w7CA ]"}

经检测,发现code只能用一次,获取之后直接将其作为参数调用获取openid接口,中途不要将code附带在其他url里跳转!否则code会失效,即报上述错误!


2.通过openid查询用户信息时报错

{"errcode":40003,"errmsg":"invalid openid hint: [ADFNBA0125adf7]"}

经检测,发现同事们本地开发所用的个人测试号对应的access_token都是不同的,但存入redis是同一个database,会互相覆盖,导致我的测试号的openid与access_token(被同事覆盖)对不上!
而且不同的本地测试号对应同一个用户的openid是不一样的!

解决办法: 每个人本地开发选择不同的redis database,确保access_token和appid、secret、openid都要保持一致!


3.数据库存储微信昵称时emoji表情问题

数据库以及JDBC默认的字符集都是utf8,在存储emoji表情时会报错:

Incorrect string value: '\xF0\x9F\xA4\xA9' for column 'nickname'

原因是utf8不支持4字节的emoji表情

具体如下:

utf8utf8mb4具有相同存储特性:相同的代码值,相同的编码,相同的长度。不过utf8mb4扩展到一个字符最多可有4位元,所以能支持更多的位元集。utf8mb4不只兼容utf8,还能比utf8能展示更新的字符。将编码改为utf8mb4外不需要做其他转换。

为了要跟国际接轨,原本的utf8编码在存储某些国家的文字(或是罕见字)已经不敷使用,因此在mysql5.5.3版以上,可以开始使用4-Byte UTF-8 Unicode的编码方式。

Mysql查看版本方式:

select version();

Mysql在5.5.3版本之后增加了utf8mb4字符编码,mb4即most bytes 4。简单说utf8mb4是utf8的超集并完全兼容utf8,能够用四个字元存储更多的字符。

utf8mb4可以向下兼容utf8,而且比utf8可以表示更多的字符。此外,将编码改为utf8mb4不需 要其他转换。

解决办法:

1.修改mysql配置文件my.cnf,同时将库表的默认字符集都改为utf8mb4;

default-character-set=utf8mb4

同时修改项目的JDBC连接配置,增加ConnectionInitSqls属性(配置SQL语句的集合,在首次创建物理连接时将使用这些语句初始化物理连接。这些语句仅在配置的连接工厂创建连接时执行一次。),由于我们SpringBoot项目采用的是druid数据源无法在application.yml中直接配置,需要自定义数据源配置类

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.StringTokenizer;

@Configuration
public class DruidDBConfig {
    private Logger logger = LoggerFactory.getLogger(DruidDBConfig.class);

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.maxWait}")
    private int maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private boolean testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private boolean testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private boolean testOnReturn;

    @Value("${spring.datasource.poolPreparedStatements}")
    private boolean poolPreparedStatements;

    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Value("{spring.datasource.connectionProperties}")
    private String connectionProperties;

    @Bean     //声明其为Bean实例
    @Primary  //在同样的DataSource中,首先使用被标注的DataSource
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();

        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);

        //configuration
        String connectionInitSqls = "SET NAMES utf8mb4";
        StringTokenizer tokenizer = new StringTokenizer(connectionInitSqls, ";");
        datasource.setConnectionInitSqls(Collections.list(tokenizer));//重点设置该参数
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        datasource.setPoolPreparedStatements(poolPreparedStatements);
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter", e);
        }
        datasource.setConnectionProperties(connectionProperties);

        return datasource;
    }
}

2.手动Base64编解码

这个是代码层面解决,不用更改数据库相关配置;个人建议如果业务库改动不方便可以采用这种办法,如果能把库utf8改为utf8mb4尽量修改,因为随着网络时代字符的扩展,utf8可能不够用,改为utf8mb4能增加健壮性;

发布了73 篇原创文章 · 获赞 373 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/Abysscarry/article/details/89071667