1.3.3. 修改用戶狀態
- 請求路徑:users/:uId/state/:type
- 請求方法:put
- 請求參數
參數名 | 參數說明 | 備注 |
---|---|---|
uId | 用戶 ID | 不能為空攜帶在url中 |
type | 用戶狀態 | 不能為空攜帶在url中 ,值為 true 或者 false |
{
"data": {
"id": 566,
"rid": 30,
"username": "admin",
"mobile": "123456",
"email": "bb@itcast.com",
"mg_state": 0
},
"meta": {
"msg": "設置狀態成功",
"status": 200
}
}
A.首先監聽用戶點擊switch組件的事件,並將作用域插槽的數據當做事件參數進行傳遞
<!--html-->
<el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)"></el-switch>
B.在事件中發送請求完成狀態的更改
async userStateChanged(row) {
//發送請求進行狀態修改
const { data: res } = await this.$http.put(
`users/${row.id}/state/${row.mg_state}`
)
//如果返回狀態為異常狀態則報錯並返回
if (res.meta.status !== 200) {
row.mg_state = !row.mg_state
return this.$message.error('修改狀態失敗')
}
this.$message.success('更新狀態成功')
},