AddGoodsclassification.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <view class="detail-view">
  3. <u-form label-width="140" :model="add_form" ref="uForm">
  4. <view class="form-model-view">
  5. <u-form-item required label="分类名称">
  6. <u-input type="text" v-model="form.title" placeholder="分类名称" />
  7. </u-form-item>
  8. <u-form-item label="上级分类">
  9. <view class="float_right" @click="this.$refs.tkitree._show();">
  10. <text v-if="category_name">{{ category_name }}</text>
  11. <text class="input-pl" v-else>请选择上级分类</text>
  12. </view>
  13. <view slot="right">
  14. <u-icon name="arrow-down-fill" size="24"></u-icon>
  15. </view>
  16. </u-form-item>
  17. <u-form-item label="分类图片" label-position="top">
  18. <upload :images="form.images" @handleRemove="imgRemove" @uploadSuccess="uploadSuccess" />
  19. </u-form-item>
  20. <u-form-item label="一级分类广告图片" label-position="top">
  21. <upload :images="form.adImage" @handleRemove="uploadAdRemove" @uploadSuccess="uploadAdSuccess" />
  22. </u-form-item>
  23. <u-form-item label="分类排序" prop="name">
  24. <u-input type="text" v-model="form.sort" placeholder="请输入分类排序" />
  25. </u-form-item>
  26. <u-form-item label="是否显示">
  27. <view class="form-value">
  28. <u-switch style="transform: translateY(14rpx);" @change="switchChange" v-model="enableStatus"
  29. :active-value="5" :inactive-value="4" size="40"></u-switch>
  30. </view>
  31. </u-form-item>
  32. </view>
  33. </u-form>
  34. <view class="detail-bottom">
  35. <u-button :loading='loading' class="handel-btn" type="primary" @click="addCategory">提交</u-button>
  36. </view>
  37. <tki-tree ref="tkitree" :selectParent="true" :range="cate_list" rangeKey="title" @confirm="cateConfirm">
  38. </tki-tree>
  39. </view>
  40. </template>
  41. <script>
  42. import tkiTree from '@/components/tki-tree/tki-tree.vue';
  43. import upload from '@/components/qiniu/QiniuUpload.vue';
  44. export default {
  45. components: {
  46. tkiTree,
  47. upload
  48. },
  49. data() {
  50. return {
  51. form: {
  52. code: "",
  53. images: '',
  54. sort: "",
  55. title: "",
  56. pid: "",
  57. enableStatus: 5,
  58. link: "",
  59. adImage: '',
  60. },
  61. cate_list: [],
  62. category_name: '', //分类名称
  63. enableStatus: true,
  64. id: '',
  65. loading: false
  66. };
  67. },
  68. onLoad(options) {
  69. if (options.id) {
  70. this.id = options.id,
  71. uni.setNavigationBarTitle({
  72. title: '编辑分类'
  73. })
  74. this.getCategoryInfoById()
  75. }
  76. this.getAllCategory();
  77. },
  78. methods: {
  79. // 获取所有商品分类
  80. getAllCategory() {
  81. this.$u.api.getAllCategory({
  82. enableStatus: 5
  83. }).then(res => {
  84. this.cate_list = res.data;
  85. });
  86. },
  87. cateConfirm(arr) {
  88. this.category_name = arr[0].title;
  89. this.form.pid = arr[0].id;
  90. this.form.link = arr[0].parents.map(item => item.id).join(',') + ',' + arr[0].id;
  91. },
  92. // 分类图片上传成功
  93. uploadSuccess(imgUrl) {
  94. if (this.form.images.length > 0) {
  95. this.$u.toast('图片只能添加一张')
  96. return
  97. }
  98. this.form.images = []
  99. this.form.images.push(imgUrl);
  100. },
  101. //移除分类图片
  102. imgRemove(arr) {
  103. this.form.images = '';
  104. },
  105. // 一级分类图片上传成功
  106. uploadAdSuccess(imgUrl) {
  107. if (this.form.adImage.length > 0) {
  108. this.$u.toast('图片只能添加一张')
  109. return
  110. }
  111. this.form.adImage = []
  112. this.form.adImage.push(imgUrl);
  113. },
  114. //移除一级分类图片
  115. uploadAdRemove(arr) {
  116. this.form.adImage = '';
  117. },
  118. switchChange(val) {
  119. this.form.enableStatus = val;
  120. console.log(this.form.enableStatus);
  121. },
  122. addCategory() {
  123. if (!this.form.title.trim()) {
  124. this.$u.toast('分类名称不能为空')
  125. return
  126. }
  127. this.loading = true
  128. try {
  129. if (this.id) {
  130. if (this.form.images) {
  131. this.form.images = this.form.images[0]
  132. }
  133. if (this.form.adImage) {
  134. this.form.adImage = this.form.adImage[0]
  135. }
  136. this.$u.api.editCategory(this.id, {
  137. ...this.form
  138. }).then(res => {
  139. this.$u.toast('编辑成功')
  140. setTimeout(() => {
  141. uni.navigateBack()
  142. }, 500)
  143. })
  144. } else {
  145. this.$u.api.addCategory({
  146. ...this.form
  147. }).then(res => {
  148. this.$u.toast('新增成功')
  149. setTimeout(() => {
  150. uni.navigateBack()
  151. }, 500)
  152. })
  153. }
  154. this.loading = false
  155. } catch {
  156. this.loading = false
  157. }
  158. },
  159. getCategoryInfoById() {
  160. this.$u.api.getCategoryInfoById(this.id).then(res => {
  161. this.form = res.data
  162. if (this.form.images) {
  163. let images = []
  164. images.push(res.data.images)
  165. this.form.images = images
  166. } else {
  167. this.form.images = []
  168. }
  169. if (this.form.adImage) {
  170. let adImage = []
  171. adImage.push(res.data.adImage)
  172. this.form.adImage = adImage
  173. }
  174. if (this.form.enableStatus === 4) {
  175. this.enableStatus = false
  176. }
  177. this.category_name = res.data.ptitle
  178. })
  179. }
  180. }
  181. }
  182. </script>
  183. <style lang="scss">
  184. </style>