chooseWarehouse.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { queryAuthWarehouse, warehouseAllList } from '@/api/warehouse'
  2. const warehouse = {
  3. template: `
  4. <a-select
  5. :placeholder="placeholder"
  6. :id="id"
  7. :allowClear="allowClear"
  8. :value="defaultVal"
  9. :showSearch="true"
  10. :disabled="disabled"
  11. :mode="modeType"
  12. @change="handleChange"
  13. :filter-option="filterOption">
  14. <a-select-option v-for="item in warehouseData" :key="item.warehouseSn" :value="item.warehouseSn">{{ item.name }}</a-select-option>
  15. </a-select>
  16. `,
  17. props: {
  18. value: {
  19. type: [String, Array],
  20. defatut: ''
  21. },
  22. isPermission: {// false无权限 true有权限
  23. type: Boolean,
  24. default: false
  25. },
  26. allowClear: {
  27. type: Boolean,
  28. default: true
  29. },
  30. id: {
  31. type: String,
  32. default: ''
  33. },
  34. placeholder: {
  35. type: String,
  36. default: '请选择仓库'
  37. },
  38. disabled: {
  39. type: Boolean,
  40. default: false
  41. },
  42. modeType: {
  43. type: String,
  44. default: 'default'
  45. },
  46. isDefault: { // 是否有默认值
  47. type: Boolean,
  48. default: undefined
  49. }
  50. },
  51. data () {
  52. return {
  53. defaultVal: this.value,
  54. warehouseData: [],
  55. pageNo: 1,
  56. pageSize: 1000
  57. }
  58. },
  59. mounted () {
  60. // 如果已经存在仓库数据
  61. const a = this.$store.state.app.warehouseAuthList
  62. const b = this.$store.state.app.warehouseAllList
  63. if (this.isPermission) {
  64. this.$store.state.app.defWarehouse = null
  65. this.$store.state.app.isWarehouse = true
  66. if(a){
  67. this.setWarehouseData(a)
  68. }else{
  69. // 不存在,远程获取
  70. this.getWarehouse()
  71. }
  72. }else{
  73. if(b){
  74. this.setWarehouseData(b)
  75. }else{
  76. // 不存在,远程获取
  77. this.getWarehouse()
  78. }
  79. }
  80. },
  81. watch: {
  82. value (newValue, oldValue) {
  83. this.defaultVal = newValue
  84. }
  85. },
  86. methods: {
  87. filterOption (input, option) {
  88. return (
  89. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  90. )
  91. },
  92. handleChange (value) {
  93. this.defaultVal = value
  94. this.$emit('input', value)
  95. this.$emit('change', value)
  96. },
  97. // 设置仓库数据
  98. setWarehouseData(data){
  99. // 有权限控制的
  100. if (this.isPermission) {
  101. this.$store.state.app.isWarehouse = true
  102. this.$store.state.app.defWarehouse = data && data[0]
  103. }
  104. this.warehouseData = data || []
  105. let defaultWarehouseVal = {}
  106. if (this.isDefault) {
  107. defaultWarehouseVal = data.find(con => {
  108. return con.defaultFlag == 1 ? con.warehouseSn : ''
  109. })
  110. this.defaultVal = defaultWarehouseVal.warehouseSn
  111. this.$emit('load', defaultWarehouseVal.warehouseSn, this.warehouseData)
  112. } else {
  113. this.$emit('load', null, this.warehouseData)
  114. }
  115. },
  116. // 获取仓库列表
  117. getWarehouse () {
  118. const ajaxName = this.isPermission ? queryAuthWarehouse : warehouseAllList
  119. ajaxName({}).then(res => {
  120. if (res.status == 200) {
  121. this.setWarehouseData(res.data)
  122. // 存储仓库数据本地
  123. if(this.isPermission){
  124. this.$store.state.app.warehouseAuthList = this.warehouseData
  125. }else{
  126. this.$store.state.app.warehouseAllList = this.warehouseData
  127. }
  128. } else {
  129. if (this.isPermission) {
  130. this.$store.state.app.isWarehouse = true
  131. this.$store.state.app.defWarehouse = null
  132. }
  133. this.warehouseData = []
  134. }
  135. })
  136. },
  137. hasWarehouse(sn){
  138. return this.warehouseData.find(item => item.warehouseSn == sn)
  139. },
  140. clearData () {
  141. this.handleChange([])
  142. },
  143. // 清空缓存的数据
  144. clearCacheData(){
  145. this.$store.state.app.warehouseAuthList = null
  146. this.$store.state.app.warehouseAllList = null
  147. }
  148. }
  149. }
  150. export default warehouse