后台顾客管理
This commit is contained in:
parent
c3fff65925
commit
ed4d678c80
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
/**
|
||||
* CustomerController.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-28 16:17:04
|
||||
* @modified 2022-06-28 16:17:04
|
||||
*/
|
||||
|
||||
namespace Beike\Admin\Http\Controllers;
|
||||
|
||||
use Beike\Admin\Http\Resources\CustomerResource;
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Repositories\CustomerRepo;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class CustomerController extends Controller
|
||||
{
|
||||
protected string $defaultRoute = 'customers.index';
|
||||
public function index(Request $request)
|
||||
{
|
||||
$customers = CustomerRepo::list($request->only(['name', 'email', 'status', 'from', 'customer_group_name']));
|
||||
|
||||
$data = [
|
||||
'customers' => CustomerResource::collection($customers),
|
||||
];
|
||||
|
||||
return view('admin::pages.customers.index', $data);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$data = [
|
||||
'customer' => new Customer(),
|
||||
'_redirect' => $this->getRedirect(),
|
||||
];
|
||||
return view('admin::pages.customers.form', $data);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
CustomerRepo::create($request->all());
|
||||
|
||||
return redirect($this->getRedirect())->with('success', 'customer created');
|
||||
}
|
||||
|
||||
public function edit(Request $request, Customer $customer)
|
||||
{
|
||||
$data = [
|
||||
'customer' => $customer,
|
||||
'_redirect' => $this->getRedirect(),
|
||||
];
|
||||
|
||||
return view('admin::pages.customers.form', $data);
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
{
|
||||
$customerId = $request->id ?? 0;
|
||||
CustomerRepo::update($customerId, $request->all());
|
||||
|
||||
return redirect($this->getRedirect())->with('success', 'customer created');
|
||||
}
|
||||
|
||||
public function destroy(Request $request)
|
||||
{
|
||||
$customerId = $request->id ?? 0;
|
||||
CustomerRepo::delete($customerId);
|
||||
|
||||
return ['success' => true];
|
||||
}
|
||||
|
||||
public function restore(Request $request)
|
||||
{
|
||||
$customerId = $request->id ?? 0;
|
||||
Customer::withTrashed()->find($customerId)->restore();
|
||||
|
||||
return ['success' => true];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Beike\Admin\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class CustomerResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$data = [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'email' => $this->email,
|
||||
'status' => $this->status ? '启用' : '禁用',
|
||||
'avatar' => image_resize($this->avatar),
|
||||
'from' => $this->from,
|
||||
'customer_group_name' => $this->customer_group_name,
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,8 @@ Route::prefix('admin')
|
|||
|
||||
Route::Resource('files', \Beike\Admin\Http\Controllers\FileController::class);
|
||||
|
||||
Route::Resource('customers', \Beike\Admin\Http\Controllers\CustomerController::class);
|
||||
|
||||
Route::put('products/restore', [\Beike\Admin\Http\Controllers\ProductController::class, 'restore']);
|
||||
Route::resource('products', \Beike\Admin\Http\Controllers\ProductController::class);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@
|
|||
namespace Beike\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
|
||||
class Customer extends Authenticatable
|
||||
{
|
||||
use HasFactory;
|
||||
use SoftDeletes;
|
||||
|
||||
const AUTH_GUARD = 'web_shop';
|
||||
|
||||
|
|
|
|||
|
|
@ -17,11 +17,74 @@ class CustomerRepo
|
|||
{
|
||||
/**
|
||||
* 创建一个customer记录
|
||||
* @param $customerData
|
||||
* @return int
|
||||
*/
|
||||
public static function create($customerData)
|
||||
{
|
||||
return Customer::query()->insertGetId($customerData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $data
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function update($id, $data)
|
||||
{
|
||||
return Customer::query()->find($id)->update($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
|
||||
*/
|
||||
public static function find($id)
|
||||
{
|
||||
return Customer::query()->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return void
|
||||
*/
|
||||
public static function delete($id)
|
||||
{
|
||||
Customer::query()->find($id)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public static function list($data)
|
||||
{
|
||||
$builder = Customer::query()->from('customers AS c')
|
||||
->leftJoin('customer_groups AS cg', 'c.customer_group_id', 'cg.id')
|
||||
->leftJoin('customer_group_descriptions AS cgd', function ($join) {
|
||||
$join->on('cgd.customer_group_id', 'cg.id')
|
||||
->where('cgd.language_id', current_language_id());
|
||||
})
|
||||
->select(['c.id', 'c.email', 'c.name', 'c.avatar', 'c.status', 'c.from', 'cgd.name AS customer_group_name']);
|
||||
|
||||
|
||||
if (isset($data['name'])) {
|
||||
$builder->where('c.name', 'like', "%{$data['name']}%");
|
||||
}
|
||||
if (isset($data['email'])) {
|
||||
$builder->where('c.email', 'like', "%{$data['email']}%");
|
||||
}
|
||||
if (isset($data['status'])) {
|
||||
$builder->where('c.status', $data['status']);
|
||||
}
|
||||
if (isset($data['from'])) {
|
||||
$builder->where('c.from', $data['from']);
|
||||
}
|
||||
if (isset($data['customer_group_name'])) {
|
||||
$builder->where('cgd.name', 'like', "%{$data['name']}%");
|
||||
}
|
||||
|
||||
return $builder->paginate(20)->withQueryString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,97 @@
|
|||
@extends('admin::admin.layouts.master')
|
||||
|
||||
@section('title', '分类管理')
|
||||
|
||||
@push('header')
|
||||
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.min.js"></script>
|
||||
@endpush
|
||||
|
||||
@section('content')
|
||||
<div id="app">
|
||||
<el-card class="box-card">
|
||||
<div slot="header" class="clearfix">
|
||||
<span>编辑分类</span>
|
||||
</div>
|
||||
|
||||
<el-form label-width="200px" size="small1">
|
||||
|
||||
<el-form-item label="分类名称">
|
||||
<div style="max-width: 400px">
|
||||
@foreach (locales() as $locale)
|
||||
<el-input class="mb-1">
|
||||
<template slot="append">{{ $locale['name'] }}</template>
|
||||
</el-input>
|
||||
@endforeach
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="分类描述">
|
||||
<div style="max-width: 400px">
|
||||
@foreach (locales() as $locale)
|
||||
<el-input v-model="form.descriptions['{{ $locale['code'] }}'].content" class="mb-1">
|
||||
<template slot="append">{{ $locale['name'] }}</template>
|
||||
</el-input>
|
||||
@endforeach
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="上级分类">
|
||||
<el-select v-model="form.parent_id" placeholder="请选择上级分类">
|
||||
@foreach ($categories as $_category)
|
||||
<el-option label="{{ $_category->name }}" value="{{ $_category->id }}"></el-option>
|
||||
@endforeach
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="状态">
|
||||
<el-radio-group v-model="form.active">
|
||||
<el-radio :label="1">启用</el-radio>
|
||||
<el-radio :label="0">禁用</el-radio>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="save">立即创建</el-button>
|
||||
<el-button>取消</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('footer')
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#app',
|
||||
data() {
|
||||
return {
|
||||
form: {
|
||||
parent_id: 0,
|
||||
active: 1,
|
||||
descriptions: {
|
||||
zh_cn: {
|
||||
name: '',
|
||||
content: '',
|
||||
},
|
||||
en: {
|
||||
name: '',
|
||||
content: '',
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
methods: {
|
||||
save() {
|
||||
axios.post(@json(admin_route('categories.store')), this.form).then(response => {
|
||||
this.loading = false;
|
||||
}).catch(error => {
|
||||
// this.$message.error(error.response.data.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
</script>
|
||||
@endpush
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
@extends('admin::layouts.master')
|
||||
|
||||
@section('title', '顾客管理')
|
||||
|
||||
@section('content')
|
||||
<div id="customer-app" class="card">
|
||||
<div class="card-header">
|
||||
编辑顾客
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form action="{{ admin_route($customer->id ? 'customers.update' : 'customers.store', $customer) }}"
|
||||
method="POST">
|
||||
@csrf
|
||||
@method($customer->id ? 'PUT' : 'POST')
|
||||
<input type="hidden" name="_redirect" value="{{ $_redirect }}">
|
||||
|
||||
|
||||
<x-admin-form-switch title="状态" name="active" :value="old('active', $customer->active ?? 1)"/>
|
||||
|
||||
<x-admin::form.row>
|
||||
<button type="submit" class="btn btn-primary">保存</button>
|
||||
<a href="{{ $_redirect }}" class="btn btn-outline-secondary">返回</a>
|
||||
</x-admin::form.row>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('footer')
|
||||
@endpush
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
@extends('admin::layouts.master')
|
||||
|
||||
@section('title', '顾客管理')
|
||||
|
||||
@push('header')
|
||||
<style>
|
||||
.el-tree-node__content {
|
||||
height: 32px;
|
||||
border-bottom: 1px solid #f9f9f9;
|
||||
}
|
||||
</style>
|
||||
@endpush
|
||||
|
||||
@section('content')
|
||||
<div id="customer-app" class="card">
|
||||
<div class="card-body">
|
||||
<a href="{{ admin_route('customers.create') }}" class="btn btn-primary">创建顾客</a>
|
||||
<div class="mt-4" style="">
|
||||
<el-tree :data="categories" default-expand-all :expand-on-click-node="false">
|
||||
<div class="custom-tree-node" slot-scope="{ node, data }" style="flex:1;display:flex">
|
||||
<span>@{{ data.id }}</span>
|
||||
<span>@{{ data.email }}</span>
|
||||
<span>@{{ data.avatar }}</span>
|
||||
<span>@{{ data.from }}</span>
|
||||
<div style="flex:1"></div>
|
||||
<span class="mr-4">@{{ data.status ? '启用' : '禁用' }}</span>
|
||||
<div>
|
||||
<a :href="data.url_edit">编辑</a>
|
||||
<a>删除</a>
|
||||
</div>
|
||||
</div>
|
||||
</el-tree>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('footer')
|
||||
<script>
|
||||
new Vue({
|
||||
el: '#customer-app',
|
||||
data: {
|
||||
categories: @json($customers),
|
||||
defaultProps: {
|
||||
children: 'children',
|
||||
label: 'name'
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
handleNodeClick(data) {
|
||||
console.log(data);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
</script>
|
||||
@endpush
|
||||
Loading…
Reference in New Issue