wufeng 2 rokov pred
rodič
commit
a9ed0299a6

+ 16 - 0
app/admin/config/validate_rules.php

@@ -4,6 +4,7 @@ use app\model\AdminMenuModel;
 use app\model\CommonModel;
 use app\model\GroupModel;
 use app\model\RoleModel;
+use app\model\ThemeModel;
 
 return [
     //【通用】
@@ -209,5 +210,20 @@ return [
         'remark|备注' => 'max:255',
     ],
 
+    //【手机主题】
+    //添加
+    'ThemeAdd' => [
+        'group_id|分组id' => 'require|number|gt:0',
+        'code|主题编号' => 'require|max:255',
+        'modular|模块' => 'require|array|length:4',
+    ],
+
+    //手机主题数据
+    'ThemeDataAdd'=>[
+        'img|图片' => 'require|max:255',
+        'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
+        'jump_param|跳转参数' => 'max:255',
+        'good_id|商品id' => 'number|gt:0'
+    ],
 
 ];

+ 73 - 0
app/admin/controller/Theme.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace app\admin\controller;
+
+use app\admin\logic\ThemeLogic;
+use app\BaseController;
+use think\exception\ValidateException;
+use think\facade\Config;
+use think\facade\Validate;
+
+//手机主题
+class Theme extends BaseController
+{
+    //列表
+    public function list()
+    {
+        $param = $this->request->only(['page' => 1, 'size' => 10, 'company_title' => '', 'card_title' => '', 'status' => ''], 'post');
+
+        return ThemeLogic::list($param);
+    }
+
+    //添加
+    public function add()
+    {
+        $param = $this->request->only([
+            'group_id',
+            'code',
+            'modular',
+        ], 'post');
+
+        $val = Validate::rule(Config::get('validate_rules.ThemeAdd'));
+        if (!$val->check($param)) throw new ValidateException($val->getError());
+
+        return ThemeLogic::add($param);
+    }
+
+    //详情
+    public function read()
+    {
+        $id = $this->request->post('id/d', 0);
+        return ThemeLogic::read($id);
+    }
+
+    //修改
+    public function edit()
+    {
+        $param = $this->request->only([
+            'id',
+            'group_id',
+            'code',
+            'modular',
+        ], 'post');
+
+        $val = Validate::rule(array_merge(Config::get('validate_rules.ThemeAdd'), ['id|主题id' => 'require|number|gt:0']));
+        if (!$val->check($param)) throw new ValidateException($val->getError());
+
+        return ThemeLogic::edit($param);
+    }
+
+
+    //启禁用
+    public function status()
+    {
+        $param = $this->request->only(['id', 'status'], 'post');
+
+        $val = Validate::rule(Config::get('validate_rules.status'));
+
+        if (!$val->check($param)) throw new ValidateException($val->getError());
+        return ThemeLogic::status($param);
+    }
+
+
+}

+ 358 - 0
app/admin/logic/ThemeLogic.php

@@ -0,0 +1,358 @@
+<?php
+
+
+namespace app\admin\logic;
+
+
+use app\model\CommonModel;
+use app\model\GoodModel;
+use app\model\GroupModel;
+use app\model\ThemeModel;
+use think\Exception;
+use think\facade\Db;
+use think\facade\Validate;
+use think\response\Json;
+
+class ThemeLogic extends BaseLogic
+{
+
+    //列表
+    public static function list(array $data = []): Json
+    {
+        $db = ThemeModel::alias('a')
+            ->leftJoin('group b', 'b.id=a.group_id AND b.is_del=' . CommonModel::$del_normal)
+            ->leftJoin('company c', 'c.id=b.company_id AND c.is_del=' . CommonModel::$del_normal)
+            ->leftJoin('card d', 'd.id=b.card_id AND d.is_del=' . CommonModel::$del_normal)
+            ->where('a.is_del', CommonModel::$del_normal);
+
+        if ($data['company_title'] !== '') $db->whereLike('c.title', '%' . $data['company_title'] . '%');
+        if ($data['card_title']) $db->whereLike('d.title', '%' . $data['card_title'] . '%');
+        if ($data['status']) $db->where('a.status', $data['status']);
+
+        $count = $db->count('a.id');
+
+        $video = $db
+            ->field('a.id,c.title company_title,d.title card_title,a.code,a.status,a.creater,a.addtime')
+            ->page($data['page'], $data['size'])
+            ->order('a.addtime', 'desc')
+            ->select()
+            ->toArray();
+
+        return json_show(CommonModel::$success, '获取成功', ['list' => $video, 'count' => $count]);
+
+    }
+
+    //添加
+    public static function add(array $data = []): Json
+    {
+
+        Db::startTrans();
+        try {
+
+            $rs = GroupModel::field('id')
+                ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
+                ->findOrEmpty()
+                ->isEmpty();
+            if ($rs) throw new Exception('该分组不存在');
+
+            $rs = ThemeModel::field('id')
+                ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
+                ->findOrEmpty()
+                ->isEmpty();
+            if (!$rs) throw new Exception('该主题编码已存在');
+
+            $rs = ThemeModel::field('id')
+                ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
+                ->findOrEmpty()
+                ->isEmpty();
+            if (!$rs) throw new Exception('该分组已存在对应手机主题');
+
+            $date = date('Y-m-d H:i:s');
+
+            //手机主题
+            $theme_id = Db::name('theme')->insertGetId([
+                'group_id' => $data['group_id'],
+                'code' => $data['code'],
+                'status' => CommonModel::$status_normal,
+                'is_del' => CommonModel::$del_normal,
+                'createrid' => self::$uid,
+                'creater' => self::$uname,
+                'addtime' => $date,
+                'updaterid' => self::$uid,
+                'updater' => self::$uname,
+                'updatetime' => $date,
+            ]);
+
+            //模块验证规则
+            $val_modular = Validate::rule([
+                'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
+                'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
+                'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|requireIf:type,' . ThemeModel::$type_propaganda . '|max:255',
+                'data|模块数据' => 'require|array|max:100',
+            ]);
+            //数据验证规则
+
+            $val_data = Validate::rule([
+                'img|图片' => 'require|max:255',
+                'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
+                'jump_param|跳转参数' => 'max:255',
+                'good_id|商品id' => 'number|gt:0'
+            ]);
+            foreach ($data['modular'] as $modular) {
+
+                if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
+
+                //模块记录表
+                $modular_id = Db::name('theme_modular')->insertGetId([
+                    'theme_id' => $theme_id,
+                    'title' => $modular['title'],
+                    'type' => $modular['type'],
+                    'status' => $modular['status'],
+                    'is_del' => CommonModel::$del_normal,
+                    'addtime' => $date,
+                    'updatetime' => $date,
+                ]);
+
+                $insert_data = [];
+                $da = $modular['data'];
+
+                $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
+                    ->where('is_del', CommonModel::$del_normal)
+                    ->column('good_name', 'id');
+
+                foreach ($da as &$v) {
+
+                    if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
+
+                    if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
+
+                    $v['theme_modular_id'] = $modular_id;
+                    $v['is_del'] = CommonModel::$del_normal;
+                    $v['good_name'] = $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '';
+                    $v['addtime'] = $date;
+                    $v['updatetime'] = $date;
+                }
+
+                $insert_data = array_merge($insert_data, $da);
+
+                //模块数据记录表
+                Db::name('theme_modular_data')->insertAll($insert_data);
+
+            }
+
+            Db::commit();
+            return json_show(CommonModel::$success, '添加手机主题成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(CommonModel::$error_param, '添加手机主题失败,' . $exception->getMessage());
+        }
+
+    }
+
+    //详情
+    public static function read(int $id = 0): Json
+    {
+        $rs = ThemeModel::field(true)
+            ->where(['is_del' => CommonModel::$del_normal, 'id' => $id])
+            ->append(['modular'])
+            ->withAttr('modular', function ($val, $data) {
+                return Db::name('theme_modular')
+                    ->field(true)
+                    ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
+                    ->order(['addtime' => 'desc', 'id' => 'desc'])
+                    ->append(['data'])
+                    ->withAttr('data', function ($v, $d) {
+                        return Db::name('theme_modular_data')
+                            ->field(true)
+                            ->where(['is_del' => CommonModel::$del_normal, 'theme_modular_id' => $d['id']])
+                            ->order(['addtime' => 'desc', 'id' => 'desc'])
+                            ->select()
+                            ->toArray();
+                    })
+                    ->select()
+                    ->toArray();
+            })
+            ->findOrEmpty()
+            ->toArray();
+        return empty($rs) ? json_show(CommonModel::$error_param, '该手机主题不存在') : json_show(CommonModel::$success, '获取手机主题详情成功', $rs);
+    }
+
+    //修改
+    public static function edit(array $data = []): Json
+    {
+
+        Db::startTrans();
+        try {
+
+            $rs = ThemeModel::field('id,code,group_id')
+                ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])
+                ->findOrEmpty();
+
+            if ($rs->isEmpty()) throw new Exception('该主题不存在');
+
+            if ($rs->code != $data['code']) {
+                //校验code是否重复
+                $temp = ThemeModel::field('id')
+                    ->where(['is_del' => CommonModel::$del_normal, 'code' => $data['code']])
+                    ->findOrEmpty()
+                    ->isEmpty();
+                if (!$temp) throw new Exception('该编码已存在');
+            }
+
+            if ($rs->group_id != $data['group_id']) {
+                //校验分组id是否存在
+                $temp = GroupModel::field('id')
+                    ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['group_id']])
+                    ->findOrEmpty()
+                    ->isEmpty();
+                if ($temp) throw new Exception('该分组不存在');
+
+                //校验分组id是否重复
+                $temp = ThemeModel::field('id')
+                    ->where(['is_del' => CommonModel::$del_normal, 'group_id' => $data['group_id']])
+                    ->findOrEmpty()
+                    ->isEmpty();
+                if (!$temp) throw new Exception('该分组下手机主题重复');
+            }
+
+            $date = date('Y-m-d H:i:s');
+
+            //手机主题
+            ThemeModel::where(['is_del' => CommonModel::$del_normal, 'id' => $data['id']])->save([
+                'group_id' => $data['group_id'],
+                'code' => $data['code'],
+                'updaterid' => self::$uid,
+                'updater' => self::$uname,
+                'updatetime' => $date,
+            ]);
+
+            //模块验证规则
+            $val_modular = Validate::rule([
+                'id|主题模块id' => 'require|number|gt:0',
+                'type|模块类型' => 'require|number|in:' . ThemeModel::$type_advantage . ',' . ThemeModel::$type_banner . ',' . ThemeModel::$type_exhibition . ',' . ThemeModel::$type_propaganda,
+                'status|状态' => 'require|number|in:' . CommonModel::$status_normal . ',' . CommonModel::$status_disable,
+                'title|模块名称' => 'requireIf:type,' . ThemeModel::$type_advantage . '|requireIf:type,' . ThemeModel::$type_exhibition . '|requireIf:type,' . ThemeModel::$type_propaganda . '|max:255',
+                'data|模块数据' => 'require|array|max:100',
+            ]);
+
+            //数据验证规则
+            $val_data = Validate::rule([
+                'id|主题模块数据id' => 'number',
+                'img|图片' => 'require|max:255',
+                'jump_type|跳转类型' => 'require|number|in:' . ThemeModel::$jump_type_external . ',' . ThemeModel::$jump_type_inside . ',' . ThemeModel::$jump_type_no,
+                'jump_param|跳转参数' => 'max:255',
+                'good_id|商品id' => 'number'
+            ]);
+
+            $del = $ins = [];
+
+            foreach ($data['modular'] as $modular) {
+
+                if (!$val_modular->check($modular)) throw new Exception('模块参数有误,' . $val_modular->getError());
+
+                //模块记录表
+                Db::name('theme_modular')
+                    ->where(['is_del' => CommonModel::$del_normal, 'id' => $modular['id']])
+                    ->update([
+                        'title' => $modular['title'],
+                        'status' => $modular['status'],
+                        'updatetime' => $date,
+                    ]);
+
+                $da = $modular['data'];
+
+                $goods = GoodModel::whereIn('id', array_column($da, 'good_id'))
+                    ->where('is_del', CommonModel::$del_normal)
+                    ->column('good_name', 'id');
+
+
+                foreach ($da as &$v) {
+
+                    if ($modular['type'] == ThemeModel::$type_exhibition) $val_data->append('good_id', 'require');
+
+                    if (!$val_data->check($v)) throw new Exception('模块数据参数有误,' . $val_data->getError());
+
+                    if (isset($v['id'])) {
+                        if ($v['is_del'] == CommonModel::$del_deleted) $del[] = $v['id'];
+                        else {
+                            Db::name('theme_modular_data')
+                                ->where(['is_del' => CommonModel::$del_normal, 'id' => $v['id']])
+                                ->update([
+                                    'img' => $v['img'],
+                                    'jump_type' => $v['jump_type'],
+                                    'jump_param' => $v['jump_param'],
+                                    'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
+                                    'good_id' => $v['good_id'],
+                                    'style_type' => $v['style_type'],
+                                    'updatetime' => $date,
+                                ]);
+
+                        }
+                    } else {
+                        $ins[] = array_merge($v, [
+                            'theme_modular_id' => $modular['id'],
+                            'is_del' => CommonModel::$del_normal,
+                            'good_name' => $modular['type'] == ThemeModel::$type_exhibition ? $goods[$v['good_id']] ?? '' : '',
+                            'addtime' => $date,
+                            'updatetime' => $date,
+                        ]);
+                    }
+
+                }
+
+            }
+
+            if ($del) {
+                Db::name('theme_modular_data')
+                    ->where(['is_del' => CommonModel::$del_normal])
+                    ->whereIn('id', $del)
+                    ->update([
+                        'is_del' => CommonModel::$del_deleted,
+                        'updatetime' => $date,
+                    ]);
+            }
+            if ($ins) {
+                
+                Db::name('theme_modular_data')->insertAll($ins);
+            }
+
+            Db::commit();
+            return json_show(CommonModel::$success, '修改手机主题成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(CommonModel::$error_param, '修改手机主题失败,' . $exception->getMessage());
+        }
+
+    }
+
+    //启禁用
+    public static function status(array $data = []): Json
+    {
+        Db::startTrans();
+        try {
+
+            $where = [
+                ['id', '=', $data['id']],
+                ['is_del', '=', CommonModel::$del_normal],
+                ['status', '<>', $data['status']],
+            ];
+
+            $date = date('Y-m-d H:i:s');
+
+            $res = ThemeModel::where($where)->save(array_merge($data, ['updater' => self::$uname, 'updaterid' => self::$uid, 'updatetime' => $date]));
+
+            if (!$res) throw new Exception('该手机主题不存在或重复操作');
+
+            Db::name('theme_modular')
+                ->where(['is_del' => CommonModel::$del_normal, 'theme_id' => $data['id']])
+                ->update(['status' => $data['status'], 'updatetime' => $date]);
+
+            Db::commit();
+            return json_show(CommonModel::$success, '操作成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return json_show(CommonModel::$error_param, '操作失败,' . $exception->getMessage());
+        }
+    }
+
+}

+ 9 - 0
app/admin/route/app.php

@@ -138,3 +138,12 @@ Route::rule('serviceStatus', 'admin/Service/status');//上下架
 Route::rule('orderExchangeList', 'admin/OrderExchange/list');//列表
 Route::rule('orderExchangeRead', 'admin/OrderExchange/read');//详情
 
+//【手机主题】
+Route::rule('themeList', 'admin/Theme/list');//列表
+Route::rule('themeAdd', 'admin/Theme/add');//添加
+Route::rule('themeRead', 'admin/Theme/read');//详情
+Route::rule('themeEdit', 'admin/Theme/edit');//修改
+Route::rule('themeStatus', 'admin/Theme/status');//启禁用
+
+
+

+ 26 - 0
app/model/ThemeModel.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace app\model;
+
+use think\Model;
+
+class ThemeModel extends Model
+{
+    protected $table = 'fc_theme';
+    protected $pk = 'id';
+
+    //模块类型,1轮播,2产品宣传,3产品展示,4产品优势
+    public static $type_banner = 1;
+    public static $type_propaganda = 2;
+    public static $type_exhibition = 3;
+    public static $type_advantage = 4;
+
+    //跳转类型,1无跳转,2内部跳转,3外部跳转
+    public static $jump_type_no = 1;
+    public static $jump_type_inside = 2;
+    public static $jump_type_external = 3;
+
+
+
+
+}