| @@ -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; | |||
| @@ -0,0 +1,26 @@ | |||
| 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.beans.factory.annotation.Value; | |||
| import org.springframework.web.bind.annotation.GetMapping; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| @RestController | |||
| @Api(description = "") | |||
| public class HomeController extends BaseController { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Value("${version}") | |||
| private String version; | |||
| @ApiOperation("获取后端版本号") | |||
| @GetMapping("/version") | |||
| public ResultData version() { | |||
| logger.debug("[" + getIpAddr() + "] HomeController::version"); | |||
| return new ResultData(version); | |||
| } | |||
| } | |||
| @@ -0,0 +1,63 @@ | |||
| package com.iformall.controller; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.utils.HttpUtil; | |||
| import com.iformall.utils.sign.SignUtils; | |||
| 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.Date; | |||
| import java.util.HashMap; | |||
| 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<String, String> map) { | |||
| logger.info("UserBasicInfoController >>>>>>>> userRegister >>>>>>>>>>>>>>>>>"+map.toString()); | |||
| String phone = map.get("phone"); | |||
| logger.info("UserBasicInfoController >>>>>>>> userRegister >>>>>>>>>>>>>>>>>phone="+phone); | |||
| return new ResultData(); | |||
| } | |||
| public static void main(String[] args) { | |||
| String appId = "CP6nwHvZ"; | |||
| String appKey = "b257eb97bb9380714306a077a66086e8b1f10639"; | |||
| String signKey = "C61842C8570524AA81756C53B8096978A06AE00D"; | |||
| Map<String,String> headMap = new HashMap<>(); | |||
| headMap.put("cookie",appId+"&"+appKey); | |||
| Map<String,String> paramMap = new HashMap<>(); | |||
| paramMap.put("timeStamp",Long.toString(new Date().getTime()));//当前时间戳 | |||
| paramMap.put("phone","17600293031"); | |||
| paramMap.put("userName","昵称"); | |||
| paramMap.put("userSex","1"); | |||
| String sign = SignUtils.getSign(signKey, paramMap, "MD5"); | |||
| headMap.put("sign",sign); | |||
| HttpUtil.doPost("https://openapitest.malls.iformall.com/api/userInfo/register",headMap,paramMap); | |||
| } | |||
| } | |||
| @@ -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,84 @@ import javax.servlet.http.HttpServletResponse; | |||
| @Component | |||
| public class AuthorizationInterceptor extends HandlerInterceptorAdapter { | |||
| private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |||
| @Autowired | |||
| @Qualifier("objectCommonRedisTemplate") | |||
| RedisTemplate<String, Object> 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(),"非法请求"); | |||
| } | |||
| if(StringUtils.isBlank(nonceStr) || "null".equals(nonceStr) || "undefined".equals(nonceStr)){ | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"nonceStr为空["+nonceStr+"]"); | |||
| 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(signKey) || "null".equals(signKey) || "undefined".equals(signKey)){ | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"signKey为空["+signKey+"]"); | |||
| String signature = request.getHeader("sign"); | |||
| logger.info("sign={}"+signature); | |||
| //没有加密 | |||
| if (StringUtils.isBlank(signature)) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"缺少加密串"); | |||
| } | |||
| //nonceStr必须唯一,为防止接口盗刷,每次只能调用一次 | |||
| Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, "publicApi:"+nonceStr); | |||
| if (null != cache) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"不能重复调用"); | |||
| 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(),"请求过期"); | |||
| } | |||
| // String nonceStr = request.getParameter("nonceStr"); | |||
| // //重复调用 | |||
| // Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+nonceStr); | |||
| // if (null != cache) { | |||
| // throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||
| // } | |||
| Enumeration<String> 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); | |||
| Integer cache = RedisCacheUtils.getCacheInteger(redisTemplate, Constant.publicApiNonce+signature); | |||
| if (null != cache) { | |||
| throw new MallinkException(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"重复调用"); | |||
| } | |||
| RedisCacheUtils.cache(redisTemplate, Constant.publicApiNonce+signature, 1, 300); | |||
| return true; | |||
| } | |||
| } | |||
| @@ -1,7 +1,7 @@ | |||
| server: | |||
| port: 7000 | |||
| port: 7070 | |||
| servlet: | |||
| context-path: /C | |||
| context-path: /public | |||
| spring: | |||
| application: | |||
| @@ -599,6 +599,7 @@ public enum ErrorCode{ | |||
| QUESTION_USER_LINE(61007, "已经参与!"), | |||
| QUESTION_NOT_START(61008, "问卷未开始"), | |||
| QUESTION_END(61009, "问卷已结束"), | |||
| QUESTION_END_LINE(61010, "问卷已下线"), | |||
| /** | |||
| * 数衍信息 | |||
| @@ -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; | |||
| } | |||
| @@ -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<WxThirdPartyApi, Long> { | |||
| List<WxThirdPartyApi> findList(WxThirdPartyApi apiConfig); | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| package com.iformall.service; | |||
| import com.iformall.domain.po.WxThirdPartyApi; | |||
| import java.util.List; | |||
| /** | |||
| * | |||
| */ | |||
| public interface WxThirdPartyApiService { | |||
| /** | |||
| * 查询列表 | |||
| * | |||
| * @param record | |||
| */ | |||
| List<WxThirdPartyApi> findList(WxThirdPartyApi record); | |||
| WxThirdPartyApi findByApp(String appId, String appKey); | |||
| } | |||
| @@ -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; | |||
| @@ -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){ | |||
| @@ -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); | |||
| } | |||
| @@ -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<String, Object> redisTemplate; | |||
| @Override | |||
| public List<WxThirdPartyApi> 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; | |||
| } | |||
| } | |||
| @@ -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"; | |||
| @@ -33,10 +33,7 @@ import java.net.URL; | |||
| import java.nio.charset.Charset; | |||
| import java.security.KeyStore; | |||
| import java.security.SecureRandom; | |||
| import java.util.ArrayList; | |||
| import java.util.Iterator; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.*; | |||
| /** | |||
| @@ -169,6 +166,72 @@ public class HttpUtil { | |||
| return null; | |||
| } | |||
| /** | |||
| * post请求(用于key-value格式的参数) | |||
| * @param url | |||
| * @param params | |||
| * @return | |||
| */ | |||
| public static String doPost(String url,Map<String,String> headMap, Map params){ | |||
| // 定义HttpClient | |||
| CloseableHttpClient client = HttpClients.createDefault(); | |||
| BufferedReader in = null; | |||
| try { | |||
| // 实例化HTTP方法 | |||
| HttpPost request = new HttpPost(); | |||
| request.setURI(new URI(url)); | |||
| if(headMap != null){ | |||
| Set<String> set = headMap.keySet(); | |||
| for(String key: set){ | |||
| request.addHeader(key,headMap.get(key)); | |||
| } | |||
| } | |||
| //设置参数 | |||
| List<NameValuePair> nvps = new ArrayList<NameValuePair>(); | |||
| for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { | |||
| String name = (String) iter.next(); | |||
| String value = String.valueOf(params.get(name)); | |||
| nvps.add(new BasicNameValuePair(name, value)); | |||
| //System.out.println(name +"-"+value); | |||
| } | |||
| request.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8)); | |||
| HttpResponse response = client.execute(request); | |||
| int code = response.getStatusLine().getStatusCode(); | |||
| if(code == 200){ //请求成功 | |||
| in = new BufferedReader(new InputStreamReader(response.getEntity() | |||
| .getContent(),"utf-8")); | |||
| StringBuilder sb = new StringBuilder(""); | |||
| String line = ""; | |||
| String NL = System.getProperty("line.separator"); | |||
| while ((line = in.readLine()) != null) { | |||
| sb.append(line + NL); | |||
| } | |||
| in.close(); | |||
| client.close(); | |||
| return sb.toString(); | |||
| } | |||
| else{ // | |||
| logger.info("状态码:" + code); | |||
| client.close(); | |||
| } | |||
| } | |||
| catch(Exception e){ | |||
| logger.error(e.getMessage()); | |||
| return null; | |||
| } | |||
| return null; | |||
| } | |||
| /** | |||
| * post请求(用于key-value格式的参数,wiwide) | |||
| * @param url | |||
| @@ -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: <p> | |||
| * 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。 | |||
| * 本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符, | |||
| * 这样重复率大大降低。 | |||
| * 经测试,在生成一千万个数据也没有出现重复,完全满足大部分需求。 | |||
| * </p> | |||
| */ | |||
| 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(); | |||
| } | |||
| /** | |||
| * <p> | |||
| * 通过appId和内置关键词生成APP key | |||
| * </P> | |||
| */ | |||
| 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(); | |||
| } | |||
| } | |||
| /** | |||
| * <p> | |||
| * | |||
| * </P> | |||
| */ | |||
| 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); | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package com.iformall.utils.gooagoo; | |||
| package com.iformall.utils.sign; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| @@ -0,0 +1,65 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
| <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | |||
| <mapper namespace="com.iformall.mapper.WxThirdPartyApiMapper"> | |||
| <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxThirdPartyApi"> | |||
| <id column="id" jdbcType="BIGINT" property="id"/> | |||
| <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/> | |||
| <result column="parent_tenant_id" jdbcType="VARCHAR" property="parentTenantId" /> | |||
| <result column="type" jdbcType="INTEGER" property="type"/> | |||
| <result column="app_id" jdbcType="VARCHAR" property="appId"/> | |||
| <result column="app_key" jdbcType="VARCHAR" property="appKey"/> | |||
| <result column="sign_key" jdbcType="VARCHAR" property="signKey"/> | |||
| <result column="api_url" jdbcType="VARCHAR" property="apiUrl"/> | |||
| <result column="token" jdbcType="VARCHAR" property="token"/> | |||
| <result column="token_expired_time" jdbcType="TIMESTAMP" property="tokenExpiredTime"/> | |||
| <result column="user_name" jdbcType="VARCHAR" property="userName"/> | |||
| <result column="password" jdbcType="VARCHAR" property="password"/> | |||
| <result column="version" jdbcType="VARCHAR" property="version"/> | |||
| <result column="tp_id" jdbcType="VARCHAR" property="tpId"/> | |||
| </resultMap> | |||
| <sql id="allColumns"> | |||
| `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` | |||
| </sql> | |||
| <sql id="dynamicWhereConditions"> | |||
| where 1 = 1 | |||
| <if test=" null != id "> | |||
| and `id` = #{id} | |||
| </if> | |||
| <if test=" null != tenantId and '' != tenantId"> | |||
| and `tenant_id` = #{tenantId} | |||
| </if> | |||
| <if test=" null != parentTenantId and '' != parentTenantId"> | |||
| and `parent_tenant_id` = #{parentTenantId} | |||
| </if> | |||
| <if test=" null != type "> | |||
| and `type` = #{type} | |||
| </if> | |||
| <if test=" null != version and '' != version"> | |||
| and `version` = #{version} | |||
| </if> | |||
| <if test=" null != ids "> | |||
| and id in | |||
| <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")"> | |||
| #{idItem} | |||
| </foreach> | |||
| </if> | |||
| <if test=" null != sortColumns">order by ${sortColumns}</if> | |||
| </sql> | |||
| <select id="findList" parameterType="com.iformall.domain.po.WxThirdPartyApi" resultMap="BaseResultMap"> | |||
| select | |||
| <include refid="allColumns"/> | |||
| from wx_third_party_api | |||
| <include refid="dynamicWhereConditions"/> | |||
| </select> | |||
| </mapper> | |||