index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. const util = require("../../../utils/util.js");
  2. var app = getApp();
  3. const config = require('../../../utils/config')
  4. Page({
  5. data: {
  6. selectMore: false,
  7. checkedGroup: [],
  8. showShare: false,
  9. picGroup: [],
  10. activeDate: 0,
  11. info: {},
  12. picArr: [],
  13. saveImageList: [],
  14. liveId: "",
  15. teamId: "",
  16. options: {},
  17. imgArr: [],
  18. num: 24,
  19. change: 0,
  20. imgPath: util.config.imgPath,
  21. current: '', //预览当前index
  22. ceilingShow: false, //吸顶
  23. },
  24. scroll(e) {
  25. let query = wx.createSelectorQuery();
  26. let height = ''
  27. query.select('#topImg').boundingClientRect(rect => {
  28. //获取到元素
  29. height = rect.height;
  30. // console.log(6767, height, e.detail.scrollTop)
  31. if (e.detail.scrollTop >= height) {
  32. if (!this.data.ceilingShow) {
  33. this.setData({
  34. ceilingShow: true
  35. })
  36. }
  37. } else {
  38. if (this.data.ceilingShow) {
  39. this.setData({
  40. ceilingShow: false
  41. })
  42. }
  43. }
  44. }).exec();
  45. },
  46. showpic(e) {
  47. let url = e.currentTarget.dataset.url;
  48. wx.previewImage({
  49. current: url, // 当前显示图片的 http 链接
  50. saveImageList: [],
  51. photos: [],
  52. activeInfo: {},
  53. })
  54. },
  55. onShareAppMessage: function () {
  56. let shareImg = this.data.activeInfo.sharePhoto ? this.data.activeInfo.sharePhoto : 'https://img.bbztx.com/weixin/applet/share/activity_cover.jpg'
  57. return {
  58. title: '查看活动过程中的精彩图片',
  59. path: `/pages/growup/activePhoto/index?liveId=${this.data.liveId}`,
  60. imageUrl: shareImg
  61. }
  62. },
  63. onLoad(options) {
  64. if (options.liveId || options.scene) {
  65. this.setData({
  66. liveId: options.liveId ? options.liveId : options.scene
  67. })
  68. this.getInfor()
  69. this.getPicture()
  70. return
  71. }
  72. },
  73. onShow(options) {
  74. // this.getInfor()
  75. let option = app.globalData.info
  76. if (option && option.liveId != "") {
  77. this.setData({
  78. liveId: option.liveId
  79. })
  80. this.getInfor()
  81. this.getPicture()
  82. // return
  83. }
  84. },
  85. // 获取活动信息
  86. getInfor() {
  87. let that = this
  88. util.ajax({
  89. func: "v2/child/liveRoomDetail",
  90. data: {
  91. "liveId": that.data.liveId
  92. }
  93. }, function (res) {
  94. if (res.code == 0) {
  95. that.data.activeInfo = res.data
  96. wx.setStorageSync('info', res.data)
  97. that.setData({
  98. activeInfo: that.data.activeInfo
  99. })
  100. }
  101. })
  102. },
  103. finishLoad: function (e) {
  104. let index = e.currentTarget.dataset.index
  105. this.data.imgArr[index].finishLoadFlag = true
  106. this.setData({
  107. imgArr: this.data.imgArr
  108. })
  109. },
  110. // 切换图片
  111. dataChange(e) {
  112. this.setData({
  113. picArr: []
  114. })
  115. let i = e.currentTarget.dataset.index
  116. // let photo=this.data.picGroup.cameraPhotos[i].photoList
  117. let arr = this.data.picGroup.cameraPhotos[i].photoList.slice(0, this.data.num)
  118. let obj = {}
  119. let newArr = []
  120. let newArr1 = []
  121. let copy = []
  122. arr.forEach((ele, i) => {
  123. copy.push(ele)
  124. obj.url = ele,
  125. obj.flag = true,
  126. // 显示展位图
  127. obj.finishLoadFlag = true
  128. newArr.push(JSON.parse(JSON.stringify(obj)))
  129. })
  130. // todo:左右两边高度问题
  131. // newArr.forEach(item=>{
  132. // item.image=item.url
  133. // })
  134. // console.log(newArr)
  135. // this._select(newArr, false).then(() => {
  136. // success && success();
  137. // }).catch(err => {
  138. // console.error(err);
  139. // });
  140. this.setData({
  141. imgArr: newArr,
  142. activeDate: i,
  143. picArr: copy,
  144. change: 1,
  145. })
  146. },
  147. _select(data, refresh) {
  148. const query = wx.createSelectorQuery().in(this);
  149. this.columnNodes = query.selectAll('#left, #right');
  150. return new Promise((resolve) => {
  151. this._render(data, 0, refresh, () => {
  152. resolve();
  153. });
  154. });
  155. },
  156. _render(data, i, refresh, success) {
  157. if ((data.length > i || refresh) && this.data.imgArr.length !== 0) {
  158. this.columnNodes.boundingClientRect().exec(res => {
  159. console.log(res)
  160. const rects = res[0];
  161. this.data.leftHeight = rects[0].height;
  162. this.data.rightHeight = rects[1].height;
  163. if (this.data.leftHeight <= this.data.rightHeight || refresh) {
  164. data[i].location = 'left'
  165. } else {
  166. data[i].location = 'right'
  167. }
  168. console.log(6555555, this.data.rightHeight, this.data.leftHeight, data)
  169. this.setData({
  170. imgArr: data,
  171. }, () => {
  172. this._render(data, ++i, false, success);
  173. });
  174. });
  175. } else {
  176. success && success();
  177. }
  178. },
  179. imgListBinderror(e) {
  180. var photos = this.data.photos,
  181. index = e.currentTarget.dataset.index;
  182. photos[index] = '/images/noimg.png'
  183. this.setData({
  184. photos
  185. });
  186. },
  187. // 预览图片
  188. openpreviewImg(e) {
  189. this.selectComponent("#previewComponent").showPreview();
  190. let index = e.target.dataset.index
  191. let abbreviationPic = []
  192. this.data.picArr.forEach(item => {
  193. let item1 = {
  194. url: '',
  195. originalShow: false
  196. }
  197. item1.url = item + '?x-oss-process=image/quality,q_10'
  198. abbreviationPic.push(item1)
  199. })
  200. console.log(abbreviationPic[index], 888, this.data.picArr)
  201. this.setData({
  202. defImg: abbreviationPic[index].url, //当前图片
  203. preList: abbreviationPic, //缩略图列表
  204. originalArr: this.data.picArr //原图列表
  205. })
  206. },
  207. // 选择图片
  208. checkRadio(e) {
  209. let picInfo = e.currentTarget.dataset;
  210. let that = this
  211. if (picInfo.flag == true) { //已选中,修改选中项数组
  212. this.data.checkedGroup.forEach((el, i) => {
  213. if (picInfo.id == i) {
  214. this.data.checkedGroup.splice(i, 1)
  215. }
  216. });
  217. this.data.imgArr[picInfo.id].flag = false;
  218. } else { //未选中,修改原数组
  219. this.data.imgArr[picInfo.id].flag = true;
  220. if (this.data.checkedGroup.length <= 300) {
  221. this.data.checkedGroup.push(picInfo.id);
  222. } else {
  223. util.showTips('最多可选300张图片。');
  224. }
  225. }
  226. this.setData({
  227. imgArr: this.data.imgArr
  228. })
  229. },
  230. // 保存图片
  231. // save(event) {
  232. // // 获取图片
  233. // wx.getImageInfo({
  234. // src: this.data.saveImageList,
  235. // success: function (res) {
  236. // let path = res.path
  237. // wx.saveImageToPhotosAlbum({
  238. // filePath: path,
  239. // success: function (result) {
  240. // wx.showToast({
  241. // title: '下载图片成功',
  242. // duration: 2000,
  243. // mask: true
  244. // })
  245. // },
  246. // fail: function () {
  247. // wx.openSetting({
  248. // success: function () {}
  249. // })
  250. // }
  251. // })
  252. // }
  253. // })
  254. // },
  255. // 查看更多
  256. // jumPage(){
  257. // wx.navigateTo({
  258. // url: '/pages/growup/photo/index',
  259. // })
  260. // this.setData({
  261. // imgArr:that.data.imgArr
  262. // })
  263. // },
  264. // 保存图片的延迟回调
  265. getTempPath(url) {
  266. return new Promise((resolve, reject) => {
  267. wx.downloadFile({
  268. url: url,
  269. success: function (res) {
  270. var temp = res.tempFilePath
  271. wx.saveImageToPhotosAlbum({
  272. filePath: temp,
  273. success(res) {
  274. return resolve(res)
  275. },
  276. fail: function (err) {
  277. reject(url + JSON.stringify(err))
  278. }
  279. })
  280. },
  281. fail: function (err) {
  282. reject(url + JSON.stringify(err))
  283. }
  284. })
  285. })
  286. },
  287. // 保存图片
  288. save(event) {
  289. // 获取图片
  290. let saveList = []
  291. this.data.imgArr.filter((item, index) => {
  292. if (item.flag == false) {
  293. saveList.push(this.data.imgArr[index].url)
  294. }
  295. })
  296. // let copyList=saveList.join(",")
  297. wx.showLoading({
  298. title: '图片下载中',
  299. mask: true
  300. })
  301. const imageList = []
  302. // 循环数组
  303. for (let i = 0; i < saveList.length; i++) {
  304. imageList.push(this.getTempPath(saveList[i]))
  305. }
  306. const loadTask = []
  307. let index = 0
  308. while (index < imageList.length) {
  309. loadTask.push(
  310. new Promise((resolve, reject) => {
  311. // 将数据分割成多个promise数组
  312. Promise.all(imageList.slice(index, (index += 8)))
  313. .then(res => {
  314. resolve(res)
  315. })
  316. .catch(err => {
  317. reject(err)
  318. })
  319. })
  320. )
  321. }
  322. // Promise.all 所有图片下载完成后弹出
  323. Promise.all(loadTask)
  324. .then(res => {
  325. wx.showToast({
  326. title: '下载完成',
  327. duration: 3000
  328. })
  329. })
  330. .catch(err => {
  331. wx.showToast({
  332. title: `下载完成`,
  333. icon: 'none',
  334. duration: 3000
  335. })
  336. })
  337. },
  338. // 查看更多
  339. jumPage() {
  340. wx.setStorageSync('liveId', this.data.liveId)
  341. wx.navigateTo({
  342. url: `/pages/growup/photo/index?liveId=${this.data.liveId}`,
  343. })
  344. },
  345. // 批量选择
  346. selectMore() {
  347. this.setData({
  348. selectMore: true
  349. })
  350. },
  351. swipeUpload() {
  352. if (this.data.num < this.data.picGroup.cameraPhotos[this.data.activeDate].photoList.length) {
  353. this.data.num = this.data.num + 24
  354. let arr = this.data.picGroup.cameraPhotos[this.data.activeDate].photoList.slice(0, this.data.num)
  355. let obj = {}
  356. let newArr = []
  357. let copy = []
  358. arr.forEach((ele, i) => {
  359. copy.push(ele)
  360. obj.url = ele,
  361. obj.flag = true
  362. newArr.push(JSON.parse(JSON.stringify(obj)))
  363. })
  364. this.setData({
  365. imgArr: newArr,
  366. picArr: copy,
  367. })
  368. this.setData({
  369. num: this.data.num
  370. })
  371. }
  372. },
  373. onPullDownRefresh() {
  374. console.log('下拉')
  375. this.setData({
  376. num: 20
  377. })
  378. this.getPicture()
  379. },
  380. // 获取图片
  381. getPicture() {
  382. let that = this;
  383. util.ajax({
  384. func: "v2/child/photoList",
  385. data: {
  386. "liveId": that.data.liveId,
  387. }
  388. }, function (res) {
  389. if (res.code == 0) {
  390. let copyRes = res.data.cameraPhotos[0].photoList.slice(0, that.data.num)
  391. let obj = {}
  392. let newArr = []
  393. let copy = []
  394. copyRes.forEach((ele, i) => {
  395. copy.push(ele)
  396. obj.url = ele,
  397. obj.flag = true
  398. newArr.push(JSON.parse(JSON.stringify(obj)))
  399. })
  400. that.setData({
  401. picArr: copy,
  402. picGroup: res.data,
  403. imgArr: newArr
  404. })
  405. }
  406. })
  407. },
  408. // 跳转至活动详情
  409. toDetail() {
  410. let status = this.data.activeInfo.status
  411. if (status == 2) {
  412. util.showTips('该活动已下架,无法查看。可联系宝大大进行咨询~');
  413. } else if (status == 0) {
  414. // 跳转至活动详情页
  415. wx.navigateTo({
  416. url: `/pages/product/activity/index?aid=${this.data.activeInfo.aid}`,
  417. })
  418. } else if (status == 1) {
  419. // 跳转至详情
  420. wx.navigateTo({
  421. url: `/pages/product/activity/index?aid=${this.data.activeInfo.aid}`,
  422. })
  423. }
  424. },
  425. cancel() {
  426. this.setData({
  427. selectMore: false
  428. })
  429. },
  430. share() {
  431. this.setData({
  432. showShare: true
  433. })
  434. },
  435. onUnload() {
  436. this.data.setData({
  437. liveId: ""
  438. })
  439. }
  440. })