index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. console.log("list", list)
  26. this.setData({
  27. list
  28. });
  29. this.getResult();
  30. resolve(list.length);
  31. });
  32. });
  33. },
  34. onOption(e) {
  35. const {
  36. index,
  37. value
  38. } = e.currentTarget.dataset;
  39. if (this.data.list[index].value == value) return;
  40. this.setData({
  41. [`list[${index}].value`]: value
  42. });
  43. this.getResult();
  44. },
  45. getResult(isSubmit = false) {
  46. let obj = {};
  47. let breakOff = false;
  48. this.data.list.filter(v => {
  49. if (breakOff) return;
  50. if (v.type == '自定义') {
  51. if (!v.value) v.value = 0;
  52. }
  53. obj[v.key] = v.value || '';
  54. if (isSubmit && !obj[v.key]) {
  55. wx.showToast({
  56. title: `请完成定制"${v.label}"选项`,
  57. icon: "none"
  58. })
  59. breakOff = true;
  60. } else if (v.tips) {
  61. wx.showToast({
  62. title: v.tips,
  63. icon: "none"
  64. })
  65. breakOff = true;
  66. }
  67. })
  68. this.triggerEvent("onChange", breakOff ? false : obj)
  69. return breakOff ? false : obj
  70. },
  71. valueChange(e) {
  72. const {
  73. index,
  74. item
  75. } = e.currentTarget.dataset;
  76. wx.hideToast()
  77. item.tips = '';
  78. item.value = e.detail.value;
  79. if (item.decimalplaces && item.value.includes(".") && item.value.split(".")[1].length > item.decimalplaces) item.tips = `自定义${item.label}仅允许${item.decimalplaces}位小数!`
  80. if (item.value > item.max) {
  81. item.tips = `自定义${item.label}超出可选范围!`
  82. wx.showToast({
  83. title: item.tips,
  84. icon: "none"
  85. })
  86. }
  87. if (item.value < item.min) item.tips = `自定义${item.label}少于可选范围!`;
  88. this.setData({
  89. [`list[${index}]`]: item
  90. })
  91. },
  92. valueBlur(e) {
  93. const {
  94. index,
  95. item
  96. } = e.currentTarget.dataset;
  97. if (item.tips) wx.showToast({
  98. title: item.tips,
  99. icon: "none"
  100. })
  101. }
  102. }
  103. })