123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\shop\logic;
-
-
- use app\common\basics\Logic;
- use app\common\enum\FileEnum;
- use app\common\model\File;
- use app\common\model\FileCate;
- use Exception;
-
- class FileLogic extends Logic
- {
-
-
-
- public static function getFile($get)
- {
- try {
- $where = [
- ['del', '=', 0],
- ['type', '=', $get['type'] ?? FileEnum::IMAGE_TYPE],
- ['shop_id', '=', $get['shop_id']]
- ];
-
- if (!empty($get['cid']) and $get['cid'] > 0) {
- $lists = FileCate::where(['del'=>0 ])->select();
- $lists = !empty($lists) ? $lists->toArray() : [];
- $childs = self::getChildCid($lists, $get['cid'], true);
- array_push($childs, $get['cid']);
- $where[] = ['cid', 'in', $childs];
- }
-
- $model = new File();
- return $model->field(true)
- ->where($where)
- ->order('id', 'desc')
- ->paginate([
- 'page' => $get['page'] ?? 1,
- 'list_rows' => $get['limit'] ?? 20,
- 'var_page' => 'page'
- ])->toArray();
-
- } catch (Exception $e) {
- return [];
- }
- }
-
-
-
- public static function getChildCid($lists, $cid, $clear)
- {
- static $temp = [];
- if($clear) {
- $temp = [];
- }
- foreach($lists as $item) {
- if($item['pid'] == $cid) {
- $temp[] = $item['id'];
- self::getChildCid($lists, $item['id'], false);
- }
- }
- return $temp;
- }
-
-
-
- public static function move($post)
- {
- try {
- $model = new File();
-
-
-
-
-
-
- $model->whereIn('id', $post['file_ids'])
- ->update(['cid' => $post['cid']]);
-
- return true;
- } catch (Exception $e) {
- static::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
- public static function del($post)
- {
- try {
- $model = new File();
- $model->whereIn('id', $post['file_ids'])
- ->update(['del' => 1]);
-
- return true;
- } catch (Exception $e) {
- static::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
-
-
- public static function getCate($get)
- {
- try {
- $lists = FileCate::where([
- ['del', '=', 0],
- ['type', '=', $get['type']],
- ['shop_id', '=', $get['shop_id']],
- ])->order('id', 'asc')->order('sort', 'desc')->select();
- $tree = self::cateListToTree($lists, 0);
-
- $all = [
- 'id' => 0,
- 'field' => 'all',
- 'title' => '全部',
- 'children' => [],
- ];
-
- array_unshift($tree, $all);
- return $tree;
- } catch (Exception $e) {
- return [];
- }
- }
-
-
-
- public static function cateListToTree($lists, $pid = 0)
- {
- $tree = [];
- foreach ($lists as $k => $v) {
- if ($v['pid'] == $pid) {
- $temp['id'] = $v['id'];
- $temp['field'] = 'id';
- $temp['title'] = $v['name'];
- $temp['children'] = self::cateListToTree($lists, $v['id']);
- $temp['spread'] = true;
- $tree[] = $temp;
- }
- }
- return $tree;
- }
-
-
-
- public static function getCateById($id)
- {
- $model = new FileCate();
- return $model->field(true)
- ->findOrEmpty($id)->toArray();
- }
-
-
-
- public static function addCate($post)
- {
- try {
- if($post['pid'] == 0) {
- $level = 1;
- }else{
- $parent = FileCate::find($post['pid']);
- $level = $parent['level'] + 1;
- }
- if($post['sort'] < 0) {
- throw new \think\Exception('排序值不能为负数');
- }
- FileCate::create([
- 'name' => $post['name'],
- 'pid' => $post['pid'],
- 'sort' => $post['sort'],
- 'shop_id' => $post['shop_id'],
- 'level' => $level,
- 'type' => $post['type']
- ]);
-
- return true;
- } catch (Exception $e) {
- static::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
-
- public static function editCate($post)
- {
- try {
- self::checkEdit($post);
-
- FileCate::update([
- 'name' => $post['name'],
- 'pid' => $post['cid'],
- 'sort' => $post['sort'],
- 'update_time' => time()
- ], ['id'=>$post['id']]);
-
- self::updateLevel($post['id']);
-
- return true;
- } catch (Exception $e) {
- static::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
- public static function delCate($id)
- {
- try {
- self::checkDel($id);
-
- FileCate::update([
- 'del' => 1,
- 'update_time' => time()
- ], ['id'=>$id]);
-
- return true;
- } catch (Exception $e) {
- static::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
- public static function categoryToSelect($get)
- {
- try {
- $model = new FileCate();
- $lists = $model->field(true)
- ->where('del', '=', 0)
- ->where('type', '=', $get['type'])
- ->where('shop_id', '=', $get['shop_id'])
- ->order('id', 'asc')
- ->select();
-
- $tree = [];
- foreach ($lists as $val) {
-
- if ($val['pid'] != 0) {
- continue;
- }
-
- $tree[$val['id']] = "|----" . $val['name'];
- foreach ($lists as $val2) {
- if ($val2['pid'] == $val['id']) {
- $tree[$val2['id']] = "|--------" . $val2['name'];
- }
- }
-
- }
-
- return $tree;
- } catch (Exception $e) {
- return [];
- }
- }
-
- public static function categoryToSelectThree($get)
- {
- try {
- $model = new FileCate();
- $lists = $model->field(true)
- ->where('del', '=', 0)
- ->where('type', '=', $get['type'])
- ->where('shop_id', '=', $get['shop_id'])
- ->order('id', 'asc')
- ->select();
-
- $tree = [];
- foreach ($lists as $val) {
-
- if ($val['pid'] != 0) {
- continue;
- }
-
- $tree[$val['id']] = "|----" . $val['name'];
- foreach ($lists as $val2) {
- if ($val2['pid'] == $val['id']) {
- $tree[$val2['id']] = "|--------" . $val2['name'];
- foreach($lists as $val3) {
- if($val3['pid'] == $val2['id']) {
- $tree[$val3['id']] = "|------------" . $val3['name'];
- }
- }
- }
- }
-
- }
-
- return $tree;
- } catch (Exception $e) {
- return [];
- }
- }
-
-
-
- public static function checkEdit($post)
- {
- if(empty(trim($post['name']))) {
- throw new \think\Exception('分类名称不能为空');
- }
-
- if($post['id'] == $post['cid']) {
- throw new \think\Exception('上级不能是自己');
- }
-
- if($post['sort'] < 0) {
- throw new \think\Exception('排序值不能为负数');
- }
-
-
- $lists = FileCate::where(['del'=>0 ])->select();
- $lists = !empty($lists) ? $lists->toArray() : [];
- $childs = self::getChildCid($lists, $post['id'], true);
- if(in_array($post['cid'], $childs)) {
- throw new \think\Exception('上级不能自己的后代分类');
- }
-
-
- $level = self::calcLevel($post['id']);
- $parent = FileCate::find($post['cid']);
- if($level + $parent['level'] > 3) {
- throw new \think\Exception('分类不允许超过三级');
- }
- }
-
-
-
- public static function calcLevel($id)
- {
- $level = 1;
- $two_ids = FileCate::where(['pid' => $id, 'del' => 0])->column('id');
- if ($two_ids) {
- $level = 2;
- $three_ids = FileCate::where([
- ['del', '=', 0],
- ['pid', 'in', $two_ids]
- ])->column('id');
- if ($three_ids) $level = 3;
- }
- return $level;
- }
-
-
-
- public static function updateLevel($id)
- {
- $me = FileCate::find($id);
- if($me['pid'] == 0) {
- FileCate::update([
- 'id' => $id,
- 'level' => 1,
- 'update_time' => time()
- ]);
- $two_ids = FileCate::where([
- 'pid' => $id,
- 'del' => 0
- ])->column('id');
- if($two_ids) {
- FileCate::where('id', 'in', $two_ids)->update([
- 'level' => 2,
- 'update_time' => time()
- ]);
- $three_ids = FileCate::where([
- ['pid', 'in', $two_ids],
- ['del', '=', 0]
- ])->column('id');
- if($three_ids) {
- FileCate::where('id', 'in', $three_ids)->update([
- 'level' => 3,
- 'update_time' => time()
- ]);
- }
- }
- }else{
- $parent = FileCate::find($me['pid']);
- if($parent['level'] == 1) {
- FileCate::update([
- 'id' => $id,
- 'level' => 2,
- 'update_time' => time()
- ]);
- $three_ids = FileCate::where([
- 'pid' => $id,
- 'del' => 0
- ])->column('id');
- if($three_ids) {
- FileCate::where('id', 'in', $three_ids)->update([
- 'level' => 3,
- 'update_time' => time()
- ]);
- }
- }else if($parent['level'] == 2){
- FileCate::update([
- 'id' => $id,
- 'level' => 3,
- 'update_time' => time()
- ]);
- }
-
- }
- }
-
-
-
- public static function checkDel($id)
- {
- $file = File::where([
- 'cid' => $id,
- 'del' => 0
- ])->findOrEmpty();
- if(!$file->isEmpty()) {
- throw new \think\Exception('有文件正在使用当前分类,不允许删除');
- }
- $son = FileCate::where([
- 'del' => 0,
- 'pid' => $id
- ])->findOrEmpty();
- if(!$son->isEmpty()){
- throw new \think\Exception('分类下还有子分类,不允许删除');
- }
- }
- }
|