PosterService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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\admin\sys;
  12. use app\dict\poster\ComponentDict;
  13. use app\model\sys\Poster;
  14. use app\service\core\poster\CorePosterService;
  15. use core\base\BaseAdminService;
  16. use core\exception\AdminException;
  17. use Exception;
  18. use think\db\exception\DbException;
  19. use think\facade\Db;
  20. /**
  21. * 自定义海报服务类
  22. * Class AgreementService
  23. * @package app\service\admin\sys
  24. */
  25. class PosterService extends BaseAdminService
  26. {
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $this->model = new Poster();
  31. }
  32. /**
  33. * 获取自定义海报分页列表
  34. * @param array $where
  35. * @return array
  36. * @throws DbException
  37. */
  38. public function getPage(array $where = [])
  39. {
  40. $field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon';
  41. $order = "update_time desc";
  42. $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ 'name', 'type' ], $where)->field($field)->order($order)->append([ 'type_name' ]);
  43. return $this->pageQuery($search_model);
  44. }
  45. /**
  46. * 获取自定义海报列表
  47. * @param array $where
  48. * @param string $field
  49. * @return mixed
  50. */
  51. public function getList(array $where = [], $field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon')
  52. {
  53. $order = "update_time desc";
  54. return $this->model->where([ [ 'site_id', "=", $this->site_id ] ])->withSearch([ "name", 'type' ], $where)->field($field)->order($order)->select()->toArray();
  55. }
  56. /**
  57. * 获取自定义海报信息
  58. * @param int $id
  59. * @param string $field
  60. * @return mixed
  61. */
  62. public function getInfo(int $id, $field = 'id, site_id, name, type, channel, value, status,is_default, create_time, update_time, addon')
  63. {
  64. return $this->model->field($field)->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  65. }
  66. /**
  67. * 添加自定义海报
  68. * @param array $data
  69. * @return mixed
  70. */
  71. public function add(array $data)
  72. {
  73. $data[ 'site_id' ] = $this->site_id;
  74. $res = $this->model->create($data);
  75. return $res->id;
  76. }
  77. /**
  78. * 编辑自定义海报
  79. * @param int $id
  80. * @param array $data
  81. * @return bool
  82. */
  83. public function edit(int $id, array $data)
  84. {
  85. $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->update($data);
  86. return true;
  87. }
  88. /**
  89. * 删除自定义海报
  90. * @param int $id
  91. * @return bool
  92. */
  93. public function del(int $id)
  94. {
  95. return $this->model->where([ [ 'id', '=', $id ], [ 'site_id', '=', $this->site_id ] ])->delete();
  96. }
  97. /**
  98. * 修改自定义海报启用状态
  99. * @param $data
  100. * @return mixed
  101. */
  102. public function modifyStatus($data)
  103. {
  104. return $this->model->where([
  105. [ 'id', '=', $data[ 'id' ] ],
  106. [ 'site_id', '=', $this->site_id ]
  107. ])->update([ 'status' => $data[ 'status' ] ]);
  108. }
  109. /**
  110. * 将自定义海报修改为默认海报
  111. * @param $data
  112. * @return mixed
  113. */
  114. public function modifyDefault($data)
  115. {
  116. try {
  117. $info = $this->getInfo($data[ 'id' ]);
  118. if (empty($info)) {
  119. return false;
  120. }
  121. Db::startTrans();
  122. $this->model->where([ [ 'site_id', '=', $this->site_id ], [ 'type', '=', $info[ 'type' ] ] ])->update([ 'is_default' => 0 ]);
  123. $this->model->where([ [ 'site_id', '=', $this->site_id ], [ 'id', '=', $data[ 'id' ] ] ])->update([ 'is_default' => 1, 'update_time' => time() ]);
  124. Db::commit();
  125. return true;
  126. } catch (Exception $e) {
  127. Db::rollback();
  128. throw new AdminException($e->getMessage());
  129. }
  130. }
  131. /**
  132. * 查询海报类型
  133. * @param string $type
  134. * @return array
  135. */
  136. public function getType($type = '')
  137. {
  138. return ( new CorePosterService() )->getType($type);
  139. }
  140. /**
  141. * 查询海报模板
  142. * @param string $addon
  143. * @param string $type
  144. * @return array|null
  145. */
  146. public function getTemplateList($addon = '', $type = '')
  147. {
  148. return ( new CorePosterService() )->getTemplateList($addon, $type);
  149. }
  150. /**
  151. * 获取组件列表
  152. * @param array $params 查询指定条件的海报组件
  153. * @return array
  154. */
  155. public function getComponentList($params = [])
  156. {
  157. $data = ComponentDict::getComponent();
  158. foreach ($data as $k => $v) {
  159. // 该分组下若没有组件则清空
  160. if (empty($v[ 'list' ])) {
  161. unset($data[ $k ]);
  162. continue;
  163. }
  164. // 查询组件支持的页面
  165. if (!empty($v[ 'support' ])) {
  166. if ($params[ 'type' ] != $k && !empty($params[ 'type' ]) && !empty($v[ 'support' ]) && !in_array($params[ 'type' ], $v[ 'support' ])) {
  167. unset($data[ $k ]);
  168. continue;
  169. }
  170. }
  171. $sort_arr = [];
  172. foreach ($v[ 'list' ] as $ck => $cv) {
  173. $sort_arr[] = $cv[ 'sort' ];
  174. unset($data[ $k ][ 'list' ][ $ck ][ 'sort' ]);
  175. }
  176. array_multisort($sort_arr, SORT_ASC, $data[ $k ][ 'list' ]); //排序,根据 sort 排序
  177. }
  178. return $data;
  179. }
  180. /**
  181. * 页面加载初始化
  182. * @param array $params
  183. * @return array
  184. * @throws DbException
  185. */
  186. public function getInit(array $params = [])
  187. {
  188. $data = [
  189. 'id' => 0,
  190. 'name' => $params[ 'name' ],
  191. 'type' => $params[ 'type' ],
  192. 'channel' => '',
  193. 'value' => '',
  194. 'status' => 1,
  195. 'is_default' => 0,
  196. 'addon' => ''
  197. ];
  198. if (!empty($params[ 'id' ])) {
  199. $field = 'id, name, type, channel, value, status,is_default, addon';
  200. $info = $this->getInfo($params[ 'id' ], $field);
  201. if (!empty($info)) {
  202. $data = $info;
  203. }
  204. }
  205. $data[ 'poster_type' ] = ( new CorePosterService() )->getType($data[ 'type' ]);
  206. if (empty($data[ 'addon' ])) {
  207. $data[ 'addon' ] = $data[ 'poster_type' ][ 'addon' ];
  208. }
  209. $data[ 'component' ] = $this->getComponentList([ 'type' => $data[ 'type' ] ]);
  210. return $data;
  211. }
  212. }