<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', children: 'childList', 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[obj.departmentId.length - 1] 
 | 
        // 调用新建接口 
 | 
        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[obj.departmentId.length - 1] 
 | 
        // 调用新建接口 
 | 
        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> 
 |