album.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // pages/album/album.js
  2. // 获取显示区域长宽
  3. const device = wx.getSystemInfoSync()
  4. const W = device.windowWidth
  5. const H = device.windowHeight - 50
  6. let cropper = require('../../component/welCropper/welCropper.js');
  7. console.log(device)
  8. const HOST = require('../../utils/const.js').apiUrl;
  9. import httpRequestApi from '../../utils/APIRequest';
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. stateFlag: true,
  16. photoList: [],
  17. photoBoxToken: []
  18. },
  19. //更改相册
  20. setState: function () {
  21. this.setData({
  22. stateFlag: !this.data.stateFlag
  23. })
  24. const photoBox = this.data.stateFlag ? 'OPEN' : 'PRIVACY';
  25. wx.setStorageSync('photoBox', photoBox);
  26. httpRequestApi.setPhoto(photoBox).success(res => {
  27. const photoBoxToken = this.data.stateFlag ? [] : res.data.data.photoBoxToken.toString().split('');
  28. this.setData({
  29. photoBoxToken,
  30. })
  31. });
  32. },
  33. //上传相册
  34. addPhoto: function (e) {
  35. let that = this
  36. let mode = e.currentTarget.dataset.mode
  37. //如果用户没有填写信息不能上传图片
  38. const userInfo = wx.getStorageSync('userInfo');
  39. if(!userInfo.data.data.mobileNo || !userInfo.data.data.avatar) {
  40. wx.navigateTo({
  41. url: '/pages/setName/setName'
  42. })
  43. return false;
  44. };
  45. //提示图片上传不能超过15张
  46. if(this.data.photoList.length >= 15) {
  47. wx.showModal({
  48. title: '温馨提示',
  49. content: '只能上传15张图片'
  50. })
  51. return false;
  52. };
  53. wx.chooseImage({
  54. count: 9, // 默认9
  55. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  56. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  57. success: (res) => {
  58. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  59. const tempFilePaths = res.tempFilePaths;
  60. //启动上传等待中...
  61. wx.showToast({
  62. title: '正在上传...',
  63. icon: 'loading',
  64. mask: true,
  65. duration: 1000
  66. })
  67. /*
  68. //调用图片截取方法
  69. that.showCropper({
  70. src: tempFilePaths[0],
  71. mode: mode,
  72. sizeType: ['original', 'compressed'], //'original'(default) | 'compressed'
  73. callback: (res) => {
  74. if (mode == 'rectangle') {
  75. // wx.previewImage({
  76. // current: res,
  77. // urls: [res]
  78. // })
  79. that.hideCropper() //隐藏,项目里是点击完成就上传,所以如果回调是上传,那么隐藏掉就行了,不用previewImage
  80. wx.showToast({
  81. title: '玩命加载中...',
  82. icon: 'loading',
  83. mask: true,
  84. duration: 1000
  85. })
  86. //上传文件
  87. wx.uploadFile({
  88. url: HOST + 'wx/file/upload',
  89. filePath: res,
  90. name: 'uploadfile_ant',
  91. header: {
  92. "Content-Type": "multipart/form-data"
  93. },
  94. success: (res) => {
  95. const data = JSON.parse(res.data);
  96. if(data.success) {
  97. //上传文件成功后放到相册里
  98. httpRequestApi.addPhotoList({
  99. path: data.data
  100. }).success((res) => {
  101. if(res.data.success) {
  102. //再次调用相册列表
  103. that.getPhotoList();
  104. }
  105. }).fail(() => {
  106. wx.showModal({
  107. title: '错误提示',
  108. content: '图片上传到相册失败'
  109. })
  110. });
  111. }
  112. },
  113. fail: function (res) {
  114. wx.hideToast();
  115. wx.showModal({
  116. title: '错误提示',
  117. content: '上传图片失败'
  118. })
  119. }
  120. });
  121. }
  122. else {
  123. wx.showModal({
  124. title: '',
  125. content: JSON.stringify(res),
  126. })
  127. }
  128. }
  129. })
  130. */
  131. /*
  132. wx.navigateTo({
  133. url: '/pages/clipPhoto/clipPhoto?imageUrl=' + tempFilePaths[0]
  134. })
  135. */
  136. const that = this;
  137. tempFilePaths.forEach(item => {
  138. //上传文件
  139. wx.uploadFile({
  140. url: HOST + 'wx/file/upload',
  141. filePath: item,
  142. name: 'uploadfile_ant',
  143. header: {
  144. "Content-Type": "multipart/form-data"
  145. },
  146. success: (res) => {
  147. const data = JSON.parse(res.data);
  148. if(data.success) {
  149. //上传文件成功后放到相册里
  150. httpRequestApi.addPhotoList({
  151. path: data.data
  152. }).success((res) => {
  153. if(res.data.success) {
  154. //再次调用相册列表
  155. that.getPhotoList();
  156. }
  157. }).fail(() => {
  158. wx.showModal({
  159. title: '错误提示',
  160. content: '图片上传到相册失败'
  161. })
  162. });
  163. }
  164. },
  165. fail: function (res) {
  166. wx.hideToast();
  167. wx.showModal({
  168. title: '错误提示',
  169. content: '上传图片失败'
  170. })
  171. }
  172. });
  173. });
  174. }
  175. })
  176. },
  177. //预览图片
  178. preview: function ({ currentTarget }) {
  179. const index = currentTarget.dataset.index;
  180. const imageUrl = [];
  181. for (let item of this.data.photoList) {
  182. imageUrl.push(item.path)
  183. }
  184. console.log(this.data.photoList[index].path)
  185. wx.previewImage({
  186. urls: imageUrl,
  187. current: this.data.photoList[index].path
  188. })
  189. },
  190. //删除相册
  191. removePhoto: function ({ currentTarget }) {
  192. httpRequestApi.removePhotoList(currentTarget.dataset.id).success((res) => {
  193. console.log('删除册列表', res);
  194. if(res.data.success) {
  195. this.getPhotoList();
  196. }
  197. })
  198. },
  199. /**
  200. * 生命周期函数--监听页面加载
  201. */
  202. onLoad: function (options) {
  203. var that = this
  204. // 初始化组件数据和绑定事件
  205. cropper.init.apply(that, [W, H]);
  206. //获取相册列表
  207. this.getPhotoList();
  208. //初始化相册是否加密
  209. const photoBox = (wx.getStorageSync('photoBox') === 'OPEN' ? true : false);
  210. this.setData({
  211. stateFlag: photoBox
  212. })
  213. if(!photoBox) {
  214. httpRequestApi.setPhoto(wx.getStorageSync('photoBox')).success(res => {
  215. const photoBoxToken = this.data.stateFlag ? [] : res.data.data.photoBoxToken.toString().split('');
  216. this.setData({
  217. photoBoxToken,
  218. })
  219. });
  220. }
  221. },
  222. /**
  223. * 页面上拉触底事件的处理函数
  224. */
  225. onReachBottom: function () {
  226. },
  227. //获取相册列表
  228. getPhotoList: function () {
  229. httpRequestApi.getPhotoList({
  230. pageNo: 1,
  231. pageSize: 15
  232. }).success((res) => {
  233. console.log('相册列表', res.data.data.list);
  234. this.setData({
  235. photoList: res.data.data.list
  236. })
  237. })
  238. }
  239. })