suerprisePlus
2024-05-30 d4c810f9803aa13d806724a5f4f71fe85abac38e
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
<template>
<el-dialog title="选择用户" :visible.sync="openSelectUser" width="800px">
    <div style="margin-top:-50px;">
        <el-divider></el-divider>
    </div>
    <!--用户数据-->
    <el-form :model="queryParams" ref="queryForm" :rules="rules" :inline="true" label-width="80px">
        <el-form-item label="手机号码" prop="phonenumber">
            <el-input type="text" placeholder="请输入用户手机号码" v-model="queryParams.phonenumber" minlength="10" clearable size="small" show-word-limit style="width: 240px" @keyup.enter.native="handleQuery"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        </el-form-item>
    </el-form>
 
    <el-table v-loading="loading" :data="userList" highlight-current-row size="mini" @current-change="handleCurrentChange" border>
        <el-table-column label="选择" width="50" align="center">
            <template slot-scope="scope">
                <input type="radio" :checked="scope.row.isSelect" name="user" />
            </template>
        </el-table-column>
        <el-table-column label="用户编号" align="center" key="userId" prop="userId" width="120" />
        <el-table-column label="用户名称" align="center" key="userName" prop="userName"  />
        <el-table-column label="用户昵称" align="center" key="nickName" prop="nickName" />
        <el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber" width="120" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
            <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
        </el-table-column>
 
    </el-table>
    <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addDeviceUser">分享</el-button>
        <el-button @click="closeSelectUser">关 闭</el-button>
    </div>
</el-dialog>
</template>
 
<script>
import {
    listUser
} from "@/api/iot/tool";
import {
    addDeviceUser,
    addDeviceUsers,
} from "@/api/iot/deviceuser";
 
export default {
    name: "user-list",
    props: {
        device: {
            type: Array,
            default: null
        }
    },
    watch: {
        // 获取到父组件传递的device
        device: function (newVal, oldVal) {
            this.deviceInfo = newVal;
        }
    },
    data() {
        return {
            // 遮罩层
            loading: false,
            // 选中数组
            ids: [],
            // 弹出层标题
            title: "",
            // 用户列表
            userList: [],
            // 选中的用户
            user: {},
            // 设备信息
            deviceInfo: {},
            // 是否显示选择用户弹出层
            openSelectUser: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: undefined,
                phonenumber: undefined,
                status: 0,
                deptId: undefined
            },
            // 表单校验
            rules: {
                phonenumber: [{
                    required: true,
                    message: "手机号码不能为空",
                    trigger: "blur"
                }, {
                    min: 11,
                    max: 11,
                    message: '手机号码长度为11位',
                    trigger: 'blur'
                }],
            },
        };
    },
    created() {},
    methods: {
        /** 查询用户列表 */
        getList() {
            this.loading = true;
            listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                this.userList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.$refs["queryForm"].validate(valid => {
                if (valid) {
                    this.queryParams.pageNum = 1;
                    this.getList();
                }
            });
 
        },
        // 重置查询
        resetQuery() {
            this.$refs["queryForm"].resetFields();
            this.userList = [];
        },
        //设置单选按钮选中
        setRadioSelected(userId) {
            for (let i = 0; i < this.userList.length; i++) {
                if (this.userList[i].userId == userId) {
                    this.userList[i].isSelect = true;
                    this.user = this.userList[i];
                } else {
                    this.userList[i].isSelect = false;
                }
            }
        },
        // 单选数据
        handleCurrentChange(user) {
            if (user != null) {
                this.setRadioSelected(user.userId);
                this.user = user;
            }
        },
        // 关闭选择用户
        closeSelectUser() {
            this.openSelectUser = false;
            this.resetQuery();
        },
        // 添加设备用户
        addDeviceUser() {
            if (this.deviceInfo != null && this.deviceInfo.length > 0 && this.user != null) {
                if (this.deviceInfo.length == 1) {
                    var form = {};
                    form.deviceId = this.deviceInfo[0].deviceId;
                    form.deviceName = this.deviceInfo[0].deviceName;
                    form.userId = this.user.userId;
                    form.userName = this.user.userName;
                    form.phonenumber=this.user.phonenumber;
                    addDeviceUser(form).then(response => {
                        this.$modal.msgSuccess("新增成功");
                        this.resetQuery();
                        this.openSelectUser = false;
                        this.$parent.getList();
                    });
                } else {
                    var form = [];
                    this.deviceInfo.forEach(device => {
                        let data = {};
                        data.deviceId = device.deviceId;
                        data.deviceName = device.deviceName
                        data.userId = this.user.userId;
                        data.userName = this.user.userName;
                        data.phonenumber=this.user.phonenumber;
                        form.push(data);
                    });
 
                    addDeviceUsers(form).then(response => {
                        this.$modal.msgSuccess("新增成功");
                        this.resetQuery();
                        this.openSelectUser = false;
                        this.$parent.getList();
                    });
 
                }
            } else {
                this.openSelectUser = false;
            }
        },
    }
};
</script>