20], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => '主键', 'name' => '分类key', 'pid' => '父级ID', 'title' => '中文名', 'is_list' => '是否是列表数据', 'remark' => '注意事项', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } /** * 获取分类名称 * @param $mall_id * @return array */ public static function getCategoryName($is_format = true) { $data = []; $field = "id,name,title,remark"; $list = self::find()->select($field)->where(['status' => StatusEnum::ENABLED])->asArray()->all(); if ($is_format) { foreach ($list as $item) { $data[$item['id']] = $item['title']; } } else { $data = $list; } return $data; } /** * 获取树形结构 * @Author: lun * @DateTime: 2021/10/13 0013 9:17 * @Copyright: copyright (c) 2021 广东七件事集团 * @param array $menu * @param int $pid * @return array */ public static function getTreeMenu(array $menu, int $pid = 0) { $tree = []; foreach ($menu as $k => $v) { $v['value'] = $v['id']; $v['label'] = $v['title']; if ($v['pid'] == $pid) { $tmp = $v; // 当前路由的菜单被选中 $children = self::getTreeMenu($menu, $v['value']); $children && $tmp['children'] = $children; $tree[] = $tmp; } } return $tree; } /** * 保存数据 * @Author: lun * @DateTime: 2021/10/13 0013 18:56 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $params * @return bool */ public static function setData($params) { try{ if (!empty($params['id'])) { $model = self::findOne(['id' => $params['id'], 'status' => [StatusEnum::ENABLED]]); if (empty($model)) throw new \Exception('分类不存在或已删除'); } else { $model = new self(); $model->loadDefaultValues(); } $model->name = $params['name']; $model->title = $params['title']; $model->pid = $params['pid']; $model->is_list = $params['is_list']; $model->remark = $params['remark']; if(!$model->save(false)){ throw new \Exception($model->getErrorMessage()); } return true; }catch(\Exception $e){ self::$static_error = $e->getMessage(); return false; } } /** * 获取分类列表 * @Author: lun * @DateTime: 2021/10/13 0013 9:34 * @Copyright: copyright (c) 2021 广东七件事集团 * @param array $cond * @param array $with * @param bool $is_array * @param string $select * @return array|\yii\db\ActiveRecord[] */ public static function getLinkCategory(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); $data = $query->asArray($is_array)->all(); return $data; } }