mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.panzhihua.service_community.util;
 
import javax.annotation.Resource;
 
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.user.SysAppConfigVO;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.StringUtils;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
 
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: wexin
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-23 15:07
 **/
@Slf4j
@Component
public class WxMaConfiguration {
 
    @Resource
    private WxMaProperties properties;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
 
    @Resource
    private UserService userService;
 
 
    public WxMaService getMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppid());
        config.setSecret(properties.getSecret());
        config.setMsgDataFormat(properties.getMsgDataFormat());
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }
 
    public WxMaService getMaService(String appid) {
        WxRedisOps redisOps = new RedisTemplateWxRedisOps(stringRedisTemplate);
        WxMaRedisBetterConfigImpl config = new WxMaRedisBetterConfigImpl(redisOps, "access_token");
        if(StringUtils.isNotEmpty(appid)){
            R<SysAppConfigVO> r=userService.selectByAppid(appid);
            if(R.isOk(r)){
                SysAppConfigVO sysAppConfigVO= r.getData();
                config.setAppid(sysAppConfigVO.getAppId());
                config.setSecret(sysAppConfigVO.getSecret());
                config.setMsgDataFormat(properties.getMsgDataFormat());
            }
        }
        else{
            config.setAppid(properties.getAppid());
            config.setSecret(properties.getSecret());
            config.setMsgDataFormat(properties.getMsgDataFormat());
        }
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }
}