提交代码

master
胡锐 6 months ago
parent e1e96467c5
commit 15db7e3d24
  1. 44
      service/src/main/java/com/hfkj/common/utils/WxUtils.java
  2. 27
      user/src/main/java/com/user/controller/BsUserController.java

@ -167,6 +167,50 @@ public class WxUtils {
return new String(nonceChars); return new String(nonceChars);
} }
/**
* 用SHA1算法生成安全签名
* @param data 数据
* @return 安全签名
* @throws Exception
*/
public static String getSHA1(final Map<String, Object> data) throws Exception
{
try {
Set<String> keySet = data.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals(WXPayConstants.FIELD_SIGN) || k.equals("apiKey")) {
continue;
}
if (data.get(k) != null) // 参数值为空,则不参与签名
{
sb.append(k).append("=").append(data.get(k)).append("&");
}
}
String str = sb.substring(0 , sb.length()-1);
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuffer hexstr = new StringBuffer();
String shaHex = "";
for (int i = 0; i < digest.length; i++) {
shaHex = Integer.toHexString(digest[i] & 0xFF);
if (shaHex.length() < 2) {
hexstr.append(0);
}
hexstr.append(shaHex);
}
return hexstr.toString();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(String.format("Invalid sign_type: %s"));
}
}
/** /**
* 拼接签名数据 * 拼接签名数据
* *

@ -7,9 +7,7 @@ import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp; import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode; import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter; import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.MemberValidateUtil; import com.hfkj.common.utils.*;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.WxMaConfig; import com.hfkj.config.WxMaConfig;
import com.hfkj.entity.BsUser; import com.hfkj.entity.BsUser;
import com.hfkj.model.ResponseData; import com.hfkj.model.ResponseData;
@ -26,14 +24,12 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -139,6 +135,23 @@ public class BsUserController {
} }
} }
@RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "【H5】获取AccessToken")
public ResponseData getH5AccessToken(@RequestParam(value = "code", required = true) String code) {
try {
Map<String, Object> params = new HashMap<>();
params.put("appid", "wxa075e8509802f826");
params.put("secret", "0e606fc1378d35e359fcf3f15570b2c5");
params.put("code", code);
params.put("grant_type", "authorization_code");
return ResponseMsgUtil.success(HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params));
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/wechatMaPhone", method = RequestMethod.POST) @RequestMapping(value = "/wechatMaPhone", method = RequestMethod.POST)
@ResponseBody @ResponseBody
@ApiOperation(value = "微信小程序手机号登录") @ApiOperation(value = "微信小程序手机号登录")

Loading…
Cancel
Save