Forráskód Böngészése

feat:微信开放接口对接;

hanchaolong 2 hete
szülő
commit
5da251cb5e

+ 44 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/system/config/WxMaConfig.java

@@ -0,0 +1,44 @@
+package com.ruoyi.system.config;
+
+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.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.PostConstruct;
+
+
+/**
+ * 微信小程序配置类
+ */
+@Slf4j
+@Configuration
+@ConditionalOnBean(WxMaProperties.class)
+@RequiredArgsConstructor
+public class WxMaConfig {
+    private final WxMaProperties properties;
+
+    @PostConstruct
+    public void init() {
+        log.info("WX miniapp service   initialized  successfully");
+    }
+
+    @Bean
+    public WxMaService wxMaService() {
+        WxMaService maService = new WxMaServiceImpl();
+        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
+        config.setAppid(properties.getAppId());
+        config.setSecret(properties.getSecret());
+        config.setToken(properties.getToken());
+        config.setAesKey(properties.getAesKey());
+        config.setMsgDataFormat(properties.getMsgDataFormat());
+        maService.setWxMaConfig(config);
+        return maService;
+    }
+
+}
+

+ 43 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/system/config/WxMaProperties.java

@@ -0,0 +1,43 @@
+package com.ruoyi.system.config;
+
+
+import lombok.Data;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * 微信小程序参数配置类
+ */
+@Data
+@Component
+@ConfigurationProperties(prefix = "wx.miniapp")
+@ConditionalOnProperty(prefix = "wx.miniapp", value = "appId")
+public class WxMaProperties {
+    /**
+     * 设置微信小程序的appId
+     */
+    private String appId;
+
+    /**
+     * 设置微信小程序的Secret
+     */
+    private String secret;
+
+    /**
+     * 设置微信小程序消息服务器配置的token
+     */
+    private String token;
+
+    /**
+     * 设置微信小程序消息服务器配置的EncodingAESKey
+     */
+    private String aesKey;
+
+    /**
+     * 消息格式,XML或者JSON
+     */
+    private String msgDataFormat;
+
+}
+

+ 18 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/system/enums/EnvVersion.java

@@ -0,0 +1,18 @@
+package com.ruoyi.system.enums;
+
+import lombok.Getter;
+
+/**
+ * @author 小程序 发布状态
+ */
+
+@Getter
+public enum EnvVersion {
+    DEVELOP("develop"),
+    TRIAL("trial"),
+    RELEASE("release");
+    private final String value;
+    EnvVersion(String value) {
+        this.value = value;
+    }
+}

+ 37 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/system/service/IWxService.java

@@ -0,0 +1,37 @@
+package com.ruoyi.system.service;
+
+import com.ruoyi.system.enums.EnvVersion;
+import me.chanjar.weixin.common.error.WxErrorException;
+
+import java.io.File;
+
+/**
+ * 微信小程序服务接口
+ */
+public interface IWxService {
+    /**
+     * 根据授权码获取用户OpenId
+     * @param jsCode 获取用户openid授权码
+     * @return 用户OpenId
+     * @throws WxErrorException 微信API通信异常信息
+     */
+    public String getOpenId(String jsCode) throws WxErrorException;
+
+    /**
+     * 根据授权码获取用户手机号
+     * @param code 获取用户手机后授权码
+     * @return 微信用户手机号
+     * @throws WxErrorException 微信API通信异常信息
+     */
+    public String getPhoneNumber(String code) throws WxErrorException;
+
+    /**
+     * 获取小程序二维码
+     * @param scene 参数  示例: a=1&b=2
+     * @param page 页面   示例: pages/index/index
+     * @param envVersion  环境  示例: develop
+     * @return 二维码文件
+     */
+    public File createWxaCode(String scene, String page, EnvVersion envVersion) throws WxErrorException;
+
+}

+ 58 - 0
jd-logistics-modules/jd-logistics-system/src/main/java/com/ruoyi/system/service/impl/WxMiniAppServiceImpl.java

@@ -0,0 +1,58 @@
+package com.ruoyi.system.service.impl;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
+import com.ruoyi.system.enums.EnvVersion;
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+
+/**
+ * 微信小程序服务类
+ * @author 金鹏
+ */
+@Service
+@RequiredArgsConstructor
+@ConditionalOnBean(WxMaService.class)
+public class WxMiniAppServiceImpl implements com.ruoyi.system.service.IWxService {
+
+    private final WxMaService wxMaService;
+
+    /**
+     * 根据授权码获取用户OpenId
+     * @param jsCode 获取用户openid授权码
+     * @return 用户OpenId
+     * @throws WxErrorException 微信API通信异常信息
+     */
+    @Override
+    public String getOpenId(String jsCode) throws WxErrorException {
+        WxMaJscode2SessionResult wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(jsCode);
+       return wxMaJscode2SessionResult.getOpenid();
+    }
+    /**
+     * 根据授权码获取用户OpenId
+     * @param code 获取用户手机后授权码
+     * @return 微信用户手机号
+     * @throws WxErrorException 微信API通信异常信息
+     */
+    @Override
+    public String getPhoneNumber(String code) throws WxErrorException {
+        WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNumber(code);
+        return phoneNoInfo.getPhoneNumber();
+    }
+
+    /**
+     * 根据授权码获取用户OpenId
+     * @param scene 获取用户手机后授权码
+     * @return 微信用户手机号
+     */
+    @Override
+    public File createWxaCode(String scene, String page, EnvVersion envVersion) throws WxErrorException {
+        return wxMaService.getQrcodeService().createWxaCodeUnlimit(scene, page, false, envVersion.getValue(), 430, true, null, false);
+    }
+
+}