wang-hao-jie
2021-10-29 e05949ce9f3db79981e54b194eafc1e27c8be1b5
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<style lang="less">
@import "@/styles/single-common.less";
</style>
<template>
  <div>
    <Card>
      <div slot="title">
        <div class="edit-head">
          <a @click="close" class="back-title">
            <Icon type="ios-arrow-back" />返回
          </a>
          <div class="head-name">
            {{ title }}
          </div>
          <span></span>
          <a @click="close" class="window-close">
            <Icon type="ios-close" size="31" class="ivu-icon-ios-close" />
          </a>
        </div>
      </div>
      <Form
        ref="form"
        :model="form"
        :label-width="90"
        :rules="formValidate"
        style="position: relative"
      >
        <FormItem label="消息ID:" prop="id" v-show="type == 1">
          {{ form.id }}
        </FormItem>
        <FormItem label="消息类型" prop="type">
          <dict dict="message_type" v-model="form.type" style="width: 300px" />
        </FormItem>
        <FormItem label="标题" prop="title">
          <Input v-model="form.title" style="width: 600px" />
        </FormItem>
        <FormItem style="margin-bottom: 5px" v-show="form.isTemplate">
          <Alert show-icon
            >消息模版变量格式:${变量名}【标题及内容都支持】
            示例:您的验证码为:${code},该验证码5分钟内有效,请勿泄漏于他人
            <a
              href="https://www.kancloud.cn/exrick/xboot/1021404"
              target="_blank"
              >详见开发文档</a
            ></Alert
          >
        </FormItem>
        <FormItem label="内容" prop="content">
          <editor v-model="form.content" height="250"></editor>
        </FormItem>
        <FormItem label="新创建的账号也推送" prop="createSend">
          <i-switch size="large" v-model="form.createSend">
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </i-switch>
        </FormItem>
        <div v-if="type == 0">
          <FormItem label="发送范围">
            <RadioGroup v-model="form.range">
              <Radio :label="0" border>全体用户</Radio>
              <Radio :label="1" border>指定用户成员</Radio>
            </RadioGroup>
          </FormItem>
          <div>
            <FormItem label="选择用户" v-if="form.range == 1">
              <user-choose
                text="选择发送用户"
                v-model="selectUsers"
              ></user-choose>
            </FormItem>
          </div>
        </div>
        <FormItem class="br">
          <Button
            type="primary"
            :loading="submitLoading"
            @click="handelSubmit"
            style="width: 100px"
            >提交</Button
          >
          <Button @click="close">取消</Button>
        </FormItem>
        <Spin size="large" fix v-if="loading"></Spin>
      </Form>
    </Card>
  </div>
</template>
 
<script>
import { getMessageDataById, addMessage, editMessage } from "@/api/index";
import editor from "@/views/my-components/xboot/editor";
import userChoose from "@/views/my-components/xboot/user-choose";
import dict from "@/views/my-components/xboot/dict";
export default {
  name: "add_edit_message",
  components: {
    userChoose,
    editor,
    dict,
  },
  props: {
    transferData: Object,
  },
  data() {
    return {
      type: 0,
      title: "",
      loading: false, // 表单加载状态
      selectUsers: [],
      userModalVisible: false,
      modalTitle: "", // 添加或编辑标题
      form: {
        // 添加或编辑表单对象初始化数据
        title: "",
        content: "",
        type: "",
        range: 0,
        createSend: false,
        isTemplate: false,
      },
      formValidate: {
        // 表单验证规则
        type: [
          { required: true, message: "消息类型不能为空", trigger: "change" },
        ],
        title: [{ required: true, message: "标题不能为空", trigger: "change" }],
        content: [
          { required: true, message: "内容不能为空", trigger: "change" },
        ],
      },
      submitLoading: false, // 添加或编辑提交状态
      backRoute: "",
    };
  },
  methods: {
    init() {
      this.type = this.transferData.type;
      if (this.type == 1) {
        this.title = "编辑消息";
        this.form.id = this.transferData.id;
        this.getData();
      } else if (this.type == 0) {
        this.title = "发送新消息";
        this.selectUsers = [];
      } else if (this.type == -1) {
        this.title = "添加系统模版消息";
        this.selectUsers = [];
        this.form.isTemplate = true;
        this.form.range = -1;
      }
    },
    getData() {
      this.loading = true;
      getMessageDataById(this.form.id).then((res) => {
        this.loading = false;
        if (res.success) {
          // 转换null为""
          let v = res.result;
          for (let attr in v) {
            if (v[attr] == null) {
              v[attr] = "";
            }
          }
          let str = JSON.stringify(v);
          let data = JSON.parse(str);
          this.form = data;
        }
      });
    },
    handelSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          if (this.type == 0) {
            // 添加 避免编辑后传入id等数据 记得删除
            delete this.form.id;
            // 用户id数据
            let ids = [];
            this.selectUsers.forEach((e) => {
              ids += e.id + ",";
            });
            if (ids.length > 0) {
              ids = ids.substring(0, ids.length - 1);
            }
            this.form.userIds = ids;
            addMessage(this.form).then((res) => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.submited();
              }
            });
          } else if (this.type == 1) {
            // 编辑
            editMessage(this.form).then((res) => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.submited();
              }
            });
          } else {
            addMessage(this.form).then((res) => {
              this.submitLoading = false;
              if (res.success) {
                this.$Message.success("操作成功");
                this.submited();
              }
            });
          }
        }
      });
    },
    close() {
      this.$emit("close", true);
    },
    submited() {
      this.$emit("submited", true);
    },
  },
  mounted() {
    this.init();
  },
};
</script>