123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- // 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 () {
- }
- })
|