淘宝云开发——小程序使用
小程序网络请求
js
my.tb.request({
url: 'https://ability.taobao.com/handler/ability/getLatestAbilities?_tb_token_=g2lSCQ7rS6Wk4',
method: 'POST',
headers: {
'content-type': 'application/json',
'x-ada': 'dasd',
},
params: {
aaa: 'bbb',
ccc: 'dddd',
},
body: {
1111: '22222',
3333: '4444',
},
success: (res) => {
console.log(res)
my.alert({
title: 'success',
content: res.content,
})
},
fail: (res) => {
my.alert({
title: 'fail',
content: JSON.stringify(res),
})
},
})
小程序使用云函数
云函数结构
- index.js
- package.json
- cloud.json
云函数使用
js
// server/demo/index.js
exports.uploadFile = async (context) => {
const cloud = context.cloud
let result
try {
result = await cloud.file.uploadFile({
fileContent: new Buffer('333434'),
fileName: 'test',
})
} catch (e) {
console.log('e', e)
}
console.log('uploadFileResult', result)
return 'uploadFile'
}
云函数使用外联
js
exports.main = async (context) => {
const result = await context.cloud.httpApi.invoke({
// domain 必须是白名单里的
domain: 'http://11.19.128.205:10511',
params: {
sign: 'addadad',
test: 1,
},
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=UTF-8',
},
body: {
name: 'cx',
age: 18,
},
})
return {
success: true,
msg: result,
}
}
云数据库使用 —— mongodb
插件使用
加载插件
js
// demoPage/demoPage.js
Page({
data: {
pluginId: 'xxxxx',
isPluginReady: false
},
onLoad() {
this.loadPlugin()
},
loadPLugin() {
my.loadPlugin({
plugin: pluginId, // 插件id
success: () => {
// ....
this.setData({
isPluginReady: true
})
}
})
}
})
html
<!-- demoPage/demoPage.axml -->
<!-- 注意,插件必须在 js 加载完对应的插件后在显示,避免出现加载失败导致的显示异常 -->
<component
is="{{ `dynamic-plugin://${pluginId}/index` }}"
a:if="isPluginReady"
></component>
插件使用云函数
js
// 通过 cloud 就可以使用宿主小程序提供的通信能力了
const { cloud } = getApp()
local-webview
基本介绍
local-webview 注意:
- 仅在千牛PC端 7.35.66N 及以上支持
- 若需要和小程序通信,必须添加 id 属性
通信能力: 通过创建 webviewContext 提供从小程序向 web-view 发送消息的能力
作用: 供开发者在小程序中自由使用 web 生态能力,类似 iframe
使用此能力需要将页面资源(html、css、js等)放在研发工程中,上传后会一并打入小程序包。
注意:web原生网络能力会受到限制。
一般目录如下:
- dist
// 自动生成的,插件开发必须依赖宿主小程序(容器)
// 所以这个目录是作为开发阶段宿主小程序,发布的时候会被真正的小程序替换
- client
// 插件的真正源码
- plugin
- app.acss
- app.js
- app.json
- pages //插件页面
- demoPage
- demoPage.axml
- demoPage.js
- demoPage.acss
- demoPage.json
- webview
- // ....
// local-webview 可以使用 react、vue、原生 html 等
// 只要将最后打包结果放到 webview 里面就行
- local-webview
- src
- // ...
local-webview 可以在千牛小程序 、千牛小程序插件中使用