Assign.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <div class="l-flex">
  3. <template v-if="groups">
  4. <div class="c-tree-sidebar u-overflow-y--auto">
  5. <el-tree
  6. ref="groupTree"
  7. :data="groups"
  8. class="c-tree-sidebar__main"
  9. node-key="path"
  10. highlight-current
  11. @node-click="onGroupTreeClick"
  12. />
  13. </div>
  14. <schema-table
  15. ref="table"
  16. :schema="schema"
  17. >
  18. <table-dialog
  19. ref="stockDialog"
  20. title="总库存"
  21. :schema="stockSchema"
  22. @choosen="onChoosen"
  23. />
  24. <confirm-dialog
  25. ref="addDialog"
  26. title="分配"
  27. @confirm="onSave"
  28. >
  29. <div class="c-grid-form u-align-self--center">
  30. <span class="c-grid-form__label">类型:</span>
  31. <schema-select
  32. v-model="credit.auditType"
  33. :schema="auditTypeSchema"
  34. disabled
  35. />
  36. <span class="c-grid-form__label">次数:</span>
  37. <div class="l-flex--row">
  38. <el-input-number
  39. v-model="credit.initialAmount"
  40. class="c-sibling-item"
  41. :min="1"
  42. :max="remaining"
  43. step-strictly
  44. />
  45. <span class="c-sibling-item u-color--info light">1 ~ {{ remaining }}</span>
  46. </div>
  47. <span class="c-grid-form__label required">有效期:</span>
  48. <el-date-picker
  49. v-model="credit.date"
  50. class="u-width--auto"
  51. type="daterange"
  52. range-separator="至"
  53. start-placeholder="开始日期"
  54. end-placeholder="结束日期"
  55. value-format="yyyy-MM-dd"
  56. :picker-options="pickerOptions"
  57. />
  58. </div>
  59. </confirm-dialog>
  60. </schema-table>
  61. </template>
  62. <template v-else>
  63. <div
  64. v-loading="loading"
  65. class="l-flex__auto l-flex--row center"
  66. >
  67. <template v-if="!loading">
  68. <warning
  69. v-if="error"
  70. @click="getTreeData"
  71. />
  72. <div
  73. v-else
  74. class="u-bold"
  75. >
  76. 暂无租户,请先添加租户
  77. </div>
  78. </template>
  79. </div>
  80. </template>
  81. </div>
  82. </template>
  83. <script>
  84. import { getTopGroups } from '@/api/user'
  85. import {
  86. getCredits,
  87. getTenantCredits,
  88. addCredit,
  89. deleteCredit
  90. } from './api'
  91. export default {
  92. name: 'StockAssign',
  93. data () {
  94. const auditTypeOptions = [
  95. { value: 1, label: '图片' },
  96. { value: 2, label: '视频' }
  97. ]
  98. return {
  99. loading: true,
  100. error: false,
  101. groups: null,
  102. group: null,
  103. auditTypeSchema: {
  104. options: auditTypeOptions
  105. },
  106. schema: {
  107. condition: { auditType: void 0 },
  108. list: this.getTenantCredits,
  109. buttons: [
  110. { type: 'add', label: '分配', on: this.onAdd }
  111. ],
  112. filters: [
  113. { key: 'auditType', type: 'select', placeholder: '全部类型', options: auditTypeOptions }
  114. ],
  115. cols: [
  116. { label: '审核类型', render: this.transformAuditType },
  117. { prop: 'effectiveDate', label: '生效日期' },
  118. { prop: 'expiryDate', label: '结束日期' },
  119. { prop: 'initialAmount', label: '总次数' },
  120. { prop: 'remaining', label: '剩余次数' },
  121. { type: 'invoke', render: [
  122. { label: '删除', on: this.onDel }
  123. ] }
  124. ]
  125. },
  126. stockSchema: {
  127. condition: { auditType: void 0 },
  128. list: getCredits,
  129. filters: [
  130. { key: 'auditType', type: 'select', placeholder: '全部类型', options: auditTypeOptions }
  131. ],
  132. cols: [
  133. { label: '审核类型', render: this.transformAuditType },
  134. { prop: 'effectiveDate', label: '生效日期' },
  135. { prop: 'expiryDate', label: '结束日期' },
  136. { label: '可分配', render ({ initialAmount, allocatedAmount }) {
  137. return initialAmount - allocatedAmount
  138. } }
  139. ]
  140. },
  141. fromCredit: {},
  142. credit: {},
  143. pickerOptions: {
  144. disabledDate: this.disabledDate
  145. }
  146. }
  147. },
  148. computed: {
  149. remaining () {
  150. return this.fromCredit
  151. ? this.fromCredit.initialAmount - this.fromCredit.allocatedAmount
  152. : 0
  153. }
  154. },
  155. created () {
  156. this.getTreeData()
  157. },
  158. methods: {
  159. disabledDate (date) {
  160. if (this.fromCredit) {
  161. const { effectiveDate, expiryDate } = this.fromCredit
  162. return date < new Date(`${effectiveDate} 00:00:00`) || date > new Date(`${expiryDate} 00:00:00`)
  163. }
  164. return false
  165. },
  166. transformAuditType (data) {
  167. return ['', '图片', '视频'][data.auditType]
  168. },
  169. getTreeData () {
  170. this.loading = true
  171. this.error = false
  172. getTopGroups().then(
  173. ({ data }) => {
  174. if (data.length) {
  175. this.groups = data
  176. this.group = this.groups[0]
  177. this.$nextTick(() => {
  178. this.$refs.groupTree.setCurrentKey(this.group.path)
  179. })
  180. }
  181. },
  182. () => {
  183. this.error = true
  184. }
  185. ).finally(() => {
  186. this.loading = false
  187. })
  188. },
  189. onGroupTreeClick (group) {
  190. if (!this.group || this.group.id !== group.id) {
  191. this.group = group
  192. this.$refs.table.pageTo(1)
  193. }
  194. },
  195. getTenantCredits (params) {
  196. return getTenantCredits({
  197. tenant: this.group.path,
  198. ...params
  199. })
  200. },
  201. onAdd () {
  202. this.$refs.stockDialog.show()
  203. },
  204. onChoosen ({ value, done }) {
  205. const { auditType, initialAmount, allocatedAmount } = value
  206. if (initialAmount - allocatedAmount < 1) {
  207. this.$message({
  208. type: 'warning',
  209. message: '可分配次数不足,请重新选择'
  210. })
  211. return
  212. }
  213. this.fromCredit = value
  214. this.credit = {
  215. auditType,
  216. initialAmount: 1,
  217. date: ''
  218. }
  219. done()
  220. this.$refs.addDialog.show()
  221. },
  222. onSave (done) {
  223. const { auditType, initialAmount, date } = this.credit
  224. if (!date) {
  225. this.$message({
  226. type: 'warning',
  227. message: '请选择有效期'
  228. })
  229. return
  230. }
  231. addCredit({
  232. admin: 0,
  233. fromCredit: this.fromCredit.id,
  234. tenant: this.group.path,
  235. auditType,
  236. initialAmount,
  237. effectiveDate: date[0],
  238. expiryDate: date[1]
  239. }).then(() => {
  240. done()
  241. this.$refs.table.pageTo(1)
  242. })
  243. },
  244. onDel (credit) {
  245. deleteCredit(credit).then(() => {
  246. this.$refs.table.decrease(1)
  247. })
  248. }
  249. }
  250. }
  251. </script>