wallpaper.js 4.8 KB

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