CacheService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Services;
  3. use App\Helpers\ProUtils;
  4. use App\Helpers\Utils;
  5. use App\Services\CallApiService;
  6. use Exception;
  7. use Illuminate\Support\Facades\Storage;
  8. class CacheService
  9. {
  10. private $callApiService;
  11. public function __construct(CallApiService $callApiService)
  12. {
  13. $this->callApiService = $callApiService;
  14. }
  15. public function putTabbedMenuHash()
  16. {
  17. $this->actMenu('User');
  18. $this->actMenu('Main');
  19. $this->actMenu('Member');
  20. }
  21. public function actMenu($type)
  22. {
  23. switch ($type) {
  24. case 'User':
  25. $menuList = Utils::getMainMenu('all');
  26. $apiUrl = 'user-menu-act';
  27. break;
  28. case 'Main':
  29. $menuList = Utils::getProMainMenu();
  30. $apiUrl = 'main-menu-act';
  31. break;
  32. case 'Member':
  33. $menuList = ProUtils::getMainMenu('all');
  34. $apiUrl = 'member-menu-act';
  35. break;
  36. }
  37. if (! $menuList) {
  38. return;
  39. }
  40. $menuReq = collect($menuList['Page'])->map(function ($menu) use ($type) {
  41. $resut['Id'] = $menu[$type . 'MenuId'];
  42. $resut['TabbedMenuHash'] = '';
  43. if ($type === 'User' || $type === 'Member') {
  44. $resut['TabbedMenuHash'] = md5($menu['PageUri'] . $menu['ParaName']);
  45. } else if ($type === 'Main') {
  46. $resut['TabbedMenuHash'] = md5($menu['PageUri'] . $menu['MenuCode']);
  47. }
  48. return $resut;
  49. })->toArray();
  50. $response = $this->callApiService->callApi([
  51. 'url' => $apiUrl,
  52. 'data' => [
  53. 'Page' => $menuReq
  54. ],
  55. ]);
  56. if ($this->callApiService->verifyApiError($response)) {
  57. notify()->error($response['body'], 'Error', 'bottomRight');
  58. return redirect()->to('/user-logout');
  59. }
  60. }
  61. public function putMainMenu()
  62. {
  63. $data = $this->callApiService->callApi([
  64. 'url' => 'igroup-page',
  65. 'data' => [
  66. 'PageVars' => [
  67. 'Limit' => 9999999,
  68. 'Offset' => 0
  69. ]
  70. ],
  71. ]);
  72. if ($data['Page']) {
  73. $mainMenuList = Utils::formatIgroupMenuList($data['Page'], 'IgroupCode');
  74. $fullFileUrl = "dabory-footage/basic/igroup.json";
  75. Storage::put($fullFileUrl, json_encode($mainMenuList));
  76. }
  77. }
  78. public function putEtcBrand()
  79. {
  80. $data = $this->callApiService->callApi([
  81. 'url' => 'etc-page',
  82. 'data' => [
  83. 'Query' => "status = '0' and select_name = 'brand'",
  84. 'PageVars' => [
  85. 'Limit' => 100000,
  86. ]
  87. ]
  88. ]);
  89. if ($data['Page']) {
  90. $fullFileUrl = "dabory-footage/basic/etc/brand.json";
  91. Storage::put($fullFileUrl, json_encode($data));
  92. }
  93. }
  94. public function putSetup()
  95. {
  96. $data = $this->callApiService->callApi([
  97. 'url' => 'setup-page',
  98. 'data' => [
  99. 'PageVars' => [
  100. 'Limit' => 9999999,
  101. 'Offset' => 0
  102. ]
  103. ],
  104. ]);
  105. if ($data['Page']) {
  106. foreach ($data['Page'] as $setup) {
  107. $setupCode = $setup['SetupCode'];
  108. $brandCode = $setup['BrandCode'] ? '-'.$setup['BrandCode'] : $setup['BrandCode'];
  109. $fullFileUrl = "dabory-footage/basic/setup/$setupCode$brandCode.json";
  110. Storage::put($fullFileUrl, json_encode($setup));
  111. }
  112. }
  113. }
  114. public function get($fullFileUrl)
  115. {
  116. if (Storage::disk()->exists($fullFileUrl)) {
  117. return json_decode(Storage::get($fullFileUrl), true);
  118. }
  119. return null;
  120. }
  121. public function getSetup($setupCode, $brandCode = null)
  122. {
  123. $brandCode = $brandCode ? '-'.$brandCode : $brandCode;
  124. $fullFileUrl = "dabory-footage/basic/setup/$setupCode$brandCode.json";
  125. if (Storage::disk()->exists($fullFileUrl)) {
  126. $setup = json_decode(Storage::get($fullFileUrl), true);
  127. return json_decode($setup['SetupJson'], true);
  128. }
  129. return null;
  130. }
  131. }