更新设备绑定

This commit is contained in:
yaooo 2023-11-30 14:20:58 +08:00
parent 924a7c35e8
commit c3c734f595
6 changed files with 235 additions and 6 deletions

View File

@ -16,6 +16,12 @@ export function apiProductLists(params: any) {
return request.get({ url: '/land.product/lists', params }) return request.get({ url: '/land.product/lists', params })
} }
// 监测设备列表
export function apiDeviceLists(params: any) {
return request.get({ url: '/device.device/lists', params })
}
// 添加产品列表 // 添加产品列表
export function apiProductAdd(params: any) { export function apiProductAdd(params: any) {
return request.post({ url: '/land.product/add', params }) return request.post({ url: '/land.product/add', params })
@ -34,4 +40,9 @@ export function apiProductDelete(params: any) {
// 产品列表详情 // 产品列表详情
export function apiProductDetail(params: any) { export function apiProductDetail(params: any) {
return request.get({ url: '/land.product/detail', params }) return request.get({ url: '/land.product/detail', params })
}
// 绑定设备
export function apiProductBind(params: any) {
return request.post({ url: '/land.product/bind', params })
} }

View File

@ -0,0 +1,148 @@
<template>
<div class="edit-popup">
<popup
ref="popupRef"
:title="popupTitle"
:async="true"
width="650px"
@confirm="handleSubmit"
@close="handleClose"
>
<el-form ref="formRef" :model="formData" label-width="120px" :rules="formRules">
<el-form-item label="产品ID" prop="id">
<el-input v-model="formData.id" disabled clearable placeholder="请输入产品ID" />
</el-form-item>
<el-form-item label="产品名称" prop="name">
<el-input
v-model="formData.name"
disabled
clearable
placeholder="请输入产品名称"
/>
</el-form-item>
<el-form-item label="绑定设备" prop="product_id">
<el-select
v-model="formData.device_id"
filterable
multiple
remote
reserve-keyword
placeholder="请输入设备信息"
:remote-method="queryDevice"
:loading="loading"
>
<el-option
v-for="item in deviceOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
</el-form>
</popup>
</div>
</template>
<script lang="ts" setup name="landEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiProductBind, apiDeviceLists } from '@/api/product'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
dictData: {
type: Object as PropType<Record<string, any[]>>,
default: () => ({})
}
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
//
const popupTitle = computed(() => {
return (mode.value = '绑定设备')
})
//
const formData = reactive({
id: '',
name: '',
device_id: ''
})
//
const formRules = reactive<any>({
device_id: [{
required: true,
message: '请选择绑定设备',
trigger: ['blur']
}],
})
//
const setFormData = async (data: Record<any, any>) => {
for (const key in formData) {
if (data[key] != null && data[key] != undefined) {
//@ts-ignore
formData[key] = data[key]
}
}
}
interface ListItem {
value: string
label: string
}
const deviceOptions = ref<ListItem[]>([])
const loading = ref(false)
const queryDevice = async (query: string) => {
if (query) {
loading.value = true
const deviceList = await apiDeviceLists({
name: query
})
loading.value = false
if (deviceList.count > 0) {
deviceOptions.value = deviceList.lists.map((device: any) => {
return { value: `${device.id}`, label: `ID: ${device.id} / 名称: ${device.name}` }
})
} else {
deviceOptions.value = []
}
loading.value = false
} else {
deviceOptions.value = []
}
}
//
const handleSubmit = async () => {
await formRef.value?.validate()
const data = { ...formData, }
await apiProductBind(data)
popupRef.value?.close()
emit('success')
}
//
const open = (type = 'add') => {
mode.value = type
popupRef.value?.open()
}
//
const handleClose = () => {
emit('close')
}
defineExpose({
open,
setFormData
})
</script>

View File

@ -95,10 +95,10 @@
编辑 编辑
</el-button> </el-button>
<el-button <el-button
v-perms="['land.product/edit']" v-perms="['land.product/bind']"
type="primary" type="primary"
link link
@click="handleEdit(row)" @click="handleBind(row)"
> >
绑定设备 绑定设备
</el-button> </el-button>
@ -131,6 +131,7 @@
</div> </div>
</el-card> </el-card>
<edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" /> <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
<bind-popup v-if="bindEdit" ref="bindRef" :dict-data="dictData" @success="getLists" @close="bindEdit = false" />
</div> </div>
</template> </template>
@ -142,11 +143,15 @@ import { apiProductLists, apiProductDelete } from '@/api/product'
import { timeFormat } from '@/utils/util' import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback' import feedback from '@/utils/feedback'
import EditPopup from './edit.vue' import EditPopup from './edit.vue'
import BindPopup from './bind.vue'
const { query } = useRoute() const { query } = useRoute()
const editRef = shallowRef<InstanceType<typeof EditPopup>>() const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const bindRef = shallowRef<InstanceType<typeof BindPopup>>()
// //
const showEdit = ref(false) const showEdit = ref(false)
const bindEdit = ref(false)
let land_id = query.land_id let land_id = query.land_id
if (typeof land_id == 'undefined') { if (typeof land_id == 'undefined') {
@ -196,6 +201,14 @@ const handleEdit = async (data: any) => {
editRef.value?.setFormData(data) editRef.value?.setFormData(data)
} }
//
const handleBind = async (data: any) => {
bindEdit.value = true
await nextTick()
bindRef.value?.open('bind')
bindRef.value?.setFormData(data)
}
// //
const handleDelete = async (id: number | any[]) => { const handleDelete = async (id: number | any[]) => {
await feedback.confirm('确定要删除?') await feedback.confirm('确定要删除?')

View File

@ -16,9 +16,9 @@
namespace app\adminapi\controller\land; namespace app\adminapi\controller\land;
use app\adminapi\controller\BaseAdminController; use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\land\ProductLists; use app\adminapi\lists\land\ProductLists;
use app\adminapi\logic\land\ProductLogic; use app\adminapi\logic\land\ProductLogic;
use app\adminapi\validate\land\ProductValidate; use app\adminapi\validate\land\ProductValidate;
@ -104,5 +104,17 @@ class ProductController extends BaseAdminController
return $this->data($result); return $this->data($result);
} }
/**
* @notes 绑定产品
* @return \think\response\Json
* @author likeadmin
* @date 2023/11/22 16:35
*/
public function bind()
{
$params = (new ProductValidate())->post()->goCheck('bind');
ProductLogic::bind($params);
return $this->success('绑定成功', [], 1, 1);
}
} }

View File

@ -134,4 +134,38 @@ class ProductLogic extends BaseLogic
{ {
return Product::findOrEmpty($params['id'])->toArray(); return Product::findOrEmpty($params['id'])->toArray();
} }
public static function bind($params): bool
{
$userId = (request()->adminInfo)['user_id'];
if (!empty($params['id'])) {
$userId = Db::name('product')->where('id', $params['id'])->value('user_id');
}
Db::startTrans();
try {
Db::name('product_device')->whereIn('device_id', $params['device_id'])->delete();
Db::name('product_device')->where('product_id', $params['id'])->delete();
Db::name('device')->whereIn('id', $params['device_id'])->update([
'user_id' => $userId
]);
$deviceList = Db::name('device')->whereIn('id', $params['device_id'])->select()->toArray();
$insertData = [];
foreach($deviceList as $d) {
$insertData[] = [
'product_id' => $params['id'],
'device_id' => $d['id'],
'device_type' => $d['type'],
'create_time' => time(),
'update_time' => time()
];
}
Db::name('product_device')->insertAll($insertData);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
} }

View File

@ -32,12 +32,12 @@ class ProductValidate extends BaseValidate
*/ */
protected $rule = [ protected $rule = [
'id' => 'require', 'id' => 'require',
'device_id' => 'require',
'code' => 'require', 'code' => 'require',
'name' => 'require', 'name' => 'require',
'status' => 'require', 'status' => 'require',
]; ];
/** /**
* 参数描述 * 参数描述
* @var string[] * @var string[]
@ -106,4 +106,15 @@ class ProductValidate extends BaseValidate
return true; return true;
} }
/**
* @notes 绑定设备
* @return LandValidate
* @author likeadmin
* @date 2023/11/22 16:35
*/
public function sceneBind()
{
return $this->only(['id', 'device_id']);
}
} }