123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- // pages/pyip/pyip.js
- const COS = require('../../utils/cos-wx-v5.js');
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- carWin_img_hidden: true, //展示照片的view是否隐藏
- carWin_img: '' //存放照片路径的
- },
- openCamera() {
- var that = this;
- wx.chooseImage({
- count: 1,
- success: (res) => {
- var filePath = res.tempFilePaths[0];
- that.setData({
- carWin_img: filePath, //把照片路径存到变量中,
- carWin_img_hidden: false //让展示照片的view显示
- });
- console.log(this.data.carWin_img)
- // 这个是使用微信接口保存文件到数据库
- // wx.uploadFile({
- // url: "",
- // filePath: filePath,
- // name: 'file',
- // success: function (res) {
- // }
- // })
- },
- fail: function (error) {
- console.error("调用本地相册文件时出错")
- console.warn(error)
- },
- complete: function () {
- }
- });
- },
- submitImgToTxy() {
- // 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: 'GET',
- url: 'https://example.com/sts.php', // 服务端签名,参考上文说的获取临时密钥
- dataType: 'json',
- success: function (result) {
- console.log(result)
- var data = result.data;
- var credentials = data.credentials;
- if (credentials) {
- stsCache = data
- } 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 signPathname = '/'; // PostObject 接口 Key 是放在 Body 传输,所以请求路径和签名路径是 /
- if (ForcePathStyle) {
- // 后缀式请求在签名时用的路径,要包含存储桶名称,具体说明见本文上述“3.后缀式请求”
- signPathname = '/' + Bucket + '/';
- }
- getAuthorization({
- Method: 'POST',
- Pathname: signPathname
- }, function (AuthData) {
- var requestTask = wx.uploadFile({
- url: prefix,
- name: 'file',
- filePath: this.data.carWin_img,
- formData: {
- 'key': Key,
- 'success_action_status': 200,
- 'Signature': AuthData.Authorization,
- 'x-cos-security-token': AuthData.XCosSecurityToken,
- 'Content-Type': '',
- },
- success: function (res) {
- var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
- console.log(res.statusCode);
- console.log(url);
- if (/^2\d\d$/.test('' + res.statusCode)) {
- wx.showModal({
- title: '上传成功',
- content: url,
- showCancel: false
- });
- } else {
- wx.showModal({
- title: '上传失败',
- content: JSON.stringify(res),
- showCancel: false
- });
- }
- },
- fail: function (res) {
- wx.showModal({
- title: '上传失败',
- content: JSON.stringify(res),
- showCancel: false
- });
- }
- });
- requestTask.onProgressUpdate(function (res) {
- console.log('进度:', res);
- });
- });
- // };
- // }
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady: function () {
- this.setData({
- carWin_img_hidden: true,
- carWin_img: ''
- });
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow: function () {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide: function () {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload: function () {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh: function () {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom: function () {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {
- }
- })
|