index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. const util = require("../../../utils/util.js");
  2. const app = getApp();
  3. Page({
  4. data: {
  5. childInfo: [],
  6. childs: [],
  7. childIndex: 0,
  8. activity: [],
  9. actives: [],
  10. activityIndex: 0,
  11. teamInfo: {
  12. cameraPhotos: [],
  13. teamPhotos: []
  14. },
  15. list: [],
  16. photoList: [],
  17. photos: [],
  18. activeIndex: 0,
  19. activeDate: 0,
  20. finished: false,
  21. pageSize: 32,
  22. pageIndex: 1,
  23. finishedText: '',
  24. npsOptons: {},
  25. npsStatus: false, //nps组件是否显示
  26. isGet: true,
  27. liveId: "",
  28. liveFlag: false,
  29. idcard: "",
  30. cardType: "",
  31. Copyactives: "",
  32. childName: "",
  33. butFlag: true,
  34. actives2: [],
  35. imgArr: [],
  36. picGroup: [],
  37. saveImageList: [],
  38. picArr: [],
  39. dateList: [],
  40. photoFlag: true
  41. },
  42. selectMore() {
  43. this.setData({
  44. butFlag: false
  45. })
  46. },
  47. // 选择图片
  48. checkRadio(e) {
  49. let picInfo = e.currentTarget.dataset;
  50. let that = this
  51. if (picInfo.flag == true) { //已选中,修改选中项数组
  52. // this.data.checkedGroup.forEach((el,i) => {
  53. // if (picInfo.id==i) {
  54. // this.data.checkedGroup.splice(i,1)
  55. // }
  56. // });
  57. this.data.imgArr[picInfo.id].flag = false;
  58. } else { //未选中,修改原数组
  59. this.data.imgArr[picInfo.id].flag = true;
  60. // if (this.data.checkedGroup.length<=300) {
  61. // this.data.checkedGroup.push(picInfo.id);
  62. // }else{
  63. // util.showTips('最多可选300张图片。');
  64. // }
  65. }
  66. this.setData({
  67. imgArr: this.data.imgArr
  68. })
  69. },
  70. showpic(e) {
  71. let url = e.currentTarget.dataset.url;
  72. wx.previewImage({
  73. current: url, // 当前显示图片的 http 链接
  74. saveImageList: [],
  75. photos: [],
  76. activeInfo: {}
  77. })
  78. },
  79. // 保存图片
  80. save(event) {
  81. // 获取图片
  82. let saveList = []
  83. this.data.imgArr.filter((item, index) => {
  84. if (item.flag == false) {
  85. saveList.push(this.data.imgArr[index].url)
  86. }
  87. })
  88. // let copyList=saveList.join(",")
  89. wx.showLoading({
  90. title: '图片下载中',
  91. mask: true
  92. })
  93. const imageList = []
  94. // 循环数组
  95. for (let i = 0; i < saveList.length; i++) {
  96. imageList.push(this.getTempPath(saveList[i]))
  97. }
  98. const loadTask = []
  99. let index = 0
  100. while (index < imageList.length) {
  101. loadTask.push(
  102. new Promise((resolve, reject) => {
  103. // 将数据分割成多个promise数组
  104. Promise.all(imageList.slice(index, (index += 8)))
  105. .then(res => {
  106. resolve(res)
  107. })
  108. .catch(err => {
  109. reject(err)
  110. })
  111. })
  112. )
  113. }
  114. // Promise.all 所有图片下载完成后弹出
  115. Promise.all(loadTask)
  116. .then(res => {
  117. wx.showToast({
  118. title: '下载完成',
  119. duration: 3000
  120. })
  121. })
  122. .catch(err => {
  123. wx.showToast({
  124. title: `下载完成`,
  125. icon: 'none',
  126. duration: 3000
  127. })
  128. })
  129. },
  130. // 保存图片的延迟回调
  131. getTempPath(url) {
  132. return new Promise((resolve, reject) => {
  133. wx.downloadFile({
  134. url: url,
  135. success: function (res) {
  136. var temp = res.tempFilePath
  137. wx.saveImageToPhotosAlbum({
  138. filePath: temp,
  139. success(res) {
  140. return resolve(res)
  141. },
  142. fail: function (err) {
  143. reject(url + JSON.stringify(err))
  144. }
  145. })
  146. },
  147. fail: function (err) {
  148. reject(url + JSON.stringify(err))
  149. }
  150. })
  151. })
  152. },
  153. cancel() {
  154. this.setData({
  155. butFlag: true
  156. })
  157. },
  158. onShow() {
  159. },
  160. onLoad: function (options) {
  161. if (options.liveId) {
  162. this.data.liveId = options.liveId
  163. this.setData({
  164. liveId: this.data.liveId || JSON.parse(wx.getStorageSync('liveId')),
  165. liveFlag: true,
  166. })
  167. this.getChild();
  168. return
  169. } else {
  170. this.setData({
  171. liveFlag: false
  172. })
  173. this.getChild();
  174. }
  175. },
  176. getInfo() {
  177. let that = this
  178. util.ajax({
  179. func: "v2/child/photoDefaultItems",
  180. data: {
  181. "liveId": that.data.liveId
  182. }
  183. }, function (res) {
  184. if (res.data&&res.data.cardType&&res.data.idcard) { //有默认项数据
  185. that.data.childInfo.forEach((item, index) => {
  186. if (res.data.cardType == item.cardType && res.data.idcard == item.cardNo) {
  187. that.setData({
  188. idcard: res.data.idcard,
  189. cardType: res.data.cardType,
  190. childName: item.name,
  191. childIndex: index //保存默认儿童位置
  192. })
  193. wx.setNavigationBarTitle({
  194. title: that.data.childName
  195. });
  196. // 为真 跳转过来有liveid
  197. if (that.data.liveFlag) {
  198. that.CopygetActivity()
  199. } else {
  200. that.getActivity()
  201. }
  202. }
  203. })
  204. } else {
  205. that.getActivity()
  206. }
  207. });
  208. },
  209. // 如果是跳转页面的话执行
  210. CopygetActivity() {
  211. let that = this,
  212. arr = [];
  213. // console.log(this.data.idcard);
  214. let arr2 = []
  215. // console.log(this.data.cardType);
  216. util.ajax({
  217. func: "daily/activity_list",
  218. data: {
  219. "idcard": that.data.idcard,
  220. "cardType": that.data.cardType
  221. }
  222. }, function (res) {
  223. if (res.code == 0) {
  224. res.data.forEach((item, i) => {
  225. if (item.liveId === that.data.liveId) {
  226. // console.log(8888, i, item.liveId, that.data.liveId)
  227. that.data.activityIndex = i
  228. for (var i = 0; i < res.data.length; i++) {
  229. arr.push(res.data[i].sname + " " + res.data[i].pstitle);
  230. arr2.push(res.data[i].sname + " " + res.data[i].pstitle)
  231. }
  232. that.setData({
  233. activityIndex: that.data.activityIndex,
  234. activity: res.data,
  235. Copyactives: item.sname + "" + item.atitle,
  236. actives: arr,
  237. actives2: arr2
  238. })
  239. if (res.data.length > 0) {
  240. that.team();
  241. }
  242. }
  243. })
  244. }
  245. });
  246. },
  247. selectImage(e) {
  248. this.setData({
  249. isGet: e.detail.isGet
  250. });
  251. },
  252. close(e) { //关闭nps
  253. this.setData({
  254. npsStatus: false
  255. });
  256. },
  257. getChild() {
  258. let that = this,
  259. arr = [];
  260. util.ajax({
  261. func: "user/childs"
  262. }, function (res) {
  263. if (res.code == 0) {
  264. for (var i = 0; i < res.data.length; i++) {
  265. arr.push(res.data[i].name);
  266. }
  267. that.setData({
  268. childInfo: res.data,
  269. childs: arr
  270. });
  271. // if (num == 1) {
  272. // that.getInfo()
  273. // return
  274. // // 获取儿童信息不需要调用获取照片接口
  275. // } else {
  276. that.getInfo()
  277. // }
  278. } else {
  279. util.showTips(res.reason)
  280. }
  281. });
  282. },
  283. getActivity() {
  284. let that = this,
  285. arr = [];
  286. wx.setNavigationBarTitle({
  287. title: that.data.childs[that.data.childIndex]
  288. });
  289. util.ajax({
  290. func: "daily/activity_list",
  291. data: {
  292. "idcard": that.data.childInfo[that.data.childIndex].cardNo,
  293. "cardType": that.data.childInfo[that.data.childIndex].cardType
  294. }
  295. }, function (res) {
  296. if (res.code == 0) {
  297. for (var i = 0; i < res.data.length; i++) {
  298. arr.push(res.data[i].sname + " " + res.data[i].pstitle);
  299. }
  300. that.setData({
  301. activity: res.data,
  302. actives: arr,
  303. activityIndex: 0
  304. })
  305. if (res.data.length > 0) that.team();
  306. } else {
  307. util.showTips(res.reason)
  308. }
  309. });
  310. },
  311. team() {
  312. let that = this;
  313. util.ajax({
  314. func: "v2/child/photoList",
  315. data: {
  316. "liveId": this.data.activity[that.data.activityIndex].liveId,
  317. "teamId": this.data.activity[that.data.activityIndex].teamId,
  318. }
  319. }, function (res) {
  320. if (res.code == 0) {
  321. let teamInfo = res.data,
  322. list = [];
  323. if (teamInfo.cameraPhotos && teamInfo.teamPhotos && ((teamInfo.cameraPhotos.length > 0 && teamInfo.teamPhotos.length > 0) || (teamInfo.cameraPhotos.length > 0 && teamInfo.teamPhotos.length <= 0))) {
  324. list = teamInfo.cameraPhotos;
  325. that.data.activeIndex = 0
  326. } else if (teamInfo.teamPhotos && teamInfo.teamPhotos.length > 0) {
  327. list = teamInfo.teamPhotos;
  328. that.data.activeIndex = 1
  329. }
  330. let obj = {}
  331. let copyArr = []
  332. let newArr = []
  333. if (list[0]) {
  334. // 时间列表
  335. list.forEach(item => {
  336. copyArr.push(item.date)
  337. })
  338. list[0].photoList.forEach((ele, i) => {
  339. obj.url = ele,
  340. obj.flag = true
  341. newArr.push(JSON.parse(JSON.stringify(obj)))
  342. that.data.picArr.push(ele)
  343. })
  344. }
  345. if (that.data.activeIndex == 0) {
  346. that.data.photoFlag = true
  347. } else {
  348. that.data.photoFlag = false
  349. }
  350. that.setData({
  351. photoFlag: that.data.photoFlag,
  352. picGroup: res.data,
  353. teamInfo,
  354. list,
  355. imgArr: newArr,
  356. dateList: copyArr,
  357. activeIndex: that.data.activeIndex
  358. })
  359. if (list.length > 0) {
  360. that.resetlist();
  361. }
  362. } else {
  363. util.showTips(res.reason);
  364. }
  365. });
  366. },
  367. resetlist() {
  368. this.setData({
  369. photoList: this.data.list[this.data.activeDate].photoList,
  370. pageIndex: 1,
  371. finished: false
  372. })
  373. this.getList(1);
  374. },
  375. getList(type) {
  376. wx.hideLoading();
  377. let data = this.data.photoList.slice(this.data.pageSize * (this.data.pageIndex - 1), this.data.pageSize * this.data.pageIndex);
  378. this.setData({
  379. photos: (type ? data : [...this.data.photos, ...data])
  380. })
  381. if (this.data.photos.length >= this.data.photoList.length || this.data.photos.length < this.data.pageSize) {
  382. this.setData({
  383. finishedText: this.data.photos.length < this.data.pageSize ? '' : "~已经到底了~",
  384. finished: true
  385. })
  386. }
  387. },
  388. onReachBottom() {
  389. if (this.data.finished) return false
  390. wx.showLoading();
  391. this.setData({
  392. pageIndex: this.data.pageIndex + 1
  393. })
  394. this.getList()
  395. },
  396. childsPickerChange(e) {
  397. this.setData({
  398. childIndex: e.detail.value,
  399. activityIndex: 0,
  400. activeIndex: 0,
  401. activeDate: 0,
  402. teamInfo: [],
  403. list: [],
  404. dateList: [],
  405. childName: "",
  406. picArr: []
  407. });
  408. this.getActivity();
  409. },
  410. activesPickerChange(e) {
  411. this.setData({
  412. activityIndex: e.detail.value,
  413. activeIndex: 0,
  414. activeDate: 0,
  415. teamInfo: [],
  416. list: [],
  417. Copyactives: "",
  418. picArr: []
  419. });
  420. this.team();
  421. },
  422. tabsChange(e) {
  423. let activeIndex = e.currentTarget.dataset.index,
  424. activeDate = 0;
  425. if (activeIndex == this.data.activeIndex) return false;
  426. this.setData({
  427. picArr: [],
  428. imgArr: []
  429. })
  430. let obj = {}
  431. let copyobj = {}
  432. let copyArr = []
  433. let newArr = []
  434. let list = activeIndex == 1 ? this.data.teamInfo.teamPhotos : this.data.teamInfo.cameraPhotos;
  435. list.forEach((item, index) => {
  436. copyArr.push(item.date)
  437. if (index == 0) {
  438. item.photoList.forEach(ele => {
  439. obj.url = ele,
  440. obj.flag = true
  441. newArr.push(JSON.parse(JSON.stringify(obj)))
  442. this.data.picArr.push(ele)
  443. })
  444. }
  445. })
  446. if (activeIndex == 0) {
  447. this.data.photoFlag = true
  448. } else {
  449. this.data.photoFlag = false
  450. }
  451. this.setData({
  452. photoFlag: this.data.photoFlag,
  453. activeIndex,
  454. activeDate,
  455. imgArr: newArr,
  456. dateList: copyArr,
  457. picArr: this.data.picArr
  458. });
  459. this.resetlist();
  460. },
  461. dataChange(e) {
  462. this.setData({
  463. picArr: []
  464. })
  465. let i = e.currentTarget.dataset.index
  466. let arr
  467. if (this.data.photoFlag == true) {
  468. arr = this.data.picGroup.cameraPhotos[i].photoList
  469. } else {
  470. arr = this.data.picGroup.teamPhotos[i].photoList
  471. }
  472. let obj = {}
  473. let newArr = []
  474. arr.forEach((ele, i) => {
  475. this.data.picArr.push(ele)
  476. obj.url = ele,
  477. obj.flag = true
  478. newArr.push(JSON.parse(JSON.stringify(obj)))
  479. })
  480. this.setData({
  481. imgArr: newArr,
  482. activeDate: i
  483. })
  484. },
  485. imgListBinderror(e) {
  486. var photos = this.data.photos,
  487. index = e.currentTarget.dataset.index;
  488. photos[index] = '/images/noimg.png'
  489. this.setData({
  490. photos
  491. });
  492. },
  493. previewImage(e) {
  494. let url = e.currentTarget.dataset.url;
  495. // this.data.picArr.push(url)
  496. wx.previewImage({
  497. current: url, // 当前显示图片的 http 链接
  498. urls: this.data.picArr // 需要预览的图片 http 链接列表
  499. })
  500. },
  501. onShareAppMessage() {
  502. return {
  503. title: '儿童参加活动的精彩照片',
  504. path: '/pages/account/photo/index'
  505. }
  506. }
  507. })