redis->executeCommand('DEL', [$cacheKey]); $list = self::getAll($sjId); Yii::$app->redis->executeCommand('SET', [$cacheKey, json_encode($list)]); return $list; } /** * 取出所有分类 */ public static function getAll($sjId, $classify = null, $flag = null) { $list = self::getByCateogryId(0, $sjId, $classify, $flag); return $list; } /** * 根据分类id取子集分类 $flag false 默认不带键名 true 返回带键名的 */ public static function getByCateogryId($id, $sjId, $classify = null, $flag = null) { $condition = ['parentId' => $id, 'sjId' => $sjId]; if (isset($classify)) { $condition['classify'] = $classify; } $xhCategory = xhCategory::getAllByCondition($condition, 'inTurn asc'); $data = array(); if ($xhCategory) { foreach ($xhCategory as $k => $v) { $id = $v['id']; $name = $v['categoryName']; $arr = array( 'id' => $id, 'categoryName' => $name, 'classify' => $v['classify'], 'cover' => $v['cover'], 'description' => $v['description'], 'isShow' => $v['isShow'], 'child' => self::getByCateogryId($id, $sjId, $classify, $flag), ); if ($flag) { $data[$id] = $arr; } else { $data[] = $arr; } } } return $data; } /** * 取分类列表,没有层级关系 */ public static function getList($sjId) { $preKey = dict::getCacheKey('categoryList'); $cacheKey = $preKey . $sjId; $string = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($string)) { $arr = json_decode($string, true); return $arr; } $category = xhCategory::getAllByCondition(['sjId' => $sjId]); $list = []; if (!empty($category)) { foreach ($category as $key => $val) { $list[$val['id']] = $val['categoryName']; } } $string = json_encode($list); Yii::$app->redis->executeCommand('SET', [$cacheKey, $string]); return $list; } /** * 添加分类 */ public static function addCategory($data, $parentId, $sjId) { $c = xhCategory::getByCondition(['sjId' => $sjId, 'parentId' => $parentId], 'inTurn desc'); $inTurn = !empty($c) ? $c['inTurn'] + 1 : 0;//排到最后面 $data['inTurn'] = $inTurn; $data['showName'] = $data['categoryName']; $data['sjId'] = $sjId; $category = xhCategory::add($data); $id = $category['id']; $cacheKey = dict::getCacheKey('category') . $id; $params = [$cacheKey]; $params[] = 'info_already_get_by_sql'; $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过 foreach ($category as $key => $val) { $params[] = $key; $params[] = $val; } Yii::$app->redis->executeCommand('HMSET', $params);//添加到缓存 self::refreshMenu($sjId);//更新菜单 return $category; } /** * 修改分类 * @param $id * @param $data * @param $sjId */ public static function updateById($id, $data, $sjId) { xhCategory::updateById($id, $data); $cacheKey = dict::getCacheKey('category') . $id; $params = [$cacheKey]; foreach ($data as $key => $val) { $params[] = $key; $params[] = $val; } Yii::$app->redis->executeCommand('HMSET', $params);//更新缓存 self::refreshMenu($sjId);//更新菜单 } public static function deleteById($id, $sjId) { xhCategory::deleteById($id); $cacheKey = dict::getCacheKey('category') . $id; Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//删除缓存 $cacheKey = dict::getCacheKey('categoryGoodsList') . $id; Yii::$app->redis->executeCommand('DEL', [$cacheKey]);//删除 分类下的商品 的缓存 self::refreshMenu($sjId);//刷新菜单缓存 } public static function getById($id) { $category = xhCategory::find()->where(['id' => $id])->asArray()->one(); return $category; } //取出要显示的菜单分类 public static function getShowList($sjId) { $data = xhCategory::find()->where(['sjId' => $sjId, 'isShow' => 1])->limit(8)->orderBy('inTurn desc')->asArray()->all(); return $data; } }