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;
    }
}
相关推荐
海鸥8138 分钟前
在K8S迁移节点kubelet数据存储目录
java·kubernetes·kubelet
开开心心就好42 分钟前
无限制文本转语音解决方案
开发语言·人工智能·macos·微信·pdf·c#·语音识别
jackson凌1 小时前
【Java学习笔记】递归
java·笔记·学习
鑫—萍1 小时前
C++——入门基础(2)
java·开发语言·jvm·数据结构·c++·算法
Excuse_lighttime1 小时前
UDP数据包和TCP数据包的区别;网络编程套接字;不同协议的回显服务器
java·tcp/ip·udp
心若微尘2 小时前
C++23/26 静态反射机制深度解析:编译时元编程的新纪元
java·开发语言·c++23
金斗潼关2 小时前
使用Nexus搭建远程maven仓库
java·maven·nexus
步行cgn2 小时前
Java Properties 遍历方法详解
java·开发语言·算法·面试·intellij-idea
heyCHEEMS2 小时前
[蓝桥杯 2023 国 Python B] 划分 Java
java·python·蓝桥杯