Commit 79d57401 authored by Administrator's avatar Administrator

设备型号

parent 6290bd5b
Pipeline #111 canceled with stages
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ModelNumberRequest;
use App\Models\DeviceModelNumber;
use Illuminate\Support\Facades\DB;
class ModelNumberController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$deviceModelNumbers = DB::table('device_model_numbers AS m')->select('m.*', 't.tname')
->leftJoin('device_type AS t', 't.tid', '=', 'm.device_type_id')
->orderBy('m.device_type_id')->get()->toArray();
return $this->jsonSuccessData($deviceModelNumbers);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(ModelNumberRequest $request)
{
$data = $request->all();
$model = new DeviceModelNumber();
$model->model_number = $data['model_number'];
$model->device_type_id = $data['device_type_id'];
if($model->save()) {
return $this->jsonSuccessData();
}else{
return $this->jsonErrorData(201, 'error:2002');
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(ModelNumberRequest $request, $id)
{
$data = $request->all();
// dd($data,$id);
$model = DeviceModelNumber::where('id', $id)->update([
'model_number' => $data['model_number'],
]);
if($model) {
return $this->jsonSuccessData();
}else{
return $this->jsonErrorData(201, 'error:2002');
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if(DeviceModelNumber::destroy($id)) {
return $this->jsonSuccessData();
}else{
return $this->jsonErrorData(201, 'error:2002');
}
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ModelNumberRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
switch ($this->method()) {
case 'POST': {
return [
'model_number'=>'required|between:3,20|regex:/^[A-Za-z0-9\-\_]+$/|unique:device_model_numbers,model_number',
];
}
case 'GET':
case 'PUT': {
return [
'model_number'=>'required|between:3,20|regex:/^[A-Za-z0-9\-\_]+$/|unique:device_model_numbers,model_number',
];
}
case 'DELETE':
default: {
return [];
}
}
}
//自定义提示
public function messages()
{
return [
'model_number.required' => '型号不能为空!',
'model_number.between' => '密码需要在3到20位之间',
'model_number.regex' => '型号格式只能包括数字、字母、- 、_',
'model_number.unique' => '型号已经存在',
];
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class DeviceModelNumber extends Model
{
protected $fillable = ['model_number', 'device_type_id'];
}
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Models\DeviceModelNumber;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
$factory->define(DeviceModelNumber::class, function (Faker $faker) {
return [
'model_number' => Str::random(6),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
});
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class DeviceModelNumberTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('device_model_numbers', function (Blueprint $table) {
$table->increments('id');
$table->string('model_number')->unique()->comment('型号名称');
$table->integer('device_type_id')->comment('设备分类ID');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('device_model_numbers');
}
}
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Models\DeviceModelNumber;
use Illuminate\Support\Str;
class ModelNumberSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$device_type_ids = DB::table('device_type')->pluck('tid')->toArray();
$faker = app(Faker\Generator::class);
$modelNumbers = factory(DeviceModelNumber::class)
->times(20)
->make()
->each(function ($modelNumber, $index) use($device_type_ids, $faker){
$modelNumber->device_type_id = $faker->randomElement($device_type_ids);
});
DeviceModelNumber::insert($modelNumbers->toArray());
}
}
import request from '@/utils/request';
// modelNumbers, addModelNumber, updateModelNumber, deleteModelNumber
// 列表
export function modelNumbers() {
return request({
url: 'device/models',
method: 'get',
});
}
// 添加
export function addModelNumber(data) {
return request({
url: 'device/models',
method: 'post',
data,
});
}
// 更新
export function updateModelNumber(id, dataForm) {
return request({
url: 'device/models/' + id,
method: 'put',
data: dataForm,
});
}
// 删除
export function deleteModelNumber(id) {
return request({
url: 'device/models/' + id,
method: 'delete',
});
}
......@@ -24,6 +24,12 @@ const deviceRoutes = {
name: 'Categories',
meta: { title: '设备分类', icon: 'tree-table', permissions: ['manage categories'] },
},
{
path: 'model_numbers',
component: () => import('@/views/device/model_numbers'),
name: 'ModelNumbers',
meta: { title: '设备型号', icon: 'tree-table', permissions: ['manage model_numbers'] },
},
{
path: 'DeviceStutus',
component: () => import('@/views/device/devicedtutus'),
......
<template>
<div class="app-container">
<div class="filter-container">
<el-form ref="addForm" :model="addForm" status-icon :rules="ruleso">
<el-form-item prop="model_number" style="display: inline-block;">
<el-input v-model="addForm.model_number" placeholder="快速添加" autocomplete="off" style="width: 200px;" class="filter-item" name="modelNumberAdd" />
</el-form-item>
<el-form-item prop="model_number" style="display: inline-block;">
<el-select v-model="addForm.device_type" placeholder="请选择设备类型" autocomplete="off" class="filter-item">
<el-option
v-for="item in deviceTypes"
:key="item.tid"
:label="item.tname"
:value="item.tid"
>
</el-option>
</el-select>
</el-form-item>
<el-button v-waves class="filter-item" type="primary" icon="el-icon-plus" @click="handleClick()">
{{ $t('table.add') }}
</el-button>
</el-form>
</div>
<el-table :data="modelNumbers" :span-method="objectSpanMethod" border>
<el-table-column prop="date" label="ID" width="100">
<template slot-scope="scope">
<span>{{ scope.row.id }}</span>
</template>
</el-table-column>
<el-table-column prop="name" label="设备型号">
<template slot-scope="scope">
<span>{{ scope.row.model_number }}</span>
</template>
</el-table-column>
<el-table-column prop="name" label="设备类型">
<template slot-scope="scope">
<span>{{ scope.row.tname }}</span>
</template>
</el-table-column>
<el-table-column prop="name" label="创建时间" width="220">
<template slot-scope="scope">
<span>{{ scope.row.created_at }}</span>
</template>
</el-table-column>
<el-table-column prop="name" label="更新时间" width="220">
<template slot-scope="scope">
<span>{{ scope.row.updated_at }}</span>
</template>
</el-table-column>
<el-table-column label="操作" width="220">
<template slot-scope="scope">
<el-button type="info" size="small" @click="handleEdit(scope.row.id, scope.row.model_number)">修改</el-button>
<el-button type="danger" size="small" @click="deleteModelNumber(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
import waves from '@/directive/waves'; // 水波纹效果
import { deviceTypeList } from '@/api/device';
import { modelNumbers, addModelNumber, updateModelNumber, deleteModelNumber } from '@/api/modelNumber';
export default {
directives: { waves },
data() {
var validateNumber = (rule, value, callback) => {
if (value === '') {
callback(new Error('请输入设备型号'));
} else {
callback();
}
};
var validateType = (rule, value, callback) => {
if (value === '') {
callback(new Error('请选择设备类型'));
} else {
callback();
}
};
return {
modelNumbers: [], // 设备型号列表
modelNumber: '',
deviceTypes: [],
addForm: {
model_number: '',
device_type: '',
},
ruleso: {
model_number: [
{ validator: validateNumber, trigger: 'blur' },
],
device_type: [
{ validator: validateType, trigger: 'blur' },
],
},
};
},
created() {
this.getList();// 设备型号
this.deviceTypeList();// 设备分类
},
methods: {
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 2) {
if (rowIndex % 2 === 0) {
return {
rowspan: 2,
colspan: 1,
};
} else {
return {
rowspan: 0,
colspan: 0,
};
}
}
},
deviceTypeList() {
deviceTypeList()
.then(response => {
this.deviceTypes = response.data.devicetype;
})
.catch(err => {
console.log(err);
});
},
getList() {
modelNumbers()
.then(response => {
this.modelNumbers = response.data;
})
.catch(err => {
console.log(err);
});
},
// 添加
handleClick() {
if (this.addForm.model_number === '' || this.addForm.device_type === '') {
this.$message({
type: 'error',
message: '请输入内容',
});
return false;
}
this.$prompt('确认设备型号', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
inputPattern: /\S/,
inputErrorMessage: '型号格式不正确',
inputValue: this.addForm.model_number,
}).then(({ value }) => {
addModelNumber({ model_number: value, device_type_id: this.addForm.device_type })
.then(response => {
if (response.code === 200) {
this.$message({
type: 'success',
message: '添加成功!',
});
this.getList();
}
})
.catch(err => {
console.log(err);
});
}).catch(() => {
this.$message({
type: 'info',
message: '取消输入',
});
});
this.modelNumber = '';
},
// 修改
handleEdit(id, model_number) {
this.$prompt('请输入设备型号', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
inputPattern: /\S/,
inputErrorMessage: '型号格式不正确',
inputValue: model_number,
}).then(({ value }) => {
updateModelNumber(id, { 'model_number': value })
.then(response => {
if (response.code === 200) {
this.$message({
type: 'success',
message: '修改成功!',
});
this.getList();
}
})
.catch(err => {
console.log(err);
});
}).catch(() => {
this.$message({
type: 'info',
message: '取消输入',
});
});
},
deleteModelNumber(id){
this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning',
}).then(() => {
deleteModelNumber(id)
.then(response => {
if (response.code === 200) {
this.getList();
}
})
.catch(err => {
console.log(err);
});
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除',
});
});
},
},
};
</script>
<style>
</style>
......@@ -103,6 +103,11 @@ Route::group(['middleware'=>'auth:api'],function (){
Route::get('devices/device_info/{id}','DevicesController@device_info');//返回设备地图详情信息
Route::get('devices/devicepolice','DevicesController@devicepolice');//设备消警
Route::get('devices/devicepoliceinfo','DevicesController@devicepoliceinfo');//设备报警弹窗详情
// 设备型号
Route::get('device/models', 'ModelNumberController@index');
Route::post('device/models', 'ModelNumberController@store');
Route::put('device/models/{id}', 'ModelNumberController@update');
Route::delete('device/models/{id}', 'ModelNumberController@destroy');
});
//装维中心
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment