MrShi
2024-11-07 bf2338f5cc71890cda247d46edcffa42e21871f6
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
<template>
  <!-- 新建/修改 -->
  <GlobalWindow
    :title="title"
    :visible.sync="visible"
    :confirm-working="isWorking"
    @confirm="confirm"
  >
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="账号" prop="username" required>
        <el-input v-model="form.username" placeholder="请输入" v-trim maxlength="50"/>
      </el-form-item>
      <el-form-item label="名称" prop="realname" required>
        <el-input v-model="form.realname" placeholder="请输入" v-trim maxlength="50"/>
      </el-form-item>
      <el-form-item label="联系方式" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入" v-trim maxlength="11"/>
      </el-form-item>
      <el-form-item label="验证码" prop="captcha">
        <div style="width: 100%; display: flex; align-items: center;">
          <el-input v-model="form.captcha" maxlength="11" type="number" style="flex: 1;" placeholder="请输入" v-trim/>
          <el-button type="primary" style="flex-shrink: 0; margin-left: 15px;" @click="send" v-if="num === 0">发送验证码</el-button>
          <el-button type="primary" style="flex-shrink: 0; margin-left: 15px;" v-else>{{num}}s</el-button>
        </div>
      </el-form-item>
      <el-form-item label="部门" prop="departmentId">
        <el-cascader
          :options="organization"
          v-model="form.departmentId"
          placeholder="请选择"
          :props="{ label: 'name', value: 'id', checkStrictly: true }"
          clearable />
      </el-form-item>
      <el-form-item label="抄送文件邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入" v-trim />
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>
 
<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'
import DepartmentSelect from '@/components/common/DepartmentSelect'
import PositionSelect from '@/components/common/PositionSelect'
import { checkMobile, checkEmail } from '@/utils/form'
import { sendSms } from '@/api/business/smsEmail'
import { tree } from '@/api/business/companyDepartment'
export default {
  name: 'OperaUserWindow',
  extends: BaseOpera,
  components: { PositionSelect, DepartmentSelect, GlobalWindow },
  data () {
    return {
      // 表单数据
      form: {
        id: null,
        username: '', // 用户名
        realname: '', // 姓名
        mobile: '', // 手机号码
        email: '',
        captcha: '',
        departmentId: ''
      },
      num: 0,
      timer: null,
      organization: [],
      // 验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名' }
        ],
        realname: [
          { required: true, message: '请输入姓名' }
        ],
        mobile: [
          { required: true, validator: checkMobile }
        ],
        departmentId: [
          { required: true, message: '请选择部门' }
        ],
        email: [
          { required: true, message: '请输入邮箱' }
        ]
      }
    }
  },
  methods: {
    __confirmCreate () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        let obj = JSON.parse(JSON.stringify(this.form))
        obj.departmentId = obj.departmentId[0]
        // 调用新建接口
        this.isWorking = true
        this.api.create(obj)
          .then(() => {
            this.visible = false
            this.$tip.apiSuccess('新建成功')
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    },
    __confirmEdit () {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return
        }
        let obj = JSON.parse(JSON.stringify(this.form))
        obj.departmentId = obj.departmentId[0]
        // 调用新建接口
        this.isWorking = true
        this.api.updateById(obj)
          .then(() => {
            this.visible = false
            this.$tip.apiSuccess('修改成功')
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    },
    send() {
      if (!this.form.mobile) {
        this.$message.warning('请输入手机号')
        return
      }
      sendSms({ phone: this.form.mobile })
        .then(res => {
          this.num = 60
          this.setTime()
        })
    },
    getTree() {
      tree()
        .then(records => {
          this.organization = records
        })
    },
    setTime() {
      this.timer = setInterval(() => {
        this.num -= 1
        if (this.num === 0) {
          clearInterval(this.timer)
          this.num = 0
        }
      }, 1000)
    },
    /**
     * @title 窗口标题
     * @target 编辑的用户对象
     */
    open (title, target) {
      this.title = title
      this.visible = true
      this.getTree()
      clearInterval(this.timer)
      this.num = 0
      // 新建
      if (target == null) {
        this.$nextTick(() => {
          this.$refs.form.resetFields()
          this.form.id = null
          // this.form.departmentId = null
          // this.form.positionIds = []
        })
        return
      }
      // 编辑
      this.$nextTick(() => {
        for (const key in this.form) {
          this.form[key] = target[key]
        }
        // this.form.departmentId = target.department == null ? null : target.department.id
        // this.form.positionIds = target.positions == null ? [] : target.positions.map(p => p.id)
      })
    }
  },
  created () {
    this.config({
      api: '/system/user'
    })
  }
}
</script>
 
<style lang="scss" scoped>
.global-window {
  /deep/ .el-date-editor {
    width: 100%;
  }
  // 表单头像处理
  /deep/ .form-item-avatar {
    .el-radio.is-bordered {
      height: auto;
      padding: 6px;
      margin: 0 10px 0 0;
      .el-radio__input {
        display: none;
      }
      .el-radio__label {
        padding: 0;
        width: 48px;
        display: block;
        img {
          width: 100%;
        }
      }
    }
  }
}
</style>