Document.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\system;
  11. use think\facade\Cache;
  12. use app\model\BaseModel;
  13. /**
  14. * 系统文章
  15. */
  16. class Document extends BaseModel
  17. {
  18. /**
  19. * 设置文章内容
  20. * @param $title
  21. * @param $content
  22. * @param $condition
  23. * @return array
  24. */
  25. public function setDocument($title, $content, $condition)
  26. {
  27. $check_condition = array_column($condition, 2, 0);
  28. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  29. if ($site_id === '') {
  30. return $this->error('', 'REQUEST_SITE_ID');
  31. }
  32. $app_module = isset($check_condition['app_module']) ? $check_condition['app_module'] : '';
  33. if ($app_module === '') {
  34. return $this->error('', 'REQUEST_APP_MODULE');
  35. }
  36. $document_key = isset($check_condition['document_key']) ? $check_condition['document_key'] : '';
  37. if (empty($document_key)) {
  38. return $this->error('', 'REQUEST_DOCUMENT_KEY');
  39. }
  40. $data = $check_condition;
  41. $data['title'] = $title;
  42. $data['content'] = $content;
  43. $json_condition = json_encode($condition);
  44. $document_model = model('document');
  45. $info = $document_model->getInfo($condition, 'id');
  46. Cache::tag("document")->set("document_" . $json_condition, "");
  47. if (empty($info)) {
  48. $data['create_time'] = time();
  49. $res = $document_model->add($data);
  50. } else {
  51. $data['modify_time'] = time();
  52. $res = $document_model->update($data, $condition);
  53. }
  54. return $this->success($res);
  55. }
  56. /**
  57. * 获取系统文章
  58. * @param array $condition
  59. */
  60. public function getDocument($condition)
  61. {
  62. $json_condition = json_encode($condition);
  63. $cache = Cache::get("document_" . $json_condition, "");
  64. if (!empty($cache)) {
  65. return $this->success($cache);
  66. }
  67. $check_condition = array_column($condition, 2, 0);
  68. $site_id = isset($check_condition['site_id']) ? $check_condition['site_id'] : '';
  69. if ($site_id === '') {
  70. return $this->error('', 'REQUEST_SITE_ID');
  71. }
  72. $app_module = isset($check_condition['app_module']) ? $check_condition['app_module'] : '';
  73. if ($app_module === '') {
  74. return $this->error('', 'REQUEST_APP_MODULE');
  75. }
  76. $document_key = isset($check_condition['document_key']) ? $check_condition['document_key'] : '';
  77. if (empty($document_key)) {
  78. return $this->error('', 'REQUEST_DOCUMENT_KEY');
  79. }
  80. $info = model('document')->getInfo($condition, 'site_id, app_module, document_key, title, content, create_time, modify_time');
  81. if (empty($info)) {
  82. //默认初始值
  83. $info = [
  84. 'site_id' => $site_id,
  85. 'app_module' => $app_module,
  86. 'document_key' => $document_key,
  87. 'title' => '',
  88. 'content' => '',
  89. 'create_time' => 0,
  90. 'modify_time' => 0
  91. ];
  92. }
  93. Cache::tag("document")->set("document_" . $json_condition, $info);
  94. return $this->success($info);
  95. }
  96. }