DiyService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\diy;
  12. use app\dict\diy\PagesDict;
  13. use app\model\diy\Diy;
  14. use core\base\BaseApiService;
  15. /**
  16. * 自定义页面服务层
  17. * Class DiyService
  18. * @package app\service\api\diy
  19. */
  20. class DiyService extends BaseApiService
  21. {
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->model = new Diy();
  26. }
  27. /**
  28. * 获取自定义页面信息
  29. * @param array $params
  30. * @return array
  31. */
  32. public function getInfo(array $params = [])
  33. {
  34. $condition = [];
  35. $site_id = $params[ 'site_id' ] ?? 0;
  36. $condition[] = [ 'site_id', '=', $site_id ];
  37. if (!empty($params[ 'id' ])) {
  38. $condition[] = [ 'id', '=', $params[ 'id' ] ];
  39. } elseif (!empty($params[ 'name' ])) {
  40. $condition[] = [ 'name', '=', $params[ 'name' ] ];
  41. $condition[] = [ 'is_default', '=', 1 ];
  42. }
  43. $field = 'id,site_id,title,name,type,template, mode,value,is_default,share,visit_count';
  44. $info = $this->model->field($field)->where($condition)->findOrEmpty()->toArray();
  45. if (empty($info)) {
  46. // 查询默认页面数据
  47. if (!empty($params[ 'name' ])) {
  48. $page_data = $this->getFirstPageData($params[ 'name' ]);
  49. if (!empty($page_data)) {
  50. $info = [
  51. 'site_id' => $site_id,
  52. 'title' => $page_data[ 'title' ],
  53. 'name' => $page_data[ 'type' ],
  54. 'type' => $page_data[ 'type' ],
  55. 'template' => $page_data[ 'template' ],
  56. 'mode' => $page_data[ 'mode' ],
  57. 'value' => json_encode($page_data[ 'data' ], JSON_UNESCAPED_UNICODE),
  58. 'is_default' => 1,
  59. 'share' => '',
  60. 'visit_count' => 0
  61. ];
  62. }
  63. }
  64. }
  65. return $info;
  66. }
  67. /**
  68. * 获取默认页面数据
  69. * @param $type
  70. * @return array|mixed
  71. */
  72. public function getFirstPageData($type)
  73. {
  74. $pages = PagesDict::getPages([ 'type' => $type ]);
  75. if (!empty($pages)) {
  76. $template = array_key_last($pages);
  77. $page = array_pop($pages);
  78. $page[ 'template' ] = $template;
  79. $page[ 'type' ] = $type;
  80. return $page;
  81. }
  82. return [];
  83. }
  84. }