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;
    }
}
相关推荐
diving deep1 小时前
springboot集成日志配置文件
java·spring boot·后端·logback
蟹至之1 小时前
【Java】异常的初步认识
java·开发语言·类和对象·异常
广西千灵通网络科技有限公司2 小时前
基于Java的话剧购票小程序【附源码】
java·小程序·apache
苏小瀚2 小时前
[Java] idea的调试介绍
java·intellij-idea
JWenzz12 小时前
Redis删除策略
java·数据库·redis·缓存
幻听嵩的留香2 小时前
javaEE课程项目-壁纸管理系统
java·java-ee
liubo666_2 小时前
SpringMVC(结合源码浅析工作流程)
java·spring·springmvc
speop3 小时前
TASK05【Datawhale 组队学习】系统评估与优化
android·java·学习
星沁城3 小时前
108. 将有序数组转换为二叉搜索树
java·数据结构·leetcode
在未来等你3 小时前
互联网大厂Java求职面试:云原生架构与AI应用集成解决方案
java·spring cloud·微服务·ai·云原生·kubernetes·大模型