more.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {dibootApi} from '@/utils/dibootApi'
  2. export default {
  3. data() {
  4. return {
  5. // 请求接口基础路径
  6. baseApi: '/',
  7. // 是否从当前业务的attachMore接口中自动获取关联数据
  8. getMore: false,
  9. // 获取关联数据列表的配置列表
  10. attachMoreList: [],
  11. // 远程过滤关联数据列表的配置对象
  12. attachMoreLoader: {},
  13. // 远程过滤加载状态
  14. attachMoreLoading: false,
  15. // 关联相关的更多数据
  16. more: {}
  17. }
  18. },
  19. methods: {
  20. /**
  21. * 加载当前页面关联的对象或者字典
  22. */
  23. async attachMore() {
  24. const reqList = []
  25. // 个性化接口
  26. this.getMore === true && reqList.push(dibootApi.get(`${this.baseApi}/attachMore`))
  27. // 通用获取当前对象关联的数据的接口
  28. this.attachMoreList.length > 0 && reqList.push(dibootApi.post('/common/attachMore', this
  29. .attachMoreList))
  30. if (reqList.length > 0) {
  31. const resList = await Promise.all(reqList)
  32. resList.forEach(res => res.ok ? Object.keys(res.data).forEach(key => this.$set(this.more, key, res.data[key]))
  33. : uni.showToast({title: res.msg || '获取选项数据失败', icon: 'error'}))
  34. }
  35. },
  36. /**
  37. * 远程过滤加载选项
  38. *
  39. * @param value 输入值
  40. * @param loader 加载器类型
  41. */
  42. attachMoreFilter(value, loader) {
  43. if (value == null || (value = value.trim()).length === 0) {
  44. this.$set(this.more, `${loader}Options`, [])
  45. return
  46. }
  47. this.attachMoreLoading = true
  48. const moreLoader = this.attachMoreLoader[loader]
  49. moreLoader.keyword = value
  50. dibootApi.post('/common/attachMoreFilter', moreLoader).then(res => {
  51. res.ok ? this.$set(this.more, `${loader}Options`, res.data)
  52. : uni.showToast({title: res.msg || '获取选项数据失败', icon: 'error'})
  53. this.attachMoreLoading = false
  54. }).catch(() => {
  55. uni.showToast({title: res.msg || '获取选项数据失败', icon: 'error'})
  56. this.attachMoreLoading = false
  57. })
  58. }
  59. }
  60. }