chooseWarehouse.js 4.0 KB

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