45], [['pic', 'big_pic'], 'string', 'max' => 255], [['advert_open_type'], 'string', 'max' => 65], [['collect_service_fee',], 'number'], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'ID', 'mall_id' => 'Mall ID', 'parent_id' => '父级ID', 'name' => '分类名称', 'pic' => 'Pic', 'big_pic' => 'Big Pic', 'advert_pic' => '广告图片', 'advert_url' => '广告链接', 'advert_open_type' => '打开方式', 'advert_params' => '导航参数', 'is_show' => '是否显示', 'status' => '状态[0禁用 1启用 -1删除]', 'sort' => '排序', 'created_at' => '创建时间', 'updated_at' => '修改时间', 'is_collect_service_fee' => '', 'collect_service_fee' => '', ]; } public static function getCates(array $cond = [], array $with = [], bool $is_array = true, string $select = '*') { $default_cond = [['<>', 'status', StatusEnum::DELETE]]; $cond = array_merge($default_cond, $cond); $query = self::find()->select($select); self::paramPack(['where' => $cond, 'with' => $with], $query); $cates = $query->asArray($is_array)->all(); return $cates; } /** * 获取树状分类 * @Author ewgof * @DateTime 2021-08-26 13:00:40 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $cates * @param integer $parent_id * @return void */ public static function getTreeCate(array $cates, int $parent_id = 0) { $tree = []; foreach ($cates as $k => $v) { $v['value'] = $v['id']; $v['label'] = $v['name']; if ($v['parent_id'] == $parent_id) { $tmp = $v; // 当前路由的菜单被选中 $children = self::getTreeCate($cates, $v['id']); $children && $tmp['children'] = $children; $tree[] = $tmp; } } return $tree; } // 关联模型 public function getParent() { return $this->hasOne(self::class, ['id' => 'parent_id'])->andWhere(['<>', 'status', StatusEnum::DELETE]); } public function getChild() { return $this->hasMany(self::class, ['parent_id' => 'id'])->andWhere(['<>', 'status', StatusEnum::DELETE])->orderBy(['sort' => SORT_ASC, 'created_at' => SORT_DESC]); } public static function getAllParentName($id, array $categorys,$mall_id) { if (empty($categorys)) { $categorys = StoreCate::find() ->select('id,name,parent_id') ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]) ->asArray() ->all(); } $category_array = array_column($categorys, null, 'id'); $name = $category_array[$id]['name']; if($category_array[$id]['parent_id']){ $name = self::getAllParentName($category_array[$id]['parent_id'], $categorys,$mall_id).'/'.$name; } return $name; } /** * 排序 * @Author ewgof * @DateTime 2021-11-12 10:16:01 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $post * @return void */ public static function sort(array $post) { try { $first_list = json_decode($post['first_list'], true); $second_list = json_decode($post['second_list'], true); $third_list = json_decode($post['third_list'], true); $t = \Yii::$app->db->beginTransaction(); self::saveSort($first_list); self::saveSort($second_list); self::saveSort($third_list); $t->commit(); return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * 保存分类 * @Author ewgof * @DateTime 2021-11-12 10:18:43 * @copyright: Copyright (c) 2020 广东七件事集团 * @param [type] $sort_cates * @return void */ private static function saveSort($sort_cates) { foreach ($sort_cates as $index => $cate) { $model = self::findOne($cate['id']); if (!$model) throw new \Exception('分类不存在'); $model->sort = $index; $res = $model->save(); if (!$res) throw new \Exception($model->getErrorMessage()); } } /** * 保存数据 * @Author ewgof * @DateTime 2021-08-26 14:16:24 * @copyright: Copyright (c) 2020 广东七件事集团 * @param array $params * @return object|boolean */ public static function setData(array $params) { try { if (!empty($params['id'])) { $model = self::findOne(['and', ['id' => $params['id'], 'mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]]); if (empty($model)) throw new \Exception('分类不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); $model->mall_id = $params['mall_id']; $model->parent_id = $params['parent_id'] ?? 0; } isset($params['advert_params']) && $params['advert_params'] = json_encode($params['advert_params']); if (!$model->load($params, '') || !$model->save()) { throw new \Exception($model->getErrorMessage()); } return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } /** * 获取树状菜单 * @Author bing * @DateTime 2021-08-10 18:22:16 * @param array $menu 菜单 * @param integer $parentId * @param string $app_id APP_ID * @return array * @copyright: Copyright (c) 2020 广东七件事集团 */ public static function getTreeMenu(array $menu, int $pid = 0) { $tree = []; foreach ($menu as $k => $v) { $v['value'] = $v['id']; if ($v['parent_id'] == $pid) { $tmp = $v; $children = self::getTreeMenu($menu, $v['id']); $children && $tmp['children'] = $children; $tree[] = $tmp; } } return $tree; } public static function getCateListInDiy(int $mall_id, array $params) { $wheres[] = ['mall_id' => $mall_id, 'parent_id' => 0, 'status' => StatusEnum::ENABLED]; if (!empty($params['keyword'])) { $wheres[] = ['like', 'name', $params['keyword']]; } $params = [ 'where' => $wheres, 'order' => 'sort asc,created_at desc', 'page' => $params['page'] ?? 1, 'limit' => $params['limit'] ?? 15, ]; $list = self::listPage($params); if (false === $list) { return self::$static_error; } foreach ($list['list'] as &$lv) { $lv['title'] = $lv['name']; } return $list; } public static function getRecommendCateListInDiy(int $mall_id, array $params) { $wheres[] = ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]; if (!empty($params['keyword'])) { $wheres[] = ['like', 'name', $params['keyword']]; } $params = [ 'where' => $wheres, 'order' => 'sort asc,created_at desc', 'page' => $params['page'] ?? 1, 'limit' => $params['limit'] ?? 15, ]; $list = self::listPage($params); if (false === $list) { return self::$static_error; } foreach ($list['list'] as &$lv) { $lv['title'] = $lv['name']; } return $list; } }