project.config.json:
"cloudfunctionRoot":"cloud/",
app.js:
App({
onLaunch() {
// 云开发环境初始化
wx.cloud.init({
env:""
})
}
})
上传图片视频文件到云存储:
js:
Page({
choose(){
var that=this
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success (res) {
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
that.upload(tempFilePaths[0],'aa.png')
}
})
},
upload(tmpFile,updFile){
wx.cloud.uploadFile({
cloudPath: updFile,
filePath: tmpFile,
success: res => {
console.log("上传成功",res)
},
fail: err => {
console.log("上传失败",err)
}
})
},
chooseView(){
var that=this
wx.chooseVideo({
sourceType: ['album','camera'],
maxDuration: 60,
camera: 'back',
success(res) {
console.log(res.tempFilePath)
that.upload(res.tempFilePath,'aa.mp4')
}
})
},
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)
}
})
},
})
wxml:
<button bindtap="choose">上传图片</button>
<button bindtap="choose">上传视频</button>
<button bindtap="choose">上传文件</button>
<view>请输入下载链接</view>
<input class="lianjie" bindinput="getContent"></input>
<button bindtap="downLoad">下载</button>
wxss:
.lianjie{
border: 1rpx solid black;
}
页面跳转:
demo.js:
Page({
onLoad(){
wx.cloud.database().collection("yzy")
.get()
.then(res => {
console.log("查询成功",res)
this.setData({
list:res.data
})
})
.catch(res=>{
console.log("查询失败",res)
})
},
goDetail(e){
console.log("点击跳转商品详情",e.currentTarget.dataset.id)
wx.navigateTo({
url:'/pages/demo1-1/demo1-1?id='+e.currentTarget.dataset.id,
})
}
})
demo.wxml:
<!--pages/demo/demo.wxml-->
<view wx:for="{{list}}">
<image src="{{item.img}}" class="img"></image>
<view bindtap="goDetail" data-id="{{item._id}}">商品名:{{item.name}},价格:{{item.price}}</view>
</view>
demo1-1.js
Page({
onLoad(options){
var id=options.id
wx.cloud.database().collection("yzy")
.doc(id)
.get()
.then(res=>{
console.log("查询成功",res)
this.setData({
good:res.data
})
})
.catch(res=>{
console.log("查询失败",res)
})
},
})
demo1-1.mxml
<view> 商品名:{{good.name}},价格:{{good.price}}</view>
<image src="{{good.img}}"></image>
银行卡、身份证:
js
Page({
shibie2(){ //自动识别银行卡
var that=this
wx.cloud.callFunction({
name:"card2",
data:{
imgCard2:"https://797a-yzy1130-9gvck3py792ed069-1305877027.tcb.qcloud.la/%E9%93%B6%E8%A1%8C%E5%8D%A1.jpg?sign=030c698a769cec2e5942de3ba3804c6f&t=1624496115"
},
success(res){
console.log("识别成功",res)
that.setData({
number:res.result.number
})
},
fail(res){
console.log("识别失败",res)
},
})
},
// shibie(){ //自动识别身份证
// var that=this
// wx.cloud.callFunction({
// name:"card1",
// data:{
// imgCard:"https://797a-yzy1130-9gvck3py792ed069-1305877027.tcb.qcloud.la/%E8%BA%AB%E4%BB%BD%E8%AF%81.jpg?sign=93bb12680d0d1238cea27109065f43c8&t=1624496257"
// },
// success(res){
// console.log("识别成功",res)
// that.setData({
// name:res.result.name,
// id:res.result.id,
// gender:res.result.gender
// })
// },
// fail(res){
// console.log("识别失败",res)
// },
// })
// },
shibie(){
var that=this
wx.chooseImage({ //手动上传识别身份证
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success (res) {
console.log(res)
const tempFilePaths = res.tempFilePaths
that.upload(tempFilePaths[0])
}
})
},
upload(tmpFile){
var that=this
wx.cloud.uploadFile({ //上传图片
cloudPath: 'example.png',
filePath: tmpFile, // 文件路径
success: res => {
console.log("上传成功",res.fileID)
that.getUrl(res.fileID)
},
fail: err => {
console.log("上传失败",err)
}
})
},
getUrl(fileid){
var that=this
wx.cloud.getTempFileURL({
fileList: [{
fileID: fileid,
}]
}).then(res => {
console.log("获取URL成功",res.fileList[0].tempFileURL)
var httpUrl=res.fileList[0].tempFileURL
that.getID(httpUrl)
}).catch(error => {
console.log("获取url失败")
})
},
getID(tmp){
var that=this
wx.cloud.callFunction({
name:"card1",
data:{
imgCard:tmp
},
success(res){
console.log("识别成功",res.result) //name id gender
that.setData({
name:res.result.name,
id:res.result.id,
gender:res.result.gender
})
},
fail(res){
console.log("识别失败",res)
},
})
},
})
wxml:
<button bindtap="shibie2">识别银行卡</button>
<text>银行卡号是:{{number}}</text>
<button bindtap="shibie">识别身份证</button>
<view>姓名是:{{name}}</view>
<view>身份证号是:{{id}}</view>
<view>性别是:{{gender}}</view>
card1.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.imgCard
})
console.log(result)
return result
} catch (err) {
return err
}
}
card2.index
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.ocr.bankcard({ //识别银行卡
"type": 'photo',
"imgUrl": event.imgCard2
})
return result
} catch (err) {
return err
}
}
注册页面:
wxml:
wxml
<text>姓名</text><input class="put" placeholder="请输入姓名" bindinput="getName"></input>
<text>班级</text><input class="put" placeholder="请输入班级号" bindinput="getBanji"></input>
<text>手机号</text><input class="put" placeholder="请输入手机号" bindinput="getNumber"></input>
<text>邮箱号</text><input class="put" placeholder="请输入邮箱号" bindinput="getMail"></input>
<button type="primary" class="reg" bindtap="registry">注册</button>
js:
var name
var banji
var number
var mail
Page({
getName(e){
name=e.detail.value
console.log(name)
},
getBanji(e){
banji=e.detail.value
console.log(banji)
},
getNumber(e){
number=e.detail.value
console.log(number)
},
getMail(e){
mail=e.detail.value
console.log(mail)
},
registry(){
const DB=wx.cloud.database().collection("zhuce")
.add({
data:{name:name,
banji:banji,
number:number,
mail:mail,
},
success(res){
console.log("添加成功",res)
wx.showToast({
title:'添加成功',
})
},
})
},
})