package com.ydproject.util; \nimport java.util.ArrayList; \nimport java.util.Date; \nimport java.util.List; \nimport org.json.JSONArray; \nimport org.json.JSONObject; \nimport com.ydproject.dao.DaoWx; \nimport com.ydproject.model.WxModel; \npublic class WxCloudUtil { \n\tprivate String appid = "667778866666666666"; \n\tprivate String secret = "234556677777777777777777"; \n\tprivate String entranceBase = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri="; \n\tprivate String entranceTail = "&response_type=code&scope=snsapi_base&state=123#wechat_redirect"; \n\tprivate String tokenFile = "token.ini"; \n\tprivate String cloud_env = "cloud1-659e2"; \n\tprivate String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; \n\tprivate String query_url = "https://api.weixin.qq.com/tcb/databasequery?access_token="; \n\tprivate String count_url = "https://api.weixin.qq.com/tcb/databasecount?access_token="; \n\tprivate String update_url = "https://api.weixin.qq.com/tcb/databaseupdate?access_token="; \n\tprivate String delete_url = "https://api.weixin.qq.com/tcb/databasedelete?access_token="; \n\tprivate String add_url = "https://api.weixin.qq.com/tcb/databaseadd?access_token="; \n\tprivate String download_url = "https://api.weixin.qq.com/tcb/batchdownloadfile?access_token="; \n\tprivate String deletefile_url = "https://api.weixin.qq.com/tcb/batchdeletefile?access_token="; \n\tprivate String dbExport_url = "https://api.weixin.qq.com/tcb/databasemigrateexport?access_token="; \n\tprivate String dbExportStatus_url = "https://api.weixin.qq.com/tcb/databasemigratequeryinfo?access_token="; \n\t@SuppressWarnings("unused") \n\tprivate String downloadFolder = "user/"; \n\tpublic WxCloudUtil() { \n\t} \n\tpublic WxCloudUtil(String tokenFile, String downloadFolder, String appid, String secret, String cloud_env) { \n\t this.tokenFile = tokenFile; \n\t this.downloadFolder = downloadFolder; \n\t this.appid = appid; \n\t this.secret = secret; \n\t this.cloud_env = cloud_env; \n\t this.token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; \n\t} \n\tpublic String getCode2SessionURL(String code) { \n\t return getCode2SessionURL(this.appid, this.secret, code); \n\t} \n\tpublic String getCode2SessionURL(String appid, String secret, String code) { \n\t return "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"; \n\t} \n\tpublic String getBaseAuthUrl(String redirUrl) { \n\t //只能带一个自定义参数 \n\t return entranceBase + redirUrl + entranceTail; \n\t} \n\t//初始获取token \n\tpublic String getToken(String source) { \n\t String token = null; \n\t double expire = -1; \n\t if (source.equals("file")) { \n\t FileUtil fp = new FileUtil(tokenFile, "utf8"); \n\t String line = fp.read(); \n\t if (line != null) { \n\t token = line.split("|")[0]; \n\t expire = Double.parseDouble(line.split("|")[1]); \n\t } \n\t } else if (source.equals("db")) { \n\t WxModel wxModel = new WxModel(); \n\t wxModel.setAppid(appid); \n\t DaoWx daoWx = new DaoWx(); \n\t WxModel temp = daoWx.getAppInfo(wxModel); \n\t if (temp != null) { \n\t token = temp.getToken(); \n\t expire = Double.parseDouble(temp.getExpire()); \n\t } \n\t } \n\t long now = new Date().getTime(); \n\t if (now >= expire || token == null) { \n\t token = refreshToken(source); \n\t if (token != null) { \n\t expire = Double.parseDouble(token.split("|")[1]); \n\t token = token.split("|")[0]; \n\t } \n\t } \n\t return token == null ? token : token + "|" + expire; \n\t} \n\t//检查token有效 返回可用token|expire \n\tpublic String checkToken(String token, double expire, String source) { \n\t if (new Date().getTime() >= expire) { \n\t token = refreshToken(source); \n\t } \n\t return token; // null or token|expire \n\t} \n\t//云函数查询 query count delete update add deletefile \n\tpublic JSONObject cloudQuery(String queryType, String token, JSONObject query) { \n\t String url = ""; \n\t if (queryType.equals("query")) { \n\t // query:""; \n\t url = query_url + token; \n\t } else if (queryType.equals("count")) { \n\t // query:""; \n\t url = count_url + token; \n\t } else if (queryType.equals("delete")) { \n\t // query:""; \n\t url = delete_url + token; \n\t } else if (queryType.equals("update")) { \n\t // query:""; \n\t url = update_url + token; \n\t } else if (queryType.equals("add")) { \n\t // query:""; \n\t url = add_url + token; \n\t } \n\t else if (queryType.equals("deletefile")) { \n\t //fileid_list:[] \n\t url = deletefile_url + token; \n\t } \n\t query.put("env";, cloud_env); \n\t return sendCloudAction(url, query); //null or json \n\t} \n\t//云函数数据库导出 \n\tpublic String cloudExport(String filename, String filetype, String token, JSONObject query) { \n\t String downURL = null; \n\t filetype = filetype.equals("csv") ? "2" : "1"; \n\t String url = dbExport_url + token; \n\t query.put("env";, cloud_env); \n\t query.put("file_type";, filetype); \n\t query.put("file_path";, filename); \n\t JSONObject re = sendCloudAction(url, query); \n\t if (re != null && re.optString("errcode").equals("0")) { \n\t //success \n\t String jobID = re.optString("job_id"); \n\t url = dbExportStatus_url + token; \n\t query = new JSONObject(); \n\t query.put("job_id";, jobID); \n\t query.put("env";, cloud_env); \n\t re = sendCloudAction(url, query); \n\t if (re != null && re.optString("status").equals("success")) { \n\t downURL = re.optString("file_url"); \n\t } \n\t } \n\t return downURL; \n\t} \n\t//云函数下载文件 \n\tpublic List cloudDownload(String[] fileIDs, int maxAge) { \n\t String url = download_url; \n\t JSONArray arr = new JSONArray(); \n\t JSONObject query = new JSONObject(); \n\t query.put("env";, cloud_env); \n\t query.put("file_list";, arr); \n\t for (int i = 0; i < fileIDs.length; i++) { \n\t JSONObject temp = new JSONObject(); \n\t temp.put("fileid";, fileIDs[i]); \n\t temp.put("max_age";, maxAge); \n\t arr.put(temp); \n\t } \n\t JSONObject re = sendCloudAction(url, query); \n\t List downUrls = new ArrayList(); \n\t if (re.optString("errcode").equals("0")) { \n\t JSONArray list = re.optJSONArray("file_list"); \n\t for (int i = 0; i < list.length(); i++) { \n\t JSONObject item = list.getJSONObject(i); \n\t if (item.optString("status").equals("0")) { \n\t downUrls.add(item.optString("download_url")); \n\t } else { \n\t downUrls.add(""); \n\t } \n\t } \n\t } else { \n\t downUrls = null; \n\t } \n\t return downUrls; \n\t} \n\tpublic String cloudUrlToHttpUrl(String cloudUrl) { \n\t String url = ""; \n\t String[] arr = cloudUrl.split("."); \n\t String[] contentArr = arr[1].split("/"); \n\t url = "https://" + contentArr[0] + ".tcb.qcloud.la" + cloudUrl.replace(arr[0] + "." + contentArr[0], ""); \n\t return url; \n\t} \n\t///////////////////////////////////////////////////////////////////////////// \n\t//token更新 token|expire \n\tprivate String refreshToken(String source) { \n\t String token = null; \n\t double expire = -1; \n\t long now = new Date().getTime(); \n\t HttpClientUtil client = new HttpClientUtil("https"); \n\t try { \n\t String context = client.sendGet("", token_url, "utf8";, true); \n\t if (!context.contains("errcode")) { \n\t JSONObject temp = new JSONObject(context); \n\t token = temp.optString("access_token"); \n\t expire = now + temp.optDouble("expires_in") * 1000; \n\t if (!token.equals("")) { \n\t if (source.equals("file")) { \n\t FileUtil fp = new FileUtil(tokenFile, "utf8"); \n\t fp.write(token + "|" + expire, false); \n\t } else if (source.equals("db")) { \n\t DaoWx daoWx = new DaoWx(); \n\t WxModel wxModel = new WxModel(); \n\t wxModel.setAppid(appid); \n\t wxModel.setToken(token); \n\t wxModel.setExpire(expire + ""); \n\t int flag = daoWx.updateAppInfo(wxModel); \n\t token = flag <= 0 ? null : token; \n\t } \n\t } \n\t } \n\t } catch (Exception e) { \n\t client.closeHttpClient(); \n\t token = null; \n\t } \n\t return token == null ? token : token + "|" + expire; \n\t} \n\t//发起云函数请求 \n\tprivate JSONObject sendCloudAction(String url, JSONObject query) { \n\t JSONObject re = null; \n\t try { \n\t if (url != null && url.contains("http")) { \n\t HttpClientUtil httpclient = new HttpClientUtil("https"); \n\t String res = httpclient.httpsJsonPost(url, query, "utf8"); \n\t re = new JSONObject(res); \n\t } \n\t } catch (Exception e) { \n\t e.printStackTrace(); \n\t } \n\t return re; \n\t} \n} \nimport org.json.JSONObject; \nimport org.springframework.beans.factory.annotation.Value; \nimport org.springframework.stereotype.Service; \nimport com.ydproject.util.WxCloudUtil; \n@Service \npublic class WxService extends BaseService { \n @Value("${wx.cloud.tokenFile}") \n private String tokenFile; \n @Value("${wx.cloud.downloadFolder}") \n private String downloadFolder; \n private WxCloudUtil wxCloudUtil; \n public WxService() { \n wxCloudUtil = new WxCloudUtil(); \n } \n public void setTokenFile(String tokenFile) { \n this.tokenFile = tokenFile; \n } \n public void setDownloadFolder(String downloadFolder) { \n this.downloadFolder = downloadFolder; \n } \n public String getToken(String source) { \n return wxCloudUtil.getToken(source); \n } \n public String checkToken(String token, double expire, String source) { \n return wxCloudUtil.checkToken(token, expire, source); \n } \n public JSONObject cloudQuery(String queryType, String token, JSONObject query) { \n return wxCloudUtil.cloudQuery(queryType, token, query); \n } \n public String cloudExport(String filename, String filetype, String token, JSONObject query) { \n return wxCloudUtil.cloudExport(filename, filetype, token, query); \n } \n public List cloudDownload(String[] fileIDs, int maxAge) { \n return wxCloudUtil.cloudDownload(fileIDs, maxAge); \n } \n public String cloudUrlToHttpUrl(String cloudUrl) { \n return wxCloudUtil.cloudUrlToHttpUrl(cloudUrl); \n } \n

微信云数据库增删改查工具 - WxCloudUtil

原文地址: https://www.cveoy.top/t/topic/qmkg 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录