category.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <template>
  2. <view class="app-page flex flex-col">
  3. <view v-if="loading != 'success'" class="loading-wrap pa-24 pt-36">
  4. <block v-if="loading == 'loading'">
  5. <tm-skeleton model="listAvatr"></tm-skeleton>
  6. <tm-skeleton model="listAvatr"></tm-skeleton>
  7. <tm-skeleton model="listAvatr"></tm-skeleton>
  8. <tm-skeleton model="listAvatr"></tm-skeleton>
  9. <tm-skeleton model="listAvatr"></tm-skeleton>
  10. </block>
  11. <view v-else-if="loading == 'error'" class="loading-error flex flex-col flex-center">
  12. <tm-empty icon="icon-wind-cry" label="加载失败"><tm-button theme="light-blue" size="m" @click="fnGetData()">重新加载</tm-button></tm-empty>
  13. </view>
  14. </view>
  15. <view v-else class="app-page-content pa-24">
  16. <view v-if="dataList.length == 0" class="content-empty flex flex-center"><tm-empty icon="icon-shiliangzhinengduixiang-" label="无数据"></tm-empty></view>
  17. <block v-else>
  18. <block v-for="(category, index) in dataList" :key="index">
  19. <tm-translate animation-name="fadeUp" :wait="(index + 1) * 50">
  20. <view class="category-card flex round-3 bg-white pa-24 mb-24" @click="fnShowFormModal(category)">
  21. <text class="del-icon flex flex-center" @click.stop="fnDelete(category, index)">×</text>
  22. <view class="left">
  23. <image v-if="category.showThumbnail" class="cover round-3" :src="category.showThumbnail" mode="aspectFill"></image>
  24. <view v-else class="cover round-3 flex flex-center text-weight-b text-white text-size-m">无封面</view>
  25. </view>
  26. <view class="right pl-24 text-size-m">
  27. <view class="text-weight-b">名称:{{ category.name }}</view>
  28. <view class="mt-12">
  29. <text class="text-grey-darken-1">别名:</text>
  30. {{ category.slug || '暂无别名' }}
  31. </view>
  32. <view class="mt-12 flex desc-box">
  33. <view class="label text-grey-darken-1">路径:</view>
  34. <view class="value text-size-s">{{ category.fullPath }}</view>
  35. </view>
  36. <view class="mt-12 flex desc-box">
  37. <view class="label text-grey-darken-1">描述:</view>
  38. <view class="value text-size-s">{{ category.description || '暂无描述' }}</view>
  39. </view>
  40. </view>
  41. </view>
  42. </tm-translate>
  43. </block>
  44. <tm-flotbutton @click="fnToTopPage" :offset="[16, 80]" size="m" color="light-blue" icon="icon-angle-up"></tm-flotbutton>
  45. <tm-flotbutton @click="fnShowFormModal()" size="m" color="orange" icon="icon-plus"></tm-flotbutton>
  46. </block>
  47. </view>
  48. <!-- 编辑或新增 -->
  49. <tm-poup v-model="poupShow" position="bottom" height="85vh" @change="fnOnPoupChange">
  50. <view class="poup-content">
  51. <view class="poup-head text-align-center text-weight-b text-size-g ma-24">{{ form.id != undefined ? '编辑分类' : '新增分类' }}</view>
  52. <scroll-view class="poup-body pa-24 pt-0" :scroll-y="true" @touchmove.stop>
  53. <tm-input
  54. required
  55. :adjust-position="true"
  56. :round="3"
  57. :borderBottom="false"
  58. title="名称"
  59. bg-color="grey-lighten-5"
  60. v-model="form.name"
  61. placeholder="请输入分类名称"
  62. ></tm-input>
  63. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:页面上所显示的名称</view>
  64. <tm-input
  65. :borderBottom="false"
  66. :adjust-position="true"
  67. :round="3"
  68. title="别名"
  69. bg-color="grey-lighten-5"
  70. v-model="form.slug"
  71. placeholder="请输入分类别名"
  72. ></tm-input>
  73. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:一般为单个分类页面的标识,最好为英文</view>
  74. <tm-input
  75. :borderBottom="false"
  76. :adjust-position="true"
  77. :round="3"
  78. title="密码"
  79. bg-color="grey-lighten-5"
  80. v-model="form.password"
  81. placeholder="请输入分类密码"
  82. ></tm-input>
  83. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:分类的访问密码</view>
  84. <tm-input
  85. :borderBottom="false"
  86. :vertical="true"
  87. :adjust-position="true"
  88. inputType="textarea"
  89. :round="3"
  90. title="描述"
  91. :height="160"
  92. bg-color="grey-lighten-5"
  93. v-model="form.description"
  94. placeholder="请输入分类描述"
  95. ></tm-input>
  96. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:分类的描述信息</view>
  97. <view class="ma-30 mt-12 pb-12">
  98. <view class="mb-12">
  99. <text class="text-size-m">封面图</text>
  100. <text class="text-grey text-size-s">(点击下方图片区域选择封面)</text>
  101. </view>
  102. <image v-if="form.showThumbnail" class="thumbnail round-3" :src="form.showThumbnail" mode="aspectFill" @click="attachmentsSelectShow = true"></image>
  103. <view v-else class="thumbnail round-3 text-grey grey-lighten-5 flex flex-col flex-center " @click="attachmentsSelectShow = true">
  104. <text class="iconfont icon-picture" style="font-size: 46rpx;"></text>
  105. <text class="mt-12 text-size-m">选择封面图</text>
  106. </view>
  107. </view>
  108. </scroll-view>
  109. <view class="btn-wrap flex flex-center">
  110. <tm-button size="m" theme="bg-gradient-blue-accent" @click="fnSubmit()">保存</tm-button>
  111. <tm-button v-if="form.id != undefined" size="m" theme="bg-gradient-red-accent" @click="fnDelete()">删除</tm-button>
  112. <tm-button size="m" theme="bg-gradient-blue-grey-accent" @click="poupShow = false">关 闭</tm-button>
  113. </view>
  114. </view>
  115. </tm-poup>
  116. <!-- 附件选择文件 -->
  117. <attachment-select selectType="image" v-if="attachmentsSelectShow" @on-select="fnOnAttachmentsSelect" @on-close="fnOnAttachmentsSelectClose"></attachment-select>
  118. </view>
  119. </template>
  120. <script>
  121. import tmSkeleton from '@/tm-vuetify/components/tm-skeleton/tm-skeleton.vue';
  122. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  123. import tmEmpty from '@/tm-vuetify/components/tm-empty/tm-empty.vue';
  124. import tmTranslate from '@/tm-vuetify/components/tm-translate/tm-translate.vue';
  125. import tmFlotbutton from '@/tm-vuetify/components/tm-flotbutton/tm-flotbutton.vue';
  126. import tmPoup from '@/tm-vuetify/components/tm-poup/tm-poup.vue';
  127. import tmInput from '@/tm-vuetify/components/tm-input/tm-input.vue';
  128. import AttachmentSelect from '@/components/attachment-select/attachment-select.vue';
  129. export default {
  130. components: {
  131. tmSkeleton,
  132. tmButton,
  133. tmEmpty,
  134. tmTranslate,
  135. tmFlotbutton,
  136. tmPoup,
  137. tmInput,
  138. AttachmentSelect
  139. },
  140. data() {
  141. return {
  142. loading: 'loading',
  143. queryParams: {
  144. size: 10,
  145. page: 0,
  146. more: true
  147. },
  148. dataList: [],
  149. poupShow: false,
  150. attachmentsSelectShow: false,
  151. form: {
  152. description: undefined,
  153. password: undefined,
  154. createTime: undefined,
  155. fullPath: undefined,
  156. id: undefined,
  157. name: '',
  158. slug: undefined,
  159. thumbnail: undefined,
  160. showThumbnail: undefined
  161. }
  162. };
  163. },
  164. onLoad() {
  165. this.fnSetPageTitle('分类管理');
  166. },
  167. created() {
  168. this.fnGetData();
  169. },
  170. onPullDownRefresh() {
  171. this.isLoadMore = false;
  172. this.queryParams.page = 0;
  173. this.fnGetData();
  174. },
  175. methods: {
  176. fnGetData() {
  177. uni.showLoading({
  178. mask: true,
  179. title: '加载中...'
  180. });
  181. // 设置状态为加载中
  182. if (!this.isLoadMore) {
  183. this.loading = 'loading';
  184. }
  185. this.loadMoreText = '加载中...';
  186. this.$httpApi.admin
  187. .getCategoryList(this.queryParams)
  188. .then(res => {
  189. console.log('getCategoryList:');
  190. console.log(res);
  191. if (res.status == 200) {
  192. this.loading = 'success';
  193. this.dataList = res.data.map(item => {
  194. if (item.thumbnail) {
  195. item['showThumbnail'] = this.$utils.checkThumbnailUrl(item.thumbnail);
  196. } else {
  197. item['showThumbnail'] = '';
  198. }
  199. return item;
  200. });
  201. } else {
  202. this.loading = 'error';
  203. }
  204. console.log(this.dataList);
  205. })
  206. .catch(err => {
  207. console.error(err);
  208. this.loading = 'error';
  209. this.loadMoreText = '加载失败,请下拉刷新!';
  210. })
  211. .finally(() => {
  212. setTimeout(() => {
  213. uni.hideLoading();
  214. uni.stopPullDownRefresh();
  215. }, 800);
  216. });
  217. },
  218. fnSelectColor() {
  219. this.$refs.colorPicker.open();
  220. },
  221. fnOnConfirmSelectColor(e) {
  222. this.selectColor = e.rgba;
  223. this.$set(this.form, 'color', e.hex);
  224. },
  225. fnOnAttachmentsSelectClose() {
  226. this.attachmentsSelectShow = false;
  227. },
  228. fnOnPoupChange(e) {
  229. if (!e) {
  230. this.fnResetForm();
  231. }
  232. },
  233. // 监听附件选择
  234. fnOnAttachmentsSelect(file) {
  235. this.form.thumbnail = file.path;
  236. this.form.showThumbnail = this.$utils.checkThumbnailUrl(file.path);
  237. this.attachmentsSelectShow = false;
  238. },
  239. fnShowFormModal(category) {
  240. if (category) {
  241. this.form = Object.assign({}, {}, category);
  242. if (category.thumbnail) {
  243. this.form.showThumbnail = this.$utils.checkThumbnailUrl(category.thumbnail);
  244. }
  245. } else {
  246. this.fnResetForm();
  247. }
  248. this.poupShow = true;
  249. },
  250. fnResetForm() {
  251. this.form = {
  252. color: undefined,
  253. createTime: undefined,
  254. fullPath: undefined,
  255. id: undefined,
  256. name: '',
  257. slug: undefined,
  258. thumbnail: undefined,
  259. showThumbnail: undefined
  260. };
  261. },
  262. fnSubmit() {
  263. if (this.form.name.trim() == '') {
  264. return uni.$tm.toast('分类名称未填写!');
  265. }
  266. if (this.form.id == undefined) {
  267. this.$httpApi.admin
  268. .createCategory(this.form)
  269. .then(res => {
  270. if (res.status == 200) {
  271. uni.$tm.toast(`保存成功!`);
  272. uni.startPullDownRefresh();
  273. } else {
  274. uni.$tm.toast('操作失败,请重试!');
  275. }
  276. })
  277. .catch(err => {
  278. uni.$tm.toast('操作失败,请重试!');
  279. });
  280. } else {
  281. this.$httpApi.admin
  282. .updateCategoryById(this.form.id, this.form)
  283. .then(res => {
  284. if (res.status == 200) {
  285. uni.$tm.toast(`保存成功!`);
  286. let updateIndex = this.dataList.findIndex(x => x.id == this.form.id);
  287. this.$set(this.dataList, updateIndex, this.form);
  288. } else {
  289. uni.$tm.toast('操作失败,请重试!');
  290. }
  291. })
  292. .catch(err => {
  293. uni.$tm.toast('操作失败,请重试!');
  294. });
  295. }
  296. },
  297. // 删除
  298. fnDelete(category) {
  299. const _category = category || this.form;
  300. uni.$eShowModal({
  301. title: '提示',
  302. content: `您是否要将 ${_category.name} 的分类删除?`,
  303. showCancel: true,
  304. cancelText: '否',
  305. cancelColor: '#999999',
  306. confirmText: '是',
  307. confirmColor: '#03a9f4'
  308. })
  309. .then(res => {
  310. this.$httpApi.admin
  311. .deleteCategoryById(_category.id)
  312. .then(res => {
  313. if (res.status == 200) {
  314. uni.$tm.toast(`${_category.name} 分类已删除!`);
  315. const delIndex = this.dataList.findIndex(x => x.id == _category.id);
  316. this.dataList.splice(delIndex, 1);
  317. } else {
  318. uni.$tm.toast('操作失败,请重试!');
  319. }
  320. })
  321. .catch(err => {
  322. uni.$tm.toast('操作失败,请重试!');
  323. });
  324. })
  325. .catch(err => {});
  326. }
  327. }
  328. };
  329. </script>
  330. <style lang="scss" scoped>
  331. .app-page {
  332. width: 100vw;
  333. min-height: 100vh;
  334. }
  335. .loading-wrap {
  336. .loading-error {
  337. height: 65vh;
  338. }
  339. }
  340. .content-empty {
  341. width: 100%;
  342. height: 60vh;
  343. }
  344. .poup-content {
  345. overflow: hidden;
  346. }
  347. .poup-body {
  348. height: 71vh;
  349. box-sizing: border-box;
  350. touch-action: none;
  351. }
  352. .thumbnail {
  353. width: 100%;
  354. height: 260rpx;
  355. }
  356. .category-card {
  357. position: relative;
  358. box-sizing: border-box;
  359. box-shadow: 0rpx 2rpx 24rpx rgba(0, 0, 0, 0.03);
  360. .del-icon {
  361. width: 46rpx;
  362. height: 46rpx;
  363. position: absolute;
  364. right: 24rpx;
  365. top: 24rpx;
  366. border-radius: 50%;
  367. box-sizing: border-box;
  368. border: 4rpx solid #fff;
  369. background-color: #ff8177;
  370. box-shadow: 0rpx 0rpx 24rpx rgba(0, 0, 0, 0.05);
  371. font-size: 32rpx;
  372. color: #fff;
  373. }
  374. .left {
  375. width: 200rpx;
  376. height: 190rpx;
  377. .cover {
  378. width: 100%;
  379. height: inherit;
  380. background-color: #607d8b;
  381. }
  382. }
  383. .right {
  384. width: 0;
  385. flex-grow: 1;
  386. box-sizing: border-box;
  387. .color {
  388. color: #fff;
  389. }
  390. }
  391. }
  392. .desc-box {
  393. box-sizing: border-box;
  394. .label {
  395. width: 86rpx;
  396. }
  397. .value {
  398. width: 0;
  399. flex-grow: 1;
  400. white-space: nowrap;
  401. overflow: hidden;
  402. text-overflow: ellipsis;
  403. word-wrap: break-word;
  404. word-break: break-all;
  405. }
  406. }
  407. </style>