diff --git a/src/main/java/com/hxgk/lowcode/controller/QrCodeController.java b/src/main/java/com/hxgk/lowcode/controller/QrCodeController.java index dcd5f78..d0c32c2 100644 --- a/src/main/java/com/hxgk/lowcode/controller/QrCodeController.java +++ b/src/main/java/com/hxgk/lowcode/controller/QrCodeController.java @@ -1,20 +1,25 @@ package com.hxgk.lowcode.controller; +import com.alibaba.fastjson.JSONObject; +import com.hxgk.lowcode.model.entity.UserByPort; +import com.hxgk.lowcode.model.entity.qywx.config.QyWxConfig; +import com.hxgk.lowcode.model.entity.qywx.security.pool.AuthenticationTokenPool; +import com.hxgk.lowcode.model.entity.qywx.security.pool.RedisPool; import com.hxgk.lowcode.model.entity.response.QrCodeDetailsResponseEntity; import com.hxgk.lowcode.service.QrCodeService; import com.hxgk.lowcode.utils.JsonData; +import com.hxgk.lowcode.utils.Md5; +import com.hxgk.lowcode.utils.ObjectFormatUtil; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletRequest; import java.awt.image.BufferedImage; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.Map; +import java.util.*; +import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/lowCode") @@ -22,6 +27,8 @@ public class QrCodeController { @Autowired QrCodeService qrCodeService; + @Autowired + private RedisPool redisPool; public QrCodeController(){ @@ -60,4 +67,103 @@ public class QrCodeController { } + /** + * 用于获取用于调用企业微信接口的签名 + * + * @param request + * @return + */ + @GetMapping("/QrCode/QyWxSignature") + @ResponseBody + public JsonData getQyWXSignature(@RequestParam Map map, HttpServletRequest request) { + // 随机字符串 + + + /*String token = request.getHeader("user-token"); + if (token == null) { + token = (String) request.getSession().getAttribute("userToken"); + }*/ + //UserByPort userToken = AuthenticationTokenPool.getUserToken(token); + + + Map result = new HashMap<>(); +// jsapi_ticket_app加密后 + String s = ""; +// jsapi_ticket_enterprises加密后 + String s1 = ""; + + // 时间戳 + long timestamp = 0; + + // 随机字符串 + String noncestr = ""; + + // 访问url + String url = QyWxConfig.jsApiUrl; + + s = (String) redisPool.getRedisTemplateByDb(14).opsForHash().get("JavaLowCode_QyWxScanQrCodeSignature", "jsapi_ticket_app"); + if (s != null) { + s1 = (String) redisPool.getRedisTemplateByDb(14).opsForHash().get("JavaLowCode_QyWxScanQrCodeSignature", "jsapi_ticket_enterprises"); + timestamp = ObjectFormatUtil.toLong((String) Objects.requireNonNull(redisPool.getRedisTemplateByDb(14).opsForHash().get("JavaLowCode_QyWxScanQrCodeSignature", "timestamp"))); + url = (String) redisPool.getRedisTemplateByDb(14).opsForHash().get("JavaLowCode_QyWxScanQrCodeSignature", "url"); + noncestr = (String) redisPool.getRedisTemplateByDb(14).opsForHash().get("JavaLowCode_QyWxScanQrCodeSignature", "noncestr"); + } else { + // 时间戳 + timestamp = System.currentTimeMillis() / 1000; + //生成从ASCII 32到126组成的随机字符串 +// String noncestr = RandomStringUtils.randomAscii(10); + // 时间戳字符串 + noncestr = String.valueOf(timestamp); + + // 获取企业的jsapi_ticket + + // user-agent加密 + String header = request.getHeader("user-agent"); + String crypt = Md5.crypt(header); + + JSONObject ticketForApp = QyWxConfig.getQyWxJsApiTicketForApp(crypt); + JSONObject ticketForEnterprise = QyWxConfig.getQyWxJsApiTicketForEnterprise(crypt); + Integer errcodeForEnterprise = ticketForEnterprise.getInteger("errcode"); + Integer errcodeForApp = ticketForApp.getInteger("errcode"); + // 获取企业的jsapi_ticket + String ticketForEnterprises = ""; + String ticketForEnterApps = ""; + if (Integer.compare(0, errcodeForEnterprise) == 0) { + ticketForEnterprises = ticketForEnterprise.getString("ticket"); + } + if (Integer.compare(0, errcodeForApp) == 0) { + ticketForEnterApps = ticketForApp.getString("ticket"); + } + Integer expires_in = ticketForEnterprise.getInteger("expires_in"); + + // 步骤1. 将这些参数拼接成字符串string1: + String jsapi_ticket_app = "jsapi_ticket=" + ticketForEnterApps + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; + String jsapi_ticket_enterprises = "jsapi_ticket=" + ticketForEnterprises + "&noncestr=" + noncestr + "×tamp=" + timestamp + "&url=" + url; + // 步骤2. 对string1进行sha1签名,得到signature + s = DigestUtils.sha1Hex(jsapi_ticket_app); + s1 = DigestUtils.sha1Hex(jsapi_ticket_enterprises); + + redisPool.getRedisTemplateByDb(14).opsForHash().put("JavaLowCode_QyWxScanQrCodeSignature", "jsapi_ticket_app", s); + redisPool.getRedisTemplateByDb(14).opsForHash().put("JavaLowCode_QyWxScanQrCodeSignature", "jsapi_ticket_enterprises", s1); + redisPool.getRedisTemplateByDb(14).opsForHash().put("JavaLowCode_QyWxScanQrCodeSignature", "timestamp", String.valueOf(timestamp)); + redisPool.getRedisTemplateByDb(14).opsForHash().put("JavaLowCode_QyWxScanQrCodeSignature", "url", url); + redisPool.getRedisTemplateByDb(14).opsForHash().put("JavaLowCode_QyWxScanQrCodeSignature", "noncestr", noncestr); + + redisPool.getRedisTemplateByDb(14).expire("JavaLowCode_QyWxScanQrCodeSignature", expires_in, TimeUnit.SECONDS); + + + } + result.put("timestamp", String.valueOf(timestamp)); + result.put("noncestr", noncestr); + result.put("jsapi_ticket_app", s); + result.put("jsapi_ticket_enterprises", s1); + result.put("corpid", QyWxConfig.corpid); + result.put("agentid", String.valueOf(QyWxConfig.AgentId)); + result.put("url", url); + + + return JsonData.buildSuccess(result); + } + + } diff --git a/src/main/java/com/hxgk/lowcode/model/entity/UserByPort.java b/src/main/java/com/hxgk/lowcode/model/entity/UserByPort.java new file mode 100644 index 0000000..7ff4677 --- /dev/null +++ b/src/main/java/com/hxgk/lowcode/model/entity/UserByPort.java @@ -0,0 +1,813 @@ +package com.hxgk.lowcode.model.entity; + + + +import java.time.Instant; + + +public class UserByPort { + // 登录令牌,默认有效期为三小时 + final long DEFAULT_TERM = 60 * 60 * 3; + + + /** + * id + */ + private Long id; + + /** + * 工号 + */ + private String number; + + /** + * 姓名 + */ + private String name; + + /** + * 头像 + */ + private String icon; + + /** + * 头像图片 + */ + private String iconphoto; + + /** + * 雇佣类型(1:雇佣入职;2:再入职;) + */ + private Integer hireclass; + + /** + * 用工关系(1:临时工 , 2:编外人员 ;3:实习&实习生;4:试用员工;5:待分配;6:待岗;7:临时调入;8:正式员工;9:长期病假;10:停薪留职;11:退休;12:辞职;13:辞退;14:离职) + */ + private Integer emptype; + + /** + * 入职公司 + */ + private Long company; + + /** + * 主部门 + */ + private Long maindeparment; + + /** + * 子部门 + */ + private Long sunmaindeparment; + + /** + * 部门 + */ + private String deparment; + + /** + * 所属行政组织 + */ + private Long adminorg; + + /** + * 职位 + */ + private Long position; + + /** + * 职务分类 + */ + private Integer jobclass; + + /** + * 职务 + */ + private Long jobid; + + /** + * 职务等级 + */ + private Integer jobleve; + + /** + * 写入时间 + */ + private Long time; + + /** + * 编辑时间 + */ + private Long eitetime; + + /** + * /微信UserId + */ + private String wechat; + + /** + * 企业微信UserId + */ + private String workwechat; + + /** + * 状态(1:启用;2:禁用;3:删除) + */ + private Integer state; + + /** + * 身份识别 + */ + private Long key; + + /** + * 是否为管理员(1:不是;2:分公司;3:集团管理员;4:超级管 + */ + private Integer isadmin; + + /** + * 密码 + */ + private String password; + + /** + * 角色 + */ + private String role; + + /** + * 身份证号 + */ + private String idcardno; + + /** + * 护照号码 + */ + private String passportno; + + /** + * 国际区号 + */ + private String globalroaming; + + /** + * 手机号码 + */ + private String mobilephone; + + /** + * 电子邮箱,比如 example@qq.com + */ + private String email; + + /** + * 性别(1:男性;2:女性;3:中性) + */ + private Integer gender; + + /** + * 生日 + */ + private Long birthday; + + /** + * 民族 + */ + private String myfolk; + + /** + * 籍贯 + */ + private String nativeplace; + + /** + * 身份证有效期开始 + */ + private Long idcardstartdate; + + /** + * 身份证有效期结束 + */ + private Long idcardenddate; + + /** + * 身份证地址 + */ + private String idcardaddress; + + /** + * 身份证签发机关 + */ + private String idcardIssued; + + /** + * 健康状况(1:良好;2:一般;3:较弱,4:有生理缺陷;5:残废) + */ + private Integer health; + + /** + * /婚姻状况(1:未婚;2:已婚;3:丧偶;4:离异) + */ + private Integer maritalstatus; + + /** + * 内线电话 + */ + private String internaltelephone; + + /** + * 现居住地址 + */ + private String currentresidence; + + /** + * 星座(1:白羊座;2:金牛座;3:双子座;4:巨蟹座;5:狮子座;6:处女座;7:天枰座;8:天蝎座;9:射手座;10:摩羯座;11:水瓶座;12:双鱼座 + */ + private Integer constellationing; + + /** + * 是否双职工(1:是;2:否) + */ + private Integer isdoubleworker; + + /** + * 是否为退役军人(1:是;2:否) + */ + private Integer isveterans; + + /** + * 退役证编号 + */ + private String veteransnumber; + + /** + * 参加工作日期 + */ + private Long jobstartdate; + + /** + * 入职日期 + */ + private Long entrydate; + + /** + * 试用期 + */ + private Integer probationperiod; + + /** + * 预计转正日期 + */ + private Long planformaldate; + + /** + * 政治面貌(1:群众;2:无党派;3:台盟会员;4:九三社员;5:致公党员;6:农工党员;7:民进会员;8:民建会员;9:民盟盟员;10:民革会员,11:共青团员;12:预备党员;13:中共党员) + */ + private Integer politicaloutlook; + + /** + * 公司名称 + */ + private String companyname; + + /** + * 主部门 + */ + private String maindeparmentname; + + /** + * 岗位 + */ + private String sunmaindeparmentname; + + private String workpostname; + + /** + * 职位 + */ + private String positionname; + + /** + * 是否为本部门负责人(1:是;2:否) + */ + private Integer personincharge; + + /** + * 身份认证 + */ + private String keystr; + + /** + * 放入的时间 + */ + private Instant instant; + + /** + * 有效期(单位:秒) + */ + private long term = DEFAULT_TERM; + + //version4 + private String teamid; + + /** + * 根据时间判断是否有效 + * + * @return 有效则返回true,否则返回false + */ + public boolean isValid() { + return Instant.now().getEpochSecond() - instant.getEpochSecond() <= term; + } + + public long getDEFAULT_TERM() { + return DEFAULT_TERM; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } + + public String getIconphoto() { + return iconphoto; + } + + public void setIconphoto(String iconphoto) { + this.iconphoto = iconphoto; + } + + public Integer getHireclass() { + return hireclass; + } + + public void setHireclass(Integer hireclass) { + this.hireclass = hireclass; + } + + public Integer getEmptype() { + return emptype; + } + + public void setEmptype(Integer emptype) { + this.emptype = emptype; + } + + public Long getCompany() { + return company; + } + + public void setCompany(Long company) { + this.company = company; + } + + public Long getMaindeparment() { + return maindeparment; + } + + public void setMaindeparment(Long maindeparment) { + this.maindeparment = maindeparment; + } + + public Long getSunmaindeparment() { + return sunmaindeparment; + } + + public void setSunmaindeparment(Long sunmaindeparment) { + this.sunmaindeparment = sunmaindeparment; + } + + public String getDeparment() { + return deparment; + } + + public void setDeparment(String deparment) { + this.deparment = deparment; + } + + public Long getAdminorg() { + return adminorg; + } + + public void setAdminorg(Long adminorg) { + this.adminorg = adminorg; + } + + public Long getPosition() { + return position; + } + + public void setPosition(Long position) { + this.position = position; + } + + public Integer getJobclass() { + return jobclass; + } + + public void setJobclass(Integer jobclass) { + this.jobclass = jobclass; + } + + public Long getJobid() { + return jobid; + } + + public void setJobid(Long jobid) { + this.jobid = jobid; + } + + public Integer getJobleve() { + return jobleve; + } + + public void setJobleve(Integer jobleve) { + this.jobleve = jobleve; + } + + public Long getTime() { + return time; + } + + public void setTime(Long time) { + this.time = time; + } + + public Long getEitetime() { + return eitetime; + } + + public void setEitetime(Long eitetime) { + this.eitetime = eitetime; + } + + public String getWechat() { + return wechat; + } + + public void setWechat(String wechat) { + this.wechat = wechat; + } + + public String getWorkwechat() { + return workwechat; + } + + public void setWorkwechat(String workwechat) { + this.workwechat = workwechat; + } + + public Integer getState() { + return state; + } + + public void setState(Integer state) { + this.state = state; + } + + public Long getKey() { + return key; + } + + public void setKey(Long key) { + this.key = key; + } + + public Integer getIsadmin() { + return isadmin; + } + + public void setIsadmin(Integer isadmin) { + this.isadmin = isadmin; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getRole() { + return role; + } + + public void setRole(String role) { + this.role = role; + } + + public String getIdcardno() { + return idcardno; + } + + public void setIdcardno(String idcardno) { + this.idcardno = idcardno; + } + + public String getPassportno() { + return passportno; + } + + public void setPassportno(String passportno) { + this.passportno = passportno; + } + + public String getGlobalroaming() { + return globalroaming; + } + + public void setGlobalroaming(String globalroaming) { + this.globalroaming = globalroaming; + } + + public String getMobilephone() { + return mobilephone; + } + + public void setMobilephone(String mobilephone) { + this.mobilephone = mobilephone; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Integer getGender() { + return gender; + } + + public void setGender(Integer gender) { + this.gender = gender; + } + + public Long getBirthday() { + return birthday; + } + + public void setBirthday(Long birthday) { + this.birthday = birthday; + } + + public String getMyfolk() { + return myfolk; + } + + public void setMyfolk(String myfolk) { + this.myfolk = myfolk; + } + + public String getNativeplace() { + return nativeplace; + } + + public void setNativeplace(String nativeplace) { + this.nativeplace = nativeplace; + } + + public Long getIdcardstartdate() { + return idcardstartdate; + } + + public void setIdcardstartdate(Long idcardstartdate) { + this.idcardstartdate = idcardstartdate; + } + + public Long getIdcardenddate() { + return idcardenddate; + } + + public void setIdcardenddate(Long idcardenddate) { + this.idcardenddate = idcardenddate; + } + + public String getIdcardaddress() { + return idcardaddress; + } + + public void setIdcardaddress(String idcardaddress) { + this.idcardaddress = idcardaddress; + } + + public String getIdcardIssued() { + return idcardIssued; + } + + public void setIdcardIssued(String idcardIssued) { + this.idcardIssued = idcardIssued; + } + + public Integer getHealth() { + return health; + } + + public void setHealth(Integer health) { + this.health = health; + } + + public Integer getMaritalstatus() { + return maritalstatus; + } + + public void setMaritalstatus(Integer maritalstatus) { + this.maritalstatus = maritalstatus; + } + + public String getInternaltelephone() { + return internaltelephone; + } + + public void setInternaltelephone(String internaltelephone) { + this.internaltelephone = internaltelephone; + } + + public String getCurrentresidence() { + return currentresidence; + } + + public void setCurrentresidence(String currentresidence) { + this.currentresidence = currentresidence; + } + + public Integer getConstellationing() { + return constellationing; + } + + public void setConstellationing(Integer constellationing) { + this.constellationing = constellationing; + } + + public Integer getIsdoubleworker() { + return isdoubleworker; + } + + public void setIsdoubleworker(Integer isdoubleworker) { + this.isdoubleworker = isdoubleworker; + } + + public Integer getIsveterans() { + return isveterans; + } + + public void setIsveterans(Integer isveterans) { + this.isveterans = isveterans; + } + + public String getVeteransnumber() { + return veteransnumber; + } + + public void setVeteransnumber(String veteransnumber) { + this.veteransnumber = veteransnumber; + } + + public Long getJobstartdate() { + return jobstartdate; + } + + public void setJobstartdate(Long jobstartdate) { + this.jobstartdate = jobstartdate; + } + + public Long getEntrydate() { + return entrydate; + } + + public void setEntrydate(Long entrydate) { + this.entrydate = entrydate; + } + + public Integer getProbationperiod() { + return probationperiod; + } + + public void setProbationperiod(Integer probationperiod) { + this.probationperiod = probationperiod; + } + + public Long getPlanformaldate() { + return planformaldate; + } + + public void setPlanformaldate(Long planformaldate) { + this.planformaldate = planformaldate; + } + + public Integer getPoliticaloutlook() { + return politicaloutlook; + } + + public void setPoliticaloutlook(Integer politicaloutlook) { + this.politicaloutlook = politicaloutlook; + } + + public String getCompanyname() { + return companyname; + } + + public void setCompanyname(String companyname) { + this.companyname = companyname; + } + + public String getMaindeparmentname() { + return maindeparmentname; + } + + public void setMaindeparmentname(String maindeparmentname) { + this.maindeparmentname = maindeparmentname; + } + + public String getSunmaindeparmentname() { + return sunmaindeparmentname; + } + + public void setSunmaindeparmentname(String sunmaindeparmentname) { + this.sunmaindeparmentname = sunmaindeparmentname; + } + + public String getWorkpostname() { + return workpostname; + } + + public void setWorkpostname(String workpostname) { + this.workpostname = workpostname; + } + + public String getPositionname() { + return positionname; + } + + public void setPositionname(String positionname) { + this.positionname = positionname; + } + + public Integer getPersonincharge() { + return personincharge; + } + + public void setPersonincharge(Integer personincharge) { + this.personincharge = personincharge; + } + + public String getKeystr() { + return keystr; + } + + public void setKeystr(String keystr) { + this.keystr = keystr; + } + + public Instant getInstant() { + return instant; + } + + public void setInstant(Instant instant) { + this.instant = instant; + } + + public long getTerm() { + return term; + } + + public void setTerm(long term) { + this.term = term; + } + + public String getTeamid() { + return teamid; + } + + public void setTeamid(String teamid) { + this.teamid = teamid; + } +} diff --git a/src/main/java/com/hxgk/lowcode/model/entity/qywx/config/QyWxConfig.java b/src/main/java/com/hxgk/lowcode/model/entity/qywx/config/QyWxConfig.java new file mode 100644 index 0000000..322439f --- /dev/null +++ b/src/main/java/com/hxgk/lowcode/model/entity/qywx/config/QyWxConfig.java @@ -0,0 +1,119 @@ +package com.hxgk.lowcode.model.entity.qywx.config; + + +// 用于实现企业微信相关配置 + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.hxgk.lowcode.model.entity.qywx.security.pool.RedisPool; +import com.hxgk.lowcode.utils.HttpUtils; +import com.hxgk.lowcode.utils.ObjectFormatUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + + +@Component +public class QyWxConfig { + public static String corpid = "ww02f310301953277a"; // 企业的CorpID + public static String secret = "YdBDEO9nawZaYuKcgS9jFme8x4eEbTPkIgBnlarG_GQ"; // 应用的凭证密钥 + public static int AgentId = 1000108; //应用agentid + + + + + public static RedisPool redisPool; + + // 用于jsapi的url + public static String jsApiUrl = "https://wab.hxgk.group/"; + + + @Autowired + public void setRedisTemplate(RedisPool redisPool) { + QyWxConfig.redisPool = redisPool; + } + + + + // 用于获取企业微信对应token + public static String GetQYWXToken(String UserAgent) { + // 获取redis中当前用户的user-agent + String token_redis = redisPool.getRedisTemplateByDb(14).opsForValue().get("JavaLowCode_access_token_" + UserAgent); + if (token_redis != null) { + return token_redis; + } else { + String url = String.format(" https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", QyWxConfig.corpid, QyWxConfig.secret); + String get = HttpUtils.doGet(url); + JSONObject jsonObject = JSONObject.parseObject(get); + Integer errcord = ObjectFormatUtil.toInteger(jsonObject.get("errcode")); + String accessToken = (String) jsonObject.get("access_token"); + String errmsg = (String) jsonObject.get("errmsg"); + if (errcord == 0) { + // 如果成功获取access_token + + // 定义redis过期时间 + Long expires_in = 7200L; + + // 获取当前redis中存储的key值 + Set keys = redisPool.getRedisTemplateByDb(14).keys("JavaLowCode_access_token_*"); + // 获取遍历器 + for (String next : keys) { // 如果有下个,则进行遍历 + // 获取当前key + // 当前key对应的token + String next_access_token = redisPool.getRedisTemplateByDb(14).opsForValue().get(next); + if (accessToken.equals(next_access_token)) { + // 如果当前token相等 + + // 获取当前的过期时间 + expires_in = redisPool.getRedisTemplateByDb(14).opsForValue().getOperations().getExpire(next, TimeUnit.SECONDS); + } + } + + redisPool.getRedisTemplateByDb(14).opsForValue().set("JavaLowCode_access_token_" + UserAgent, accessToken); + redisPool.getRedisTemplateByDb(14).expire("JavaLowCode_access_token_" + UserAgent, expires_in, TimeUnit.SECONDS); + + return accessToken; + } else { + // 否则返回空值 + return "visitToFail:" + errmsg; + } + } + } + + + + // 获取企业的jsapi_ticket + public static JSONObject getQyWxJsApiTicketForEnterprise(String UserAgent) { + // 如果token不为"",则使用token,否则获取新token + String tempToken = GetQYWXToken(UserAgent); + String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=%s", tempToken); + String get = HttpUtils.doGet(url); + JSONObject jsonObject = JSONObject.parseObject(get); + return jsonObject; + } + + // 获取应用的jsapi_ticket + public static JSONObject getQyWxJsApiTicketForApp(String UserAgent) { + // 如果token不为"",则使用token,否则获取新token + String tempToken = GetQYWXToken(UserAgent); + String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=%s&type=agent_config", tempToken); + String get = HttpUtils.doGet(url); + JSONObject jsonObject = JSONObject.parseObject(get); + return jsonObject; + } + + + + + + + + +} diff --git a/src/main/java/com/hxgk/lowcode/model/entity/qywx/security/pool/AuthenticationTokenPool.java b/src/main/java/com/hxgk/lowcode/model/entity/qywx/security/pool/AuthenticationTokenPool.java new file mode 100644 index 0000000..ef27700 --- /dev/null +++ b/src/main/java/com/hxgk/lowcode/model/entity/qywx/security/pool/AuthenticationTokenPool.java @@ -0,0 +1,55 @@ +package com.hxgk.lowcode.model.entity.qywx.security.pool; + + +import com.hxgk.lowcode.model.entity.UserByPort; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class AuthenticationTokenPool { + /** + * 认证后的令牌连接池 + */ + private static Map pool = new ConcurrentHashMap<>(100); + + public static void addUserToken(String token, UserByPort userToken) { + pool.put(token, userToken); + } + + /** + * 根据token凭证获取未过期的令牌,如果没有未过期的令牌则返回null + * + * @param token 凭证 + * @return userToken 未过期的令牌 + */ + public static UserByPort getUserToken(String token) { + if(token == null){ + return null; + } + UserByPort userToken = pool.get(token); + + //如果没有相应令牌则直接返回null + if (userToken == null) { + return null; + } + + //判断令牌是否过期 + if (userToken.isValid()) { + return userToken; + } else { + //清除过期令牌 + pool.remove(token); + return null; + } + } + + /** + * 根据凭证删除对应的令牌 + * + * @param token 凭证 + */ + public static void removeUserToken(String token) { + pool.remove(token); + } + +} diff --git a/src/main/java/com/hxgk/lowcode/model/entity/qywx/security/pool/RedisPool.java b/src/main/java/com/hxgk/lowcode/model/entity/qywx/security/pool/RedisPool.java new file mode 100644 index 0000000..9eafe82 --- /dev/null +++ b/src/main/java/com/hxgk/lowcode/model/entity/qywx/security/pool/RedisPool.java @@ -0,0 +1,111 @@ +package com.hxgk.lowcode.model.entity.qywx.security.pool; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import javax.annotation.PostConstruct; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * redis连接池 + */ +@Configuration +public class RedisPool { + //redis地址 + @Value("${redisPool.host}") + private String host; + + //redis端口号 + @Value("${redisPool.port}") + private int port; + + //redis密码 + @Value("${redisPool.password}") + private String password; + + //默认数据库 + private int defaultDB; + + //多个数据库集合 + @Value("${redisPool.dbs}") + private List dbList; + + //RedisTemplate实例 + private static Map> redisTemplateMap = new HashMap<>(); + + /** + * 初始化连接池 + */ + @PostConstruct + public void initRedisTemplate() { + defaultDB = dbList.get(0);//设置默认数据库 + for (Integer db : dbList) { + //存储多个RedisTemplate实例 + redisTemplateMap.put(db, redisTemplate(db)); + } + } + + public LettuceConnectionFactory redisConnection(int db) { + RedisStandaloneConfiguration server = new RedisStandaloneConfiguration(); + server.setHostName(host); // 指定地址 + server.setDatabase(db); // 指定数据库 + server.setPort(port); //指定端口 + server.setPassword(password); //指定密码 + LettuceConnectionFactory factory = new LettuceConnectionFactory(server); + factory.afterPropertiesSet(); //刷新配置 + return factory; + } + + //RedisTemplate模板 + public RedisTemplate redisTemplate(int db) { + + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnection(db)); + + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(StandardCharsets.UTF_8); + // key 的序列化规则 + redisTemplate.setKeySerializer(stringRedisSerializer); + //String的序列化 + + //key采用String的序列化方式 + redisTemplate.setKeySerializer(stringRedisSerializer); + //hash的key采用String的序列化方式 + redisTemplate.setHashKeySerializer(stringRedisSerializer); + //value序列化方式采用String + redisTemplate.setValueSerializer(stringRedisSerializer); + //hash序列化方式采用String + redisTemplate.setHashValueSerializer(stringRedisSerializer); + + + redisTemplate.afterPropertiesSet(); + + return redisTemplate; + } + + + + /** + * 指定数据库进行切换 + * @param db 数据库索引 + * @return + */ + public RedisTemplate getRedisTemplateByDb(int db) { + return redisTemplateMap.get(db); + } + + /** + * 使用默认数据库 + * + * @return + */ + public RedisTemplate getRedisTemplate() { + return redisTemplateMap.get(defaultDB); + } +} diff --git a/src/main/java/com/hxgk/lowcode/utils/Md5.java b/src/main/java/com/hxgk/lowcode/utils/Md5.java new file mode 100644 index 0000000..c281a51 --- /dev/null +++ b/src/main/java/com/hxgk/lowcode/utils/Md5.java @@ -0,0 +1,38 @@ +package com.hxgk.lowcode.utils; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * MD5加密 + */ +public class Md5 { + + /** + * Encodes a string + * @param str String to encode + * @return Encoded String + */ + public static String crypt(String str) { + if (str == null || str.length() == 0) { + throw new IllegalArgumentException("String to encript cannot be null or zero length"); + } + StringBuilder hexString = new StringBuilder(); + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(str.getBytes()); + byte[] hash = md.digest(); + for (byte b : hash) { + if ((0xff & b) < 0x10) { + hexString.append("0").append(Integer.toHexString((0xFF & b))); + } else { + hexString.append(Integer.toHexString(0xFF & b)); + } + } + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return hexString.toString(); + } + +} diff --git a/src/main/java/com/hxgk/lowcode/utils/ObjectFormatUtil.java b/src/main/java/com/hxgk/lowcode/utils/ObjectFormatUtil.java new file mode 100644 index 0000000..d029263 --- /dev/null +++ b/src/main/java/com/hxgk/lowcode/utils/ObjectFormatUtil.java @@ -0,0 +1,150 @@ +package com.hxgk.lowcode.utils; + + +import java.math.BigDecimal; +import java.util.*; + +/** + * 此工具类封装一些常用类型转换操作 + */ +public class ObjectFormatUtil { + /** + * 一种较为安全的Object转换Integer方式 + * + * @param o 要转化的对象 + * @return 转化的Integer类型结果 + */ + public static Integer toInteger(Object o) { + if (o == null) { + return null; + } + String s = o.toString(); + s = s.replaceAll(",", ""); + return Integer.parseInt(s); + } + + /** + * 一种较为安全的Object转换Long方式 + * + * @param o 要转化的对象 + * @return 转化的Long类型结果 + */ + public static Long toLong(Object o) { + if (o == null) { + return null; + } + String s = o.toString(); + s = s.replaceAll(",", ""); + return Long.parseLong(s); + } + + public static Boolean toBoolean(Object o) { + return (o == null) ? null : Boolean.parseBoolean(o.toString()); + } + + public static Double toDouble(Object o) { + if (o == null) { + return null; + } + String s = o.toString(); + s = s.replaceAll(",", ""); + return Double.parseDouble(s); + } + + // double类型相加 + public static double sum(double d1, double d2) { + BigDecimal bd1 = new BigDecimal(Double.toString(d1)); + BigDecimal bd2 = new BigDecimal(Double.toString(d2)); + return bd1.add(bd2).doubleValue(); + } + + // double类型相减 + public static double subtract(double d1, double d2) { + BigDecimal bd1 = new BigDecimal(Double.toString(d1)); + BigDecimal bd2 = new BigDecimal(Double.toString(d2)); + return bd1.subtract(bd2).doubleValue(); + } + + // double 类型相乘 + public static double multiply(double d1, double d2) { + BigDecimal bd1 = new BigDecimal(Double.toString(d1)); + BigDecimal bd2 = new BigDecimal(Double.toString(d2)); + return bd1.multiply(bd2).doubleValue(); + } + + // double 类型相除,保留位数 + public static double divide(double a, double b, int scale) { + BigDecimal bd1 = new BigDecimal(Double.toString(a)); + BigDecimal bd2 = new BigDecimal(Double.toString(b)); + return bd1.divide(bd2, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); + } + + + /** + * 用于Object转为指定类型的列表 + * + * @param obj 待转换obj + * @param cla 要转换为的类型 + * @param 泛型 + * @return + */ + public static List objToList(Object obj, Class cla) { + List list = new ArrayList(); + if (obj instanceof ArrayList) { + for (Object o : (List) obj) { + list.add(cla.cast(o)); + } + return list; + } + return null; + } + + + /** + * author:liwenxuan + * + */ + public static ArrayList mapToList(Map map){ + HashMap longHashMap = new HashMap(); + ArrayList ids = new ArrayList<>(); + Iterator iterator = map.keySet().iterator(); + while (iterator.hasNext()) { + String key = iterator.next(); + ArrayList valueList = (ArrayList) map.get(key); + String value_str = ""; + for(int i = 0;i