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;
    }
}
相关推荐
羚羊角uou几秒前
【Linux】线程池
java·开发语言
阿拉-M838 分钟前
IntelliJ IDEA Windows 系统高频快捷键使用手册
java·windows·intellij-idea
lingggggaaaa25 分钟前
小迪安全v2023学习笔记(一百三十四讲)—— Windows权限提升篇&数据库篇&MySQL&MSSQL&Oracle&自动化项目
java·数据库·windows·笔记·学习·安全·网络安全
迦蓝叶1 小时前
JAiRouter v1.0.0 正式发布:企业级 AI 服务网关的开源解决方案
java·运维·人工智能·网关·spring·ai·开源
安卓开发者1 小时前
鸿蒙NEXT应用接入快捷栏:一键直达,提升用户体验
java·harmonyos·ux
yudiandian20141 小时前
03 Eclipse 配置 JDK 环境
java·ide·eclipse
_码力全开_1 小时前
P1005 [NOIP 2007 提高组] 矩阵取数游戏
java·c语言·c++·python·算法·矩阵·go
陈一Tender1 小时前
JavaWeb后端实战(登录认证 & 令牌技术 & 拦截器 & 过滤器)
java·开发语言·spring boot·mysql
Camel卡蒙1 小时前
红黑树详细介绍(五大规则、保持平衡操作、Java实现)
java·开发语言·算法