You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/admin/model/Store.php

322 lines
13 KiB

11 months ago
<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace app\admin\model;
use app\admin\model\Page as PageModel;
use app\admin\model\store\User as StoreUserModel;
10 months ago
use app\admin\model\user\User;
use app\common\model\Store as StoreModel;
11 months ago
use app\common\model\User as UserModel;
9 months ago
use think\facade\Db;
use app\store\model\Category as CategoryModel;
use app\common\model\UploadFile;
7 months ago
use app\store\model\store\Role as RoleModel;
11 months ago
/**
* 商家记录表模型
* Class Store
* @package app\admin\model
*/
class Store extends StoreModel
{
/**
* 获取列表数据
* @param bool $isRecycle
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getList(bool $isRecycle = false): \think\Paginator
{
return $this->where('is_recycle', '=', (int)$isRecycle)
->where('is_delete', '=', 0)
->order(['sort' => 'asc', 'create_time' => 'desc'])
->paginate(15);
}
/**
* 新增记录
* @param array $data
* @return bool|mixed
*/
public function add(array $data)
{
if ($data['password'] !== $data['password_confirm']) {
$this->error = '确认密码不正确';
return false;
}
if (StoreUserModel::checkExist($data['user_name'])) {
$this->error = '商家用户名已存在';
return false;
}
return $this->transaction(function () use ($data) {
// 添加小程序记录
$data['is_recycle'] = 0;
if ($status = $this->save($data)) {
// 新增商家用户信息
(new StoreUserModel)->add((int)$this['store_id'], $data);
// 新增默认首页数据
(new PageModel)->insertDefault((int)$this['store_id']);
10 months ago
//注册用户
$user_id = (new User())->register($data, $this['store_id']);
if ($user_id) {
(new StoreUserModel)->where(['store_id' => $this['store_id']])->save(['user_id' => $user_id]);
}
7 months ago
//复制商品分类
9 months ago
$this->copyCategory((int)$this['store_id']);
7 months ago
//多商户版本新增一个商户角色
if (isset($data['store_version']) && $data['store_version'] == 1) {
$this->addMerchantRoleAndMenu((int)$this['store_id']);
}
11 months ago
}
return $status;
});
}
6 months ago
/**
* 更新记录
* @param array $data
* @return bool
*/
public function edit(array $data): bool
{
return $this->save($data) !== false;
}
7 months ago
/**
* 新增一个多商户角色,不能删除
* [addMerchantRoleAndMenu description]
* @param int $storeId [description]
*/
public function addMerchantRoleAndMenu(int $storeId){
$data = [
'role_name' => '商户',
'parent_id' => 0,
'sort' => 100,
'menus' => [
10034,10035,10204,10038,10036,10037,10141,10044,10045,
10046,10243,10244,10246,10245,10050,10051,
7 months ago
10052,10205,10054,10189,10055,10223,10139,10252,10056,10058,
10059,10057,10201,10238,10241,10239,10240,10242,10202,10203,
6 months ago
10206,10207,10208,10209,10213,10210,10211,10115,10247,10248,
6 months ago
10249,10285,10039
7 months ago
],
'store_id' => $storeId
];
$model = new RoleModel;
$model->add($data);
}
9 months ago
public function copyCategory(int $new_store_id){
$store_id = 0;
$model = new CategoryModel;
$list = $model->getList(['store_id' =>$store_id]);
if (!$list) {
return;
}
foreach ($list as $value) {
$value = $value->toArray();
6 months ago
if ($value['status'] != 1) {
continue;
}
9 months ago
$value['store_id'] = $new_store_id;
$value['create_time'] = time();
$value['update_time'] = time();
$value['image_id'] = $value['image_id'] ? $this->copyImage($value['image_id'], $new_store_id) : 0;
9 months ago
$value['rank_image_id'] = $value['rank_image_id'] ? $this->copyImage($value['rank_image_id'], $new_store_id) : 0;
$value['original_category_id'] = $value['category_id'];
9 months ago
$temp = $value;
unset($temp['children']);
9 months ago
unset($temp['rankimage']);
9 months ago
unset($temp['image']);
unset($temp['category_id']);
$firstid = Db::table('yoshop_category')->insertGetId($temp);
if (!isset($value['children']) || !$value['children']) {
continue;
}
foreach ($value['children'] as $value1) {
$value1 = $value1->toArray();
6 months ago
if ($value1['status'] != 1) {
continue;
}
9 months ago
$value1['parent_id'] = $firstid;
$value1['store_id'] = $new_store_id;
$value1['create_time'] = time();
$value1['update_time'] = time();
$value1['image_id'] = $value1['image_id'] ? $this->copyImage($value1['image_id'], $new_store_id) : 0;
9 months ago
$value1['rank_image_id'] = $value1['rank_image_id'] ? $this->copyImage($value1['rank_image_id'], $new_store_id) : 0;
$value1['original_category_id'] = $value1['category_id'];
9 months ago
$temp1 = $value1;
unset($temp1['children']);
unset($temp1['image']);
9 months ago
unset($temp1['rankimage']);
9 months ago
unset($temp1['category_id']);
$secondid = Db::table('yoshop_category')->insertGetId($temp1);
if (!isset($value1['children']) || !$value1['children']) {
continue;
}
foreach ($value1['children'] as $value2) {
$value2 = $value2->toArray();
6 months ago
if ($value2['status'] != 1) {
continue;
}
9 months ago
$value2['parent_id'] = $secondid;
$value2['store_id'] = $new_store_id;
$value2['create_time'] = time();
$value2['update_time'] = time();
$value2['image_id'] = $value2['image_id'] ? $this->copyImage($value2['image_id'], $new_store_id) : 0;
9 months ago
$value2['rank_image_id'] = $value2['rank_image_id'] ? $this->copyImage($value2['rank_image_id'], $new_store_id) : 0;
$value2['original_category_id'] = $value2['category_id'];
9 months ago
$temp2 = $value2;
unset($temp2['children']);
unset($temp2['image']);
9 months ago
unset($temp2['rankimage']);
9 months ago
unset($temp2['category_id']);
11 months ago
7 months ago
$thirdid = Db::table('yoshop_category')->insertGetId($temp2);
9 months ago
if (!isset($value2['children']) || !$value2['children']) {
continue;
}
foreach ($value2['children'] as $value3) {
$value3 = $value3->toArray();
6 months ago
if ($value3['status'] != 1) {
continue;
}
7 months ago
$value3['parent_id'] = $thirdid;
9 months ago
$value3['store_id'] = $new_store_id;
$value3['create_time'] = time();
$value3['update_time'] = time();
$value3['image_id'] = $value3['image_id'] ? $this->copyImage($value3['image_id'], $new_store_id) : 0;
9 months ago
$value3['rank_image_id'] = $value3['rank_image_id'] ? $this->copyImage($value3['rank_image_id'], $new_store_id) : 0;
$value3['original_category_id'] = $value3['category_id'];
9 months ago
$temp3 = $value3;
unset($temp3['children']);
unset($temp3['image']);
9 months ago
unset($temp3['rankimage']);
9 months ago
unset($temp3['category_id']);
7 months ago
$fourthid = Db::table('yoshop_category')->insertGetId($temp3);
if (!isset($value3['children']) || !$value3['children']) {
continue;
}
foreach ($value3['children'] as $value4) {
$value4 = $value4->toArray();
6 months ago
if ($value4['status'] != 1) {
continue;
}
7 months ago
$value4['parent_id'] = $fourthid;
$value4['store_id'] = $new_store_id;
$value4['create_time'] = time();
$value4['update_time'] = time();
$value4['image_id'] = $value4['image_id'] ? $this->copyImage($value4['image_id'], $new_store_id) : 0;
$value4['rank_image_id'] = $value4['rank_image_id'] ? $this->copyImage($value4['rank_image_id'], $new_store_id) : 0;
$value4['original_category_id'] = $value4['category_id'];
$temp4 = $value4;
unset($temp4['children']);
unset($temp4['image']);
unset($temp4['rankimage']);
unset($temp4['category_id']);
Db::table('yoshop_category')->insertGetId($temp4);
}
9 months ago
}
9 months ago
}
}
}
}
public function copyImage($image_id, $store_id){
$upload_file = DB::table("yoshop_upload_file")->where('file_id', $image_id)->find();
if (!$upload_file) {
return 0;
}
$upload_file['store_id'] = $store_id;
$upload_file['create_time'] = time();
unset($upload_file['file_id']);
$new_image_id = DB::table("yoshop_upload_file")->insertGetId($upload_file);
return $new_image_id;
}
11 months ago
/**
* 移入移出回收站
* @param bool $isRecycle
* @return bool|false
*/
public function recycle(bool $isRecycle = true): bool
{
return $this->save(['is_recycle' => (int)$isRecycle]);
}
/**
* 软删除
* @return false|int
*/
public function setDelete()
{
return $this->transaction(function () {
// 删除商家用户信息
StoreUserModel::setDelete($this['store_id']);
// 设置当前商城为已删除
return $this->save(['is_delete' => 1]);
});
}
11 months ago
public function auditStore(array $data)
{
$storeInfo = $this->toArray();
11 months ago
if ($storeInfo['status'] != 0) {
11 months ago
$this->error = '状态异常,请勿重复操作';
return false;
}
11 months ago
$user = UserModel::where(['user_id' => $storeInfo['user_id']])->find();
if (empty($user) || empty($user->mobile)) {
$this->error = '用户不存在,或未绑定手机';
return false;
}
if (StoreUserModel::checkExist($user->mobile)) {
11 months ago
$this->error = '商家用户名已存在';
return false;
}
11 months ago
if ($data['status'] == 1) {
$data['user_id'] = $storeInfo['user_id'];
$data['mobile'] = $user->mobile;
11 months ago
return $this->transaction(function () use ($data) {
// 添加小程序记录
11 months ago
$update['is_recycle'] = 0;
$update['status'] = 1;
if ($status = $this->save($update)) {
11 months ago
// 新增商家用户信息
11 months ago
$storeUser = [
'user_name' => $data['mobile'],
'password' => $data['mobile'],
];
(new StoreUserModel)->add((int)$this['store_id'], $storeUser);
11 months ago
// 新增默认首页数据
(new PageModel)->insertDefault((int)$this['store_id']);
11 months ago
//指定用户类型更改为商家
(new UserModel)->where(['user_id' => $data['user_id']])->update(['user_type' => 40]);
11 months ago
}
return $status;
});
11 months ago
} else {
return $this->save(['status' => 2]);
11 months ago
}
11 months ago
}
11 months ago
}