PageLinkRepository.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\system\diy;
  12. use app\common\dao\system\diy\PageLinkDao;
  13. use app\common\repositories\BaseRepository;
  14. use FormBuilder\Factory\Elm;
  15. use think\exception\ValidateException;
  16. use think\facade\Route;
  17. /**
  18. * 页面链接
  19. */
  20. class PageLinkRepository extends BaseRepository
  21. {
  22. public function __construct(PageLinkDao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * 获取列表数据
  28. *
  29. * 根据给定的条件数组、页码和每页数量,从数据库中检索并返回列表数据。
  30. * 这个方法主要用于处理数据的分页查询,通过where条件筛选数据,同时包含分类信息。
  31. *
  32. * @param array $where 查询条件数组
  33. * @param int $page 当前页码
  34. * @param int $limit 每页的数据数量
  35. * @return array 返回包含总数和列表数据的数组
  36. */
  37. public function getList(array $where, int $page, int $limit)
  38. {
  39. // 根据条件查询数据,并包含分类信息,按排序降序排列
  40. $query = $this->dao->getSearch($where)->with(['category'])->order('sort DESC');
  41. // 计算满足条件的数据总数
  42. $count = $query->count();
  43. // 根据当前页码和每页数量进行分页查询,并获取数据列表
  44. $list = $query->page($page, $limit)->select();
  45. // 返回包含数据总数和列表数据的数组
  46. return compact('count', 'list');
  47. }
  48. /**
  49. * 创建或编辑页面链接表单
  50. *
  51. * 根据$id$是否存在来决定是创建新页面链接还是编辑已有的页面链接。$isMer$参数用于区分商家和平台,
  52. * 以提供不同的路由地址和数据选项。
  53. *
  54. * @param int|null $id 页面链接的ID,如果为null,则表示创建新链接;否则,表示编辑已有的链接。
  55. * @param bool $isMer 标识当前操作是否属于商家,用于区分商家和平台的操作。
  56. * @return \FormBuilder\Form|\think\form\Form
  57. */
  58. public function form(?int $id, $isMer)
  59. {
  60. // 根据$id$是否存在来决定是加载现有数据还是创建新表单
  61. if ($id) {
  62. // 加载现有页面链接的数据
  63. $formData = $this->dao->get($id)->toArray();
  64. // 创建编辑页面链接的表单,路由根据$isMer$决定是商家还是平台的编辑路由
  65. $form = Elm::createForm(Route::buildUrl($isMer ? 'systemDiyPageLinkMerUpdate' : 'systemDiyPageLinkUpdate', ['id' => $id])->build());
  66. } else {
  67. // 创建新页面链接的表单,路由根据$isMer$决定是商家还是平台的新建路由
  68. $form = Elm::createForm(Route::buildUrl($isMer ? 'systemDiyPageLinkMerCreate' : 'systemDiyPageLinkCreate')->build());
  69. // 新建表单默认数据为空
  70. $formData = [];
  71. }
  72. // 定义表单的规则和字段
  73. $rule = [
  74. // 上级分类选择器,使用级联选择器实现,选项根据$isMer$动态获取
  75. Elm::cascader('cate_id', '上级分类:')->options(function () use ($isMer) {
  76. $options = app()->make(PageCategoryRepository::class)->getSearch(['status' => 1, 'is_mer' => $isMer, 'type' => 'link', 'level' => 3])->column('id value, name label');
  77. return $options;
  78. })->placeholder('请选择上级分类')->props(['props' => ['checkStrictly' => true, 'emitPath' => false]])
  79. ->filterable(true)
  80. ->appendValidate(Elm::validateInt()->required()->message('请选择上级分类')),
  81. // 页面名称输入框,必填
  82. Elm::input('name', '页面名称:')->placeholder('请输入页面名称')->required(),
  83. // 页面链接输入框,必填
  84. Elm::input('url', '页面链接:')->placeholder('请输入页面链接')->required(),
  85. // 参数输入框,可选
  86. Elm::text('param', '参数:')->placeholder('请输入参数'),
  87. // 是否显示开关,默认开启
  88. Elm::switches('status', '是否显示:', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  89. // 排序数字输入框,默认为0,最大值99999
  90. Elm::number('sort', '排序:', 0)->precision(0)->max(99999),
  91. ];
  92. // 设置表单的规则
  93. $form->setRule($rule);
  94. // 设置表单标题和数据,标题根据$id$是否为空来决定,数据使用之前加载的或空数组
  95. return $form->setTitle(is_null($id) ? '添加分类' : '编辑分类')->formData($formData);
  96. }
  97. /**
  98. * 分类下的链接列表
  99. * @param int $pid
  100. * @param int $merId
  101. * @return mixed
  102. * @author Qinii
  103. * @day 3/24/22
  104. */
  105. public function getLinkList(int $pid, int $merId)
  106. {
  107. $where['pid'] = $pid;
  108. $where['is_mer'] = $merId ? 1 : 0;
  109. $make = app()->make(PageCategoryRepository::class);
  110. $list = $make->getSearch($where)->with([
  111. 'pageLink',
  112. ])->select();
  113. return $list;
  114. }
  115. }