mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 04:39:00 +08:00
✨ feat: 微信JSSDK配置
This commit is contained in:
1 parent
2abb4faacf
commit
bf410db2eb
2 files changed
+34
-56
No files matched your search
-56
@@ -1,56 +0,0 @@
|
||||
package com.pig4cloud.pigx.mp.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.security.annotation.Inner;
|
||||
import com.pig4cloud.pigx.mp.config.WxMpInitConfigRunner;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信JSSDK配置
|
||||
*
|
||||
* @author akhan
|
||||
* @date 13:59 2025/4/1
|
||||
*/
|
||||
@Slf4j
|
||||
@Inner(value = false)
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/{appId}/jssdk-config")
|
||||
public class WxJssdkController {
|
||||
|
||||
@GetMapping
|
||||
public R getJssdkConfig(@PathVariable("appId") String appId,
|
||||
@RequestParam(name = "url", required = false) String url){
|
||||
if (StrUtil.isAllBlank(url)) {
|
||||
throw new IllegalArgumentException("请求参数非法,请核实!");
|
||||
}
|
||||
|
||||
final WxMpService wxService = WxMpInitConfigRunner.getMpServices().get(appId);
|
||||
|
||||
if (wxService == null) {
|
||||
throw new IllegalArgumentException(String.format("未找到对应appid=[%d]的配置,请核实!", appId));
|
||||
}
|
||||
|
||||
try {
|
||||
WxJsapiSignature signature = wxService.createJsapiSignature(url);
|
||||
|
||||
return R.ok(Map.of(
|
||||
"appId", signature.getAppId(),
|
||||
"timestamp", signature.getTimestamp(),
|
||||
"nonceStr", signature.getNonceStr(),
|
||||
"signature", signature.getSignature()
|
||||
));
|
||||
} catch (WxErrorException e) {
|
||||
log.error("获取微信js api出现错误: {}", e.getLocalizedMessage());
|
||||
return R.failed("获取微信js api出现错误: " + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
+34
@@ -3,6 +3,7 @@ package com.pig4cloud.pigx.mp.controller;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.common.api.encrypt.annotation.NoEncrypt;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import com.pig4cloud.pigx.common.data.tenant.TenantBroker;
|
||||
import com.pig4cloud.pigx.common.security.annotation.Inner;
|
||||
import com.pig4cloud.pigx.common.xss.core.XssCleanIgnore;
|
||||
@@ -10,11 +11,15 @@ import com.pig4cloud.pigx.mp.config.WxMpContextHolder;
|
||||
import com.pig4cloud.pigx.mp.config.WxMpInitConfigRunner;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Binary Wang
|
||||
* @author lengleng
|
||||
@@ -87,6 +92,35 @@ public class WxPortalController {
|
||||
(id) -> handleMessage(appId, requestBody, signature, timestamp, nonce, openid, encType, msgSignature));
|
||||
}
|
||||
|
||||
@XssCleanIgnore
|
||||
@GetMapping
|
||||
public R getJssdkConfig(@PathVariable("appId") String appId,
|
||||
@RequestParam(name = "url", required = false) String url){
|
||||
if (StrUtil.isAllBlank(url)) {
|
||||
throw new IllegalArgumentException("请求参数非法,请核实!");
|
||||
}
|
||||
|
||||
final WxMpService wxService = WxMpInitConfigRunner.getMpServices().get(appId);
|
||||
|
||||
if (wxService == null) {
|
||||
throw new IllegalArgumentException(String.format("未找到对应appid=[%d]的配置,请核实!", appId));
|
||||
}
|
||||
|
||||
try {
|
||||
WxJsapiSignature signature = wxService.createJsapiSignature(url);
|
||||
|
||||
return R.ok(Map.of(
|
||||
"appId", signature.getAppId(),
|
||||
"timestamp", signature.getTimestamp(),
|
||||
"nonceStr", signature.getNonceStr(),
|
||||
"signature", signature.getSignature()
|
||||
));
|
||||
} catch (WxErrorException e) {
|
||||
log.error("获取微信js api出现错误: {}", e.getLocalizedMessage());
|
||||
return R.failed("获取微信js api出现错误: " + e.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信消息处理
|
||||
* @param appId 多公众号标志位
|
||||
|
||||
Reference in new issue
Block a user