wx35a7ff57185ab700
配置云开发环境
"cloudfunctionRoot":"cloud",
.js
App({
onLaunch(){
wx.cloud.init({
env:"lhg-2g7jq8bb8504af6c"
})
}
})
云数据库
商品详情页
wxml
<view wx:for="{{list}}">
<view> 商品名:{{item.name}},价格:{{item.price}}</view>
</view>
.js
Page({
onLoad(){
wx.cloud.database().collection("fruit")//这题只改数据库名字
.get()
.then(res =>{
console.log("商品列表请求成功",res)
this.setData({
list:res.data
})
})
.catch(res=>{
console.log("商品列表请求失败",res)
})
},
})
上传图片
新建数据库别忘记更改权限
上传文件
.wxml
<button bindtap="chooseFile">选择文件</button>
.js
Page({
chooseFile(){
var that=this
wx.chooseMessageFile({
count: 1,
type: 'all',
success (res) {
const tempFilePaths = res.tempFiles
console.log(tempFilePaths[0])
that.upload(tempFilePaths[0].path,tempFilePaths[0].name)
}
})
},
upload(tmpFile){
wx.cloud.uploadFile({
cloudPath: '上传的名字',//上传给起个名字
filePath: tmpFile,
success: res => {
console.log("上传成功",res)
},
fail: err => {
console.log("上传失败",err)
}
})
},
})
下载文件并打开
.wxml
<view>请输入下载链接</view>
<input class="lianjie" bindinput="getContent"></input>
<button bindtap="downLoad">下载</button>
.wxss
.lianjie{
border: 1rpx solid black;
}
.js
Page({
chooseView(){
var that=this
wx.chooseVideo({
sourceType: ['album','camera'],
maxDuration: 60,
camera: 'back',
success(res) {
console.log(res.tempFilePath)
that.uploadView(res.tempFilePath)
}
})
},
uploadView(tmpFile){
wx.cloud.uploadFile({
cloudPath: 'lhg05',//给下载的文件起个名字
filePath: tmpFile,
success: res => {
console.log("上传成功",res)
},
fail: err => {
console.log("上传失败",err)
}
})
},
chooseFile(){
var that=this
wx.chooseMessageFile({
count: 1,
type: 'all',
success (res) {
const tempFilePaths = res.tempFiles
console.log(tempFilePaths[0])
that.upload(tempFilePaths[0].path,tempFilePaths[0].name)
}
})
},
getContent(e){
console.log(e.detail.value)
this.setData({
fileID:e.detail.value
})
},
downLoad(){
var fileID
fileID=this.data.fileID
console.log("下载链接为:",fileID)
wx.cloud.downloadFile({
fileID: fileID,
success: res => {
console.log("下载成功",res)
},
fail: err => {
console.log("下载失败",res)
}
})
},
downLoad(){
var fileID
fileID=this.data.fileID
console.log("下载链接为:",fileID)
wx.cloud.downloadFile({
fileID:fileID,
success:res=>{
console.log("下载成功",res)
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
success:function(res){
console.log('打开文档成功')
}
})
}
})
}
})
识别身份证
先创建card02
card02/index
const cloud = require('wx-server-sdk')
cloud.init({
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.ocr.idcard({
"type": 'photo',
"imgUrl": event.lhg05//改名字
})
console.log(result)
return result
} catch (err) {
return err
}
}
.wxml
<button bindtap="shibie">识别身份证</button>
<view>姓名是:{{name}}</view>
<view>身份证号是:{{id}}</view>
<view>性别是:{{gender}}</view>
.js
Page({
shibie(){
var that=this
wx.cloud.callFunction({
name:"card01",//跟创建的card名字一样
data:{
acy01:"下载地址"//跟index.js名字一样,并复制身份证链接
},
success(res){
console.log("识别成功",res)
that.setData({
name:res.result.name,
id:res.result.id,
gender:res.result.gender
})
},
fail(res){
console.log("识别失败",res)
},
})
},
})
添加商品
.wxml
<input placeholder="请输入商品名" bindinput="addName"></input>
<input placeholder="请输入价格" bindinput="addPrice"></input>
<button bindtap="addDate" type="primary">添加商品</button>
.js
const DB=wx.cloud.database().collection("haha")
var name
var price
var id
Page({
addName(e){
name=e.detail.value
},
addPrice(e){
price=e.detail.value
},
addDate(){
if(name==null){
wx.showToast({
icon:'none',
title:'商品名空了',
})
}else if(price==null){
wx.showToast({
icon:'none',
title:'价格空了'
})
}else{
DB.add({
data:{
name:name,
price:price
},
success(res){
console.log("添加成功",res)
},
fail(res){
console.log("添加失败",res)
}
})
}
}
})
删除商品
.wxml
<input placeholder="请输入ID" bindinput="delDataInput"></input>
<button bindtap="delData" type="primary">删除数据</button>
.js
var id
Page({
delDataInput(e){
id=e.detail.value
console.log(id)
},
delData(){
wx.cloud.database().collection("haha")
.doc(id).remove()
.then(res => {
console.log("删除成功",res)
})
.catch(err =>{
console.log("删除失败",err)
})
},
})