From 1493c206aa5af0c6b5345db0606b39e98f34dc6e Mon Sep 17 00:00:00 2001 From: xhxu Date: Wed, 14 Apr 2021 15:47:11 +0800 Subject: [PATCH] api --- .../V202104140001__for_wx_third_party_api.sql | 18 +++ .../controller/UserBasicInfoController.java | 36 ++++++ .../interceptor/AuthorizationInterceptor.java | 90 +++++++++---- .../java/com/iformall/common/ErrorCode.java | 1 + .../iformall/domain/po/WxThirdPartyApi.java | 50 +++++++ .../mapper/WxThirdPartyApiMapper.java | 12 ++ .../service/WxThirdPartyApiService.java | 21 +++ .../service/impl/WxGooagooServiceImpl.java | 2 +- .../impl/WxQuestionOneselfServiceImpl.java | 9 +- .../WxQuestionOneselfUserServiceImpl.java | 8 +- .../impl/WxThirdPartyApiServiceImpl.java | 54 ++++++++ .../java/com/iformall/utils/Constant.java | 3 + .../com/iformall/utils/sign/AppUtils.java | 122 ++++++++++++++++++ .../utils/{gooagoo => sign}/SignUtils.java | 2 +- .../mapper/WxThirdPartyConfigApi.xml | 65 ++++++++++ 15 files changed, 464 insertions(+), 29 deletions(-) create mode 100644 mallinkAdmin/src/main/resources/db/migration/V202104140001__for_wx_third_party_api.sql create mode 100644 mallinkPublicApi/src/main/java/com/iformall/controller/UserBasicInfoController.java create mode 100644 mallinkService/src/main/java/com/iformall/domain/po/WxThirdPartyApi.java create mode 100644 mallinkService/src/main/java/com/iformall/mapper/WxThirdPartyApiMapper.java create mode 100644 mallinkService/src/main/java/com/iformall/service/WxThirdPartyApiService.java create mode 100644 mallinkService/src/main/java/com/iformall/service/impl/WxThirdPartyApiServiceImpl.java create mode 100644 mallinkService/src/main/java/com/iformall/utils/sign/AppUtils.java rename mallinkService/src/main/java/com/iformall/utils/{gooagoo => sign}/SignUtils.java (98%) create mode 100644 mallinkService/src/main/resources/mapper/WxThirdPartyConfigApi.xml diff --git a/mallinkAdmin/src/main/resources/db/migration/V202104140001__for_wx_third_party_api.sql b/mallinkAdmin/src/main/resources/db/migration/V202104140001__for_wx_third_party_api.sql new file mode 100644 index 000000000..71075b6cd --- /dev/null +++ b/mallinkAdmin/src/main/resources/db/migration/V202104140001__for_wx_third_party_api.sql @@ -0,0 +1,18 @@ +CREATE TABLE `wx_third_party_api` ( + `id` bigint(20) NOT NULL COMMENT '主键ID', + `tenant_id` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '租户ID', + `parent_tenant_id` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '父租户ID', + `type` tinyint(6) NOT NULL DEFAULT 1 COMMENT '第三方类型,', + `app_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, + `app_key` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL, + `sign_key` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '加密key,', + `api_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `token_expired_time` datetime(0) DEFAULT NULL COMMENT 'token过期时间', + `user_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '登陆账号', + `password` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '登陆密码', + `version` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '接口版本', + `tp_id` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE, + UNIQUE INDEX `app_id`(`app_id`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic; \ No newline at end of file diff --git a/mallinkPublicApi/src/main/java/com/iformall/controller/UserBasicInfoController.java b/mallinkPublicApi/src/main/java/com/iformall/controller/UserBasicInfoController.java new file mode 100644 index 000000000..7412b838a --- /dev/null +++ b/mallinkPublicApi/src/main/java/com/iformall/controller/UserBasicInfoController.java @@ -0,0 +1,36 @@ +package com.iformall.controller; + +import com.iformall.common.ResultData; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.*; +import java.util.Map; + +@RestController +@RequestMapping("/api/userInfo") +@Api(description = "会员相关接口") +public class UserBasicInfoController extends BaseController { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + + /** + * 用户注册 + * + * @param map + * @return + */ + @PostMapping("/register") + @ApiOperation(value = "用户注册", notes = "") + public ResultData userRegister(@RequestParam Map map) { + logger.info("UserBasicInfoController >>>>>>>> userRegister >>>>>>>>>>>>>>>>>"+map.toString()); + String phone = map.get("phone"); + logger.info("UserBasicInfoController >>>>>>>> userRegister >>>>>>>>>>>>>>>>>phone="+phone); + + return new ResultData(); + } + + +} diff --git a/mallinkPublicApi/src/main/java/com/iformall/interceptor/AuthorizationInterceptor.java b/mallinkPublicApi/src/main/java/com/iformall/interceptor/AuthorizationInterceptor.java index f19903526..9acbdecf5 100644 --- a/mallinkPublicApi/src/main/java/com/iformall/interceptor/AuthorizationInterceptor.java +++ b/mallinkPublicApi/src/main/java/com/iformall/interceptor/AuthorizationInterceptor.java @@ -2,11 +2,16 @@ package com.iformall.interceptor; import com.iformall.common.ErrorCode; +import com.iformall.domain.po.WxThirdPartyApi; import com.iformall.exception.MallinkException; -import com.iformall.utils.HashUtil; +import com.iformall.service.WxThirdPartyApiService; +import com.iformall.utils.Constant; import com.iformall.utils.RedisCacheUtils; +import com.iformall.utils.sign.SignUtils; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.RedisTemplate; @@ -15,6 +20,7 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.*; /** * 权限(Token)验证 @@ -25,44 +31,80 @@ import javax.servlet.http.HttpServletResponse; @Component public class AuthorizationInterceptor extends HandlerInterceptorAdapter { + private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired @Qualifier("objectCommonRedisTemplate") RedisTemplate redisTemplate; + + @Autowired + WxThirdPartyApiService wxThirdPartyApiService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String cid = request.getParameter("cId");//调用方ID - String nonceStr = request.getParameter("nonceStr");//随机字符串 - String signKey = request.getParameter("signKey");//随机字符串 - if(StringUtils.isBlank(cid) || "null".equals(cid) || "undefined".equals(cid)){ - throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"cId为空["+cid+"]"); + String cookie = request.getHeader("cookie"); + if (StringUtils.isBlank(cookie) || !cookie.contains("&")) { + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); + } + String[] split = cookie.split("&"); + WxThirdPartyApi apiConfig = wxThirdPartyApiService.findByApp(split[0], split[1]); + if(apiConfig == null){ + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"非法请求"); } - - if(StringUtils.isBlank(nonceStr) || "null".equals(nonceStr) || "undefined".equals(nonceStr)){ - throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"nonceStr为空["+nonceStr+"]"); + + String signature = request.getHeader("sign"); + logger.info("sign={}"+signature); + //没有加密 + if (StringUtils.isBlank(signature)) { + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少加密串"); } - - if(StringUtils.isBlank(signKey) || "null".equals(signKey) || "undefined".equals(signKey)){ - throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"signKey为空["+signKey+"]"); + + String timeStamp = request.getParameter("timeStamp"); + long timestampDate = Long.valueOf(timeStamp) + 1000*60*5;//五分钟有效 + long currDate = System.currentTimeMillis(); + // 请求过期 + if (timestampDate < currDate) { + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"请求过期"); } - //nonceStr必须唯一,为防止接口盗刷,每次只能调用一次 - Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, "publicApi:"+nonceStr); + String nonceStr = request.getParameter("nonceStr"); + //重复调用 + Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+nonceStr); if (null != cache) { - throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"不能重复调用"); + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); + } + + + + Enumeration paramNames = request.getParameterNames(); + Map map = new HashMap(); + + //获取所有的请求参数 + while (paramNames.hasMoreElements()) { + String paramName = paramNames.nextElement(); + String[] paramValues = request.getParameterValues(paramName); + + if (paramValues.length > 0) { + String paramValue = paramValues[0]; + if (paramValue.length() != 0 + && !"sign".equals(paramName) + && !"appId".equals(paramName) + && !"appKey".equals(paramName) + && !"signKey".equals(paramName)) { + map.put(paramName, paramValue); + } + } } - - - //TODO singnKey是根据cid+cid对应的密钥+nonceStr - String secretKey = "";//TODO 根据cid查询密钥,缓存 - String signstr = HashUtil.md5(cid+secretKey+nonceStr); - if (!signKey.equals(signstr)) { - throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); + logger.info("sign={}"+signature); + String signKey = apiConfig.getSignKey();//TODO 根据appId查询密钥,缓存 + String newSignature = SignUtils.getSign(signKey, map, "MD5"); + //加密串不匹配 + if (!signature.equals(newSignature)) { + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"加密串校验失败"); } - - RedisCacheUtils.cache(redisTemplate, "publicApi:"+nonceStr, 1, 600); + RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+nonceStr, 1, 300); return true; + } } diff --git a/mallinkService/src/main/java/com/iformall/common/ErrorCode.java b/mallinkService/src/main/java/com/iformall/common/ErrorCode.java index e2feb3eb6..89a348fb3 100644 --- a/mallinkService/src/main/java/com/iformall/common/ErrorCode.java +++ b/mallinkService/src/main/java/com/iformall/common/ErrorCode.java @@ -599,6 +599,7 @@ public enum ErrorCode{ QUESTION_USER_LINE(61007, "已经参与!"), QUESTION_NOT_START(61008, "问卷未开始"), QUESTION_END(61009, "问卷已结束"), + QUESTION_END_LINE(61010, "问卷已下线"), /** * 数衍信息 diff --git a/mallinkService/src/main/java/com/iformall/domain/po/WxThirdPartyApi.java b/mallinkService/src/main/java/com/iformall/domain/po/WxThirdPartyApi.java new file mode 100644 index 000000000..bf4d81939 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/domain/po/WxThirdPartyApi.java @@ -0,0 +1,50 @@ +package com.iformall.domain.po; + +import com.baomidou.mybatisplus.annotation.TableName; +import com.iformall.domain.po.base.TenantEntity; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.Date; + +@TableName(value = "wx_third_party_Api") +@Data +@EqualsAndHashCode(callSuper = true) +public class WxThirdPartyApi extends TenantEntity { + + protected Long id; + + @io.swagger.annotations.ApiModelProperty(value="",name="type") + private Integer type; + + @io.swagger.annotations.ApiModelProperty(value="",name="appId") + private String appId; + + @io.swagger.annotations.ApiModelProperty(value="",name="appKey") + private String appKey; + + @io.swagger.annotations.ApiModelProperty(value="加密key",name="signKey") + private String signKey; + + @io.swagger.annotations.ApiModelProperty(value="",name="apiUrl") + private String apiUrl; + + @io.swagger.annotations.ApiModelProperty(value="token",name="token") + private String token; + + @io.swagger.annotations.ApiModelProperty(value="",name="tokenExpiredTime") + private Date tokenExpiredTime; + + @io.swagger.annotations.ApiModelProperty(value="登陆username",name="userName") + private String userName; + + @io.swagger.annotations.ApiModelProperty(value="登陆password",name="password") + private String password; + + @io.swagger.annotations.ApiModelProperty(value="",name="version") + private String version; + + @io.swagger.annotations.ApiModelProperty(value="",name="tpId") + private String tpId; + +} diff --git a/mallinkService/src/main/java/com/iformall/mapper/WxThirdPartyApiMapper.java b/mallinkService/src/main/java/com/iformall/mapper/WxThirdPartyApiMapper.java new file mode 100644 index 000000000..1374b8ced --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/mapper/WxThirdPartyApiMapper.java @@ -0,0 +1,12 @@ +package com.iformall.mapper; + +import com.iformall.common.CommonMapper; +import com.iformall.domain.po.WxThirdPartyApi; + +import java.util.List; + +public interface WxThirdPartyApiMapper extends CommonMapper { + + List findList(WxThirdPartyApi apiConfig); + +} diff --git a/mallinkService/src/main/java/com/iformall/service/WxThirdPartyApiService.java b/mallinkService/src/main/java/com/iformall/service/WxThirdPartyApiService.java new file mode 100644 index 000000000..4f37ecd07 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/WxThirdPartyApiService.java @@ -0,0 +1,21 @@ +package com.iformall.service; + +import com.iformall.domain.po.WxThirdPartyApi; + +import java.util.List; + +/** + * + */ +public interface WxThirdPartyApiService { + + /** + * 查询列表 + * + * @param record + */ + List findList(WxThirdPartyApi record); + + WxThirdPartyApi findByApp(String appId, String appKey); + +} diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxGooagooServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxGooagooServiceImpl.java index 369f170e2..5c086ec7b 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxGooagooServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxGooagooServiceImpl.java @@ -13,7 +13,7 @@ import com.iformall.mapper.WxThirdPartyConfigMapper; import com.iformall.service.*; import com.iformall.utils.DateUtils; import com.iformall.utils.HttpUtil; -import com.iformall.utils.gooagoo.SignUtils; +import com.iformall.utils.sign.SignUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfServiceImpl.java index d28ff37fd..4cce1f0b7 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfServiceImpl.java @@ -116,9 +116,14 @@ public class WxQuestionOneselfServiceImpl implements WxQuestionOneselfService { topic.setUpdateDate(now); topic.setSort(i+1); - } - wxQuestionOneselfTopicMapper.insertList(topicList); + try{ + wxQuestionOneselfTopicMapper.insertList(topicList); + }catch (Exception e){ + logger.error("添加答题问卷: {}" + e.getMessage()); + throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR); + } + } if(record.getCountTopic() == null || record.getCountTopic().intValue() != countTopic){ diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfUserServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfUserServiceImpl.java index 104b792fc..c605e3be2 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfUserServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxQuestionOneselfUserServiceImpl.java @@ -8,6 +8,7 @@ import com.github.pagehelper.PageInfo; import com.iformall.common.ErrorCode; import com.iformall.common.IdWorker; import com.iformall.domain.po.*; +import com.iformall.enums.EnumQuestionOneselfStatus; import com.iformall.enums.EnumScoreType; import com.iformall.enums.EnumUserType; import com.iformall.exception.MallinkException; @@ -138,12 +139,17 @@ public class WxQuestionOneselfUserServiceImpl implements WxQuestionOneselfUserSe } wxQuestionOneselfLogMapper.insertList(logList); }else{ - throw new MallinkException(ErrorCode.QUESTION_UPUP_LINE); + throw new MallinkException(ErrorCode.QUESTION_USER_NOT); } wxQuestionOneselfMapper.updateCountUser(record.getQuestionId()); WxQuestionOneself wxQuestionOneself = wxQuestionOneselfMapper.selectById(record.getQuestionId()); + if(!wxQuestionOneself.getStatus().equals(EnumQuestionOneselfStatus.INJECT_CAMPAIGN.getCode()) + && !wxQuestionOneself.getStatus().equals(EnumQuestionOneselfStatus.INJECT_ONLINE.getCode())){ + throw new MallinkException(ErrorCode.QUESTION_END_LINE); + } + if(now.before(wxQuestionOneself.getStartDate())){ throw new MallinkException(ErrorCode.QUESTION_NOT_START); } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxThirdPartyApiServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxThirdPartyApiServiceImpl.java new file mode 100644 index 000000000..2e553e46d --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxThirdPartyApiServiceImpl.java @@ -0,0 +1,54 @@ +package com.iformall.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.iformall.domain.po.WxAppinfo; +import com.iformall.domain.po.WxThirdPartyApi; +import com.iformall.mapper.WxThirdPartyApiMapper; +import com.iformall.service.WxThirdPartyApiService; +import com.iformall.utils.Constant; +import com.iformall.utils.RedisCacheUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.List; + + +@Service +public class WxThirdPartyApiServiceImpl implements WxThirdPartyApiService { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + WxThirdPartyApiMapper wxThirdPartyApiMapper; + + @Autowired + @Qualifier("objectCommonRedisTemplate") + RedisTemplate redisTemplate; + + @Override + public List findList(WxThirdPartyApi record) { + return wxThirdPartyApiMapper.findList(record); + } + + @Override + public WxThirdPartyApi findByApp(String appId, String appKey) { + if(StringUtils.isBlank(appId) || StringUtils.isBlank(appKey)){ + return null; + } + WxThirdPartyApi apiConfig = null; + apiConfig = RedisCacheUtils.getCacheObject(redisTemplate, Constant.publicApi + appId, WxThirdPartyApi.class); + if(apiConfig != null){ + return apiConfig; + } + WxThirdPartyApi apiQ = new WxThirdPartyApi(); + apiQ.setAppId(appId); + apiQ.setAppKey(appKey); + apiConfig = wxThirdPartyApiMapper.selectOne(new QueryWrapper<>(apiQ)); + RedisCacheUtils.cache(redisTemplate, Constant.publicApi + appId, apiConfig,0l); + return apiConfig; + } +} diff --git a/mallinkService/src/main/java/com/iformall/utils/Constant.java b/mallinkService/src/main/java/com/iformall/utils/Constant.java index f683a0c32..9ca94a7ec 100644 --- a/mallinkService/src/main/java/com/iformall/utils/Constant.java +++ b/mallinkService/src/main/java/com/iformall/utils/Constant.java @@ -42,6 +42,9 @@ public class Constant { public static final String cuserQr = "weapp:cuser-qr:"; + public static final String publicApi = "publicApi:"; + public static final String publicApiNonce = publicApi + "nonce:"; + public static final String LOGIN_USER_KEY = "LOGIN_USER_KEY"; public static final String LOGIN_MEMBER_KEY = "LOGIN_MEMBER_KEY"; public static final String TENANT_ID = "TENANT_ID"; diff --git a/mallinkService/src/main/java/com/iformall/utils/sign/AppUtils.java b/mallinkService/src/main/java/com/iformall/utils/sign/AppUtils.java new file mode 100644 index 000000000..9737df7f0 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/utils/sign/AppUtils.java @@ -0,0 +1,122 @@ +package com.iformall.utils.sign; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Date; +import java.util.UUID; + + +/** + * @Title: AppUtils + * @Description: 随机产生唯一的app_key和app_secret + */ +public class AppUtils { + //生成 app_secret 密钥 + private final static String SERVER_NAME = "formall"; + private final static String[] chars = new String[]{"a", "b", "c", "d", "e", "f", + "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", + "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", + "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", + "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", + "W", "X", "Y", "Z"}; + + /** + * @Description:

+ * 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。 + * 本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符, + * 这样重复率大大降低。 + * 经测试,在生成一千万个数据也没有出现重复,完全满足大部分需求。 + *

+ */ + public static String getAppId() { + StringBuffer shortBuffer = new StringBuffer(); + String uuid = UUID.randomUUID().toString().replace("-", ""); + for (int i = 0; i < 8; i++) { + String str = uuid.substring(i * 4, i * 4 + 4); + int x = Integer.parseInt(str, 16); + shortBuffer.append(chars[x % 0x3E]); + } + return shortBuffer.toString(); + + } + + /** + *

+ * 通过appId和内置关键词生成APP key + *

+ */ + public static String getAppKey(String appId) { + try { + String[] array = new String[]{appId, SERVER_NAME}; + StringBuffer sb = new StringBuffer(); + // 字符串排序 + Arrays.sort(array); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + } + String str = sb.toString(); + 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 (NoSuchAlgorithmException e) { + e.printStackTrace(); + throw new RuntimeException(); + } + } + + /** + *

+ * + *

+ */ + public static String getSignKey(String appId,String appKey) { + try { + String[] array = new String[]{appId, appKey, SERVER_NAME,Long.toString(new Date().getTime())}; + StringBuffer sb = new StringBuffer(); + // 字符串排序 + Arrays.sort(array); + for (int i = 0; i < array.length; i++) { + sb.append(array[i]); + } + String str = sb.toString(); + 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().toUpperCase(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + throw new RuntimeException(); + } + } + + public static void main(String[] args) { + String appId = getAppId(); + String appKey = getAppKey(appId); + String signKey = getSignKey(appId,appKey); + System.out.println("appId: "+appId); + System.out.println("appKey: "+appKey); + System.out.println("signKey: "+signKey); + } +} \ No newline at end of file diff --git a/mallinkService/src/main/java/com/iformall/utils/gooagoo/SignUtils.java b/mallinkService/src/main/java/com/iformall/utils/sign/SignUtils.java similarity index 98% rename from mallinkService/src/main/java/com/iformall/utils/gooagoo/SignUtils.java rename to mallinkService/src/main/java/com/iformall/utils/sign/SignUtils.java index dbcf11485..491577993 100644 --- a/mallinkService/src/main/java/com/iformall/utils/gooagoo/SignUtils.java +++ b/mallinkService/src/main/java/com/iformall/utils/sign/SignUtils.java @@ -1,4 +1,4 @@ -package com.iformall.utils.gooagoo; +package com.iformall.utils.sign; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/mallinkService/src/main/resources/mapper/WxThirdPartyConfigApi.xml b/mallinkService/src/main/resources/mapper/WxThirdPartyConfigApi.xml new file mode 100644 index 000000000..e9201195f --- /dev/null +++ b/mallinkService/src/main/resources/mapper/WxThirdPartyConfigApi.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + `id`,`tenant_id`,`parent_tenant_id`,`type`,`app_id`,`app_key`,`sign_key`,`api_url`, `token`,`token_expired_time`,`user_name`,`password`,`version`,`tp_id` + + + + where 1 = 1 + + + and `id` = #{id} + + + + and `tenant_id` = #{tenantId} + + + and `parent_tenant_id` = #{parentTenantId} + + + + and `type` = #{type} + + + + and `version` = #{version} + + + + and id in + + #{idItem} + + + order by ${sortColumns} + + + + + + +