增加微信jsConfig
This commit is contained in:
parent
2cb2bd3cb1
commit
6a977e75e4
|
|
@ -3,7 +3,6 @@ package com.mdd.admin.controller.finance;
|
||||||
import com.mdd.admin.service.IFinanceRefundService;
|
import com.mdd.admin.service.IFinanceRefundService;
|
||||||
import com.mdd.admin.validate.commons.PageValidate;
|
import com.mdd.admin.validate.commons.PageValidate;
|
||||||
import com.mdd.admin.validate.finance.FinanceRefundSearchValidate;
|
import com.mdd.admin.validate.finance.FinanceRefundSearchValidate;
|
||||||
import com.mdd.admin.vo.finance.FinanceRechargeListVo;
|
|
||||||
import com.mdd.admin.vo.finance.FinanceRefundListVo;
|
import com.mdd.admin.vo.finance.FinanceRefundListVo;
|
||||||
import com.mdd.admin.vo.finance.FinanceRefundLogVo;
|
import com.mdd.admin.vo.finance.FinanceRefundLogVo;
|
||||||
import com.mdd.common.core.AjaxResult;
|
import com.mdd.common.core.AjaxResult;
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.TransactionDefinition;
|
import org.springframework.transaction.TransactionDefinition;
|
||||||
import org.springframework.transaction.TransactionStatus;
|
import org.springframework.transaction.TransactionStatus;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,15 @@ package com.mdd.common.plugin.wechat;
|
||||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||||
import com.mdd.common.util.ConfigUtils;
|
import com.mdd.common.util.ConfigUtils;
|
||||||
|
import com.mdd.common.util.HttpUtils;
|
||||||
|
import com.mdd.common.util.MapUtils;
|
||||||
|
import com.mdd.common.util.StringUtils;
|
||||||
import me.chanjar.weixin.mp.api.WxMpService;
|
import me.chanjar.weixin.mp.api.WxMpService;
|
||||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.security.MessageDigest;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -72,4 +76,89 @@ public class WxMnpDriver {
|
||||||
|
|
||||||
return wxMpService;
|
return wxMpService;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取授权页ticket
|
||||||
|
*
|
||||||
|
* @author fzr
|
||||||
|
* @param accessToken token
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public static String getJsSdkGetTicket(String accessToken) {
|
||||||
|
String jsSdkGetTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi";
|
||||||
|
String url = String.format(jsSdkGetTicketUrl, accessToken);
|
||||||
|
String results = HttpUtils.sendGet(url);
|
||||||
|
Map<String, String> resultMap = MapUtils.jsonToMap(results);
|
||||||
|
String jsapi_ticket = null;
|
||||||
|
if (resultMap != null && StringUtils.isNotNull(resultMap.get("ticket"))) {
|
||||||
|
jsapi_ticket = resultMap.get("ticket");
|
||||||
|
}
|
||||||
|
return jsapi_ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构建JsSDK
|
||||||
|
*
|
||||||
|
* @author fzr
|
||||||
|
* @param ticket ticket
|
||||||
|
* @param timestamp 时间戳
|
||||||
|
* @param nonceStr 串
|
||||||
|
* @param url 地址
|
||||||
|
* @return String
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
public static String buildJSSDKSignature(String ticket, String timestamp, String nonceStr, String url) throws Exception {
|
||||||
|
String orderedString = "jsapi_ticket=" + ticket
|
||||||
|
+ "&noncestr=" + nonceStr
|
||||||
|
+ "×tamp=" + timestamp
|
||||||
|
+ "&url=" + url;
|
||||||
|
return sha1(orderedString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sha1算法
|
||||||
|
*
|
||||||
|
* @author fzr
|
||||||
|
* @param orderedString 字符串
|
||||||
|
* @return String
|
||||||
|
* @throws Exception 异常
|
||||||
|
*/
|
||||||
|
private static String sha1(String orderedString) throws Exception {
|
||||||
|
String ciphertext;
|
||||||
|
MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||||
|
byte[] digest = md.digest(orderedString.getBytes());
|
||||||
|
ciphertext = byteToStr(digest);
|
||||||
|
return ciphertext.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字节转字符
|
||||||
|
*
|
||||||
|
* @author fzr
|
||||||
|
* @param byteArray 字节
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
private static String byteToStr(byte[] byteArray) {
|
||||||
|
StringBuilder strDigest = new StringBuilder();
|
||||||
|
for (byte b : byteArray) {
|
||||||
|
strDigest.append(byteToHexStr(b));
|
||||||
|
}
|
||||||
|
return strDigest.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字节转Hex
|
||||||
|
*
|
||||||
|
* @author fzr
|
||||||
|
* @param mByte 字节
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
private static String byteToHexStr(byte mByte) {
|
||||||
|
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
||||||
|
char[] tempArr = new char[2];
|
||||||
|
tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
|
||||||
|
tempArr[1] = Digit[mByte & 0X0F];
|
||||||
|
return new String(tempArr);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.mdd.front.controller;
|
||||||
|
|
||||||
|
import com.mdd.common.aop.NotLogin;
|
||||||
|
import com.mdd.common.core.AjaxResult;
|
||||||
|
import com.mdd.front.service.IWechatService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.validation.constraints.NotEmpty;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("api/wechat")
|
||||||
|
@Api(tags = "微信管理")
|
||||||
|
public class WechatController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
IWechatService iWechatService;
|
||||||
|
|
||||||
|
@NotLogin
|
||||||
|
@GetMapping("/jsConfig")
|
||||||
|
@ApiOperation("微信jsConfig")
|
||||||
|
public AjaxResult<Object> jsConfig(@Validated @NotEmpty() @RequestParam("url") String url) throws Exception {
|
||||||
|
Map<String, Object> map = iWechatService.jsConfig(url);
|
||||||
|
return AjaxResult.success(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.mdd.front.service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface IWechatService {
|
||||||
|
|
||||||
|
Map<String, Object> jsConfig(String url) throws Exception;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.mdd.front.service.impl;
|
||||||
|
|
||||||
|
import com.mdd.common.plugin.wechat.WxMnpDriver;
|
||||||
|
import com.mdd.common.util.ConfigUtils;
|
||||||
|
import com.mdd.front.service.IWechatService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class WechatServiceImpl implements IWechatService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> jsConfig(String url) throws Exception {
|
||||||
|
String appId = ConfigUtils.get("oa_channel", "appId");
|
||||||
|
String accessToken = WxMnpDriver.mnp().getAccessToken();
|
||||||
|
String jsapiTicket = WxMnpDriver.getJsSdkGetTicket(accessToken);
|
||||||
|
String timestamp = Long.toString(System.currentTimeMillis() / 1000);
|
||||||
|
String nonceStr = UUID.randomUUID().toString();
|
||||||
|
String signature = WxMnpDriver.buildJSSDKSignature(jsapiTicket,timestamp,nonceStr,url);
|
||||||
|
|
||||||
|
List<String> array = Arrays.asList(
|
||||||
|
"onMenuShareTimeline",
|
||||||
|
"onMenuShareAppMessage",
|
||||||
|
"onMenuShareQQ",
|
||||||
|
"onMenuShareWeibo",
|
||||||
|
"onMenuShareQZone",
|
||||||
|
"openLocation",
|
||||||
|
"getLocation",
|
||||||
|
"chooseWXPay",
|
||||||
|
"updateAppMessageShareData",
|
||||||
|
"updateTimelineShareData",
|
||||||
|
"openAddress",
|
||||||
|
"scanQRCode");
|
||||||
|
|
||||||
|
Map<String,Object> map = new HashMap<>();
|
||||||
|
map.put("url", url);
|
||||||
|
map.put("jsapi_ticket", jsapiTicket);
|
||||||
|
map.put("nonceStr", nonceStr);
|
||||||
|
map.put("timestamp", timestamp);
|
||||||
|
map.put("signature", signature);
|
||||||
|
map.put("appid", appId);
|
||||||
|
map.put("jsApiList", array);
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue