1 Star 0 Fork 0

Coder / WXYHModelPage

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
YHModelPage.js 19.60 KB
一键复制 编辑 原始数据 按行查看 历史
Coder 提交于 2020-09-22 15:22 . update close button postion
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
// libs/ModelPage/YHModelPage.js
const config = require("../config.js");
var touchStartPoint = {},
touchMove = {},
mainInfoViewContainerLeftFlag = 0,
scrollDistance = 0,
scrollXDistance = 0;
Component({
/**
* 组件的初始数据
*/
data: {
showYHModelPageAnimation: {},
mainHeight: "0px",
YHOpacity: 0,
YHModelViewTypeFlag: "VIEW",
showClose: true,
YHModelViewInfoData: {},
mainInfoViewContainerAnimation: {},
mainInfoViewWidth: "",
mainInfoViewHeight: "",
mainInfoViewContainerWidth: "",
mainInfoViewContainerHeight: "",
timestamp: '',
closeBottom: 0
// scrollDistance: 0,
// scrollXDistance: 0
},
/**
* 组件的属性列表
*/
properties: {
hwScale: {
type: Number,
value: 1.5
},
toShowPage: {
type: Boolean,
value: false,
observer(newVal, oldVal, changedPath) {
// 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
// 通常 newVal 就是新设置的数据, oldVal 是旧数据
if (newVal) {
this.showYHModelPage();
} else {
this.closeYHModelPage();
}
}
},
YHModelViewInfoData: {
type: Object,
value: {
title: "提示",
content: "通常 newVal 就是新设置的数据, oldVal 是旧数据",
src: "",
showClose: true,
type: "VIEW",
responseList: []
},
observer(newVal, oldVal, changedPath) {
var that = this;
// 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
// 通常 newVal 就是新设置的数据, oldVal 是旧数据
// // console.log("that = " + JSON.stringify(that));
if (newVal) {
switch (newVal.type) {
case ("VIEW"):
{
// // console.log(" VIEW");
};
break;
case ("IMAGE"):
{
// // console.log(" IMAGE");
var items = newVal.responseList;
if (!items) {
console.error(" -- YHModelPage - IMAGE has no property :responseList ! --");
}
items.forEach(function (item, index) {
if (index) {
item.show = false;
} else {
item.show = true;
}
})
if (that.properties.yhModelPageDidScroll) {
// console.log(" mainInfoViewContainerLeftFlag -- " + mainInfoViewContainerLeftFlag);
var option = newVal.responseList[Math.abs(mainInfoViewContainerLeftFlag)];
that.properties.yhModelPageDidScroll(JSON.stringify(option));
}
};
break;
case ("CONTENT"):
{
};
break;
default:
{ };
break;
};
// 判断赋值是否为空
if (!newVal.type) {
return;
}
that.setData({
YHModelViewTypeFlag: newVal.type,
showClose: newVal.showClose || false,
YHModelViewInfoData: newVal
});
// // console.log(" if(newVal){ = =" + JSON.stringify(newVal));
} else {
// // console.log(" if(newVal){ }else{");
}
}
},
confrimFunc: {
type: Function,
value: new Function(),
observer(newVal, oldVal, changedPath) {
// 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
// 通常 newVal 就是新设置的数据, oldVal 是旧数据
if (newVal) {
// // console.log(" if(newVal){");
} else {
// // console.log(" if(newVal){ }else{");
}
}
},
closeFunc: {
type: Function,
value: new Function()
},
YHModelViewType: {
type: String,
value: "VIEW",
observer(newVal, oldVal, changedPath) {
// 属性被改变时执行的函数(可选),也可以写成在methods段中定义的方法名字符串, 如:'_propertyChange'
// 通常 newVal 就是新设置的数据, oldVal 是旧数据
}
},
imageClickFunc: {
type: Function,
value: new Function()
},
yhModelPageDidScroll: {
type: Function,
value: new Function()
}
},
created: function () {
const animation = wx.createAnimation({
duration: 200,
timingFunction: "ease"
});
this.animation = animation;
},
/**
* 组件的方法列表
*/
methods: {
showYHModelPage: function () {
// init data
var animation = this.animation;
if (!animation) {
animation = wx.createAnimation({
duration: 200,
timingFunction: "ease"
});
this.animation = animation;
}
animation.translateX(0).step({
duration: 0
});
let time = new Date();
let timestamp = "?timestamp=" + time.getTime();
this.setData({
mainInfoViewContainerAnimation: animation.export(),
timestamp: timestamp
});
touchStartPoint = {},
touchMove = {},
mainInfoViewContainerLeftFlag = 0,
scrollDistance = 0,
scrollXDistance = 0;
// init size
var systemInfo = this.systemInfo();
var sHeight = systemInfo.windowHeight;
var that = this;
// // console.log("this = " + JSON.stringify(this));
var systemInfo = that.systemInfo();
var wW = systemInfo.windowWidth * 0.8;
// scale 1.5 iphone6 height/width
var wH = wW * this.data.hwScale;
var closeBottom = sHeight - wH -120;
var YHModelViewTypeFlag = that.data.YHModelViewTypeFlag;
if (YHModelViewTypeFlag == "VIEW") {
that.setData({
mainInfoViewWidth: wW,
mainInfoViewHeight: wH,
mainHeight: "100%",
YHOpacity: 1,
closeBottom
});
} else {
let imageNum = that.data.YHModelViewInfoData.responseList.length;
let cw = wW * imageNum;
that.setData({
mainInfoViewWidth: wW,
mainInfoViewHeight: wH,
mainInfoViewContainerWidth: cw,
mainInfoViewContainerHeight: wH,
mainHeight: "100%",
YHOpacity: 1, closeBottom
});
}
// // console.log("that1 = " + JSON.stringify(this));
},
closeYHModelPage: function () {
// reset property showPage
// console.log("closeYHModelPage");
this.properties.toShowPage = false;
this.setData({
mainHeight: "0px",
YHOpacity: 0
});
this.properties.closeFunc();
},
loadImageError: function (res) {
const logger = wx.getLogManager();
const logStr = JSON.stringify(res);
logger.warn('NET ERROR : ' + {
res: logStr
});
},
confrimYHModelPage: function (e) {
// reset property showPage
// console.log("e = " + JSON.stringify(e));
var that = this,
pageIndex = Math.abs(mainInfoViewContainerLeftFlag);
var YHModelViewTypeFlag = that.data.YHModelViewTypeFlag,
option = "VIEW";
if (YHModelViewTypeFlag == "IMAGE") {
option = that.data.YHModelViewInfoData.responseList[pageIndex];
}
if (!this.properties.confrimFunc) {
console.error("MODEL PAGE COMPNENT HAS NO FUNC - confrimYHModelPage -");
return;
}
this.properties.confrimFunc(JSON.stringify(option));
},
getLocalData(options) {
try {
return wx.getStorageSync(options);
} catch (err) {
return false;
}
},
mainInfoViewTouchMove: function (e) {
if (JSON.stringify(touchStartPoint) == "{}") {
return;
};
var that = this;
touchMove.moveOnOff = true;
var YHModelViewTypeFlag = that.data.YHModelViewTypeFlag;
if (YHModelViewTypeFlag == "IMAGE") {
var YHModelViewInfoData = that.data.YHModelViewInfoData;
var responseList = YHModelViewInfoData.responseList;
var imageNum = responseList.length;
var scrollWdith = that.data.mainInfoViewWidth;
var animation = that.animation;
// console.log("mainInfoViewTouchEnd = " + JSON.stringify(e));
// let scrollXDistance = false;
scrollXDistance = false;
if (touchMove.moveOnOff && touchMove.firstCall) {
var touchEndPoint = e.changedTouches[0];
if (!touchEndPoint || JSON.stringify(touchEndPoint) == "{}") {
return;
};
scrollXDistance = touchStartPoint.clientX - touchEndPoint.clientX;
} else {
return;
}
var scrollLeft = false;
if (scrollXDistance) {
scrollLeft = scrollXDistance >= 0;
if (scrollLeft) {
if (mainInfoViewContainerLeftFlag - 1 == -imageNum) {
return;
}
mainInfoViewContainerLeftFlag = mainInfoViewContainerLeftFlag - 1;
var mainInfoViewContainerLeft = scrollWdith * (mainInfoViewContainerLeftFlag);
animation.translateX(mainInfoViewContainerLeft).step();
} else {
if (mainInfoViewContainerLeftFlag == 0) {
return;
}
mainInfoViewContainerLeftFlag = mainInfoViewContainerLeftFlag + 1;
var mainInfoViewContainerLeft = scrollWdith * (mainInfoViewContainerLeftFlag);
animation.translateX(mainInfoViewContainerLeft).step();
}
responseList.forEach(function (item, index) {
if (index == Math.abs(mainInfoViewContainerLeftFlag)) {
responseList[index].show = true;
} else {
responseList[index].show = false;
}
});
touchMove.firstCall = false;
that.setData({
mainInfoViewContainerAnimation: animation.export(),
YHModelViewInfoData: YHModelViewInfoData
});
if (that.properties.yhModelPageDidScroll) {
var option = that.data.YHModelViewInfoData.responseList[Math.abs(mainInfoViewContainerLeftFlag)];
that.properties.yhModelPageDidScroll(JSON.stringify(option));
}
}
}
},
mainInfoViewTouchStart: function (e) {
// console.log("mainInfoViewTouchStart = " + JSON.stringify(e));
if (e && e.changedTouches && e.changedTouches.length > 0) {
touchStartPoint = e.changedTouches[0];
touchMove = {
moveOnOff: false,
firstCall: true
};
}
},
mainInfoViewTouchEnd: function (e) {
},
systemInfo() {
var systemInfo = this.getLocalData("systemInfo");
if (!systemInfo) {
systemInfo = wx.getSystemInfoSync();
this.saveLocalData({
"systemInfo": systemInfo
}, true);
}
return systemInfo;
},
saveLocalData(options, asyncBol) {
if (asyncBol) {
try {
for (var key in options) {
wx.setStorage({
key: key,
data: options[key],
});
}
return true;
} catch (err) {
return false;
}
} else {
try {
for (var key in options) {
wx.setStorageSync(key, options[key]);
}
return true;
} catch (err) {
return false;
}
}
},
saveImageToAlbum(e) {
// console.log(JSON.stringify(e));
const item = e.target.dataset.item,
that = this;
wx.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
wx.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.saveImageToAlbumAction(item);
},
fail(failRes) {
wx.showModal({
title: '提示',
content: '请手动设置保存相册权限',
confirmColor: config.Config.confirmColor,
showCancel: true,
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
}
});
}
}
})
}
})
} else {
that.saveImageToAlbumAction(item);
}
},
fail: function () {
wx.showModal({
title: '获取不到您的授权信息',
content: '请检车配置,或者联系客服',
confirmColor: config.Config.confirmColor,
showCancel: false
})
}
})
},
saveImageToAlbumAction(item) {
// console.log("saveImageToAlbumAction = " + JSON.stringify(item) );
if (!item || !item.imgUrl) {
console.err("!item || !item.imgUrl model page line 432");
return;
};
var tempPath;
wx.getImageInfo({
src: item.imgUrl,
success: function (res) {
tempPath = res.path;
// console.log(JSON.stringify(res));
wx.saveImageToPhotosAlbum({
filePath: tempPath,
success: function (res) {
wx.showModal({
title: '保存成功',
content: '使用微信扫描相册内图片,即可分享。',
confirmColor: config.Config.confirmColor,
showCancel: false
})
//// console.log("saveImageToPhotosAlbum success = " + JSON.stringify(res));
},
fail: function (res) {
let errmsg = res.errMsg;
if ('saveImageToPhotosAlbum:fail cancel' == errmsg) {
wx.showToast({
title: '操作已取消',
mask: true,
duration: 800
});
} else if ('authorize:fail auth deny' == errmsg) {
wx.showModal({
title: '保存失败',
content: '请检查设置是否有保存相册权限',
confirmColor: config.Config.confirmColor,
showCancel: true,
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
}
});
}
}
})
}
console.error("saveImageToPhotosAlbum fail = " + JSON.stringify(res));
},
complete: function (res) {
//// console.log("saveImageToPhotosAlbum complete = " + JSON.stringify(res));
}
});
},
fail: function (res) {
wx.showModal({
title: '保存失败',
content: '请检查设置是否有保存相册权限',
confirmColor: config.Config.confirmColor,
showCancel: true,
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
}
});
}
}
})
},
complete: function (res) {
}
});
}
}
})
JavaScript
1
https://gitee.com/liuyonghu/YHModelPage.git
git@gitee.com:liuyonghu/YHModelPage.git
liuyonghu
YHModelPage
WXYHModelPage
master

搜索帮助