1
0

attachment-select.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <template>
  2. <tm-poup v-model="show" position="bottom" height="auto" @change="fnClose">
  3. <view class="poup-head pa-24 text-align-center text-weight-b ">{{ title }}</view>
  4. <view class="poup-body pa-24 pt-0 pb-0">
  5. <view v-if="loading != 'success'" class="loading-wrap flex flex-center">
  6. <view v-if="loading == 'loading'" class="loading">加载中...</view>
  7. <view v-else class="error" @click="fnGetData()">加载失败,点击刷新!</view>
  8. </view>
  9. <block v-else>
  10. <view v-if="total == 0" class="empty">无附件</view>
  11. <scroll-view v-else class="poup-content" :enable-flex="true" :scroll-y="true" @touchmove.stop>
  12. <view class="card-content">
  13. <view class="card pa-12" v-for="(file, index) in dataList" :key="index" @click="fnOnSelect(file, index)">
  14. <view class="card-inner round-3" :class="{ 'is-select': selectIndex == index }">
  15. <cache-image v-if="file.isImage" class="cover" height="160rpx" :url="file.thumbPath" :fileMd5="file.thumbPath" mode="aspectFill"></cache-image>
  16. <view v-else class="cover flex pl-46 pr-46 flex-center bg-gradient-blue-grey-accent text-align-center text-size-m">{{ file.mediaType }}</view>
  17. <view class="name text-overflow text-size-m pa-12">{{ file.name }}</view>
  18. </view>
  19. </view>
  20. </view>
  21. </scroll-view>
  22. </block>
  23. </view>
  24. <view class="poup-foot pa-30 pb-12 pt-0">
  25. <!-- 分页 -->
  26. <view v-if="total > queryParams.size" class="mt-36 pl-24 pr-24">
  27. <tm-pagination color="bg-gradient-blue-accent" :page.sync="queryParams.page" :total="total" :totalVisible="5" @change="fnGetPagingData"></tm-pagination>
  28. </view>
  29. <view class=" flex flex-center mt-12">
  30. <tm-button size="m" theme="bg-gradient-blue-accent" @click="fnSava()">确定选用</tm-button>
  31. <tm-button size="m" theme="bg-gradient-orange-accent" @click="fnUpload()">上传</tm-button>
  32. <tm-button size="m" theme="bg-gradient-blue-grey-accent" @click="fnClose(false)">关 闭</tm-button>
  33. </view>
  34. </view>
  35. </tm-poup>
  36. </template>
  37. <script>
  38. import { getAdminAccessToken } from '@/utils/auth.js';
  39. import tmPoup from '@/tm-vuetify/components/tm-poup/tm-poup.vue';
  40. import tmPagination from '@/tm-vuetify/components/tm-pagination/tm-pagination.vue';
  41. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  42. export default {
  43. name: 'attachment-select',
  44. components: { tmPoup, tmPagination, tmButton },
  45. props: {
  46. title: {
  47. type: String,
  48. default: '附件列表'
  49. },
  50. selectType: {
  51. type: String,
  52. default: ''
  53. }
  54. },
  55. data() {
  56. return {
  57. show: true,
  58. loading: 'loading',
  59. total: 0,
  60. queryParams: {
  61. size: 6,
  62. page: 1
  63. },
  64. dataList: [],
  65. select: null,
  66. selectIndex: -1
  67. };
  68. },
  69. created() {
  70. this.fnGetData();
  71. },
  72. methods: {
  73. fnGetData() {
  74. this.queryParams.page = 1;
  75. this.fnGetPagingData(1);
  76. },
  77. fnGetPagingData(page) {
  78. this.loading = 'loading';
  79. const _params = {
  80. ...this.queryParams
  81. };
  82. _params.page = page - 1;
  83. this.$httpApi.admin
  84. .getAttachmentsByPage(_params)
  85. .then(res => {
  86. if (res.status == 200) {
  87. this.total = res.data.total;
  88. this.dataList = res.data.content.map(file => {
  89. if (this.$utils.fnCheckIsFileType('image', file) && file.size / 1024 / 1024 > 2) {
  90. file.isImage = false;
  91. file.desc = '图片过大无法显示缩略图';
  92. } else {
  93. file.isImage = this.$utils.fnCheckIsFileType('image', file);
  94. }
  95. file.thumbPath = this.$utils.checkThumbnailUrl(file.thumbPath);
  96. return file;
  97. });
  98. this.loading = 'success';
  99. } else {
  100. uni.$tm.toast('加载失败,请重试!');
  101. this.loading = 'error';
  102. }
  103. })
  104. .catch(err => {
  105. console.error(err);
  106. uni.$tm.toast('加载失败,请重试!');
  107. this.loading = 'error';
  108. });
  109. },
  110. fnOnSelect(file, index) {
  111. this.select = file;
  112. this.selectIndex = index;
  113. },
  114. fnSava() {
  115. if (this.selectType) {
  116. if (this.$utils.fnCheckIsFileType(this.selectType, this.select)) {
  117. this.$emit('on-select', this.select);
  118. } else {
  119. uni.$tm.toast('该附件类型不符合!');
  120. }
  121. } else {
  122. this.$emit('on-select', this.select);
  123. }
  124. },
  125. fnClose(e) {
  126. if (!e) {
  127. this.$emit('on-close');
  128. }
  129. },
  130. fnUpload() {
  131. uni.chooseImage({
  132. count: 1,
  133. success: res => {
  134. uni.uploadFile({
  135. filePath: res.tempFilePaths[0],
  136. header: {
  137. 'admin-authorization': getAdminAccessToken()
  138. },
  139. url: this.$baseApiUrl + '/api/admin/attachments/upload',
  140. name: 'file',
  141. success: upladRes => {
  142. const _uploadRes = JSON.parse(upladRes.data);
  143. if (_uploadRes.status == 200) {
  144. uni.$tm.toast('上传成功!');
  145. this.fnGetData(1);
  146. } else {
  147. uni.$tm.toast(_uploadRes.message);
  148. }
  149. },
  150. fail: err => {
  151. uni.$tm.toast(err.message);
  152. }
  153. });
  154. }
  155. });
  156. }
  157. }
  158. };
  159. </script>
  160. <style scoped lang="scss">
  161. .poup-head {
  162. }
  163. .poup-body {
  164. height: 50vh;
  165. }
  166. .loading-wrap {
  167. height: 50vh;
  168. background-color: #fafafa;
  169. }
  170. .poup-content {
  171. height: inherit;
  172. box-sizing: border-box;
  173. .card-content {
  174. height: inherit;
  175. display: flex;
  176. flex-wrap: wrap;
  177. }
  178. }
  179. .card {
  180. width: 50%;
  181. box-sizing: border-box;
  182. &-inner {
  183. box-sizing: border-box;
  184. overflow: hidden;
  185. box-shadow: 0rpx 4rpx 24rpx rgba(0, 0, 0, 0.05);
  186. border: 4rpx solid transparent;
  187. &.is-select {
  188. border-color: rgb(13, 141, 242);
  189. }
  190. }
  191. .cover {
  192. width: 100%;
  193. height: 160rpx;
  194. flex-wrap: wrap;
  195. box-sizing: border-box;
  196. }
  197. .name {
  198. color: #303133;
  199. box-sizing: border-box;
  200. text-align: center;
  201. }
  202. }
  203. </style>