国家、省份功能
This commit is contained in:
parent
d179130fd4
commit
ae318f0dd7
|
|
@ -13,6 +13,7 @@ namespace Beike\Admin\Http\Controllers;
|
|||
|
||||
use Beike\Admin\Http\Resources\CustomerResource;
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Repositories\CountryRepo;
|
||||
use Beike\Repositories\CustomerGroupRepo;
|
||||
use Beike\Repositories\CustomerRepo;
|
||||
use Illuminate\Http\Request;
|
||||
|
|
@ -52,6 +53,7 @@ class CustomerController extends Controller
|
|||
$data = [
|
||||
'customer' => $customer,
|
||||
'customer_groups' => CustomerGroupRepo::list(),
|
||||
'country' => CountryRepo::all(),
|
||||
'_redirect' => $this->getRedirect(),
|
||||
];
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
/**
|
||||
* ZoneController.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-30 16:17:04
|
||||
* @modified 2022-06-30 16:17:04
|
||||
*/
|
||||
|
||||
namespace Beike\Admin\Http\Controllers;
|
||||
|
||||
use Beike\Admin\Http\Resources\CustomerResource;
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Repositories\CountryRepo;
|
||||
use Beike\Repositories\CustomerGroupRepo;
|
||||
use Beike\Repositories\CustomerRepo;
|
||||
use Beike\Repositories\ZoneRepo;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ZoneController extends Controller
|
||||
{
|
||||
public function index(Request $request, int $countryId)
|
||||
{
|
||||
ZoneRepo::listByCountry($countryId);
|
||||
|
||||
$data = [
|
||||
'zones' => ZoneRepo::listByCountry($countryId),
|
||||
];
|
||||
|
||||
return json_success('成功!', $data);
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ Route::prefix('admin')
|
|||
|
||||
Route::Resource('customers', \Beike\Admin\Http\Controllers\CustomerController::class);
|
||||
Route::resource('customers.addresses', \Beike\Admin\Http\Controllers\AddressController::class);
|
||||
Route::resource('countries.zones', \Beike\Admin\Http\Controllers\ZoneController::class);
|
||||
|
||||
Route::put('products/restore', [\Beike\Admin\Http\Controllers\ProductController::class, 'restore']);
|
||||
Route::resource('products', \Beike\Admin\Http\Controllers\ProductController::class);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* Country.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-30 15:22:18
|
||||
* @modified 2022-06-30 15:22:18
|
||||
*/
|
||||
|
||||
namespace Beike\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Country extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name', 'country_id', 'code', 'sort_order', 'status'];
|
||||
|
||||
public function zones(): HasMany
|
||||
{
|
||||
return $this->hasMany(Zone::class);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
/**
|
||||
* Zone.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-30 15:22:18
|
||||
* @modified 2022-06-30 15:22:18
|
||||
*/
|
||||
|
||||
namespace Beike\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
class Zone extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name', 'code', 'sort_order', 'status'];
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
/**
|
||||
* CountryRepo.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-30 15:22:05
|
||||
* @modified 2022-06-30 15:22:05
|
||||
*/
|
||||
|
||||
namespace Beike\Repositories;
|
||||
|
||||
use Beike\Models\Country;
|
||||
|
||||
class CountryRepo
|
||||
{
|
||||
/**
|
||||
* 创建一个country记录
|
||||
* @param $data
|
||||
* @return int
|
||||
*/
|
||||
public static function create($data)
|
||||
{
|
||||
$id = Country::query()->insertGetId($data);
|
||||
return self::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $data
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function update($id, $data)
|
||||
{
|
||||
$country = Country::query()->find($id);
|
||||
if (!$country) {
|
||||
throw new \Exception("国家id {$id} 不存在");
|
||||
}
|
||||
$country->update($data);
|
||||
return $country;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 Country::query()->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return void
|
||||
*/
|
||||
public static function delete($id)
|
||||
{
|
||||
$country = Country::query()->find($id);
|
||||
if ($country) {
|
||||
$country->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public static function list($data)
|
||||
{
|
||||
$builder = Country::query();
|
||||
|
||||
if (isset($data['name'])) {
|
||||
$builder->where('countries.name', 'like', "%{$data['name']}%");
|
||||
}
|
||||
if (isset($data['code'])) {
|
||||
$builder->where('countries.code', 'like', "%{$data['email']}%");
|
||||
}
|
||||
if (isset($data['status'])) {
|
||||
$builder->where('countries.status', $data['status']);
|
||||
}
|
||||
|
||||
return $builder->paginate(20)->withQueryString();
|
||||
}
|
||||
|
||||
public static function all()
|
||||
{
|
||||
return Country::query()->select('id', 'name')->get();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
/**
|
||||
* ZoneRepo.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author TL <mengwb@opencart.cn>
|
||||
* @created 2022-06-30 15:22:05
|
||||
* @modified 2022-06-30 15:22:05
|
||||
*/
|
||||
|
||||
namespace Beike\Repositories;
|
||||
|
||||
use Beike\Models\Zone;
|
||||
|
||||
class ZoneRepo
|
||||
{
|
||||
/**
|
||||
* 创建一个zone记录
|
||||
* @param $data
|
||||
* @return int
|
||||
*/
|
||||
public static function create($data)
|
||||
{
|
||||
$id = Zone::query()->insertGetId($data);
|
||||
return self::find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $data
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function update($id, $data)
|
||||
{
|
||||
$zone = Zone::query()->find($id);
|
||||
if (!$zone) {
|
||||
throw new \Exception("省份/地区id {$id} 不存在");
|
||||
}
|
||||
$zone->update($data);
|
||||
return $zone;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 Zone::query()->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return void
|
||||
*/
|
||||
public static function delete($id)
|
||||
{
|
||||
$zone = Zone::query()->find($id);
|
||||
if ($zone) {
|
||||
$zone->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public static function list($data)
|
||||
{
|
||||
$builder = Zone::query();
|
||||
|
||||
if (isset($data['name'])) {
|
||||
$builder->where('zones.name', 'like', "%{$data['name']}%");
|
||||
}
|
||||
if (isset($data['code'])) {
|
||||
$builder->where('zones.code', 'like', "%{$data['email']}%");
|
||||
}
|
||||
if (isset($data['status'])) {
|
||||
$builder->where('zones.status', $data['status']);
|
||||
}
|
||||
|
||||
return $builder->paginate(20)->withQueryString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据国家获取国家的省份
|
||||
* @param $country
|
||||
* @return \Illuminate\Database\Eloquent\HigherOrderBuilderProxy|\Illuminate\Support\HigherOrderCollectionProxy|mixed|void
|
||||
*/
|
||||
public static function listByCountry($country)
|
||||
{
|
||||
if (gettype($country) != 'object') {
|
||||
$country = CountryRepo::find($country);
|
||||
}
|
||||
if ($country) {
|
||||
return $country->zones;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CountryZone extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('countries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name', 64);
|
||||
$table->string('code', 16);
|
||||
$table->integer('sort_order');
|
||||
$table->tinyInteger('status');
|
||||
$table->timestamps();
|
||||
});
|
||||
Schema::create('zones', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedInteger('country_id');
|
||||
$table->string('name', 64);
|
||||
$table->string('code', 16);
|
||||
$table->integer('sort_order');
|
||||
$table->tinyInteger('status');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('countries');
|
||||
Schema::dropIfExists('zones');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue