wallpaper.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // pages/piano/wallpaper/wallpaper.js
  2. const { deviceWallPaper } = require('../../../request/deviceListRequest')
  3. // const sharp = require('sharp');
  4. // const fs = require('fs');
  5. import routeUtil from '../../../utils/routeUtil';
  6. import store from '../../../utils/store';
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. topImg:
  13. {
  14. // pic: "",
  15. // id: "",
  16. },
  17. imageList: [],
  18. image4List: [],
  19. _allImageList: [],
  20. _netImageList: [],
  21. _device: {},
  22. selectIndex: 0,
  23. _localImgPicKey: "_localImgPicListKey",
  24. navbarData: {
  25. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  26. title: '壁纸设置', //导航栏 中间的标题
  27. },
  28. showAllImages: false,
  29. useSystemNavBar: true // 设置为 true 以使用系统导航栏
  30. },
  31. footerTap() {
  32. var p = {
  33. "deviceId": this.data._device.deviceId,
  34. "topImg": this.data.topImg
  35. }
  36. var param = "?param=" + JSON.stringify(p);
  37. routeUtil.jumpParam("../cropper/cropper", param);
  38. },
  39. imageTopTap() {
  40. this.setData({
  41. topImg: {}
  42. })
  43. },
  44. toggleShowAll: function () {
  45. this.setData({
  46. showAllImages: !this.data.showAllImages
  47. });
  48. },
  49. imageTap(e) {
  50. console.log(e)
  51. // wxfile://tmp_d3e57489ead39c698676ff860df9cb8a37f66ee1a4777dbb.jpg
  52. let image = e.currentTarget.dataset.image;
  53. let _this = this;
  54. if (image.id == this.data.topImg.id) {
  55. _this.setData({
  56. topImg: {}
  57. })
  58. } else {
  59. _this.setData({
  60. topImg: image ?? {}
  61. })
  62. }
  63. },
  64. async wallpaperList() {
  65. let _this = this;
  66. deviceWallPaper().then(res => {
  67. let data = res ?? []
  68. data.sort((a, b) => a.sort - b.sort);
  69. // 网络的
  70. _this.data._netImageList = data;
  71. let localImgList = store.getStore(_this.data._localImgPicKey);
  72. console.log("壁纸列表", data.length, "空", localImgList);
  73. let allImgs = []
  74. if (localImgList) {
  75. console.log("壁纸列表2", data, "空", JSON.stringify(localImgList), "空");
  76. allImgs = localImgList.concat(data)
  77. }
  78. _this.data._allImageList = allImgs;
  79. _this.setData({
  80. imageList: _this.data._allImageList,
  81. image4List: _this.data._allImageList.slice(0, 4),
  82. // topImg: data[0] ?? {}
  83. })
  84. })
  85. },
  86. async updateTopImg(topUrl) {
  87. let _this = this;
  88. let newImg = {
  89. "pic": topUrl, "id": "0"
  90. }
  91. let myImgList = store.getStore(_this.data._localImgPicKey)
  92. myImgList = myImgList ?? []
  93. console.log("updateTopImg1", myImgList)
  94. // 移除保存的重复图片
  95. let myIndex = myImgList.findIndex(img => img.pic === topUrl);
  96. let imgList = _this.data._netImageList
  97. // 移除网络的重复图片
  98. let findImg = imgList.find(function (img) { return img.pic === topUrl })
  99. console.log("updateTopImg3", findImg)
  100. if (findImg) {
  101. // 官方不动位置
  102. // let index = imgList.findIndex(img => img.pic === findImg.pic);
  103. // imgList.splice(index, 1);
  104. // imgList.unshift(findImg);
  105. } else {
  106. if (myIndex < 0) {
  107. // 不是官方的壁纸,才添加进缓存
  108. myImgList.unshift(newImg)
  109. console.log("updateTopImg5", myImgList.length)
  110. }
  111. }
  112. if (myImgList.length > 1) {
  113. console.log("updateTopImg4", myImgList.length)
  114. // 本地图片缓存最多保存1张, 10张
  115. myImgList.pop()
  116. }
  117. console.log("updateTopImg2", imgList, findImg)
  118. _this.data._allImageList = myImgList.concat(imgList);
  119. _this.setData({
  120. imageList: _this.data._allImageList,
  121. image4List: _this.data._allImageList.slice(0, 4),
  122. topImg: newImg
  123. })
  124. store.setStore(_this.data._localImgPicKey, myImgList)
  125. // }
  126. },
  127. hideCut(e) {
  128. const img = arguments[0].detail
  129. if (img && img.path) {
  130. console.log("裁剪图片:", img)
  131. }
  132. this.setData({
  133. showCropImg: true,
  134. topImg: { "pic": img.path }
  135. })
  136. },
  137. /**
  138. * 生命周期函数--监听页面加载
  139. */
  140. onLoad(options) {
  141. let param = JSON.parse(options.param)
  142. console.log(param)
  143. this.data._localImgPicKey = this.data._localImgPicKey + param.deviceId;
  144. this.data._device = param
  145. this.wallpaperList();
  146. },
  147. /**
  148. * 生命周期函数--监听页面初次渲染完成
  149. */
  150. onReady() {
  151. },
  152. /**
  153. * 生命周期函数--监听页面显示
  154. */
  155. onShow() {
  156. },
  157. /**
  158. * 生命周期函数--监听页面隐藏
  159. */
  160. onHide() {
  161. },
  162. /**
  163. * 生命周期函数--监听页面卸载
  164. */
  165. onUnload() {
  166. },
  167. /**
  168. * 页面相关事件处理函数--监听用户下拉动作
  169. */
  170. onPullDownRefresh() {
  171. },
  172. /**
  173. * 页面上拉触底事件的处理函数
  174. */
  175. onReachBottom() {
  176. },
  177. /**
  178. * 用户点击右上角分享
  179. */
  180. onShareAppMessage() {
  181. }
  182. })