album.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // pages/album/album.js
  2. const HOST = require('../../utils/const.js').apiUrl;
  3. import httpRequestApi from '../../utils/APIRequest';
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. stateFlag: true,
  10. photoList: [],
  11. photoBoxToken: []
  12. },
  13. //更改相册
  14. setState: function () {
  15. this.setData({
  16. stateFlag: !this.data.stateFlag
  17. })
  18. const photoBox = this.data.stateFlag ? 'OPEN' : 'PRIVACY';
  19. wx.setStorageSync('photoBox', photoBox);
  20. httpRequestApi.setPhoto(photoBox).success(res => {
  21. const photoBoxToken = this.data.stateFlag ? [] : res.data.data.photoBoxToken.toString().split('');
  22. this.setData({
  23. photoBoxToken,
  24. })
  25. });
  26. },
  27. //上传相册
  28. addPhoto: function () {
  29. if(this.data.photoList.length >= 15) {
  30. wx.showModal({
  31. title: '温馨提示',
  32. content: '只能上传15张图片'
  33. })
  34. return false;
  35. };
  36. wx.chooseImage({
  37. count: 9, // 默认9
  38. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  39. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  40. success: (res) => {
  41. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  42. const tempFilePaths = res.tempFilePaths;
  43. //启动上传等待中...
  44. wx.showToast({
  45. title: '正在上传...',
  46. icon: 'loading',
  47. mask: true,
  48. duration: 1000
  49. })
  50. const that = this;
  51. let uploadImgCount = 0;
  52. tempFilePaths.forEach(item => {
  53. //上传文件
  54. wx.uploadFile({
  55. url: HOST + 'wx/file/upload',
  56. filePath: item,
  57. name: 'uploadfile_ant',
  58. header: {
  59. "Content-Type": "multipart/form-data"
  60. },
  61. success: (res) => {
  62. uploadImgCount++;
  63. const data = JSON.parse(res.data);
  64. if(data.success) {
  65. //上传文件成功后放到相册里
  66. httpRequestApi.addPhotoList({
  67. path: data.data
  68. }).success((res) => {
  69. if(res.data.success) {
  70. //再次调用相册列表
  71. that.getPhotoList();
  72. }
  73. }).fail(() => {
  74. wx.showModal({
  75. title: '错误提示',
  76. content: '图片上传到相册失败'
  77. })
  78. });
  79. }
  80. //如果是最后一张,则隐藏等待中
  81. if (uploadImgCount == tempFilePaths.length) {
  82. wx.hideToast();
  83. }
  84. },
  85. fail: function (res) {
  86. wx.hideToast();
  87. wx.showModal({
  88. title: '错误提示',
  89. content: '上传图片失败'
  90. })
  91. }
  92. });
  93. });
  94. }
  95. })
  96. },
  97. //预览图片
  98. preview: function ({ currentTarget }) {
  99. // const index = currentTarget.dataset.index;
  100. // console.log(this.data.photoList[index].path);
  101. const imageUrl = [];
  102. for (let item of this.data.photoList) {
  103. imageUrl.push(item.path)
  104. }
  105. wx.previewImage({
  106. urls: imageUrl
  107. })
  108. },
  109. //删除相册
  110. removePhoto: function ({ currentTarget }) {
  111. httpRequestApi.removePhotoList(currentTarget.dataset.id).success((res) => {
  112. console.log('删除册列表', res);
  113. if(res.data.success) {
  114. this.getPhotoList();
  115. }
  116. })
  117. },
  118. /**
  119. * 生命周期函数--监听页面加载
  120. */
  121. onLoad: function (options) {
  122. this.getPhotoList();
  123. //初始化相册是否加密
  124. const photoBox = (wx.getStorageSync('photoBox') === 'OPEN' ? true : false);
  125. this.setData({
  126. stateFlag: photoBox
  127. })
  128. if(!photoBox) {
  129. httpRequestApi.setPhoto(wx.getStorageSync('photoBox')).success(res => {
  130. const photoBoxToken = this.data.stateFlag ? [] : res.data.data.photoBoxToken.toString().split('');
  131. this.setData({
  132. photoBoxToken,
  133. })
  134. });
  135. }
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. },
  147. //获取相册列表
  148. getPhotoList: function () {
  149. httpRequestApi.getPhotoList({
  150. pageNo: 1,
  151. pageSize: 15
  152. }).success((res) => {
  153. console.log('相册列表', res.data.data.list);
  154. this.setData({
  155. photoList: res.data.data.list
  156. })
  157. })
  158. }
  159. })