album.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // pages/components/album/album.js
  2. const app = getApp();
  3. const common = require('../../../common');
  4. const utils = require("../../../utils/util");
  5. const appConfig = require("../../../config");
  6. const wxUploadFile = utils.promisify(wx.uploadFile);
  7. Component({
  8. /**
  9. * 组件的属性列表
  10. */
  11. properties: {
  12. albumList: Object
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. data: {
  18. imgList: [],
  19. albumList: {
  20. "pages": {
  21. "totalPage": 0,
  22. "currentPage": 0,
  23. },
  24. "lists": []
  25. },
  26. bgColor: [
  27. "grey",
  28. "blue",
  29. "red",
  30. "cyan"
  31. ],
  32. pics: [],
  33. isLoad: false,
  34. lastId: 0,
  35. },
  36. ready: function () {
  37. var _this = this;
  38. this.loadAlbum(_this, this.data.lastId, 1, appConfig.pageSize);
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. setDefaultAlbum() {
  45. this.setData({
  46. albumList: {
  47. "pages": {
  48. "totalPage": 0,
  49. "currentPage": 0,
  50. },
  51. "lists": []
  52. },
  53. lastId: 0,
  54. });
  55. },
  56. onPullDownRefresh() {
  57. wx.showNavigationBarLoading();
  58. this.setDefaultAlbum();
  59. this.loadAlbum(this, 0, 1, appConfig.pageSize);
  60. wx.stopPullDownRefresh();
  61. },
  62. onReachBottom() {
  63. if (!this.data.albumList.pages) {
  64. return;
  65. }
  66. if (this.data.albumList.pages.currentPage < this.data.albumList.pages.totalPage) {
  67. this.setData({
  68. hiddenLoading: false,
  69. });
  70. //加载下一页
  71. this.loadAlbum(this, this.data.lastId, parseInt(this.data.albumList.pages.currentPage) + 1, appConfig.pageSize);
  72. }
  73. },
  74. loadAlbum(_this, lastId, page, pageSize) {
  75. this.data.lastId = lastId;
  76. this.setData({
  77. "isLoad": false
  78. });
  79. app.albumSync = res => {
  80. this.setData({
  81. hiddenLoading: true,
  82. });
  83. var showRes = this.data.albumList;
  84. if (parseInt(res.pages.currentPage) == 1) {
  85. this.data.pics = [];
  86. showRes = res;
  87. for (var i in res.lists) {
  88. this.data.pics.push(res.lists[i].source);
  89. }
  90. }else{
  91. showRes['pages'] = res.pages;
  92. for (var i in res.lists) {
  93. this.data.lastId = res.lists[i].id;
  94. this.data.pics.push(res.lists[i].source);
  95. showRes.lists.push(res.lists[i]);
  96. }
  97. }
  98. _this.setData({
  99. albumList: showRes
  100. });
  101. if (res.pages.currentPage == res.pages.totalPage || res.pages.totalPage == 0) {
  102. this.setData({
  103. "isLoad": true
  104. });
  105. this.setData({
  106. hiddenLoading: false,
  107. });
  108. }
  109. }
  110. common.album(this, this.data.lastId, page, pageSize)
  111. },
  112. showPic(e) {
  113. var pics = [e.target.dataset.source, e.target.dataset.src];
  114. var index = e.target.dataset.index;
  115. wx.previewImage({
  116. urls: this.data.pics,
  117. current: pics[0] // 当前显示图片的http链接
  118. })
  119. },
  120. remove(e){
  121. var _this = this;
  122. var uid = e.target.dataset.uid;
  123. var id = e.target.dataset.id;
  124. var type = e.target.dataset.type;
  125. app.removeAlbumSync = res =>{
  126. if(typeof res != 'object') {
  127. app.toast("删除失败");
  128. return;
  129. }
  130. if(res.code > 0){
  131. app.toast(res.msg || "删除失败");
  132. return;
  133. }
  134. app.toast("删除成功");
  135. setTimeout(function(){
  136. _this.setDefaultAlbum();
  137. _this.loadAlbum(_this, 0, 1, appConfig.pageSize);
  138. }, 1500);
  139. }
  140. wx.showModal({
  141. title: '温馨提示',
  142. content: '是否要永久删除此照片',
  143. success: function(item){
  144. if (item.confirm) {
  145. common.albumRemove(this, uid, id, type);
  146. }
  147. }
  148. })
  149. },
  150. ChooseImage() {
  151. wx.chooseImage({
  152. count: 8, //默认9
  153. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  154. sourceType: ['album'], //从相册选择
  155. success: (res) => {
  156. if (this.data.imgList.length != 0) {
  157. this.setData({
  158. imgList: this.data.imgList.concat(res.tempFilePaths)
  159. })
  160. } else {
  161. this.setData({
  162. imgList: res.tempFilePaths
  163. })
  164. }
  165. }
  166. });
  167. },
  168. ViewImage(e) {
  169. wx.previewImage({
  170. urls: this.data.imgList,
  171. current: e.currentTarget.dataset.url
  172. });
  173. },
  174. DelImg(e) {
  175. wx.showModal({
  176. title: '召唤师',
  177. content: '确定要删除这段回忆吗?',
  178. cancelText: '再看看',
  179. confirmText: '再见',
  180. success: res => {
  181. if (res.confirm) {
  182. this.data.imgList.splice(e.currentTarget.dataset.index, 1);
  183. this.setData({
  184. imgList: this.data.imgList
  185. })
  186. }
  187. }
  188. })
  189. },
  190. submitForm: function() {
  191. var _this = this;
  192. if (this.data.imgList.length == 0) {
  193. app.warning("请选择您要上传的文件~");
  194. return;
  195. }
  196. wx.showLoading({
  197. title: '上传中',
  198. });
  199. var header = {
  200. 'content-type': 'multipart/form-data',
  201. 'cookie': wx.getStorageSync(appConfig.storeKeys.sessionId)
  202. };
  203. var url = utils.getUrl("albumUpload");
  204. var success = 0;
  205. var fail = 0;
  206. const arr = this.data.imgList.map(path => {
  207. return wxUploadFile({
  208. url: url,
  209. header: header,
  210. filePath: path,
  211. name: 'file[]'
  212. });
  213. });
  214. Promise.all(arr).then(res => {
  215. return res.map(item => JSON.parse(item.data).data.files[0]);
  216. }).catch(err => {
  217. app.warning("上传文件失败~");
  218. wx.hideLoading();
  219. }).then(res => {
  220. return res;
  221. }).then(res => {
  222. var success = 0;
  223. var fail = 0;
  224. for(var i in res) {
  225. if(res[i].code == 0){
  226. success++;
  227. }else{
  228. fail++;
  229. }
  230. }
  231. wx.hideLoading();
  232. if(fail > 0) {
  233. app.toast("失败" + fail + "个");
  234. }else{
  235. app.toast("上传成功");
  236. _this.setData({
  237. imgList:[],
  238. });
  239. }
  240. setTimeout(function () {
  241. _this.setDefaultAlbum();
  242. _this.loadAlbum(_this, 0, 1, appConfig.pageSize);
  243. }, 2000);
  244. });
  245. }
  246. }
  247. })