// 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 CosAuth = require('./cos-auth'); // COS 签名方法 https://unpkg.com/cos-js-sdk-v5/demo/common/cos-auth.min.js var Bucket = 'bucketnj1-1254259530'; var Region = 'ap-nanjing'; var ForcePathStyle = false; // 是否使用后缀式,涉及签名计算和域名白名单配置,后缀式说明看上文 // var uploadFile = function () { // 请求用到的参数 var prefix = 'https://' + Bucket + '.cos.' + Region + '.myqcloud.com/'; if (ForcePathStyle) { // 后缀式请求在签名时域名使用地域域名,而不是存储桶域名,具体说明见本文上述“3.后缀式请求” prefix = 'https://cos.' + Region + '.myqcloud.com/' + Bucket + '/'; } // 对更多字符编码的 url encode 格式 // var camSafeUrlEncode = function (str) { // return encodeURIComponent(str) // .replace(/!/g, '%21') // .replace(/'/g, '%27') // .replace(/\(/g, '%28') // .replace(/\)/g, '%29') // .replace(/\*/g, '%2A'); // }; // 获取临时密钥 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.showModal({ title: '临时密钥获取失败', content: JSON.stringify(data), showCancel: false }); } callback(stsCache && stsCache.credentials); }, error: function (err) { 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 uploadFile = function (filePath) { var Key = this.data.carWin_img.substr(this.data.carWin_img.lastIndexOf('/') + 1); // 这里指定上传的文件名 // var Key = '456abcd.png'; var signPathname = '/'; // PostObject 接口 Key 是放在 Body 传输,所以请求路径和签名路径是 / if (ForcePathStyle) { // 后缀式请求在签名时用的路径,要包含存储桶名称,具体说明见本文上述“3.后缀式请求” 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) => { console.log(res.header.Location, "11111111111111") // var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/'); 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) { console.log(res, "我把照片路径传到服务器啦") 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.showModal({ title: "上传失败啦", showCancel: false }); } } }); } else { wx.showModal({ title: '上传失败', showCancel: false }); } }, fail: function () { wx.hideLoading(); wx.showModal({ title: '上传失败', showCancel: false }); } }); requestTask.onProgressUpdate(function (res) { console.log('进度:', res); if (res.progress == 100) { wx.hideLoading(); } }); }); }, 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 }) } }) }, /** * 生命周期函数--监听页面加载 */ 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 () { } })