Element.Class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * 元素管理
  4. * Created by PhpStorm.
  5. * User: QianNiao-C
  6. * Date: 2019/10/25
  7. * Time: 14:56
  8. */
  9. namespace JinDouYun\Controller\BlockManage;
  10. use JinDouYun\Controller\BaseController;
  11. use Mall\Framework\Core\ErrorCode;
  12. use JinDouYun\Model\BlockManage\MElement;
  13. class Element extends BaseController
  14. {
  15. public $objMElement;
  16. public function __construct($isCheckAcl = true, $isMustLogin = true)
  17. {
  18. parent::__construct($isCheckAcl, $isMustLogin);
  19. $this->objMElement = new MElement();
  20. }
  21. /**
  22. * 添加,编辑元素接收公共字段
  23. *
  24. * @return array
  25. */
  26. public function commonFieldFilter()
  27. {
  28. $params = $this->request->getRawJson();
  29. if (empty($params)) {
  30. $this->sendOutput('参数为空', ErrorCode::$paramError);
  31. }
  32. $elementFieldData = [
  33. 'elementData' => isset($params['data']) ? $params['data'] : '',//字段数据
  34. ];
  35. foreach ($elementFieldData as $key => $value) {
  36. if (empty($value) && $value !== 0) {
  37. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  38. }
  39. }
  40. $elementFieldData['startTime'] = isset($params['startTime']) ? $params['startTime'] : 0;
  41. $elementFieldData['endTime'] = isset($params['endTime']) ? $params['endTime'] : 0;
  42. $elementFieldData['sort'] = isset($params['sort']) ? $params['sort'] : 0;
  43. $elementFieldData['elementData'] = json_encode($elementFieldData['elementData']);
  44. return $elementFieldData;
  45. }
  46. /**
  47. * 添加区块下元素
  48. */
  49. public function addElement()
  50. {
  51. $zoneId = $this->request->param('zoneId');
  52. if (empty($zoneId)) {
  53. $this->sendOutput('请指定zoneId', ErrorCode::$paramError);
  54. }
  55. $elementFieldData = $this->commonFieldFilter();
  56. $elementFieldData['zoneId'] = $zoneId;
  57. $result = $this->objMElement->addElement($elementFieldData);
  58. if ($result->isSuccess()) {
  59. parent::sendOutput($result->getData());
  60. }
  61. parent::sendOutput($result->getData(), $result->getErrorCode());
  62. }
  63. /**
  64. * 获取指定元素信息
  65. */
  66. public function getElementInfoById()
  67. {
  68. $id = $this->request->param('request_id');
  69. if (!$id) {
  70. $this->sendOutput('参数错误', ErrorCode::$paramError);
  71. }
  72. $result = $this->objMElement->getElementInfoById($id);
  73. if ($result->isSuccess()) {
  74. $resultData = $result->getData();
  75. $resultData['elementData'] = json_decode($resultData['elementData'], true);
  76. $this->sendOutput($resultData);
  77. }
  78. $this->sendOutput($result->getData(), $result->getErrorCode());
  79. }
  80. /**
  81. * 编辑指定元素信息
  82. */
  83. public function editElement()
  84. {
  85. $id = $this->request->param('request_id');
  86. if (empty($id)) {
  87. $this->sendOutput('参数错误', ErrorCode::$paramError);
  88. }
  89. $elementData = $this->commonFieldFilter();
  90. $elementData['id'] = $id;
  91. unset($elementData['createTime']);
  92. $result = $this->objMElement->editElement($elementData);
  93. if ($result->isSuccess()) {
  94. parent::sendOutput($result->getData());
  95. }
  96. parent::sendOutput($result->getData(), $result->getErrorCode());
  97. }
  98. /**
  99. * 删除指定元素
  100. */
  101. public function delElement()
  102. {
  103. $id = $this->request->param('request_id');
  104. if (!$id) {
  105. $this->sendOutput('参数错误', ErrorCode::$paramError);
  106. }
  107. if (!is_array($id)) {
  108. $id = [$id];
  109. }
  110. $result = $this->objMElement->delElement($id);
  111. if ($result->isSuccess()) {
  112. parent::sendOutput($result->getData());
  113. }
  114. parent::sendOutput($result->getData(), $result->getErrorCode());
  115. }
  116. /**
  117. * 获取指定区块下的所有元素
  118. */
  119. public function getElementByZoneId()
  120. {
  121. $zoneId = $this->request->param('request_id');
  122. if (empty($zoneId)) {
  123. $this->sendOutput('请指定zoneId', ErrorCode::$paramError);
  124. }
  125. $page = $this->request->param('page') ?: 1;
  126. $pageSize = $this->request->param('pageSize') ?: 10;
  127. $offset = ($page - 1) * $pageSize;
  128. $selectParams = [
  129. 'limit' => $pageSize,
  130. 'offset' => $offset,
  131. 'zoneId' => $zoneId,
  132. 'type' => 'all',//后台用获取所有数据
  133. ];
  134. $result = $this->objMElement->getElementByZoneId($selectParams);
  135. if ($result->isSuccess()) {
  136. $returnData = $result->getData();
  137. $pageData = [
  138. 'pageIndex' => $page,
  139. 'pageSize' => $pageSize,
  140. 'pageTotal' => $returnData['total'],
  141. ];
  142. parent::sendOutput($returnData['data'], 0, $pageData);
  143. }
  144. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  145. }
  146. }