微信云数据库增删改查工具 - WxCloudUtil
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
原文地址: https://www.cveoy.top/t/topic/qmkg 著作权归作者所有。请勿转载和采集!