2023-04-17 16:14:18 +08:00

255 lines
6.6 KiB
JavaScript

// pages/share/share.js
var app = getApp()
var config = {};
var font_number = 16;
var height = 0;
var screenWidth = 422;
var pic_load = false;
// var common = require('../../../common/common.js');
var screenHeight = 0;
var max_height = 0; //灰色背景应该有的最大高度
var avatar_height = 40;
var center_position = 0;
var max_pic_num = 3;
var today = new Date();
var time_now = today.getFullYear() + '.' + (today.getMonth() > 8 ? (today.getMonth() + 1) : '0' + (today.getMonth() + 1)) + '.' + (today.getDate() > 9 ? today.getDate() : '0' + today.getDate());
Page({
/**
* 页面的初始数据
*/
data: {
swiperHeight: 200,
info: {},
imagePath: "",
img_width: 0,
img_height: 0,
poster: [],
qrcode: 'https://api.gter.net/avatar/MyTnU9xKN9jCce4KMXDCQOyTNzEyODg4/=api',
canvas: {
height0: 0,
height1: 0,
height2: 0,
height4: 0,
height3: 0,
height5: 0,
height6: 0,
height7: 0,
height8: 0,
height9: 0,
height10: 0,
height11: 0
},
current_index: 0,
max_height: 500,
show_share_box: false,
user: {},
share_info: {},
imgs: [],
width: 0,
height: 0,
screenwidth: 0,
options: {}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.showLoading({
// title: 'onLoad',
})
screenWidth = wx.getSystemInfoSync().screenWidth;
// min
this.setData({
uniqid: options.uniqid || '',
token: options.token || '',
img_width: screenWidth - 104,
user: app.globalData.user,
screenwidth: wx.getSystemInfoSync().screenWidth,
options: options
})
let that = this;
wx.getStorage({
key: 'share_info',
success: function (res) {
that.data.imgs = res.data.pic.concat(res.data.text);
that.setData({
share_info: res.data,
imgs: that.data.imgs
})
for (let i = 0; i < that.data.imgs.length; i++) {
let obj = {
src: that.data.imgs[i],
height: 0,
save: false
}
that.data.poster.push(obj)
that.get_img_height(i)
}
that.setData({
poster: that.data.poster
})
},
fail: function (res) {
console.log(res)
}
})
screenHeight = wx.getSystemInfoSync().screenHeight;
},
/**
* 用户点击右上角分享
*/
get_img_height: function (index) {
let that = this;
wx.showLoading({})
wx.getImageInfo({
src: that.data.imgs[index],
success: function (res) {
console.log(res);
wx.hideLoading()
if (res.errMsg != 'getImageInfo:ok') return
// return
that.data.poster[index].height = res.height;
that.data.poster[index].src = res.path;
that.setData({
height: Math.max(that.data.height, res.height),
width: res.width,
poster: that.data.poster
})
}
})
return
wx.downloadFile({
url: that.data.imgs[index], //仅为示例,并非真实的资源
success: function (res) {
if (res.statusCode == 200) {
wx.hideLoading()
that.data.poster[index].src = res.tempFilePath;
that.setData({
poster: that.data.poster
})
wx.getImageInfo({
src: res.tempFilePath,
success: function (res) {
that.data.poster[index].height = res.height;
that.setData({
height: Math.max(that.data.height, res.height),
width: res.width,
poster: that.data.poster
})
}
})
}
}
})
// wx.getImageInfo({
// src: that.data.imgs[index],
// success: function (res) {
// that.data.poster[index].height = res.height;
// that.setData({
// height: Math.max(that.data.height, res.height),
// width: res.width,
// poster: that.data.poster
// })
// }
// })
},
change_page: function (e) {
this.setData({
current_index: e.detail.current
})
},
save_img: function () {
let that = this;
wx.saveFile({
tempFilePath: that.data.poster[that.data.current_index].src,
})
},
save_pic: function () {
let that = this;
let index = that.data.current_index;
let src = that.data.poster[index].src;
if (!that.data.poster[index].save) {
wx.saveImageToPhotosAlbum({
filePath: src,
success: function (res) {
that.data.poster[index].save = true;
that.setData({
poster: that.data.poster,
show_share_box: true
})
},
complete: function (res) {
if (res.errMsg == "saveImageToPhotosAlbum:fail:auth denied" || res.errMsg == "saveImageToPhotosAlbum:fail auth deny" || res.errMsg == "saveImageToPhotosAlbum:fail authorize no response") {
wx.showModal({
title: '提示',
content: '需要您授权保存相册',
showCancel: true,
success: modalSuccess => {
wx.openSetting({
success(settingdata) {
console.log(settingdata);
if (settingdata.authSetting['scope.writePhotosAlbum']) {
wx.showModal({
title: '提示',
content: '获取权限成功,再次点击图片即可保存',
showCancel: false,
})
}
},
fail(failData) {
console.log("failData", failData)
},
complete(finishData) {
console.log("finishData", finishData)
}
})
}
})
}
}
})
} else {
that.setData({
show_share_box: true
})
}
},
close_share: function () {
this.setData({
show_share_box: false
})
},
longtap: function (e) {
console.log('longtap', e)
},
click: function (e) {
console.log(e)
},
preview_img: function (e) {
let that = this;
let index = that.data.current_index;
wx.previewImage({
current: that.data.imgs[index], // 当前显示图片的http链接
urls: that.data.imgs // 需要预览的图片http链接列表
})
},
onload(e) {
let that = this;
let height = e.detail.height / e.detail.width * this.data.img_width;
this.setData({
swiperHeight: Math.max(that.data.swiperHeight, height)
})
}
})