diff --git a/app/admin/controller/merchant/system/merchant/MerchantType.php b/app/admin/controller/merchant/system/merchant/MerchantType.php index c0239c9..5bdfa13 100644 --- a/app/admin/controller/merchant/system/merchant/MerchantType.php +++ b/app/admin/controller/merchant/system/merchant/MerchantType.php @@ -16,6 +16,8 @@ use app\common\model\merchant\system\merchant\MerchantType as MerchantTypeModel; use app\common\model\merchant\system\auth\Menu as MenuModel; use app\common\controller\FormatList; use think\facade\View; +use think\exception\ValidateException; +use app\validate\merchant\admin\MerchantTypeValidate; /** * 店铺类型管理类 @@ -31,7 +33,8 @@ class MerchantType extends BaseController $this->path = [ 'index' => 'merchant/system/merchant/type/index', 'read' => 'merchant/system/merchant/type/read', - 'add' => 'merchant/system/merchant/type/add' + 'add' => 'merchant/system/merchant/type/add', + 'edit' => 'merchant/system/merchant/type/edit' ]; } @@ -54,6 +57,7 @@ class MerchantType extends BaseController if (!empty($id)) { // 进入编辑页,显示店铺数据 $detail = $this->merchant->Find($id); + View::assign('id', $id); } if (empty($detail)) { $detail = [ @@ -69,12 +73,25 @@ class MerchantType extends BaseController ]; } - // 查出商户所有菜单数据 - $data = $menu->Search([], 1); + // 查出商户所有菜单数据权限 tree + $data = $menu->getList([],1); + + // 获取已有权限 + $checked_list = $this->merchant->getList(['mer_type_id'=>$id]); + + // 给菜单权限附加选中状态 + foreach($data['list'] as $k=>$item) { + if (in_array($item['id'], $checked_list['list'][0]['auth_ids'])) { + $data['list'][$k]['checked'] = true; + } + } + View::assign('dataTree', $format->FormatCategory($data['list'])); View::assign('detail', $detail); - return view($this->path['add']); + $path = empty($id)?'add':'edit'; + + return View($this->path[$path]); } /** @@ -88,9 +105,9 @@ class MerchantType extends BaseController $page = empty($param['page']) ? 1 : (int)$param['page']; $limit = empty($param['limit']) ? 10 : (int)$param['limit']; - $data = $this->merchant->GetList($page, $limit); + $data = $this->merchant->getList([], $page, $limit); - return table_assign(0, '', $data); + return to_assign(0, '', $data); } /** @@ -116,7 +133,6 @@ class MerchantType extends BaseController $rows = $this->merchant->Add($batch); - return $rows>0?to_assign(0, '操作成功'):to_assign(1, '操作失败'); } @@ -149,21 +165,34 @@ class MerchantType extends BaseController */ public function Edit() { - $params = get_params(); + $id = (int)get_params('id'); + $data = get_params(['type_name', 'type_info', 'is_margin', 'margin', 'auth','description']); + $data['auth'] = explode(',',$data['auth']); - $batch['type_name'] = $params['type_name']; - $batch['description'] = $params['description']; - $batch['is_margin'] = $params['is_margin']; - $batch['margin'] = $params['margin']; - $batch['type_info'] = $params['type_info']; - $menu_ids = $params['auth']; + // 检查id是否存在 + if (!$this->merchant->exist($id)) { + return to_assign(1,'数据不存在'); + } - // 更新权限 - // $this->menu->update($menu_ids); + // 更新信息 + $msg = $this->merchant->modify($id, $this->getValidParams($data)); - $rows = $this->merchant->Add($batch); + return $msg['code']==0?to_assign(0, '操作成功'):to_assign(1, $msg['msg']); + } - return $rows>0?to_assign(0, '操作成功'):to_assign(1, '操作失败'); + /** + * 参数验证 + */ + protected function getValidParams($data) + { + $validate = app()->make(MerchantTypeValidate::class); + $validate->check($data); + if ($data['is_margin'] == 1) { + if ($data['margin'] <= 0) throw new ValidateException('保证金必须大于0'); + } else { + $data['margin'] = 0; + } + return $data; } /** @@ -180,16 +209,16 @@ class MerchantType extends BaseController } $id = (int)$param['id']; + if (!$this->merchant->exist($id)) { + return to_assign(1, '数据不存在'); + } + $rows = $this->merchant->Del($id); return $rows>0?to_assign(0, '操作成功'):to_assign(1, '操作失败'); } - /** - * 备注表单页 - */ - /** * 店铺类型备注 */ diff --git a/app/admin/model/EbStoreProduct.php b/app/admin/model/EbStoreProduct.php index 8365c20..7ed73f9 100644 --- a/app/admin/model/EbStoreProduct.php +++ b/app/admin/model/EbStoreProduct.php @@ -1,13 +1,11 @@ append(['us_status']); - $list = hasMany( + $list = $this->searchList( $data, 'mer_labels', ProductLabel::class, @@ -203,50 +201,7 @@ class EbStoreProduct extends Model return StoreCategory::whereLike('path', '%/' . $id . '/%')->column('store_category_id'); } - protected function hasMany1($collection, $field, $model, $searchKey, $insertKey, $where = [] ,$select = '*') - { - $ids = []; - $link = []; - - if (!$collection) return []; - $collection = $collection->toArray(); - foreach ($collection as $k => $item) { - if (is_array($item[$field])) { - $link[$k] = array_unique($item[$field]); - $ids = array_merge($item[$field], $ids); - } else { - $link[$k] = array_unique(explode(',', $item[$field])); - } - $ids = array_merge($link[$k], $ids); - if (isset($collection[$k][$insertKey])) unset($collection[$k][$insertKey]); - } - $ids = array_filter(array_unique($ids)); - if (!count($ids)) { - return $collection; - } - $many = $model::whereIn($searchKey, array_unique($ids))->where($where)->field($select)->select(); - - if (!$many) return $collection; - $many = $many->toArray(); - foreach ($link as $k => $val) { - foreach ($many as $item) { - if (in_array($item[$searchKey], $val)) { - - if (!isset($collection[$k][$insertKey])) $collection[$k][$insertKey] = []; - - $collection[$k][$insertKey][] = $item; - } - } - } - - return $collection; - } -} - - - -if (!function_exists('hasMany')) { - function hasMany($collection, $field, $model, $searchKey, $insertKey, $where = [] ,$select = '*') + protected function searchList($collection, $field, $model, $searchKey, $insertKey, $where = [] ,$select = '*') { $ids = []; $link = []; diff --git a/app/admin/view/merchant/system/merchant/type/add.html b/app/admin/view/merchant/system/merchant/type/add.html index e2ecbc6..1c3ab9d 100644 --- a/app/admin/view/merchant/system/merchant/type/add.html +++ b/app/admin/view/merchant/system/merchant/type/add.html @@ -105,14 +105,6 @@ ,util = layui.util //模拟数据 ,data = getData(); - // [{ - // title: '一级1' - // ,id: 1 - // ,field: 'name1' - // ,checked: true - // ,spread: true - // ,children: [{}] - // }] //开启复选框 tree.render({ @@ -170,31 +162,29 @@ } - //获取店铺权限的数据 + //获取店铺权限的数据[数据来自模板变量] function getData(){ /* 初始化页面渲染时传过来的js变量 */ - let dataContainerElem = document.querySelector('.data-box'), - - data = dataContainerElem ? dataContainerElem.dataset : {}, - dataBox = {}; //模板变量容器,`.data-box`类选择器所在的所有`data`属性值集合 + let dataContainerElem = document.querySelector('.data-box'), data = dataContainerElem ? dataContainerElem.dataset : {}, dataBox = {}; //模板变量容器,`.data-box`类选择器所在的所有`data`属性值集合 Object.keys(data).forEach(function (key) { dataBox[key] = data[key]; - if (isJsonString(data[key])) dataBox[key] = JSON.parse(data[key]); //是json格式的字串才转对象 - }); + if (isJsonString(data[key])) + dataBox[key] = JSON.parse(data[key]); //是json格式的字串才转对象 + }); /** * 判断字串是否属于json字串 */ - function isJsonString(str) { + function isJsonString(str) { let flag = false; - if (typeof str != 'string') return flag; - try { JSON.parse(str); flag = true; - } catch (e) {} + } catch (e) { + console.log(e) + } return flag; } diff --git a/app/admin/view/merchant/system/merchant/type/edit.html b/app/admin/view/merchant/system/merchant/type/edit.html new file mode 100644 index 0000000..4d26c5c --- /dev/null +++ b/app/admin/view/merchant/system/merchant/type/edit.html @@ -0,0 +1,201 @@ +{extend name="common/base"/} +{block name="style"} + +{/block} + +{block name="body"} +
+

编辑店铺类型

+ + + + + + + + + + + + + + + + + + + + + + + + +
店铺类型名称* + +
店铺类型要求 + +
店铺保证金 + + + + + + + + + +
+
+ + +
+
+ + 单位:元
+
店铺权限* +
+
+
其它说明 + +
+ +
+ + + +
+
+{/block} + + + +{block name="script"} + + + +{/block} + diff --git a/app/admin/view/merchant/system/merchant/type/index.html b/app/admin/view/merchant/system/merchant/type/index.html index c5a0624..59a1285 100644 --- a/app/admin/view/merchant/system/merchant/type/index.html +++ b/app/admin/view/merchant/system/merchant/type/index.html @@ -1,7 +1,6 @@ {extend name="common/base"/} {block name="body"} -
@@ -33,9 +32,20 @@ title: '商品表列表', toolbar: '#toolbarDemo', url: '/admin/merchant/type/lst', + parseData: function(res){ //res 即为原始返回的数据 + return { + "code": res.code, //解析接口状态 + "msg": res.msg, //解析提示文本 + "count": res.data.count, //解析数据长度 + "data": res.data.list //解析数据列表 + }; + }, + done: function (res, curr, count){ + $('table').width('100%'); + }, page: true, limit: 20, - cellMinWidth: 300, + cellMinWidth: 100, cols: [ [ { @@ -49,12 +59,12 @@ field: 'type_name', title: '店铺类型名称', align: 'center', - width: 200 + width: 180 },{ field: 'margin', title: '店铺保证金', align: 'center', - width: 100 + width: 150 },{ field: 'type_info', title: '店铺类型要求', @@ -64,19 +74,19 @@ field: 'create_time', title: '创建时间', align: 'center', - width: 150 + width: 200 },{ field: 'update_time', title: '最近修改时问', align: 'center', - width: 150 + width: 200 }, { fixed: 'right', field: 'right', title: '操作', toolbar: '#barDemo', - width: 190, + width: 180, align: 'center' } ] diff --git a/app/common/controller/FormatList.php b/app/common/controller/FormatList.php index 991e56b..d7bd668 100644 --- a/app/common/controller/FormatList.php +++ b/app/common/controller/FormatList.php @@ -24,6 +24,8 @@ class FormatList */ static function FormatCategory(array $data, string $idName = "id", string $parentId = 'pid', string $fieldName='name', $childrenKey = 'children', ?string $addId='', ?string $addtitle='') { + if (empty($data))return []; + $items = []; foreach ($data as $item) { if (!empty($addId)) diff --git a/app/common/model/merchant/system/Relevance.php b/app/common/model/merchant/system/Relevance.php index e2681b9..af30c35 100644 --- a/app/common/model/merchant/system/Relevance.php +++ b/app/common/model/merchant/system/Relevance.php @@ -9,39 +9,47 @@ namespace app\common\model\merchant\system; use app\common\model\merchant\store\product\Spu; -use think\Model; use app\common\model\merchant\community\Community; use app\common\model\merchant\store\StoreCategory; use app\common\model\merchant\system\auth\Menu; use app\common\model\merchant\system\merchant\Merchant; use app\common\model\merchant\user\User; +use think\Model; +use think\exception\ValidateException; class Relevance extends Model { - protected $connetion = 'shop'; + protected $connection = 'shop'; + protected $table = 'eb_relevance'; + protected $pk = 'relevance_id'; - /** - * TODO - * @return string - * @author Qinii - * @day 10/26/21 - */ - public static function tablePk(): string - { - return 'relevance_id'; - } + //文章关联商品 + const TYPE_COMMUNITY_PRODUCT = 'community_product'; + //社区关注 + const TYPE_COMMUNITY_FANS = 'fans'; + //社区文章点赞 + const TYPE_COMMUNITY_START = 'community_start'; + //社区评论点赞 + const TYPE_COMMUNITY_REPLY_START = 'community_reply_start'; + //商户权限 + const TYPE_MERCHANT_AUTH = 'mer_auth'; - /** - * TODO - * @return string - * @author Qinii - * @day 10/26/21 - */ - public static function tableName(): string - { - return 'relevance'; - } + //指定范围类型 + //0全部商品 + const TYPE_ALL = 'scope_type'; + //指定商品 + const SCOPE_TYPE_PRODUCT = 'scope_type_product'; + //指定分类 + const SCOPE_TYPE_CATEGORY = 'scope_type_category'; + //指定商户 + const SCOPE_TYPE_STORE = 'scope_type_store'; + //价格说明关联分类 + const PRICE_RULE_CATEGORY = 'price_rule_category'; + //商品参数关联 + const PRODUCT_PARAMES_CATE = 'product_params_cate'; + + public function fans() { return $this->hasOne(User::class,'uid','left_id'); @@ -101,4 +109,285 @@ class Relevance extends Model $query->where('type', $value); } + /** + * @return static + */ + public static function getInstance(): self + { + return new static(); + } + + /** + * TODO 搜索 + * @param $where + * @return BaseModel + * @author Qinii + * @day 2020-10-16 + */ + protected function getSearch(array $where) + { + foreach ($where as $key => $item) { + if ($item !== '') { + $keyArray[] = $key; + $whereArr[$key] = $item; + } + } + if(empty($keyArray)){ + return self::getInstance()->db(); + }else{ + return self::withSearch($keyArray, $whereArr); + } + } + + /** + * TODO 添加 + * @param int $leftId + * @param int $rightId + * @param string $type + * @param $check + * @return bool + * @author Qinii + * @day 10/28/21 + */ + public function insert(int $leftId, int $rightId, string $type, bool $check = false) + { + if ($check && $this->checkHas($leftId, $rightId, $type)) { + return false; + } + + $data = [ + 'left_id' => $leftId, + 'right_id'=> $rightId, + 'type' => $type, + ]; + + try{ + Relevance::create($data); + return true; + } catch (\Exception $exception) { + throw new ValidateException('创建失败'); + } + } + + /** + * TODO 删除 + * @param int $leftId + * @param string $type + * @param int $rightId + * @return bool + * @author Qinii + * @day 10/28/21 + */ + public function destory(int $leftId, int $rightId, string $type) + { + return $this->getSearch([ + 'left_id' => $leftId, + 'right_id'=> $rightId, + 'type' => $type, + ])->delete(); + } + + /** + * TODO 检测是否存在 + * @param int $leftId + * @param int $rightId + * @param string $type + * @return int + * @author Qinii + * @day 10/28/21 + */ + public function checkHas(int $leftId, int $rightId, string $type) + { + return $this->getSearch([ + 'left_id' => $leftId, + 'right_id'=> $rightId, + 'type' => $type, + ])->count(); + } + + /** + * TODO 根据左键批量删除 + * @param int $leftId + * @param $type + * @return bool + * @author Qinii + * @day 10/28/21 + */ + public function batchDelete(int $leftId, $type) + { + return $this->getSearch([ + 'left_id' => $leftId, + 'type' => $type, + ])->delete(); + } + + /** + * TODO 关注我的人 + * @param int $uid + * @return \think\Collection + * @author Qinii + * @day 10/28/21 + */ + public function getUserFans(int $uid, int $page, int $limit) + { + $query = $this->getSearch([ + 'right_id' => $uid, + 'type' => self::TYPE_COMMUNITY_FANS, + ])->with([ + 'fans' => function($query) { + $query->field('uid,avatar,nickname,count_fans'); + } + ]); + $count = $query->count(); + $list = $query->page($page, $limit)->select()->append(['is_start']); + return compact('count','list'); + } + + /** + * TODO 我关注的人 + * @param $uid + * @return \think\Collection + * @author Qinii + * @day 10/28/21 + */ + public function getUserFocus(int $uid, int $page, int $limit) + { + $query = $this->getSearch([ + 'left_id' => $uid, + 'type' => self::TYPE_COMMUNITY_FANS, + ])->with([ + 'focus' => function($query) { + $query->field('uid,avatar,nickname,count_fans'); + } + ]); + $count = $query->count(); + $list = $query->page($page, $limit)->select()->append(['is_fans']); + return compact('count','list'); + } + + + /** + * TODO 我点赞过的文章 + * @param int $uid + * @return \think\Collection + * @author Qinii + * @day 10/28/21 + */ + public function getUserStartCommunity(array $where, int $page, int $limit) + { + $query = $this->joinUser($where)->with([ + 'community'=> function($query) use($where){ + $query->with([ + 'author' => function($query){ + $query->field('uid,real_name,status,avatar,nickname,count_start'); + }, + 'is_start' => function($query) use ($where) { + $query->where('left_id',$where['uid']); + }, + 'topic' => function($query) { + $query->where('status', 1)->where('is_del',0); + $query->field('topic_id,topic_name,status,category_id,pic,is_del'); + }, + 'relevance' => [ + 'spu' => function($query) { + $query->field('spu_id,store_name,image,price,product_type,activity_id,product_id'); + } + ], + 'is_fans' => function($query) use($where){ + $query->where('left_id',$where['uid']); + }]); + }, + ]); + $count = $query->count(); + $list = $query->page($page, $limit)->select()->each(function ($item){ + $item['time'] = date('m月d日', strtotime($item['create_time'])); + return $item; + }); + + return compact('count','list'); + } + + /** + * TODO 我点赞过的文章 + * @param int $uid + * @return \think\Collection + * @author Qinii + * @day 10/28/21 + */ + public function getUserStartCommunityByVideos(array $where, int $page, int $limit) + { + $query = $this->joinUser($where)->with([ + 'community'=> function($query) { + $query->with(['author'=> function($query) { + $query->field('uid,avatar,nickname'); + }]); + }, + ]); + $count = $query->count(); + $list = $query->page($page, $limit)->select()->each(function ($item){ + $item['time'] = date('m月d日', strtotime($item['create_time'])); + return $item; + }); + + return compact('count','list'); + } + + public function getFieldCount(string $field, int $value, string $type) + { + return $this->getSearch([$field => $value, 'type' => $type,])->count(); + } + + /** + * + */ + public function createMany(int $leftId, array $rightId, string $type) + { + if (!empty($rightId)) { + foreach ($rightId as $value) { + $res[] = [ + 'left_id' => $leftId, + 'right_id' => $value, + 'type' => $type, + ]; + } + self::batchInsert($res); + } + } + + /** + * 批量插入 + * + * @param array $data + * @return int + * @author xaboy + * @day 2020/6/8 + */ + public function batchInsert(array $data) + { + return Relevance::insertAll($data); + } + + + // ------- 2 --------------- + public function clear(int $id, $type, string $field) + { + if (is_string($type)) $type = [$type]; + return Relevance::where($field, $id)->whereIn('type', $type)->delete(); + } + + + public function joinUser($where) + { + $query = Relevance::hasWhere('community',function($query) use($where){ + $query->where('status',1)->where('is_show',1)->where('is_del',0); + $query->when(isset($where['is_type']) && $where['is_type'] !== '',function($query) use($where){ + $query->where('is_type',$where['is_type']); + }); + }); + + $query->where('left_id',$where['uid'])->where('type',self::TYPE_COMMUNITY_START); + return $query; + } + //-------------------------- + } diff --git a/app/common/model/merchant/system/auth/Menu.php b/app/common/model/merchant/system/auth/Menu.php index 82aaff3..6ae1761 100644 --- a/app/common/model/merchant/system/auth/Menu.php +++ b/app/common/model/merchant/system/auth/Menu.php @@ -10,6 +10,7 @@ declare (strict_types = 1); namespace app\common\model\merchant\system\auth; +use think\db\BaseQuery; use think\Model; use think\exception\ValidateException; @@ -18,11 +19,33 @@ use think\exception\ValidateException; */ class Menu extends Model { - // 指定连接到商城库 protected $connection = 'shop'; - - // 指定此model关联表 protected $table = 'eb_system_menu'; + protected $pk = 'menu_id'; + + + /** + * 获取权限列表 + */ + public function getList(array $where, int $is_mer=2, ?int $page=NULL, ?int $limit=NULL) + { + $query = $this->search($where, $is_mer); + + // 查询记录总行数 + $count = $query->count(); + if (isset($page) && isset($limit)) { + $query->page($page)->limit($limit); + } + + $list = $query->order('sort DESC,menu_id ASC')->hidden(['update_time', 'path'])->select() + ->toArray(); + + + // 合并为一个数组并返回 + $data = compact('count', 'list'); + + return $data; + } /** * 按条件获取商户菜单数据 @@ -32,27 +55,28 @@ class Menu extends Model * * @return array */ - function Search(array $where=[], int $is_mer=2) + protected function search(array $where=[], int $is_mer=0) { // 按条件 拼接 select 前sql $query = self::where('is_mer', $is_mer) - ->field('menu_id as id,pid, sort, route as src,icon,menu_name as title,is_show as status, is_menu as menu') - ->order('sort DESC,menu_id ASC'); - if (isset($where['pid'])) $query->where('pid', (int)$where['pid']); - if (isset($where['keyword'])) $query->whereLike('menu_name|route', "%{$where['keyword']}%"); - if (isset($where['is_menu'])) $query->where('is_menu', (int)$where['is_menu']); + ->when(isset($where['pid'])&& $where['pid']!=='', + function($query)use($where){ + $query->where('pid', (int)$where['pid']); + } + ) + ->when(isset($where['keyword'])&&$where['keyword']!=='', + function ($query)use($where){ + $query->whereLike('menu_name|route', "%{$where['keyword']}%"); + } + ) + ->when(isset($where['is_menu'])&& $where['is_menu']!=='', + function($query)use($where){ + $query->where('is_menu', (int)$where['is_menu']); + } + ) + ->field('menu_id as id,pid, sort, route as src,icon,menu_name as title,is_show as status, is_menu as menu'); - // 查询记录总行数 - $count = $query->count(); - - if (isset($where['offset'])) $query->page($where['offset'])->limit($where['limit']); - - // 隐藏指定字段 - $list = $query->hidden(['update_time', 'path'])->select()->toArray(); - // 合并为一个数组并返回 - $data = compact('count', 'list'); - - return $data; + return $query; } /** diff --git a/app/common/model/merchant/system/merchant/Merchant.php b/app/common/model/merchant/system/merchant/Merchant.php index 0c2ae27..c08583f 100644 --- a/app/common/model/merchant/system/merchant/Merchant.php +++ b/app/common/model/merchant/system/merchant/Merchant.php @@ -132,6 +132,17 @@ class Merchant extends Model // return []; } + /** + * 更改商户表保证金标志 + */ + public function updateMargin($typeId, $margin, $is_margin) + { + return Merchant::where('type_id',$typeId)->where('is_margin','in',[0,1])->update([ + 'is_margin' => $is_margin, + 'margin' => $margin + ]); + } + /** * @param int $id * @return array|Model|null diff --git a/app/common/model/merchant/system/merchant/MerchantType.php b/app/common/model/merchant/system/merchant/MerchantType.php index 1891b74..0b3ba68 100644 --- a/app/common/model/merchant/system/merchant/MerchantType.php +++ b/app/common/model/merchant/system/merchant/MerchantType.php @@ -10,8 +10,14 @@ declare (strict_types = 1); namespace app\common\model\merchant\system\merchant; -use think\exception\ValidateException; + use think\Model; +use think\facade\Db; +use app\common\model\merchant\system\Relevance; +use Exception; +use think\exception\ValidateException; +use think\facade\Env; +use think\db\exception\DbException; /** * @mixin \think\Model @@ -20,7 +26,13 @@ class MerchantType extends Model { protected $connection = 'shop'; protected $table = 'eb_merchant_type'; + protected $pk = 'mer_type_id'; + // 权限关联表 + public function auth() + { + return $this->hasMany(Relevance::class, 'left_id', 'mer_type_id')->where('type', Relevance::TYPE_MERCHANT_AUTH); + } /** * @param int $id @@ -28,38 +40,54 @@ class MerchantType extends Model * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException - * @author xaboy - * @day 2020-03-27 */ public function get($id) { return self::find($id); } + /** * 查询店铺类型数据 * - * @param int $page 过滤字段条件 - * @param int $limit 菜单类型: 0商城平台菜单 1商户菜单 + * @param array $where 过滤字段条件 + * @param null|int $page 页码 + * @param null|int $limit 菜单类型: 0商城平台菜单 1商户菜单 * * @return array|object */ - function GetList(int $page, int $limit) + function getList($where=[], ?int $page=NULL, ?int $limit=NULL) { - $rows = empty($limit) ? get_config('app . page_size') : $limit; + $query = $this->search($where)->with(['auth']); + $count = $query->count(); + $list = $query->when(isset($page)&&$page!=''&& isset($limit) && $limit!='', + function($query)use($page,$limit){ + $query->page($page, $limit); + } + ) + ->order('mer_type_id DESC')->select(); + foreach ($list as $item){ + $item['auth_ids'] = array_column($item['auth']->toArray(), 'right_id'); + unset($item['auth']); + } - $data = self::field('mer_type_id as id,type_name,margin,is_margin,type_info,description,create_time,update_time') - ->order('mer_type_id DESC') - ->page($page) - ->limit($limit) - ->order('mer_type_id DESC')->paginate($rows, false,[ - 'query' => [], //url额外参数 - // 'total' => '', //最大数量 - // 'fragment' => '', //url锚点 - // 'var_page' => 'page', //分页变量 - ]); + return compact('count', 'list'); + } + + /** + * 查询店铺类型表 [全表扫描或指定id] + * @param array $where 条件 + * @return Query + */ + protected function search(array $where = []) + { + return MerchantType::when( + isset($where['mer_type_id']) && $where['mer_type_id'] !== '', + function($query) use($where){ + $query->where('mer_type_id',$where['mer_type_id']); + } + )->field('type_name,type_info,mer_type_id as id,mer_type_id,mark,margin,is_margin,description,update_time,create_time'); - return $data; } /** @@ -108,34 +136,71 @@ class MerchantType extends Model return $rows; } + /** * 更新指定店铺类型数据 * - * @param int $page 过滤字段条件 - * @param int $limit 菜单类型: 0商城平台菜单 1商户菜单 + * @param int $id 店铺ID + * @param array $data 更新数据 * * @return array|object */ - function Edit(int $id, array $set_data) { - if (empty($id) || empty($set_data['type_name'])) { - throw new ValidateException('未传递参数'); - return 0; + public function modify(int $id, array $data) + { + Db::startTrans(); + try{ + $auth = array_filter(array_unique($data['auth'])); + unset($data['auth']); + $inserts = []; + foreach ($auth as $aid) { + $inserts[] = [ + 'left_id' => $id, + 'right_id' => (int)$aid, + 'type' => Relevance::TYPE_MERCHANT_AUTH + ]; + } + $data['update_time'] = date('Y-m-d H:i:s',time()); + $b1 = $this->updateMarchantType($id, $data); + $make = app()->make(Relevance::class); + $b2 = $make->batchDelete($id, Relevance::TYPE_MERCHANT_AUTH); + $b3 = $make->batchInsert($inserts); + + //更新未交保证金的商户 + app()->make(Merchant::class)->updateMargin($id, $data['margin'], $data['is_margin']); + + if (empty($b1)) { + Db::rollback(); + throw new Exception('店铺表更新失败'); + }else if(empty($b2)){ + Db::rollback(); + throw new Exception('删除已有权限失败'); + }else if(empty($b3)){ + Db::rollback(); + throw new Exception('权限表添加失败'); + } + Db::commit(); + $res = ['code'=>0, 'msg'=>'操作成功']; + }catch(DbException $e){ + Db::rollback(); + $res = ['code'=>1, 'msg'=>$e->getMessage()]; } - - $batch['type_name'] = $set_data['type_name']; - $batch['description'] = empty($set_data['description'])?"":$set_data['description']; - $batch['is_margin'] = empty($set_data['is_margin']); - if (!empty($set_data['is_margin'])) { - $batch['margin'] = empty($set_data['margin'])?0:$set_data['margin']; - } - $batch['type_info'] = empty($set_data['type_info'])?'':$set_data['type_info']; - $batch['update_time'] = date('Y-m-d H:i:s',time()); - - $rows = self::update($batch)->where('mer_type_id', $id)->limit(1); - - return $rows; + return $res; } + /** + * 更新店铺类型表数据 + * + * @param int $id + * @param array $data + * @return int + * @throws DbException + */ + protected function updateMarchantType(int $id, array $data) + { + return self::where($this->getPk(), $id)->update($data); + } + + /** * 删除指定店铺类型数据 * @@ -149,10 +214,23 @@ class MerchantType extends Model throw new ValidateException('未传递参数'); return 0; } + + // return Db::transaction(function () use ($id) { + // $this->dao->delete($id); + // app()->make(Merchant::class)->clearTypeId($id); + // app()->make(Relevance::class)->batchDelete($id, Relevance::TYPE_MERCHANT_AUTH); + // }); + $rows = self::where('mer_type_id', $id)->delete(); return $rows; } - + /** + * 是否存在 + */ + public function exist(int $id) + { + return self::find($id)->count(); + } } diff --git a/app/validate/merchant/admin/MerchantApplyments.php b/app/validate/merchant/admin/MerchantApplyments.php new file mode 100644 index 0000000..4c19d90 --- /dev/null +++ b/app/validate/merchant/admin/MerchantApplyments.php @@ -0,0 +1,294 @@ + +// +---------------------------------------------------------------------- +namespace app\validate\merchant\admin; + + +use think\Validate; + +class MerchantApplymentsValidate extends Validate +{ + protected $failException = true; + + //2401:小微商户,指无营业执照的个人商家。 + //2500:个人卖家,指无营业执照,已持续从事电子商务经营活动满6个月,且期间经营收入累计超过20万元的个人商家。(若选择该主体,请在“补充说明”填写相关描述) + //4:个体工商户,营业执照上的主体类型一般为个体户、个体工商户、个体经营。 + //2:企业,营业执照上的主体类型一般为有限公司、有限责任公司。 + //3:党政、机关及事业单位,包括国内各级、各类政府机构、事业单位等(如:公安、党 团、司法、交通、旅游、工商税务、市政、医疗、教育、学校等机构)。 + //1708:其他组织,不属于企业、政府/事业单位的组织机构(如社会团体、民办非企业、基 金会),要求机构已办理组织机构代码证。 + + protected $rule = [ + 'organization_type|主体类型' => 'require|in:2,3,4,2401,2500,1708', + 'business_license_info|营业执照/登记证书信息' => 'checkBusinessInfo', +// 'organization_cert_info|组织机构代码证信息' => 'checkOrganization', + 'id_doc_type|证件类型' => 'require|in:1,2,3,4,5,6,7,8', + 'id_card_info|经营者/法人身份证信息' => 'checkIdCardInfo', + 'id_doc_info|经营者/法人身份证信息' => 'checkIdDocInfo', +// 'need_account_info|是否填写结算银行账户' => 'require|in:true,false', 废弃字段 + 'account_info|结算银行账户' => 'getAccountInfo', + 'contact_info|超级管理员信息' => 'getContactInfo', + 'sales_scene_info|店铺信息'=>'checkSalesSceneInfo', + 'merchant_shortname|商户简称' => 'require', + 'business_addition_desc' => 'checkBusinessAdditionDesc', + ]; + + /** + * TODO 营业执照/登记证书信息 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function checkBusinessInfo($item,$rule,$data) + { + if(!in_array($data['organization_type'],['2401','2500'])){ + if(empty($item)) return '营业执照/登记证书信息为空'; + + if(!isset($item['business_license_copy']) || empty($item['business_license_copy'])) return '证件扫描件为空'; + if(!isset($item['business_license_number']) || empty($item['business_license_number'])) return '证件注册号为空'; + if(!isset($item['merchant_name']) || empty($item['merchant_name'])) return '商户名称为空'; + if(!isset($item['legal_person']) || empty($item['legal_person'])) return '经营者/法定代表人姓名为空'; + + if(isset($item['business_time'])) { + $statr = $item['business_time'][0]; + $end = $item['business_time'][1]; + if ($end !== '长期') { + $statr = strtotime($statr); + $end = strtotime($end); + $t = $end - $statr; + if (($t / (3600 * 24)) <= 60) return '营业执照/登记证书有效期必须大于60天,即结束时间距当前时间需超过60天'; + } + } + + } + return true; + } + + /** + * TODO 组织机构代码证信息 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function checkOrganization($item,$rule,$data) + { + $len = strlen($data['business_license_info']['business_license_number']); + if(!in_array($data['organization_type'],['4','2401','2500']) && $len === 18){ + if(empty($item)) return '组织机构代码证信息为空'; + + if(!isset($item['organization_copy']) || empty($item['organization_copy'])) return '组织机构代码证照片为空'; + if(!isset($item['organization_number']) || empty($item['organization_number'])) return '组织机构代码为空'; + if(!isset($item['organization_time']) || empty($item['organization_time'])) return '组织机构代码有效期限为空'; + +// list($statr,$end) = explode(',',$item['organization_time']); + + $statr = $item['organization_time'][0]; + $end = $item['organization_time'][1]; + + if($end !== '长期') { + $statr = strtotime($statr); + $end = strtotime($end); + $t = $end - $statr; + if(($t/(3600 * 24)) <= 60) return '组织机构代码证有效期必须大于60天,即结束时间距当前时间需超过60天'; + } + } + return true; + } + + /** + * TODO 经营者/法人身份证信息/身份证 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function checkIdCardInfo($item,$rule,$data) + { + if($data['id_doc_type'] == 1){ + if(empty($item)) return '经营者/法人身份证信息为空'; + + if(!isset($item['id_card_copy']) || empty($item['id_card_copy'])) return '身份证人像面照片为空'; + if(!isset($item['id_card_national']) || empty($item['id_card_national'])) return '身份证国徽面照片为空'; + if(!isset($item['id_card_name']) || empty($item['id_card_name'])) return '身份证姓名为空'; + if(!isset($item['id_card_number']) || empty($item['id_card_number'])) return '身份证号码为空'; + if(!isset($item['id_card_valid_time_begin']) || empty($item['id_card_valid_time_begin'])) return '经营者/法人身份证信息身份证开始时间为空'; + if(!isset($item['id_card_valid_time']) || empty($item['id_card_valid_time'])) return '经营者/法人身份证信息身份证有效期限为空'; + + if($item['id_card_valid_time'] !== '长期') { + $statr = time(); + $end = strtotime($item['id_card_valid_time']); + $t = $end - $statr; + if(($t/(3600 * 24)) <= 60) return '经营者/法人身份证信息证件结束日期必须大于60天,即结束时间距当前时间需超过60天'; + if(strtotime($item['id_card_valid_time_begin']) >= strtotime($item['id_card_valid_time'])) return '经营者/法人身份证信息证件结束日期必须大于证件开始时间'; + } + if($data['organization_type'] === 2){ + if(!isset($item['id_card_address']) || empty($item['id_card_address'])) return '经营者/法人身份证信息身份证居住地址为空'; + } + }; + return true; + } + + /** + * TODO 经营者/法人身份证信息/通行证 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function checkIdDocInfo($item,$rule,$data) + { + if(in_array($data['organization_type'],['2401','2500']) && !empty($item)) return '小微/个人卖家可选证件类型:身份证'; + + if($data['id_doc_type'] !== 1){ + if(empty($item)) return '经营者/法人身份证信息为空'; + + if(!isset($item['id_doc_name']) || empty($item['id_doc_name'])) return '证件姓名为空'; + if(!isset($item['id_doc_number']) || empty($item['id_doc_number'])) return '证件号码为空'; + if(!isset($item['id_doc_copy']) || empty($item['id_doc_copy'])) return '经营者/法人其他类型证件信息证件正面照片为空'; + if($data['id_doc_type'] !== 2) //护照不需要传反面 + { + if(!isset($item['id_doc_copy_back']) || empty($item['id_doc_copy_back'])) return '经营者/法人其他类型证件信息证件反面照片为空'; + } + if(!isset($item['doc_period_begin']) || empty($item['doc_period_begin'])) return '经营者/法人其他类型证件信息证件有效期开始时间为空'; + if(!isset($item['doc_period_end']) || empty($item['doc_period_end'])) return '经营者/法人其他类型证件信息证件结束日期为空'; + + if($item['doc_period_end'] !== '长期') { + $statr = time(); + $end = strtotime($item['doc_period_end']); + $t = $end - $statr; + if(($t/(3600 * 24)) <= 60) return '经营者/法人其他类型证件信息证件结束日期必须大于60天,即结束时间距当前时间需超过60天'; + if(strtotime($item['doc_period_begin']) >= strtotime($item['doc_period_end'])) return '经营者/法人其他类型证件信息证件结束日期必须大于证件开始时间'; + if($data['organization_type'] === 2){ + if(!isset($item['id_doc_address']) || empty($item['id_doc_address'])) return '经营者/法人其他类型证件信息证件居住地址为空'; + } + } + } + + return true; + } + + /** + * TODO 结算银行账户 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function getAccountInfo($item,$rule,$data) + { +// if($data['need_account_info']){ + + if(empty($item)) return '结算银行账户信息为空'; + + if(!isset($item['bank_account_type']) || empty($item['bank_account_type'])) return '账户类型为空'; + if(!isset($item['account_bank']) || empty($item['account_bank'])) return '开户银行为空'; + if(!isset($item['account_name']) || empty($item['account_name'])) return '开户名称为空'; + if(!isset($item['bank_address_code']) || empty($item['bank_address_code'])) return '开户银行省市编码为空'; + if(!isset($item['account_number']) || empty($item['account_number'])) return '银行帐号为空'; + +// } + + return true; + } + + /** + * TODO 超级管理员信息 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function getContactInfo($item,$rule,$data) + { + + if(empty($item)) return '超级管理员信息信息为空'; + + if(!isset($item['contact_type']) || empty($item['contact_type'])) return '超级管理员类型为空'; + if(!isset($item['contact_name']) || empty($item['contact_name'])) return '超级管理员姓名为空'; + if(!isset($item['contact_id_card_number']) || empty($item['contact_id_card_number'])) return '超级管理员身份证件号码为空'; + if(!isset($item['mobile_phone']) || empty($item['mobile_phone'])) return '超级管理员手机为空'; + + if(!in_array($data['organization_type'],['2401','2500'])){ + if(!isset($item['contact_email']) || empty($item['contact_email'])) return '邮箱为空'; + } + + if($item['contact_type'] === 66) //当超级管理员类型为66(经办人时) + { + if(!isset($item['contact_id_doc_type']) || empty($item['contact_id_doc_type']) || !in_array($item['contact_id_doc_type'],[1,2,3,4,5,6,7,8])) return '超级管理员证件类型为空或不合法'; + if(!isset($item['contact_id_doc_copy']) || empty($item['contact_id_doc_copy'])) return '超级管理员信息证件正面照片为空'; + if($item['contact_id_doc_type'] !== 2) //护照不需要传反面 + { + if(!isset($item['contact_id_doc_copy_back']) || empty($item['contact_id_doc_copy_back'])) return '超级管理员信息证件反面照片为空'; + } + if(!isset($item['contact_id_doc_period_begin']) || empty($item['contact_id_doc_period_begin'])) return '超级管理员信息证件有效期开始时间为空'; + if(!isset($item['contact_id_doc_period_end']) || empty($item['contact_id_doc_period_end'])) return '超级管理员信息证件结束日期为空'; + + if($item['contact_id_doc_period_end'] !== '长期') { + $statr = time(); + $end = strtotime($item['contact_id_doc_period_end']); + $t = $end - $statr; + if(($t/(3600 * 24)) <= 60) return '超级管理员信息证件结束日期必须大于60天,即结束时间距当前时间需超过60天'; + if(strtotime($item['contact_id_doc_period_begin']) >= strtotime($item['contact_id_doc_period_end'])) return '超级管理员信息证件结束日期必须大于证件开始时间'; + } + if(!isset($item['business_authorization_letter']) || empty($item['business_authorization_letter'])) return '超级管理员信息业务办理授权函为空'; + } + + return true; + } + + /** + * TODO 店铺信息 + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/22/21 + */ + protected function checkSalesSceneInfo($item,$rule,$data) + { + if(empty($item)) return '店铺信息为空'; + + if(!isset($item['store_name']) || empty($item['store_name'])) return '店铺名称为空'; + + if(!isset($item['store_url']) && !isset($item['store_url'])) return '店铺链接和店铺二维码二选一'; + + return true; + } + + /** + * TODO 补充说明s + * @param $item + * @param $rule + * @param $data + * @return bool|string + * @author Qinii + * @day 6/24/21 + */ + protected function checkBusinessAdditionDesc($item,$rule,$data) + { + if($data['organization_type'] == 2500 && empty($item)) return '若主体为“个人卖家”:补充说明不能为空'; + return true; + } + +} diff --git a/app/validate/merchant/admin/MerchantTypeValidate.php b/app/validate/merchant/admin/MerchantTypeValidate.php new file mode 100644 index 0000000..e74f6bb --- /dev/null +++ b/app/validate/merchant/admin/MerchantTypeValidate.php @@ -0,0 +1,21 @@ + 'require|max:5', + 'type_info|店铺类型要求' => 'max:256', + 'is_margin|是否有保证金' => 'require|in:0,1', + 'auth|权限' => 'require|array|min:1', + 'margin|保证金(¥)' => 'requireIf:is_margin,1', + 'description|其他说明' => 'max:256', + ]; +}