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

发布于:2025-05-01 ⋅ 阅读:(12) ⋅ 点赞:(0)

 可以直接用的工具类代码

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;
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到