// pages/pyip/pyip.js const COS = require('../../utils/cos.js'); const app = require('../../utils/util.js'); const tools = require('../../utils/tools.js'); Page({ /** * 页面的初始数据 */ data: { carWin_img_hidden: true, //展示照片的view是否隐藏 carWin_img: '', //存放照片路径的 carWin_img_type: 2, //是图片还是视频 cosPath: "" //腾讯云上传的路径 }, openCamera() { wx.chooseMedia({ count: 1, mediaType: ['image', 'video'], sourceType: ['album', 'camera'], maxDuration: 30, camera: 'back', success: (res) => { console.log(res, "9999999999999999") let path = res.tempFiles[0].tempFilePath let uploadType = path.substring(path.length - 3) if (uploadType == "jpg" || uploadType == "png") { this.setData({ carWin_img: path, carWin_img_hidden: false, carWin_img_type: 0 }) } else if (uploadType == "mp4") { this.setData({ carWin_img: path, carWin_img_hidden: false, carWin_img_type: 1 }) } } }) }, submitImgToTxy() { if (this.data.carWin_img == "" || this.data.carWin_img == null) { wx.showModal({ title: '请点击上方选择照片或者视频哦', showCancel: false }); return false } wx.showLoading({ mask: true, title: '上传中', }) var Bucket = 'bucketnj1-1254259530'; var Region = 'ap-nanjing'; var ForcePathStyle = false; var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/'; if (ForcePathStyle) { prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/'; } var stsCache; var getCredentials = function (callback) { if (stsCache && Date.now() / 1000 + 30 < stsCache.expiredTime) { callback(data.credentials); return; } wx.request({ method: 'POST', url: app.globalData.publicUrl + '/wx/course/getSts', // dataType: 'json', success: function (result) { var credentials = result.data.credential.credentials; if (credentials) { stsCache = result.data.credential } else { wx.hideLoading(); wx.showModal({ title: '临时密钥获取失败', content: JSON.stringify(data), showCancel: false }); } callback(stsCache && stsCache.credentials); }, error: function (err) { wx.hideLoading(); wx.showModal({ title: '临时密钥获取失败', content: JSON.stringify(err), showCancel: false }); } }); }; // 计算签名 var getAuthorization = function (options, callback) { getCredentials(function (credentials) { callback({ XCosSecurityToken: credentials.sessionToken, Authorization: COS({ SecretId: credentials.tmpSecretId, SecretKey: credentials.tmpSecretKey, Method: options.Method, Pathname: options.Pathname, }) }); }); }; // 上传文件 var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名 var signPathname = '/'; if (ForcePathStyle) { signPathname = '/' + Bucket + '/'; } getAuthorization({ Method: 'POST', Pathname: signPathname }, (AuthData) => { var requestTask = wx.uploadFile({ url: prefix, name: 'file', filePath: this.data.carWin_img, formData: { 'key': "paiyipai/" + Key, 'success_action_status': 200, 'Signature': AuthData.Authorization, 'x-cos-security-token': AuthData.XCosSecurityToken, 'Content-Type': '', }, success: (res) => { if (/^2\d\d$/.test('' + res.statusCode)) { wx.request({ method: 'PUT', url: app.globalData.publicUrl + '/wx/shoot', data: { shootName: this.data.carWin_img, shootCosName: Key, sessionKey: this.data.sessionKey, shootType: this.data.carWin_img_type }, success: (res) => { if (res.data.code == 0) { wx.hideLoading(); wx.showModal({ title: '上传成功', showCancel: false }); this.setData({ // cosPath: res.header.Location, carWin_img: "", carWin_img_type: 2, carWin_img_hidden: true }) this.getArr(this.data.sessionKey) } else { wx.hideLoading(); wx.showModal({ title: res.data.msg, showCancel: false }); } } }); } else { wx.hideLoading(); wx.showModal({ title: '上传失败', showCancel: false }); } }, fail: function () { wx.hideLoading(); wx.showModal({ title: '上传失败', showCancel: false }); } }); requestTask.onProgressUpdate(function (res) { console.log('进度:', res); }); }); }, getArr(sessionKey) { wx.request({ url: app.globalData.publicUrl + '/wx/shoot', method: "get", data: { sessionKey }, success: (res) => { console.log(res.data.imgList, "我是查到的照片视频列表") console.log(res.data.videoList, "我是查到的照片视频列表") this.setData({ imgList: res.data.imgList, videoList: res.data.videoList }) if (res.data.imgList && res.data.imgList.length != 0) { this.setData({ isPhoto: true }) } if (res.data.videoList && res.data.videoList.length != 0) { this.setData({ isVideo: true }) } } }) }, /** * 生命周期函数--监听页面加载 */ async onLoad() { const sessionKey = await tools.checkSessionAndLogin(); this.setData({ sessionKey }) this.getArr(sessionKey) // this.getArr(sessionKey); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { this.setData({ carWin_img_hidden: true, carWin_img: '' }); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })