FooterService.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\FooterCategory;
  4. use App\Model\Website;
  5. use App\Model\FooterContent;
  6. use Hyperf\RpcServer\Annotation\RpcService;
  7. use App\Tools\Result;
  8. #[RpcService(name: "FooterService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  9. class FooterService implements FooterServiceInterface
  10. {
  11. /**
  12. * 获取底部导航
  13. * @param array $data
  14. * @return array
  15. */
  16. public function getFooterCategory(array $data): array
  17. {
  18. $where = [];
  19. if(isset($data['website_name'])){
  20. array_push($where, ['website.website_name','like','%'.$data['website_name'].'%']);
  21. }
  22. if(isset($data['name'])){
  23. array_push($where, ['footer_category.name','like','%'.$data['name'].'%']);
  24. }
  25. // var_dump($where);
  26. if(!empty($where)){
  27. $rep = FooterCategory::where($where)
  28. ->leftJoin("website","website.id","footer_category.website_id")
  29. ->select("footer_category.*","website.website_name","website.id as website_id")
  30. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("updated_at","desc")
  31. ->get();
  32. $count = count($rep);
  33. }else{
  34. $rep = FooterCategory::leftJoin("website","website.id","footer_category.website_id")
  35. ->select("footer_category.*","website.website_name","website.id as website_id")
  36. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])->orderBy("updated_at","desc")
  37. ->get();
  38. $count = FooterCategory::count();
  39. }
  40. $result = [];
  41. $result = [
  42. 'rows'=>$rep,
  43. 'count'=>$count
  44. ];
  45. if($count == 0){
  46. return Result::error("没有查到相关数据!");
  47. }
  48. return Result::success($result);
  49. }
  50. /**
  51. * 添加底部导航
  52. * @param array $data
  53. * @return array
  54. */
  55. public function addFooterCategory(array $data): array
  56. {
  57. if(empty($data)){
  58. $result = Website::select('website_name','id')->get();
  59. }else{
  60. // 底部导航类型 0:内容型;1:列表型;
  61. if(in_array($data['type'], [0, 1])){
  62. // 同一网站下的底部导航名称不能重复
  63. $name = FooterCategory::where('website_id',$data['website_id'])->where('name',$data['name'])->first();
  64. if(!empty($name)){
  65. return Result::error("该底部导航名称已存在!");
  66. }else{
  67. $result = FooterCategory::insertGetId($data);
  68. }
  69. }else{
  70. return Result::error("请输入正确的底部导航类型!");
  71. }
  72. }
  73. if(empty($result)){
  74. return Result::error("添加失败!");
  75. }else{
  76. return Result::success($result);
  77. }
  78. }
  79. /**
  80. * 修改底部导航
  81. * @param array $data
  82. * @return array
  83. */
  84. public function upFooterCategory(array $data): array
  85. {
  86. $footer_category = FooterCategory::where('id', $data['id'])->first();
  87. if (!$footer_category) {
  88. return Result::error("该底部导航不存在!");
  89. }
  90. if(empty($data['website_id'])){
  91. $web = Website::select('website_name','id')->get();
  92. $footer_category = FooterCategory::where('footer_category.id',$data['id'])
  93. ->leftJoin("website","website.id","footer_category.website_id")
  94. ->select("footer_category.*","website.website_name","website.id as website_id")
  95. ->first();
  96. $result = [
  97. 'rows'=>$footer_category,
  98. 'web'=>$web
  99. ];
  100. }else{
  101. $all_categories = FooterCategory::where('website_id',$data['website_id'])->pluck('name')->toArray();
  102. // 检查修改后的数据是否与已有数据重复
  103. if (in_array($data['name'], $all_categories)) {
  104. return Result::error("修改后的底部导航名称已存在!");
  105. }
  106. $result = FooterCategory::where('id', $data['id'])->update($data);
  107. }
  108. if (empty($result)) {
  109. return Result::error("修改失败!");
  110. }else{
  111. return Result::success($result);
  112. }
  113. }
  114. /**
  115. * 删除底部导航
  116. * @param array $data
  117. * @return array
  118. */
  119. public function delFooterCategory(array $data): array
  120. {
  121. $footer_category = FooterCategory::where('id', $data['id'])->first();
  122. if (!$footer_category) {
  123. return Result::error("该底部导航不存在!");
  124. }else{
  125. $result = FooterCategory::where('footer_category.id',$data['id'])
  126. ->leftJoin("footer_content","footer_content.fcat_id","footer_category.id")
  127. ->delete();
  128. }
  129. if(empty($result)){
  130. return Result::error("删除失败!");
  131. }else{
  132. return Result::success($result);
  133. }
  134. }
  135. /**
  136. * 添加底部导航(列表)内容
  137. * @param array $data
  138. * @return array
  139. */
  140. public function addFooterContent(array $data): array
  141. {
  142. // 底部导航类型 0:内容型;1:列表型;
  143. // var_dump($data);
  144. if($data['type'] == 0){
  145. $countent = FooterContent::where('fcat_id', $data['fcat_id'])->first();
  146. if(!empty($countent)){
  147. return Result::error("该底部导航内容已存在!");
  148. }
  149. }else{
  150. $countent = FooterContent::where('list_title', $data['list_title'])->first();
  151. if(!empty($countent)){
  152. return Result::error("该列表标题已存在!");
  153. }
  154. }
  155. unset($data['type']);
  156. $result = FooterContent::insertGetId($data);
  157. if(empty($result)){
  158. return Result::error("添加失败!");
  159. }else{
  160. return Result::success($result);
  161. }
  162. }
  163. /**
  164. * 获取底部导航(列表)内容
  165. * @param array $data
  166. * @return array
  167. */
  168. public function getFooterContent(array $data): array
  169. {
  170. $where = [];
  171. array_push($where, ['fcat_id',$data['fcat_id']]);
  172. $type = FooterCategory::where('id', $data['fcat_id'])->value('type');
  173. if($type == 1){
  174. if(isset($data['list_title'])){
  175. array_push($where, ['list_title','like','%'.$data['list_title'].'%']);
  176. }
  177. if(isset($data['con_title'])){
  178. array_push($where, ['con_title','like','%'.$data['con_title'].'%']);
  179. }
  180. }
  181. $rep = FooterContent::where($where)
  182. ->limit($data['pageSize'])->offset(($data['page']-1)*$data['pageSize'])
  183. ->orderBy("updated_at","desc")->get();
  184. $count = FooterContent::where($where)->count();
  185. if($count == 0){
  186. return Result::error("没有查到相关数据!");
  187. }else{
  188. $result = [
  189. 'rows'=>$rep,
  190. 'count'=>$count
  191. ];
  192. return Result::success($result);
  193. }
  194. }
  195. /**
  196. * 获取某个底部导航(列表)内容
  197. * @param array $data
  198. * @return array
  199. */
  200. public function getOneFooterContent(array $data): array
  201. {
  202. $result = FooterContent::where('footer_content.id', $data['id'])
  203. ->leftJoin('footer_category','footer_category.id','fcat_id')
  204. ->select('footer_content.*','footer_category.type')
  205. ->first();
  206. if(empty($result)){
  207. return Result::error("请输入正确的底部导航内容id!");
  208. }else{
  209. return Result::success($result);
  210. }
  211. }
  212. /**
  213. * 编辑底部导航(列表)内容
  214. * @param array $data
  215. * @return array
  216. */
  217. public function upFooterContent(array $data): array
  218. {
  219. if($data['type'] == 1){
  220. $list_title = FooterContent::all()->pluck('list_title')->toArray();
  221. // 检查修改后的数据是否与已有数据重复
  222. if (in_array($data['list_title'], $list_title)) {
  223. return Result::error("修改后的列表名称已存在!");
  224. }
  225. }
  226. unset($data['type']);
  227. $result = FooterContent::where('id', $data['id'])->update($data);
  228. if(empty($result)){
  229. return Result::error("修改失败!");
  230. }else{
  231. return Result::success($result);
  232. }
  233. }
  234. /**
  235. * 删除底部导航(列表)内容
  236. * @param array $data
  237. * @return array
  238. */
  239. public function delFooterContent(array $data): array
  240. {
  241. $result = FooterContent::where('id', $data['id'])->delete($data);
  242. if(empty($result)){
  243. return Result::error("删除失败!");
  244. }else{
  245. return Result::success($result);
  246. }
  247. }
  248. }