album.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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: 1, // 默认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. that.showCropper({
  69. src: tempFilePaths[0],
  70. mode: mode,
  71. sizeType: ['original', 'compressed'], //'original'(default) | 'compressed'
  72. callback: (res) => {
  73. if (mode == 'rectangle') {
  74. // wx.previewImage({
  75. // current: res,
  76. // urls: [res]
  77. // })
  78. that.hideCropper() //隐藏,项目里是点击完成就上传,所以如果回调是上传,那么隐藏掉就行了,不用previewImage
  79. wx.showToast({
  80. title: '玩命加载中...',
  81. icon: 'loading',
  82. mask: true,
  83. duration: 1000
  84. })
  85. //上传文件
  86. wx.uploadFile({
  87. url: HOST + 'wx/file/upload',
  88. filePath: res,
  89. name: 'uploadfile_ant',
  90. header: {
  91. "Content-Type": "multipart/form-data"
  92. },
  93. success: (res) => {
  94. const data = JSON.parse(res.data);
  95. if(data.success) {
  96. //上传文件成功后放到相册里
  97. httpRequestApi.addPhotoList({
  98. path: data.data
  99. }).success((res) => {
  100. if(res.data.success) {
  101. //再次调用相册列表
  102. that.getPhotoList();
  103. }
  104. }).fail(() => {
  105. wx.showModal({
  106. title: '错误提示',
  107. content: '图片上传到相册失败'
  108. })
  109. });
  110. }
  111. },
  112. fail: function (res) {
  113. wx.hideToast();
  114. wx.showModal({
  115. title: '错误提示',
  116. content: '上传图片失败'
  117. })
  118. }
  119. });
  120. /*
  121. const that = this;
  122. tempFilePaths.forEach(item => {
  123. //上传文件
  124. wx.uploadFile({
  125. url: HOST + 'wx/file/upload',
  126. filePath: item,
  127. name: 'uploadfile_ant',
  128. header: {
  129. "Content-Type": "multipart/form-data"
  130. },
  131. success: (res) => {
  132. uploadImgCount++;
  133. const data = JSON.parse(res.data);
  134. if(data.success) {
  135. //上传文件成功后放到相册里
  136. httpRequestApi.addPhotoList({
  137. path: data.data
  138. }).success((res) => {
  139. if(res.data.success) {
  140. //再次调用相册列表
  141. that.getPhotoList();
  142. }
  143. }).fail(() => {
  144. wx.showModal({
  145. title: '错误提示',
  146. content: '图片上传到相册失败'
  147. })
  148. });
  149. }
  150. },
  151. fail: function (res) {
  152. wx.hideToast();
  153. wx.showModal({
  154. title: '错误提示',
  155. content: '上传图片失败'
  156. })
  157. }
  158. });
  159. });
  160. */
  161. }
  162. else {
  163. wx.showModal({
  164. title: '',
  165. content: JSON.stringify(res),
  166. })
  167. }
  168. }
  169. })
  170. // wx.navigateTo({
  171. // url: '/pages/clipPhoto/clipPhoto?imageUrl=' + tempFilePaths[0]
  172. // })
  173. }
  174. })
  175. },
  176. //预览图片
  177. preview: function ({ currentTarget }) {
  178. // const index = currentTarget.dataset.index;
  179. // console.log(this.data.photoList[index].path);
  180. const imageUrl = [];
  181. for (let item of this.data.photoList) {
  182. imageUrl.push(item.path)
  183. }
  184. wx.previewImage({
  185. urls: imageUrl
  186. })
  187. },
  188. //删除相册
  189. removePhoto: function ({ currentTarget }) {
  190. httpRequestApi.removePhotoList(currentTarget.dataset.id).success((res) => {
  191. console.log('删除册列表', res);
  192. if(res.data.success) {
  193. this.getPhotoList();
  194. }
  195. })
  196. },
  197. /**
  198. * 生命周期函数--监听页面加载
  199. */
  200. onLoad: function (options) {
  201. var that = this
  202. // 初始化组件数据和绑定事件
  203. cropper.init.apply(that, [W, H]);
  204. //获取相册列表
  205. this.getPhotoList();
  206. //初始化相册是否加密
  207. const photoBox = (wx.getStorageSync('photoBox') === 'OPEN' ? true : false);
  208. this.setData({
  209. stateFlag: photoBox
  210. })
  211. if(!photoBox) {
  212. httpRequestApi.setPhoto(wx.getStorageSync('photoBox')).success(res => {
  213. const photoBoxToken = this.data.stateFlag ? [] : res.data.data.photoBoxToken.toString().split('');
  214. this.setData({
  215. photoBoxToken,
  216. })
  217. });
  218. }
  219. },
  220. /**
  221. * 页面上拉触底事件的处理函数
  222. */
  223. onReachBottom: function () {
  224. },
  225. /**
  226. * 用户点击右上角分享
  227. */
  228. onShareAppMessage: function () {
  229. },
  230. //获取相册列表
  231. getPhotoList: function () {
  232. httpRequestApi.getPhotoList({
  233. pageNo: 1,
  234. pageSize: 15
  235. }).success((res) => {
  236. console.log('相册列表', res.data.data.list);
  237. this.setData({
  238. photoList: res.data.data.list
  239. })
  240. })
  241. }
  242. })