前端代码:
<view class="row">
<view class="text1">证件照:</view>
<image class="zp" src="{
{zjz}}" mode="aspectFill" name="zhengjianzhao"></image>
<button class="upload-img-btn" bindtap="chooseImg" type='primary'>拍照 / 上传</button>
</view>
js代码:
// pages/xggrzl/xggrzl.js
const db = wx.cloud.database()
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
openid: '',
userInfo: '',
array: ["大一", "大二", "大三", "大四"],
array1: ["计算机科学与工程学院", "电子信息工程学院"],
xy_index: 0,
nj_index: 0,
imgs:[],
zjz: '',
img: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
const app = getApp()
var openid = app.globalData.openid
this.setData({
openid: openid
})
db.collection("user").where({ openid: this.data.openid }).get().then(res => {
console.log(res.data)
this.setData({
userInfo: res.data
})
console.log(this.data.userInfo[0].nj)
for (let i = 0; i < this.data.array.length; i++) {
if (this, this.data.array[i] == this.data.userInfo[0].nj) {
console.log(i)
this.setData({
nj_index: i
})
}
}
for (let i = 0; i < this.data.array1.length; i++) {
if (this, this.data.array1[i] == this.data.userInfo[0].xy) {
console.log(i)
this.setData({
xy_index: i
})
}
}
})
},
// 上传一张图片
chooseImg: function (e) {
/* const app = getApp()
var openid = app.globalData.openid
this.setData({
openid: openid
})
db.collection("user").where({ openid: this.data.openid }).get().then(res => {
console.log(res.data)
this.setData({
userInfo: res.data
})
if(res.data.openid==app.globalData.openid){
*/
var that = this;
let openid = app.globalData.openid || wx.getStorageSync('openid');
console.log(openid)
// const filepath=this.data.img
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
console.log(res.tempFilePaths)
let filePath = res.tempFilePaths[0]
wx.cloud.uploadFile({
cloudPath: (new Date()).valueOf() + '.png',//文件名 //云存储图片名字
filePath,//临时路径:filepath
success: res => {
console.log('[上传图片] 成功:', res)
console.log(re