discount.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. define({
  2. name: "discount",
  3. template: `
  4. <div>
  5. <el-form>
  6. <div class="vue-main-title">
  7. <div class="vue-main-title-left"></div>
  8. <div class="vue-main-title-content">折扣</div>
  9. </div>
  10. <div style="margin:0 auto;width:80%;">
  11. <el-form-item label="折扣类型">
  12. <el-radio v-model="form.level_discount_type" :label="1">{{attr_hide.level_discount_name ? attr_hide.level_discount_name : "会员等级" }}</el-radio>
  13. </el-form-item>
  14. </div>
  15. <div style="margin:0 auto;width:80%;">
  16. <el-form-item label="折扣方式">
  17. <el-radio v-if="(hide_discount_method || attr_hide.discount_method == 1)" v-model="form.discount_method" :label="1">折扣</el-radio>
  18. <el-radio v-if="(hide_discount_method || attr_hide.discount_method == 2)" v-model="form.discount_method" :label="2">固定金额</el-radio>
  19. <el-radio v-if="(hide_discount_method || attr_hide.discount_method == 3)" v-model="form.discount_method" :label="3">成本比例</el-radio>
  20. </el-form-item>
  21. <el-form-item prop="goodsFull" style="margin-left: 68px;" v-for="(item,index) in form.levels" :key="index">
  22. <el-input style="width: 300px;" v-model="item.discount_value" type="number" @input ="item.discount_value=clearNoNumTwo(item.discount_value)">
  23. <template slot="prepend">{{item.level_name}}</template>
  24. <template slot="append">{{form.discount_method == 1 ? "折" : form.discount_method == 2 ? "元" : "%" }}</template>
  25. </el-input>
  26. </el-form-item>
  27. </div>
  28. </el-form>
  29. </div>
  30. `,
  31. style: `
  32. `,
  33. props: {
  34. form: {
  35. default() {
  36. return {};
  37. },
  38. },
  39. attr_hide: {
  40. default() {
  41. return {}
  42. }
  43. },
  44. },
  45. data() {
  46. return {
  47. hide_discount_method:true,
  48. };
  49. },
  50. watch:{
  51. 'form.discount_method': {
  52. handler(){
  53. for(let item of this.form.levels) {
  54. item.discount_value = ''
  55. }
  56. },
  57. }
  58. },
  59. mounted () {
  60. if (this.attr_hide.discount_method) {
  61. this.form.discount_method = this.attr_hide.discount_method;
  62. this.hide_discount_method = false;
  63. }
  64. },
  65. methods: {
  66. validate() {
  67. let levelsData = JSON.parse(JSON.stringify(this.form.levels));
  68. discount_value = {}
  69. levelsData.forEach(item => {
  70. discount_value[item.level_id] = item.discount_value
  71. });
  72. return {
  73. discount_value,
  74. level_discount_type:this.form.level_discount_type,
  75. discount_method:this.form.discount_method
  76. }
  77. },
  78. // 处理输入框小数点两位问题
  79. clearNoNumTwo(value) {
  80. value = value.replace(/[^\d.]/g, ''); //清除“数字”和“.”以外的字符
  81. value = value.replace(/\.{2,}/g, '.'); //只保留第一个. 清除多余的
  82. value = value.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
  83. if(this.form.discount_method == 2) {
  84. value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
  85. }
  86. if (value.indexOf('.') < 0 && value != '') {
  87. //以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
  88. value = parseFloat(value);
  89. }
  90. let strObj = value.toString();
  91. if (strObj.indexOf('.') > -1 && strObj === '0.00') {
  92. value = parseFloat(value).toFixed(1);
  93. }
  94. return value;
  95. },
  96. },
  97. });