// 小程序上传图片到又拍云的js前端代码
var app = getApp();
const Upyun = require('../../sim.js/upyun'); //这个文件是又拍云官方提供的sdk文件,直接下载引用。
const upyun = new Upyun({
bucket: 'andiffnet1', //服务名称
operator: 'cheng', //操作员名称
getSignatureUrl: 'https://wx.weicu8.com/api/file/sign' //服务器url,服务端代码,请加微信索取
});
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
},
chooseImage: function () {
const self = this;
wx.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album'],
success: function (res) {
const imageSrc = res.tempFilePaths[0];
var myDate = new Date();
var year = myDate.getFullYear();
var month = myDate.getMonth()+1;
var day = myDate.getDate();
var hour = myDate.getHours();
var minutes = myDate.getMinutes();
var second = myDate.getSeconds();
var millisecond = myDate.getMilliseconds();
var rnd = Math.floor(Math.random()*100000);
upyun.upload({
localPath: imageSrc,
remotePath: '/skin/' + year + month + day + hour + minutes + second + millisecond + rnd + '.png', //又拍云服务端文件名称
success: function (res) {
console.log('uploadImage success, res is:', res)
wx.showToast({
title: '上传成功',
icon: 'success',
duration: 1000
})
self.setData({
imageSrc
})
},
fail: function ({ errMsg }) {
console.log('uploadImage fail, errMsg is', errMsg)
}
})
},
fail: function ({ errMsg }) {
console.log('chooseImage fail, err is', errMsg)
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})