GoodsCache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * GoodsCache.php UTF-8
  4. * 商品缓存
  5. *
  6. * @date : 2018/5/7 22:44
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\shop;
  13. use huo\controller\common\Base;
  14. use huo\logic\shop\GoodsLogic;
  15. use huo\model\shop\GoodsModel;
  16. use think\Cache;
  17. class GoodsCache extends Base {
  18. public static function ins() {
  19. return new static();
  20. }
  21. /**
  22. * 获取礼包KEY
  23. *
  24. * @param string $goods_id
  25. *
  26. * @return string
  27. */
  28. private function getGoodsKey($goods_id) {
  29. return 'goods_id_key_'.$goods_id;
  30. }
  31. /**
  32. * 通过商品ID 查询商品信息
  33. *
  34. * @param string $goods_id
  35. *
  36. * @return array|bool|mixed
  37. */
  38. public function getInfoByGoodsId($goods_id) {
  39. $_key = $this->getGoodsKey($goods_id);
  40. $_goods_data_json = Cache::get($_key);
  41. $_goods_data = json_decode($_goods_data_json, true);
  42. if (!is_array($_goods_data)) {
  43. $_goods_data = $_goods_data_json;
  44. if (isset($_goods_data['goods_content'])) {
  45. $_goods_data['goods_content'] = htmlspecialchars(
  46. cmf_replace_content_file_url(htmlspecialchars_decode($_goods_data['goods_content']), true)
  47. );
  48. }
  49. }
  50. if (!is_array($_goods_data) || empty($_goods_data)) {
  51. $_goods_data = (new GoodsLogic())->getInfoByGoodsId($goods_id);
  52. if (empty($_goods_data)) {
  53. return false;
  54. }
  55. $this->saveGoodsCache($goods_id, $_goods_data);
  56. }
  57. return $_goods_data;
  58. }
  59. /**
  60. * 保存礼包cache 数据
  61. *
  62. * @param $goods_id
  63. * @param $_goods_data
  64. * @param int $ttl
  65. */
  66. public function saveGoodsCache($goods_id, $_goods_data, $ttl = 3600) {
  67. $_key = $this->getGoodsKey($goods_id);
  68. if (isset($_goods_data['goods_content'])) {
  69. $_goods_data['goods_content'] = cmf_replace_content_file_url(
  70. htmlspecialchars_decode($_goods_data['goods_content'])
  71. );
  72. }
  73. Cache::set($_key, json_encode($_goods_data), $ttl);
  74. }
  75. /**
  76. * 更新商品信息
  77. *
  78. * @param string $goods_id
  79. * @param array $goods_data
  80. *
  81. * @return bool
  82. * @throws \think\Exception
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public function updateGoods($goods_id, $goods_data) {
  88. $_old_data = $this->getInfoByGoodsId($goods_id);
  89. $_data = $goods_data;
  90. if (!empty($_old_data)) {
  91. $_data = array_merge($_old_data, $goods_data);
  92. $this->saveGoodsCache($goods_id, $_data);
  93. $_rs = (new GoodsModel())->updateGoods($_data, $goods_id);
  94. } else {
  95. $_rs = (new GoodsModel())->addGoods($_data);
  96. }
  97. return $_rs;
  98. }
  99. /**
  100. * 删除礼包
  101. *
  102. * @param $goods_id
  103. * @param $goods_data
  104. *
  105. * @return bool
  106. */
  107. public function deleteGoods($goods_id, $goods_data) {
  108. $_key = $this->getGoodsKey($goods_id);
  109. Cache::rm($_key);
  110. $_rs = (new GoodsModel())->updateGoods($goods_data, $goods_id);
  111. return $_rs;
  112. }
  113. }