Page.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model;
  13. use app\common\model\Page as PageModel;
  14. use app\common\enum\page\PageType as PageTypeEnum;
  15. /**
  16. * 店铺页面模型
  17. * Class Page
  18. * @package app\common\model
  19. */
  20. class Page extends PageModel
  21. {
  22. /**
  23. * 获取列表
  24. * @param array $param
  25. * @return \think\Paginator
  26. * @throws \think\db\exception\DbException
  27. */
  28. public function getList(array $param = [])
  29. {
  30. // 检索查询条件
  31. $filter = $this->getFilter($param);
  32. // 获取列表信息
  33. return $this->where($filter)
  34. ->where(['is_delete' => 0])
  35. ->order(['create_time' => 'desc', $this->getPk()])
  36. ->paginate();
  37. }
  38. /**
  39. * 检索查询条件
  40. * @param array $param
  41. * @return array
  42. */
  43. private function getFilter(array $param = [])
  44. {
  45. $filter = [];
  46. $params = $this->setQueryDefaultValue($param, [
  47. 'name' => '', // 页面名称
  48. ]);
  49. !empty($params['name']) && $filter[] = ['page_name', 'like', "%{$params['name']}%"];
  50. return $filter;
  51. }
  52. /**
  53. * 新增页面
  54. * @param array $data
  55. * @return bool
  56. */
  57. public function add(array $data)
  58. {
  59. return $this->save([
  60. 'page_type' => 20,
  61. 'page_name' => $data['page']['params']['name'],
  62. 'page_data' => $data,
  63. 'store_id' => self::$storeId
  64. ]);
  65. }
  66. /**
  67. * 更新页面
  68. * @param array $data
  69. * @return bool
  70. */
  71. public function edit(array $data)
  72. {
  73. $pageData = $this->getFilterPageData($data);
  74. return $this->save([
  75. 'page_name' => $pageData['page']['params']['name'],
  76. 'page_data' => $pageData
  77. ]) !== false;
  78. }
  79. /**
  80. * 过滤页面数据
  81. * @param array $data
  82. * @return array
  83. */
  84. private function getFilterPageData(array $data)
  85. {
  86. foreach ($data['items'] as &$item) {
  87. if ($item['type'] === 'richText') {
  88. $item['params']['content'] = htmlspecialchars_decode($item['params']['content']);
  89. }
  90. }
  91. return $data;
  92. }
  93. /**
  94. * 删除记录
  95. * @return int
  96. */
  97. public function setDelete()
  98. {
  99. if ($this['page_type'] == PageTypeEnum::HOME) {
  100. $this->error = '默认首页不可以删除';
  101. return false;
  102. }
  103. // 删除记录
  104. return $this->save(['is_delete' => 1]);
  105. }
  106. /**
  107. * 设为默认首页
  108. * @return int
  109. */
  110. public function setHome()
  111. {
  112. // 取消原默认首页
  113. $this->where(['page_type' => PageTypeEnum::HOME])->update(['page_type' => PageTypeEnum::CUSTOM]);
  114. return $this->save(['page_type' => PageTypeEnum::HOME]);
  115. }
  116. }