wenzheng.yang
2023-04-27 31e64e0e144815e6176c1b6b81eae8dc68688570
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
<!-- 部门表单组件 -->
<template>
    <view class="m-form">
        <view class="m-form-head"><u-icon name="close-circle" color="#fff" size="40" @click="cancel"></u-icon></view>
        <!-- 修改 -->
        <view class="m-form-label" v-if="departmentType == 'update'">
            <u--form labelPosition="left" labelAlign="right" :model="model" :rules="rules" ref="form" labelWidth="100" :labelStyle="{ color: 'rgb(129, 129, 129)' }">
                <u-form-item label="部门ID" prop="department_id" borderBottom ref="item">
                    <u--input
                        :customStyle="{
                            'background-color': 'rgb(233, 233, 233)',
                            'box-shadow': 'inset 2px 1.464px 5px 0px #ccccc4, 1.464px 1.464px 5px 0px rgba(255, 255, 255, 0.75)'
                        }"
                        readonly
                        placeholderStyle="color:#000"
                        color="#000"
                        v-model="model.department_id"
                        border="none"
                    ></u--input>
                </u-form-item>
                <u-form-item label="部门名称" prop="department_name" borderBottom ref="item">
                    <u--input
                        :customStyle="{
                            'background-color': 'rgb(233, 233, 233)',
                            'box-shadow': 'inset 2px 1.464px 5px 0px #ccccc4, 1.464px 1.464px 5px 0px rgba(255, 255, 255, 0.75)'
                        }"
                        placeholderStyle="color:#000"
                        color="#000"
                        v-model="model.department_name"
                        border="none"
                    ></u--input>
                </u-form-item>
            </u--form>
        </view>
        <!-- 添加 -->
        <view class="m-form-label" v-if="departmentType == 'add'">
            <u--form labelPosition="left" labelAlign="right" :model="model" :rules="rules" ref="form" labelWidth="100" :labelStyle="{ color: 'rgb(129, 129, 129)' }">
                <u-form-item label="部门ID" prop="department_id" borderBottom ref="item">
                    <u--input
                        :customStyle="{
                            'background-color': 'rgb(233, 233, 233)',
                            'box-shadow': 'inset 2px 1.464px 5px 0px #ccccc4, 1.464px 1.464px 5px 0px rgba(255, 255, 255, 0.75)'
                        }"
                        placeholderStyle="color:#000"
                        color="#000"
                        v-model="model.department_id"
                        border="none"
                    ></u--input>
                </u-form-item>
                <u-form-item label="部门名称" prop="department_name" borderBottom ref="item">
                    <u--input
                        :customStyle="{
                            'background-color': 'rgb(233, 233, 233)',
                            'box-shadow': 'inset 2px 1.464px 5px 0px #ccccc4, 1.464px 1.464px 5px 0px rgba(255, 255, 255, 0.75)'
                        }"
                        placeholderStyle="color:#000"
                        color="#000"
                        v-model="model.department_name"
                        border="none"
                    ></u--input>
                </u-form-item>
            </u--form>
        </view>
        <view class="m-form-button-list"><u-button type="primary" text="确定" @click="save"></u-button></view>
    </view>
</template>
 
<script>
import {connChart} from '../../../common/utils.js'
import {formatDate} from '../../../common/tools.js'
export default {
    props: {
        departmentType: String,
        departmentFormData: Object
    },
    data() {
        return {
            model: {
                department_id: '',
                department_name:''
            },
            addModel: {
                department_id: ''
            },
            rules: {
                department_id: {
                    validator: (rule, value, callback) => {
                        // 返回true表示校验通过,返回false表示不通过
                        var pattern = /^((?![0-9]+$)(?![a-zA-Z]+$)|[0-9A-Za-z]{1,1000})$/;
                        console.log(pattern.test(value));
                        return pattern.test(value);
                    },
                    required: true,
                    message: '请输入字母或数字',
                    trigger: ['blur', 'change']
                }
            },
            labelModel: null,
        };
    },
    methods: {
        //保存
        save() {
            if (this.departmentType == 'add') {
                //查重
                this.labelModel.find(`department_id=${this.model.department_id}`,(e,r)=>{
                    if(r.length>0){
                        uni.$u.toast('部门ID重复')
                    }else{
                        //添加
                        this.labelModel.insert({
                            addTime:formatDate(new Date(),'yyyy-MM-dd hh:mm:ss'), 
                            department_id: this.model.department_id, 
                            department_name: this.model.department_name, 
                        }, (err, results) => {
                            console.log(err);
                            console.log(results);
                            this.$emit('submitDepartmentForm');
                        });
                    }
                })
            } else if (this.departmentType == 'update') {
                //修改
                this.labelModel.update(`id='${this.model.id}'`,{
                    department_name:this.model.department_name,
                    department_id: this.model.department_id, 
                },(err,results)=>{
                    console.log(err,results);
                    this.$emit('submitDepartmentForm');
                });
            } 
        },
        //取消
        cancel() {
            this.$emit('cancelDepartmentForm');
        },
        connDB() {
            this.labelModel = connChart('department')
        }
    },
    mounted() {
        if (this.departmentType == 'add') {
            // this.model = this.addModel;
            // console.log(this.model)
        } else if (this.departmentType == 'update') {
            this.model = this.departmentFormData;
        }
        this.connDB();
    }
};
</script>
 
<style lang="scss" scoped>
.m-form {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(233, 233, 233, 0.95);
    width: 60%;
    display: flex;
    flex-direction: column;
    z-index: 99999;
}
.m-form-head {
    width: 100%;
    border-bottom: 1px solid #fff;
}
.m-form-head .u-icon {
    float: right;
}
/deep/ .u-form-item__body__left__content__label {
    color: #fff;
}
.m-form-label {
    width: 80%;
    margin: 0 auto;
    margin-bottom: 0.5em;
}
input {
    width: 50px;
}
.m-form-button-list {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin-bottom: 1em;
}
.m-form-button-list .u-button {
    width: 5em;
}
.m-form-button-list .u-button:nth-child(2),
.m-form-button-list .u-button:nth-child(3),
.m-form-button-list .u-button:nth-child(4) {
    margin-top: 2em;
}
</style>