album.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view>
  3. <view class="album-wrap">
  4. <scroll-view scroll-y="true" class="group-wrap">
  5. <view class="item" :class="{ selected: item.album_id == albumId }" v-for="(item, index) in albumList" :key="index" @click="changeAlbum(item.album_id)">
  6. <text :class="{ 'color-base-border': item.album_id == albumId }">{{ item.album_name }}</text>
  7. </view>
  8. </scroll-view>
  9. <view class="pic-wrap">
  10. <mescroll-uni @getData="getData" ref="mescroll" size="30">
  11. <block slot="list">
  12. <view class="list-wrap">
  13. <view class="item-wrap upload" @click="photograph()">
  14. <text class="iconfont iconxiangji"></text>
  15. <text class="txt">拍摄照片</text>
  16. </view>
  17. <view class="item-wrap" v-for="(item, index) in picList" :key="index" @click="previewImg(item.pic_path)">
  18. <image :src="$util.img(item.pic_path, { size: 'mid' })" mode="scaleToFill" @error="imgError(index)"></image>
  19. <view @click.stop="checkImg(item.pic_path,index)" class="circle" :class="{ 'selected color-base-bg': isSelected(item.pic_path) }">
  20. {{ getImgIndex(item.pic_path) }}
  21. </view>
  22. <view v-show="isSelected(item.pic_path)" class="mask-layer"></view>
  23. </view>
  24. </view>
  25. </block>
  26. </mescroll-uni>
  27. </view>
  28. </view>
  29. <view class="footer-wrap">
  30. <button type="primary" :disabled="selectedImg.length == number" @click="save()" size="mini">
  31. 确定{{ selectedImg.length ? '(' + selectedImg.length + ')' : '' }}
  32. </button>
  33. </view>
  34. <loading-cover ref="loadingCover"></loading-cover>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. albumList: [],
  42. albumId: 0,
  43. picList: [],
  44. mescroll: null,
  45. number: 9,
  46. selectedImg: [],
  47. index: 0
  48. };
  49. },
  50. onLoad(option) {
  51. this.number = option.number || 9;
  52. if (!this.$util.checkToken('/pages/goods/album?number=' + this.number)) return;
  53. var selectedAlbumImgTemp = uni.getStorageSync('selectedAlbumImgTemp') ? JSON.parse(uni.getStorageSync('selectedAlbumImgTemp')) : null;
  54. if (selectedAlbumImgTemp) {
  55. if (selectedAlbumImgTemp.list) {
  56. this.selectedImg = selectedAlbumImgTemp.list.split(',');
  57. }
  58. this.index = selectedAlbumImgTemp.index;
  59. }
  60. },
  61. async onShow() {
  62. await this.getAlbumList();
  63. },
  64. methods: {
  65. async getAlbumList() {
  66. var res = await this.$api.sendRequest({
  67. url: '/shopapi/album/lists',
  68. async: false,
  69. success: res => {}
  70. });
  71. if (res.data) {
  72. this.albumList = res.data;
  73. if (this.albumList.length > 0) this.albumId = this.albumList[0].album_id;
  74. if (this.mescroll) this.mescroll.resetUpScroll();
  75. }
  76. },
  77. getData(mescroll) {
  78. this.mescroll = mescroll;
  79. if (this.albumId == 0) return;
  80. this.$api.sendRequest({
  81. url: '/shopapi/album/picList',
  82. data: {
  83. page_size: mescroll.size,
  84. page: mescroll.num,
  85. album_id: this.albumId
  86. },
  87. success: res => {
  88. let newArr = [];
  89. let msg = res.message;
  90. if (res.code == 0 && res.data) {
  91. newArr = res.data.list;
  92. } else {
  93. this.$util.showToast({
  94. title: msg
  95. });
  96. }
  97. mescroll.endSuccess(newArr.length);
  98. //设置列表数据
  99. if (mescroll.num == 1) this.picList = []; //如果是第一页需手动制空列表
  100. this.picList = this.picList.concat(newArr); //追加新数据
  101. // newArr.forEach(v => {
  102. // v.isExist = false; // 图片是否存在
  103. // });
  104. if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
  105. }
  106. });
  107. },
  108. changeAlbum(album_id) {
  109. this.albumId = album_id;
  110. if (this.mescroll) this.mescroll.resetUpScroll();
  111. },
  112. // 手机拍照
  113. photograph() {
  114. this.$util.upload(
  115. {
  116. number: this.number,
  117. path: 'album',
  118. sourceType: ['camera'],
  119. album_id: this.albumId
  120. },
  121. res => {
  122. if (this.mescroll) this.mescroll.resetUpScroll();
  123. }
  124. );
  125. },
  126. checkImg(pic_path,key) {
  127. var index = this.selectedImg.indexOf(pic_path);
  128. // if(this.picList[key].isExist) {
  129. // this.$util.showToast({
  130. // title: '该图片未找到',
  131. // });
  132. // return;
  133. // }
  134. if (index == -1) {
  135. if (this.selectedImg.length + 1 > this.number) return;
  136. this.selectedImg.push(pic_path);
  137. } else {
  138. this.selectedImg.splice(index, 1);
  139. }
  140. },
  141. isSelected(pic_path) {
  142. if (this.selectedImg.indexOf(pic_path) > -1) return true;
  143. else return false;
  144. },
  145. getImgIndex(pic_path) {
  146. var index = this.selectedImg.indexOf(pic_path);
  147. if (index > -1) return index + 1;
  148. else return '';
  149. },
  150. previewImg(pic_path) {
  151. var index = this.selectedImg.indexOf(pic_path);
  152. var paths = [];
  153. if (index > -1) {
  154. this.selectedImg.forEach(item => {
  155. paths.push(this.$util.img(item));
  156. });
  157. } else {
  158. paths = [this.$util.img(pic_path)];
  159. }
  160. uni.previewImage({
  161. current: 0,
  162. urls: paths
  163. });
  164. },
  165. imgError(index) {
  166. // this.picList[index].isExist = true;
  167. // this.picList[index].pic_path = this.$util.getDefaultImage().default_goods_img;
  168. // this.$forceUpdate();
  169. },
  170. save() {
  171. var temp = {
  172. list: this.selectedImg.toString(),
  173. index: this.index
  174. };
  175. uni.setStorageSync('selectedAlbumImg', JSON.stringify(temp));
  176. uni.navigateBack({
  177. delta: 1
  178. });
  179. }
  180. }
  181. };
  182. </script>
  183. <style lang="scss">
  184. page {
  185. overflow: hidden;
  186. }
  187. .album-wrap {
  188. display: flex;
  189. .group-wrap {
  190. width: 25%;
  191. height: 93vh;
  192. padding-bottom: constant(safe-area-inset-bottom);
  193. padding-bottom: env(safe-area-inset-bottom);
  194. .item {
  195. padding: 20rpx 20rpx 20rpx 0;
  196. overflow: hidden;
  197. text-overflow: ellipsis;
  198. white-space: nowrap;
  199. text {
  200. padding: 0 20rpx;
  201. border-left: 4rpx solid transparent;
  202. }
  203. &.selected {
  204. background-color: #fff;
  205. }
  206. }
  207. }
  208. .pic-wrap {
  209. flex: 1;
  210. .list-wrap {
  211. display: flex;
  212. flex-direction: row;
  213. flex-wrap: wrap;
  214. align-items: center;
  215. padding-bottom: constant(safe-area-inset-bottom);
  216. padding-bottom: env(safe-area-inset-bottom);
  217. .item-wrap {
  218. position: relative;
  219. width: 32.5%;
  220. height: 170rpx;
  221. margin-right: 4rpx;
  222. margin-bottom: 4rpx;
  223. line-height: initial;
  224. text-align: center;
  225. image {
  226. width: 170rpx;
  227. height: 170rpx;
  228. }
  229. .mask-layer {
  230. background-color: rgba(0, 0, 0, 0.4);
  231. position: absolute;
  232. z-index: 10;
  233. top: 0;
  234. left: 0;
  235. right: 0;
  236. bottom: 0;
  237. }
  238. .circle {
  239. border: 2rpx solid #fff;
  240. position: absolute;
  241. z-index: 20;
  242. border-radius: 50%;
  243. width: 40rpx;
  244. height: 40rpx;
  245. background-color: rgba(0, 0, 0, 0.2);
  246. top: 10rpx;
  247. right: 10rpx;
  248. text-align: center;
  249. line-height: 40rpx;
  250. color: #fff;
  251. &.selected {
  252. border-color: transparent;
  253. }
  254. }
  255. &.upload {
  256. background-color: #f2f2f2;
  257. color: $color-tip;
  258. line-height: inherit;
  259. .iconfont {
  260. font-size: 60rpx;
  261. display: block;
  262. text-align: center;
  263. }
  264. .txt {
  265. display: block;
  266. text-align: center;
  267. font-size: $font-size-tag;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. }
  274. .footer-wrap {
  275. position: fixed;
  276. bottom: 0;
  277. right: 0;
  278. padding: 20rpx 30rpx 0;
  279. z-index: 10;
  280. background-color: #fff;
  281. width: 100%;
  282. text-align: right;
  283. padding-bottom: constant(safe-area-inset-bottom);
  284. padding-bottom: env(safe-area-inset-bottom);
  285. }
  286. </style>
  287. <style scoped>
  288. .pic-wrap >>> .mescroll-uni-fixed {
  289. top: initial;
  290. bottom: initial;
  291. left: initial;
  292. right: initial;
  293. bottom: 100rpx; /* !important*/
  294. width: 75%;
  295. }
  296. </style>