受欢迎的博客标签

微信小程序云开发-云数据库交互-使用 HTTP API 开发者可在第三方服务器上访问云资源

Published

可以使用 HTTP API 在已有服务器上访问云资源,实现与云开发的互通,具体请参考文档:

https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/

思路:

step 1:获取access_token:这里获取的token是我们后续每次调用官方api都必须要带上的参数

var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
                                   grant_type, appid, secret);

如果执行成功,将返回

{"access_token":"你的access_token","expires_in":7200}

step2:发起 http api调用:

const queryApi = 'https://api.weixin.qq.com/tcb/databasequery?access_token=xedhou39709';

说明:

1.post接口入参格式为“application/json”

post默认的“application/x-www-form-urlencoded”,微信提供的接口入参格式为“application/json”

入参格式采用
application/json; charset=utf-8”

2.需要带上access_token的接口入参

const queryApi = 'https://api.weixin.qq.com/tcb/databasequery?access_token=xedhou39709';

 

调用云端的云函数

POST https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=ACCESS_TOKEN&env=ENV&name=FUNCTION_NAME

databaseCount

POST https://api.weixin.qq.com/tcb/databasecount?access_token=ACCESS_TOKEN
请求参数
{
  "env":"test2-4a89da",
  "query": "db.collection(\"geo\").where({done:true}).count()"
}

databaseQuery

POST https://api.weixin.qq.com/tcb/databasequery?access_token=ACCESS_TOKEN
请求参数
{
  "env":"test2-4a89da",
  "query": "db.collection(\"geo\").where({done:true}).limit(10).skip(1).get()"
}

返回数据格式

返回数据是字符串

1.单个对象

{
    "errcode": 0,
    "errmsg": "ok",
    "pager": {
        "Offset": 1,
        "Limit": 10,
        "Total": 2
    },
    "data": [
        "{\"_id\":\"b15498af-1a5a-40b4-a4e7-b3fc4a1df482\",\"done\":true,\"name\":\"test\"}"
    ]
}

 

多条记录结构

{"errcode":0,"errmsg":"ok","pager":{"Offset":0,"Limit":10,"Total":3},"data":["{\"Appid\":\"wx9b00e80b6177fc26\",\"AuthorOpenid\":\"oTC2f4pDGnYQFWTdePnHO2-IutkU\",\"AuthorUnionid\":\"\",\"CreateCustomerId\":\"\",\"CreateCustomerName\":\"\",\"CreatedOnUtc\":{\"$date\":1598800379645},\"Customer\":{},\"CustomerId\":\"\",\"CustomerPrice\":0,\"DisplayOrder\":0,\"EquipmentCode\":\"\",\"EquipmentNum\":\"\",\"IsDeleted\":false,\"Mark\":\"\",\"Name\":\"\",\"OldPrice\":0,\"OperateType\":0,\"OrderNumber\":\"55555\",\"OriginalId\":\"gh_96b119488287\",\"Price\":\"9\",\"ProducType\":{\"CreatedOnUtc\":\"0001-01-01T00:00:00\",\"IsDeleted\":false,\"Name\":\"QJ12B-Y\",\"UpdatedOnUtc\":\"0001-01-01T00:00:00\",\"_id\":\"5f43a46054a35fe1d20785d0\"},\"ProducTypeId\":\"\",\"ProductStockOutDate\":{},\"Published\":true,\"Quantity\":\"1\",\"Sku\":\"\",\"StockQuantity\":0,\"UpdatedOnUtc\":{\"$date\":1598800379645},\"Warehouse\":{\"CreatedOnUtc\":\"2020-08-24T20:24:14.534Z\",\"IsDeleted\":false,\"Name\":\"澧县和胜\",\"UpdatedOnUtc\":\"2020-08-25T21:44:42.144Z\",\"_id\":\"5f4421ee19c1098f3c318225\"},\"WarehouseFromId\":\"\",\"WarehouseId\":\"\",\"WarehouseToId\":\"\",\"_id\":\"5f4bc1fb703d007e371f4618\",\"_openid\":\"oTC2f4pDGnYQFWTdePnHO2-IutkU\"}","{\"Appid\":\"wx9b00e80b6177fc26\",\"AuthorOpenid\":\"oTC2f4pDGnYQFWTdePnHO2-IutkU\",\"AuthorUnionid\":\"\",\"CreateCustomerId\":\"\",\"CreateCustomerName\":\"\",\"CreatedOnUtc\":{\"$date\":1598930421209},\"Customer\":{},\"CustomerId\":\"\",\"CustomerPrice\":0,\"DisplayOrder\":0,\"EquipmentCode\":\"\",\"EquipmentNum\":\"\",\"IsDeleted\":false,\"Mark\":\"\",\"Name\":\"\",\"OldPrice\":0,\"OperateType\":0,\"OrderNumber\":\"777\",\"OriginalId\":\"gh_96b119488287\",\"Price\":\"9\",\"ProducType\":{\"CreatedOnUtc\":\"0001-01-01T00:00:00\",\"IsDeleted\":false,\"Name\":\"J3(B)-T\",\"UpdatedOnUtc\":\"0001-01-01T00:00:00\",\"_id\":\"5f43a46054a35fe1d20785d2\"},\"ProducTypeId\":\"\",\"ProductStockOutDate\":{\"$date\":1472731201000},\"Published\":true,\"Quantity\":\"1\",\"Sku\":\"\",\"StockQuantity\":0,\"UpdatedOnUtc\":{\"$date\":1598930421209},\"Warehouse\":{\"CreatedOnUtc\":\"2020-08-25T12:51:29.936Z\",\"IsDeleted\":false,\"Name\":\"澧县六合\",\"UpdatedOnUtc\":\"2020-08-25T21:45:09.566Z\",\"_id\":\"5f450951ab55274d58620482\"},\"WarehouseFromId\":\"\",\"WarehouseId\":\"\",\"WarehouseToId\":\"\",\"_id\":\"5f4dbdf3dee6911f3ac985af\",\"_openid\":\"oTC2f4pDGnYQFWTdePnHO2-IutkU\"}","{\"Appid\":\"wx9b00e80b6177fc26\",\"AuthorOpenid\":\"oTC2f4pDGnYQFWTdePnHO2-IutkU\",\"AuthorUnionid\":\"\",\"CreateCustomerId\":\"\",\"CreateCustomerName\":\"\",\"CreatedOnUtc\":{\"$date\":1599176139158},\"Customer\":{},\"CustomerId\":\"\",\"CustomerPrice\":0,\"DisplayOrder\":0,\"EquipmentCode\":\"\",\"EquipmentNum\":\"\",\"IsDeleted\":false,\"Mark\":\"5555\",\"Name\":\"\",\"OldPrice\":0,\"OperateType\":0,\"OrderNumber\":\"55555\",\"OriginalId\":\"gh_96b119488287\",\"Price\":9999.9,\"ProducType\":{\"CreatedOnUtc\":\"0001-01-01T00:00:00\",\"IsDeleted\":false,\"Name\":\"QJ12B-Y\",\"UpdatedOnUtc\":\"0001-01-01T00:00:00\",\"_id\":\"5f43a46054a35fe1d20785d0\"},\"ProducTypeId\":\"\",\"ProductStockOutDate\":{\"$date\":1472731201000},\"Published\":true,\"Quantity\":1,\"Sku\":\"\",\"StockQuantity\":0,\"UpdatedOnUtc\":{\"$date\":1599176139158},\"Warehouse\":{\"CreatedOnUtc\":\"2020-08-25T12:51:29.936Z\",\"IsDeleted\":false,\"Name\":\"澧县六合\",\"UpdatedOnUtc\":\"2020-08-25T21:45:09.566Z\",\"_id\":\"5f450951ab55274d58620482\"},\"WarehouseFromId\":\"\",\"WarehouseId\":\"\",\"WarehouseToId\":\"\",\"_id\":\"5f517dcb0ffaa06fb3e06a25\",\"_openid\":\"oTC2f4pDGnYQFWTdePnHO2-IutkU\"}"]}

 

 

databaseUpdate

POST https://api.weixin.qq.com/tcb/databaseupdate?access_token=ACCESS_TOKEN
请求参数
{
  "env":"test2-4a89da",
  "query": "db.collection(\"geo\").where({age:14}).update({data:{age: _.inc(1)}})"
}

云开发有 HTTP API
HTTP API 提供了小程序外访问云开发资源的能力,使用 HTTP API 开发者可在已有服务器上访问云资源,实现与云开发的互通。

云开发 HTTP API 文档
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api

step 1:向小程序后台发起请求。获取access_token
access_token是小程序的全局唯一后台接口调用凭据,也就是调用大部分的http接口都需要同时传入参数access_token。access_token
的有效期为 2 个小时,期间可以重复使用,因此access_token需要首先获取,获取一次后缓存起来,失效后再次获取。

API https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
auth.getAccessToken
获取access_token需要三个参数:grant_type 、appid、secret(grant_type值为credential,appid和secret在「微信公众平台之小程序后台
设置 - 开发设置」页中获得)

AppID(小程序ID)

AppSecret(小程序密钥)

string grant_type = "client_credential"
  var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type={0}&appid={1}&secret={2}",
                                   grant_type, appid, secret);

          

            var request = new HttpRequestMessage(HttpMethod.Get, url);

            var client = _clientFactory.CreateClient();

            iAspNetCore.Weixin.MP.Core.Domain.AccessToken.AccessTokenResult result;

            var response =  client.SendAsync(request).Result;