index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {
  2. getCustomItems
  3. } from "../../../../../utils/customItemType";
  4. Component({
  5. properties: {
  6. topHei: {
  7. type: String,
  8. value: "20rpx"
  9. },
  10. onChange: {
  11. type: Function
  12. }
  13. },
  14. options: {
  15. multipleSlots: true
  16. },
  17. externalClasses: ["external-custom-box", "external-custom-label", "external-custom-stepper-box", "external-explain", "external-active", "external-custom-option"],
  18. data: {
  19. list: []
  20. },
  21. methods: {
  22. init(data, prefix = '') {
  23. return new Promise((resolve) => {
  24. getCustomItems(data, prefix).then(list => {
  25. this.setData({
  26. list: list.map(v => {
  27. if (v.type == '可选' && v.rowsdetail.length == 1) {
  28. v.value = v.rowsdetail[0].num
  29. }
  30. return v
  31. })
  32. });
  33. this.getResult();
  34. resolve(list.length);
  35. });
  36. });
  37. },
  38. onOption(e) {
  39. const {
  40. index,
  41. value
  42. } = e.currentTarget.dataset;
  43. if (this.data.list[index].value == value) return;
  44. this.setData({
  45. [`list[${index}].value`]: value
  46. });
  47. this.getResult();
  48. },
  49. getResult(isSubmit = false) {
  50. let obj = {};
  51. let breakOff = false;
  52. this.data.list.filter(v => {
  53. if (breakOff) return;
  54. if (v.type == '自定义') {
  55. if (!v.value) v.value = 0;
  56. }
  57. obj[v.key] = v.value || '';
  58. if (isSubmit && !obj[v.key]) {
  59. wx.showToast({
  60. title: `请完成定制"${v.label}"选项`,
  61. icon: "none"
  62. })
  63. breakOff = true;
  64. } else if (v.tips) {
  65. wx.showToast({
  66. title: v.tips,
  67. icon: "none"
  68. })
  69. breakOff = true;
  70. }
  71. })
  72. this.triggerEvent("onChange", breakOff ? false : obj)
  73. return breakOff ? false : obj
  74. },
  75. valueChange(e) {
  76. const {
  77. index,
  78. item
  79. } = e.currentTarget.dataset;
  80. wx.hideToast()
  81. item.tips = '';
  82. item.value = e.detail.value;
  83. if (item.decimalplaces && item.value.includes(".") && item.value.split(".")[1].length > item.decimalplaces) item.tips = `自定义${item.label}仅允许${item.decimalplaces}位小数!`
  84. if (item.value > item.max) {
  85. item.tips = `自定义${item.label}超出可选范围!`
  86. wx.showToast({
  87. title: item.tips,
  88. icon: "none"
  89. })
  90. }
  91. if (item.value < item.min) item.tips = `自定义${item.label}少于可选范围!`;
  92. this.setData({
  93. [`list[${index}]`]: item
  94. })
  95. },
  96. valueBlur(e) {
  97. const {
  98. index,
  99. item
  100. } = e.currentTarget.dataset;
  101. if (item.tips) wx.showToast({
  102. title: item.tips,
  103. icon: "none"
  104. })
  105. }
  106. }
  107. })