quanyawei
2024-09-06 75c45150bcc5b1a3b45efe98ce6ec92b7b10aba3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<template>
  <a-form style="margin-top: 8px">
    <a-modal
      title="编辑-公式"
      destroyOnClose
      :visible="visible"
      @cancel="handleCancel"
      @ok="handleOk"
      okText="保存"
    >
      <a-form style="margin-top: 8px" :form="form">
        <a-form-item :labelCol="{ span: 5 }" :wrapperCol="{ span: 15 }" label="计算规则">
          <span>
            <a-select
              v-decorator="['initialMath', {initialValue:'*', rules: [{ required: true }] }]"
              style="width:60px"
            >
              <a-select-option value="+">加</a-select-option>
              <a-select-option value="-">减</a-select-option>
              <a-select-option value="*">乘</a-select-option>
              <a-select-option value="/">除</a-select-option>
            </a-select>
 
            <a-input
              v-decorator="['roleValue', {initialValue:1000 ,rules: [{ required: true}] },]"
              placeholder="请输入数字"
              style="width:100px;margin-left:10px;margin-right:10px"
            ></a-input>
            <a-button @click="AddInputClick">+</a-button>
          </span>
          <span>
            <a-input
              v-decorator="['createFormula', {initialValue:createFomulaValue, rules: [{ required: true }] }]"
              placeholder="通过上面选择框添加规则"
              style="width:200px;margin-right:10px"
              disabled
            ></a-input>
            <a-button @click="CancelInputClick">×</a-button>
          </span>
        </a-form-item>
      </a-form>
    </a-modal>
  </a-form>
</template>
 
<script lang="ts">
import {
  Component,
  Prop,
  Vue,
  Emit,
  Model,
  Watch
} from "vue-property-decorator";
import { State, Mutation, namespace } from "vuex-class";
 
import { get, post } from "@/util/request";
 
@Component({
  components: {}
})
export default class UpdateUnitRule extends Vue {
 
  @Prop({
    type: Boolean,
    default: false
  })
  private visible!: boolean;
 
  @Prop({
    type: Object,
    default() {
      return {};
    }
  })
  private record!: any;
 
  private createFomulaValue: any = null;
 
  private handleCancel(): void {
 
    this.updateVisible(false);
  }
  //编辑中 +功能
  private AddInputClick() {
    this.form.validateFields((err: any, values: any) => {
      //把值存到input框
      this.form.setFieldsValue({
        createFormula: values.createFormula
          ? "(" +
            values.createFormula +
            ")" +
            values.initialMath +
            values.roleValue
          : "初值" + values.initialMath + values.roleValue
      });
    });
  }
  //编辑中 x功能
  private CancelInputClick() {
    this.form.validateFields((err: any, values: any) => {
      console.log(values);
      //把值存到input框
      this.form.setFieldsValue({
        createFormula: null
      });
    });
  }
  private form: any = {};
  private created() {
    this.form = this.$form.createForm(this, { name: "UpdateUnitRule" });
  }
 
  // 保存 更新数据库
  private handleOk(): void {
    this.form.validateFields((err: any, values: any) => {
      if (err) {
        console.log(err);
        return;
      }
      if (this.record.formula !== values.createFormula) {
            values.createFormula=values.createFormula.replace('初值','{0}')
        post("unitConversion/update",
            {
              id:this.record.id,
              formula:values.createFormula
            }
          ).then(res => {
            if (res.data.code != 0) {
              this.$message.error(res.data.message);
            } else {
              this.$message.success(res.data.message, 2);
              //保存成功,组件刷新数据
              this.$emit("updateData");
              this.updateVisible(false);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
      else{
           this.updateVisible(false);
      }
    });
  }
 
  private updateVisible(visible: boolean): void {
    this.$emit("update:visible", visible);
  }
  private callbackData(value: any) {
    this.form.validateFields((err: any, values: any) => {
      if (err) {
        console.log(err);
      }
      console.log(values);
    });
  }
 
  //监听编辑回调值
  @Watch("record", {
    immediate: true, //监听开始后是否立即调用该回调函数;
    deep: true //深度监听
  })
  //监听编辑逻辑赋值
  private recordChange(value: any, oldValue: any): void {
    this.createFomulaValue = this.record.formula;
  }
}
</script>