wallpaper.js 4.7 KB

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