// pages/pyip/pyip.js const COS = require('../../utils/cos.js'); const app = require('../../utils/util.js'); Page({ /** * 页面的初始数据 */ data: { carWin_img_hidden: true, //展示照片的view是否隐藏 carWin_img: '' //存放照片路径的 }, openCamera() { wx.chooseMedia({ count: 1, mediaType: ['image', 'video'], sourceType: ['album', 'camera'], maxDuration: 30, camera: 'back', success: (res) => { console.log(res.tempFiles[0].tempFilePath) console.log(res.tempFiles[0].size) this.setData({ carWin_img: res.tempFiles[0].tempFilePath, carWin_img_hidden: false, }) } }) }, 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) { console.log(result, "000000000000") 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) => { var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/'); console.log('prefix' + prefix); console.log('Key' + Key); console.log('4564564654654'); console.log(res.statusCode); console.log(url); if (/^2\d\d$/.test('' + res.statusCode)) { wx.showModal({ title: '上传成功', showCancel: false }); this.setData({ carWin_img: "", carWin_img_hidden:true }) } else { wx.showModal({ title: '上传失败', // content: JSON.stringify(res), showCancel: false }); } }, fail: function (res) { wx.hideLoading(); wx.showModal({ title: '上传失败', // content: JSON.stringify(res), showCancel: false }); } }); requestTask.onProgressUpdate(function (res) { console.log('进度:', res); if (res.progress == 100) { wx.hideLoading(); } }); }); // }; // } }, /** * 生命周期函数--监听页面加载 */ onLoad: function () { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { this.setData({ carWin_img_hidden: true, carWin_img: '' }); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })