Java使用微信云服务HTTP API操作微信云开发数据库

可以直接用的工具类代码

java 复制代码
package com.kstc.qgy.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kstc.qgy.model.exception.WxException;
import com.kstc.qgy.model.service.Limit;
import java.util.List;

public class WXConnection {

    private static String accessToken = null;

    private final static String IP = "https://api.weixin.qq.com";

    private final static String env = "你的env";

    private final static String appId = "你的微信appid";

    private final static String secret = "你的微信小程序密钥";

    public synchronized static void getAccessToken() throws Exception {
        String res = HttpUtils.httpsGet(IP + "/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret);
        JSONObject jsonObject = JSON.parseObject(res);
        accessToken = jsonObject.getString("access_token");
        System.out.println("获取微信access_token:"+accessToken);
    }

    private synchronized static void resultIsSuccess(String res) throws WxException {
        JSONObject jsonObject = JSONObject.parseObject(res);
        if (jsonObject.containsKey("errcode")&&!jsonObject.get("errcode").equals(0)) throw new WxException();
    }

    public synchronized static String findList(String collection, Limit limit) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        String query = "db.collection('" + collection + "').skip(" + limit.getCurrPage() * limit.getPageSize() + ").limit(" + limit.getPageSize() + ").get()";
        body.put("query",query);
        String res = HttpUtils.httpsPost(IP + "/tcb/databasequery?access_token=" + accessToken, body.toJSONString());
        resultIsSuccess(res);
        return res;
    }

    public synchronized static String findById(String collection,String id) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        String query = "db.collection('" + collection + "').doc('" + id + "').get()";
        body.put("query",query);
        String res = HttpUtils.httpsPost(IP + "/tcb/databasequery?access_token=" + accessToken, body.toJSONString());
        resultIsSuccess(res);
        return res;
    }

    public synchronized static <T> String addList(String collection, List<T> list) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        String query = "db.collection('" + collection + "').add({" +
                "data: " + JSONObject.toJSONString(list) +
                "})";
        body.put("query",query);
        String res = HttpUtils.httpsPost(IP + "/tcb/databaseadd?access_token=" + accessToken, UnicodeUtils.strToUnicode(body.toJSONString()));
        resultIsSuccess(res);
        return res;
    }

    public synchronized static String createCollection(String collection) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        body.put("collection_name",collection);
        String res = HttpUtils.httpsPost(IP + "/tcb/databasecollectionadd?access_token=" + accessToken, body.toJSONString());
        resultIsSuccess(res);
        return res;
    }

    public synchronized static String deleteCollection(String collection) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        body.put("collection_name",collection);
        String res = HttpUtils.httpsPost(IP + "/tcb/databasecollectiondelete?access_token=" + accessToken, body.toJSONString());
        resultIsSuccess(res);
        return res;
    }

    public synchronized static <T> String findByConditionAnd(String collection,T condition,Limit limit) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        String query = "db.collection('"+ collection +"').where("+ JSONObject.toJSONString(condition) +").skip(" + limit.getCurrPage() * limit.getPageSize() + ").limit(" + limit.getPageSize() + ").get()";
        body.put("query",query);
        String res = HttpUtils.httpsPost(IP + "/tcb/databasequery?access_token=" + accessToken, UnicodeUtils.strToUnicode(body.toJSONString()));
        resultIsSuccess(res);
        return res;
    }

    public synchronized static <T> String updateById(String collection,T condition,String id) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        String query = "db.collection('"+ collection +"').doc('"+ id +"').update({data:"+ JSONObject.toJSONString(condition) +"})";
        body.put("query",query);
        String res = HttpUtils.httpsPost(IP + "/tcb/databaseupdate?access_token=" + accessToken, UnicodeUtils.strToUnicode(body.toJSONString()));
        resultIsSuccess(res);
        return res;
    }

    public synchronized static String deleteById(String collection,String id) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        String query = "db.collection('"+ collection +"').doc('"+ id +"').remove()";
        body.put("query",query);
        String res = HttpUtils.httpsPost(IP + "/tcb/databasedelete?access_token=" + accessToken, UnicodeUtils.strToUnicode(body.toJSONString()));
        resultIsSuccess(res);
        return res;
    }

    public synchronized static String getFileTempPath(List<String> fileId_list) throws Exception {
        JSONObject body = new JSONObject();
        body.put("env",env);
        JSONArray fileList = new JSONArray();
        for(int i = 0;i < fileId_list.size();i++) {
            JSONObject fileItem = new JSONObject();
            fileItem.put("fileid",fileId_list.get(i));
            fileItem.put("max_age", 7200);
            fileList.add(fileItem);
        }
        body.put("file_list", fileList);
        String res = HttpUtils.httpsPost(IP + "/tcb/batchdownloadfile?access_token=" + accessToken, UnicodeUtils.strToUnicode(body.toJSONString()));
        resultIsSuccess(res);
        return res;
    }
}
相关推荐
Doris_LMS10 分钟前
保姆级别IDEA关联数据库方式、在IDEA中进行数据库的可视化操作(包含图解过程)
java·mysql·postgresql
寒士obj23 分钟前
HTTPS的工作原理
网络协议·http·https
衍生星球25 分钟前
JSP 程序设计之 Web 技术基础
java·开发语言·jsp
Java编程乐园30 分钟前
Java函数式编程之【Stream终止操作】【下】【三】【收集操作collect()与分组分区】【下游收集器】
java
yinyan131431 分钟前
一起学springAI系列一:初体验
java·人工智能·ai
永卿00142 分钟前
设计模式-责任链模式
java·设计模式·责任链模式
hello 早上好1 小时前
深入解析AOP调用链:递归与责任链模式的协同实现
java·责任链模式
wangmengxxw1 小时前
Spring-常用注解
java·数据库·spring·注解
籍籍川草1 小时前
JVM指针压缩的那些事
java·开发语言·jvm
艾莉丝努力练剑1 小时前
【C/C++】类和对象(上):(一)类和结构体,命名规范——两大规范,新的作用域——类域
java·c语言·开发语言·c++·学习·算法