Page.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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->withoutField('page_data')
  34. ->where($filter)
  35. ->where(['is_delete' => 0])
  36. ->order(['create_time' => 'desc', $this->getPk()])
  37. ->paginate();
  38. }
  39. /**
  40. * 检索查询条件
  41. * @param array $param
  42. * @return array
  43. */
  44. private function getFilter(array $param = [])
  45. {
  46. $filter = [];
  47. $params = $this->setQueryDefaultValue($param, [
  48. 'name' => '', // 页面名称
  49. ]);
  50. !empty($params['name']) && $filter[] = ['page_name', 'like', "%{$params['name']}%"];
  51. return $filter;
  52. }
  53. /**
  54. * 新增页面
  55. * @param array $data
  56. * @return bool
  57. */
  58. public function add(array $data)
  59. {
  60. return $this->save([
  61. 'page_type' => 20,
  62. 'page_name' => $data['page']['params']['name'],
  63. 'page_data' => $data,
  64. 'store_id' => self::$storeId
  65. ]);
  66. }
  67. /**
  68. * 更新页面
  69. * @param array $data
  70. * @return bool
  71. */
  72. public function edit(array $data)
  73. {
  74. $pageData = $this->getFilterPageData($data);
  75. return $this->save([
  76. 'page_name' => $pageData['page']['params']['name'],
  77. 'page_data' => $pageData
  78. ]) !== false;
  79. }
  80. /**
  81. * 过滤页面数据
  82. * @param array $data
  83. * @return array
  84. */
  85. private function getFilterPageData(array $data)
  86. {
  87. foreach ($data['items'] as &$item) {
  88. if ($item['type'] === 'richText') {
  89. $item['params']['content'] = htmlspecialchars_decode($item['params']['content']);
  90. }
  91. }
  92. return $data;
  93. }
  94. /**
  95. * 删除记录
  96. * @return int
  97. */
  98. public function setDelete()
  99. {
  100. if ($this['page_type'] == PageTypeEnum::HOME) {
  101. $this->error = '默认首页不可以删除';
  102. return false;
  103. }
  104. // 删除记录
  105. return $this->save(['is_delete' => 1]);
  106. }
  107. /**
  108. * 设为默认首页
  109. * @return int
  110. */
  111. public function setHome()
  112. {
  113. // 取消原默认首页
  114. $this->where(['page_type' => PageTypeEnum::HOME])->update(['page_type' => PageTypeEnum::CUSTOM]);
  115. return $this->save(['page_type' => PageTypeEnum::HOME]);
  116. }
  117. }