* @version : HUOSDK 8.0 */ namespace huo\controller\shop; use huo\controller\common\Base; use huo\logic\shop\GoodsLogic; use huo\model\shop\GoodsModel; use think\Cache; class GoodsCache extends Base { public static function ins() { return new static(); } /** * 获取礼包KEY * * @param string $goods_id * * @return string */ private function getGoodsKey($goods_id) { return 'goods_id_key_'.$goods_id; } /** * 通过商品ID 查询商品信息 * * @param string $goods_id * * @return array|bool|mixed */ public function getInfoByGoodsId($goods_id) { $_key = $this->getGoodsKey($goods_id); $_goods_data_json = Cache::get($_key); $_goods_data = json_decode($_goods_data_json, true); if (!is_array($_goods_data)) { $_goods_data = $_goods_data_json; if (isset($_goods_data['goods_content'])) { $_goods_data['goods_content'] = htmlspecialchars( cmf_replace_content_file_url(htmlspecialchars_decode($_goods_data['goods_content']), true) ); } } if (!is_array($_goods_data) || empty($_goods_data)) { $_goods_data = (new GoodsLogic())->getInfoByGoodsId($goods_id); if (empty($_goods_data)) { return false; } $this->saveGoodsCache($goods_id, $_goods_data); } return $_goods_data; } /** * 保存礼包cache 数据 * * @param $goods_id * @param $_goods_data * @param int $ttl */ public function saveGoodsCache($goods_id, $_goods_data, $ttl = 3600) { $_key = $this->getGoodsKey($goods_id); if (isset($_goods_data['goods_content'])) { $_goods_data['goods_content'] = cmf_replace_content_file_url( htmlspecialchars_decode($_goods_data['goods_content']) ); } Cache::set($_key, json_encode($_goods_data), $ttl); } /** * 更新商品信息 * * @param string $goods_id * @param array $goods_data * * @return bool * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function updateGoods($goods_id, $goods_data) { $_old_data = $this->getInfoByGoodsId($goods_id); $_data = $goods_data; if (!empty($_old_data)) { $_data = array_merge($_old_data, $goods_data); $this->saveGoodsCache($goods_id, $_data); $_rs = (new GoodsModel())->updateGoods($_data, $goods_id); } else { $_rs = (new GoodsModel())->addGoods($_data); } return $_rs; } /** * 删除礼包 * * @param $goods_id * @param $goods_data * * @return bool */ public function deleteGoods($goods_id, $goods_data) { $_key = $this->getGoodsKey($goods_id); Cache::rm($_key); $_rs = (new GoodsModel())->updateGoods($goods_data, $goods_id); return $_rs; } }