Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为小程序示例添加ar功能 #56

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
## 使用

```
cd demo
npm i
cd miniprogram
npm i
npm run init
```
完成上述步骤后,使用微信开发者工具,点击【工具-构建npm】

Expand Down
6 changes: 6 additions & 0 deletions cloudfunctions/ARDemo/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"permissions": {
"openapi": [
]
}
}
58 changes: 58 additions & 0 deletions cloudfunctions/ARDemo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 云函数入口文件
const cloud = require('wx-server-sdk')
const wxgService = require('@tencent/wx-server-sdk-wxg-service')
const svrkitUtils = require('./svrkit-utils.js')

cloud.registerService(wxgService)
cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const bizuin = wxContext.APPUIN
console.log(bizuin)
console.log(event)
switch(event.type){
case "GenerateARModel":
return await cloud.callWXSvrkit({
pbInstance: svrkitUtils.generate({
serviceName: "Mmbizwxaintpar",
funcName: "GenerateARModel",
data:{
bizuin: bizuin,
name: event.name,
url: event.url,
algoType: event.algoType
},
})
});
case "GetARModelList":
return await cloud.callWXSvrkit({
pbInstance: svrkitUtils.generate({
serviceName: "Mmbizwxaintpar",
funcName: "GetARModelList",
data:{
bizuin: bizuin,
modelStatus: event.modelStatus,
algoType: event.algoType
},
})
});
case "GetARModel":
return await cloud.callWXSvrkit({
pbInstance: svrkitUtils.generate({
serviceName: "Mmbizwxaintpar",
funcName: "GetARModel",
data:{
bizuin: bizuin,
cosid: event.cosid,
modelType: event.modelType,
needData: event.needData,
useIntranet: event.useIntranet,
expireTime: event.expireTime
},
})
});
}

}
19 changes: 19 additions & 0 deletions cloudfunctions/ARDemo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "ARDemo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"svrkit": "svrkit-utils --config ./svrkit.config.js --output ./svrkit-utils.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@tencent/wx-server-sdk-wxg-service": "^0.7.0",
"wx-server-sdk": "^2.6.3"
},
"devDependencies": {
"@tencent/cloud-functions-tools": "^1.5.1"
}
}
102 changes: 102 additions & 0 deletions cloudfunctions/ARDemo/proto/mmbizwxaintparDemo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
enum enARModelStatus
{
ARModel_Status_Default = 0;
ARModel_Status_Init = 1;
ARModel_Status_Sparse_Finished = 2;
ARModel_Status_3d_Finished = 3;
ARModel_Status_Object_Finished = 4;
ARModel_Status_Marker_Finished = 5;
}

enum enARAlgorithmType
{
Algorithm_Type_3D_Object = 1;
Algorithm_Type_3D_Marker = 2;
}

enum enARModelType
{
ARModel_Type_Sparse = 1;
ARModel_Type_3D = 2;
ARModel_Type_Marker = 3;
}

message ModelCos
{
message ModelCosId
{
optional enARModelType model_type = 1;
optional string model_cosid = 2;
}
repeated ModelCosId model_list = 1;
}

message ARModel
{
//option(mmbizintpkv.KvTableID) = 493;
option(mmbizintpkv.KvTableTestID) = 916;
optional string cosid = 1; // 原始文件的cosid
optional uint32 bizuin = 2;
optional string name = 3; // 原始文件的名称
optional uint32 upload_time = 4;
optional enARModelStatus model_status = 5;
optional enARAlgorithmType algo_type = 6;
optional ModelCos model_cos = 7;
}


message GenerateARModelReq
{
optional uint32 bizuin = 1;
optional string name = 2;
optional bytes buffer = 3;
optional string url = 4;
optional enARAlgorithmType algo_type = 5;
}

message GenerateARModelResp
{
optional string url = 1;
optional string host = 2;
optional string cosid = 3;
}

message GetARModelListReq
{
optional uint32 bizuin = 1;
optional uint32 model_status = 2; // enARModelStatus
optional uint32 start_time = 3;
optional uint32 end_time = 4;
optional uint32 offset = 5;
optional uint32 limit = 6;
optional uint32 algo_type = 7; // enARAlgorithmType
}

message GetARModelListResp
{
repeated ARModel model_list = 1;
}


message ARModelData
{
optional bytes mesh_model = 1; // 文本(点面信息)
optional bytes texture_model = 2; // 图像png
}

message GetARModelReq
{
optional uint32 bizuin = 1;
optional string cosid = 2;
optional uint32 model_type = 3; // 1:稀疏点云 2:3d模型
optional uint32 need_data = 4[default=1]; // 0:不需要数据 1:需要数据
optional uint32 use_intranet = 5[default=0]; // 当need_data为0时生效 0:生成外网链接 1:内网链接
optional uint32 expire_time = 6; // url过期时间,默认5分钟,单位为秒
}

message GetARModelResp
{
optional ARModelData model_data = 1;
optional string url = 2;
optional string host = 3;
}
119 changes: 119 additions & 0 deletions cloudfunctions/ARDemo/svrkit-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@

const config = require('./svrkit.config.js')
const proto = require('./svrkit-utils.static.js')
const protoJSON = require('./svrkit-utils.static.json')

function generate(options) {
if (!options) {
throw new Error('options must be provided')
}

const { serviceName, funcName, data } = options

const serviceConfig = config.find(c => c.serviceName === serviceName)
if (!serviceConfig) {
throw new Error('service not found')
}

if (!serviceConfig.functions[funcName]) {
throw new Error('function not found')
}

const reqProtoName = serviceConfig.functions[funcName].req
const reqProto = proto[reqProtoName]

if (!reqProto) {
throw new Error('request proto not found')
}

const resProtoName = serviceConfig.functions[funcName].res
const resProto = resProtoName && proto[resProtoName]

const reqProtoVerifyErr = reqProto.verify(data)
if (reqProtoVerifyErr) {
throw new Error(`verify proto data error: ${reqProtoVerifyErr}`)
}

const reqProtoJSON = protoJSON.nested[reqProtoName]

if (reqProtoJSON && reqProtoJSON.fields) {
if (Object.prototype.toString.call(data).slice(8, -1) === 'Object') {
for (const key in data) {
if (!reqProtoJSON.fields[key]) {
throw new Error(`'${key}' doesn't exist in '${reqProtoName}' proto, valid keys are ${Object.keys(reqProtoJSON.fields)}`)
}
}
} else {
throw new Error('data must be object')
}
}

return {
data: {
serviceName,
funcName,
magic: serviceConfig.magic,
cmdid: serviceConfig.functions[funcName].cmdid,
existResp: Boolean(resProto),
reqBodyBuffer: reqProto.encode(data).finish(),
},
reqProto,
resProto,
decode: buf => resProto && resProto.decode(buf)
}
}

function generateV2(options) {
if (!options) {
throw new Error('options must be provided')
}

const { apiName, data } = options

const apiConfig = config.find(c => c.apiName === apiName)

const reqProtoName = apiConfig.req
const reqProto = proto[reqProtoName]

if (!reqProto) {
throw new Error('request proto not found')
}

const resProtoName = apiConfig.res
const resProto = proto[resProtoName]

const reqProtoVerifyErr = reqProto.verify(data)
if (reqProtoVerifyErr) {
throw new Error(`verify proto data error: ${reqProtoVerifyErr}`)
}

const reqProtoJSON = protoJSON.nested[reqProtoName]

if (reqProtoJSON && reqProtoJSON.fields) {
if (Object.prototype.toString.call(data).slice(8, -1) === 'Object') {
for (const key in data) {
if (!reqProtoJSON.fields[key]) {
throw new Error(`'${key}' doesn't exist in '${reqProtoName}' proto, valid keys are ${Object.keys(reqProtoJSON.fields)}`)
}
}
} else {
throw new Error('data must be object')
}
}

return {
data: {
apiName,
reqBodyBuffer: reqProto.encode(data).finish(),
},
reqProto,
resProto,
decode: buf => resProto && resProto.decode(buf)
}
}

module.exports = {
generate,
generateV2,
}

Loading