ProUtils.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace App\Helpers;
  3. use App\Services\CallApiService;
  4. use Illuminate\Support\Collection;
  5. use App\Exceptions\ParameterException;
  6. use Illuminate\Support\Facades\Storage;
  7. use App\Http\Controllers\Api\ApiController;
  8. class ProUtils
  9. {
  10. public static function putDirectParamCache($cacheName, $data)
  11. {
  12. $memberId = session('member')['MemberId'] ?? null;
  13. $fullFileUrl = "dabory-footage/members/{$memberId}/{$cacheName}.json";
  14. Storage::put($fullFileUrl, $data);
  15. }
  16. public static function getDirectParamCache($cacheName)
  17. {
  18. $memberId = session('member')['MemberId'] ?? null;
  19. $fullFileUrl = "dabory-footage/members/{$memberId}/{$cacheName}.json";
  20. if (Storage::disk()->exists($fullFileUrl)) {
  21. return Storage::get($fullFileUrl);
  22. }
  23. return false;
  24. }
  25. public static function getParamCache($menuCode, $fileName, $queryName = null)
  26. {
  27. $memberId = session('member')['MemberId'] ?? null;
  28. $fullFileUrl = "dabory-footage/members/{$memberId}/{$menuCode}-{$fileName}.json";
  29. if (isset($queryName)) {
  30. $fullFileUrl = "dabory-footage/members/{$memberId}/{$menuCode}-{$fileName}-{$queryName}.json";
  31. }
  32. if (Storage::disk()->exists($fullFileUrl)) {
  33. return [
  34. 'url' => $fileName,
  35. 'query' => Storage::get($fullFileUrl)
  36. ];
  37. }
  38. return false;
  39. }
  40. public static function putParamCache($menuCode, $fileName, $data)
  41. {
  42. $memberId = session('member')['MemberId'] ?? null;
  43. $fullFileUrl = "dabory-footage/members/{$memberId}/{$menuCode}-{$fileName}.json";
  44. if (isset(json_decode($data, true)['QueryVars'])) {
  45. $queryName = json_decode($data, true)['QueryVars']['QueryName'];
  46. $fullFileUrl = "dabory-footage/members/{$memberId}/{$menuCode}-{$fileName}-{$queryName}.json";
  47. }
  48. Storage::put($fullFileUrl, $data);
  49. }
  50. public static function bpaDecoding($bpa)
  51. {
  52. $bpa = json_decode(base64_decode($bpa), true);
  53. return $bpa;
  54. }
  55. public static function bpaEncoding(array $menuPages): Collection
  56. {
  57. return collect($menuPages)->map(function ($menuPage) {
  58. $bpa = [
  59. 'menu_name' => $menuPage['MenuName'],
  60. 'menu_code' => $menuPage['MenuCode'],
  61. 'disable_l_menu' => $menuPage['DisableLMenu'] ?? '',
  62. 'enable_r_menu' => $menuPage['EnableRMenu'] ?? '',
  63. 'permission' => [
  64. 'is_mymenu' => $menuPage['IsMymenu'],
  65. 'is_list' => $menuPage['IsList'],
  66. 'is_read' => $menuPage['IsRead'],
  67. 'is_create' => $menuPage['IsCreate'],
  68. 'is_update' => $menuPage['IsUpdate'],
  69. 'is_delete' => $menuPage['IsDelete'],
  70. 'is_newtab' => $menuPage['IsNewtab'] ?? '',
  71. ],
  72. 'page_uri' => $menuPage['PageUri'],
  73. 'para_name' => $menuPage['ParaName'],
  74. 'theme_dir' => $menuPage['ThemeDir'] ?? '',
  75. 'main_app_id' => $menuPage['MainAppId'],
  76. 'guest_app_id' => $menuPage['GuestAppId'],
  77. 'custom_var' => $menuPage['CustomVar'],
  78. ];
  79. self::bpaDelete($menuPage);
  80. return array_merge($menuPage, ['bpa' => base64_encode(json_encode($bpa))]);
  81. });
  82. }
  83. public static function bpaDelete(array &$array): void
  84. {
  85. foreach (['IsMymenu', 'IsList',
  86. 'IsRead', 'IsCreate', 'IsUpdate', 'IsDelete',
  87. 'ParaName'] as $item) {
  88. unset($array[$item]);
  89. }
  90. }
  91. public static function formatMenuList($menuPages)
  92. {
  93. $menuList = [];
  94. foreach ($menuPages as $key => $value) {
  95. if ($value['MenuCode'] % 10000 == 0) {
  96. $menuList[] = $value;
  97. unset($menuPages[$key]);
  98. }
  99. }
  100. $menuPages = collect($menuPages)->sortBy('MenuCode');
  101. foreach ($menuPages as $key => $value) {
  102. foreach ($menuList as $k => $v) {
  103. $valArr = str_split($value['MenuCode'], 2);
  104. $vArr = str_split($v['MenuCode'], 2);
  105. if ($valArr[0] == $vArr[0] && $valArr[1] != '00' && $valArr[2] == '00') {
  106. $menuList[$k]['child'][$value['MenuCode']] = $value;
  107. unset($menuPages[$key]);
  108. }
  109. }
  110. }
  111. foreach ($menuPages as $key => $value) {
  112. foreach ($menuList as $k => $v) {
  113. if (empty($v['child'])) { continue; }
  114. foreach ($v['child'] as $i => $j) {
  115. $valArr = str_split($value['MenuCode'], 2);
  116. $jArr = str_split($i, 2);
  117. if ($valArr[0] == $jArr[0] && $valArr[1] == $jArr[1]) {
  118. $menuList[$k]['child'][$i]['child'][$value['MenuCode']] = $value;
  119. }
  120. }
  121. }
  122. }
  123. // dump($menuPages);
  124. // dd($menuList);
  125. return $menuList;
  126. }
  127. public static function getSortMenu()
  128. {
  129. $memberPermId = session('member')['MemberPermId'] ?? null;
  130. if (empty($memberPermId)) { return []; }
  131. $cacheName = "member-sort-menu";
  132. $menuCacheData = self::getDirectParamCache($cacheName);
  133. if ($menuCacheData) {
  134. return json_decode($menuCacheData, true);
  135. }
  136. $response = app(CallApiService::class)->callApi([
  137. 'url' => 'list-type1-page',
  138. 'data' => [
  139. 'QueryVars' => [
  140. 'QueryName' => 'menu-perm/sort',
  141. 'SimpleFilter' => "is_enabled = '1' and mgt_type = 'member'",
  142. 'IsntPagination' => true
  143. ],
  144. 'PageVars' => [
  145. 'Limit' => 9999999,
  146. 'Offset' => 0
  147. ]
  148. ],
  149. ]);
  150. if (! isset($response['apiStatus'])) {
  151. self::putDirectParamCache($cacheName, json_encode($response));
  152. }
  153. return $response;
  154. }
  155. public static function getMainMenu($sortType)
  156. {
  157. $memberPermId = session('member')['MemberPermId'] ?? null;
  158. if (empty($memberPermId)) { return []; }
  159. $cacheName = "member-menu/$sortType";
  160. $menuCacheData = self::getDirectParamCache($cacheName);
  161. if ($menuCacheData) {
  162. return json_decode($menuCacheData, true);
  163. }
  164. $response = app(CallApiService::class)->callApi([
  165. 'url' => 'list-type1-page',
  166. 'data' => [
  167. 'QueryVars' => [
  168. 'QueryName' => 'menu-perm/member',
  169. 'SimpleFilter' => "mb.member_perm_id = $memberPermId and sort_type = '$sortType'",
  170. 'IsntPagination' => true
  171. ],
  172. 'PageVars' => [
  173. 'Limit' => 9999999,
  174. 'Offset' => 0
  175. ]
  176. ],
  177. ]);
  178. if (! isset($response['apiStatus'])) {
  179. $response['Page'] = self::parseApiResponse($response['Page']);
  180. self::putDirectParamCache($cacheName, json_encode($response));
  181. }
  182. return $response;
  183. }
  184. public static function parseApiResponse($data): array
  185. {
  186. return collect($data)->map(function ($menu) {
  187. return [
  188. 'MemberMenuId' => $menu['Id'],
  189. 'SortType' => $menu['C1'],
  190. 'IsMymenu' => $menu['C2'],
  191. 'IsList' => $menu['C3'],
  192. 'IsRead' => $menu['C4'],
  193. 'IsCreate' => $menu['C5'],
  194. 'IsUpdate' => $menu['C6'],
  195. 'IsDelete' => $menu['C7'],
  196. 'IsNewtab' => $menu['C8'],
  197. 'MenuCode' => $menu['C9'],
  198. 'MenuName' => $menu['C10'],
  199. 'PageUri' => $menu['C11'],
  200. 'ParaName' => $menu['C12'],
  201. 'ThemeDir' => $menu['C13'],
  202. 'Icon' => $menu['C14'],
  203. 'DisableLMenu' => $menu['C15'],
  204. 'EnableRMenu' => $menu['C16'],
  205. 'MainAppId' => (int)$menu['C17'],
  206. 'GuestAppId' => (int)$menu['C18'],
  207. 'CustomVar' => $menu['C19'],
  208. ];
  209. })->toArray();
  210. }
  211. public static function getParamFile(string $settingFileName = '', $extension = '.json', $popupOptions = false)
  212. {
  213. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  214. $setPath = "para/pro/{$countryCode}{$settingFileName}{$extension}";
  215. if (file_exists(public_path($setPath))) {
  216. $paramsFilePath = file_get_contents(public_path($setPath));
  217. } else {
  218. $publicPath = public_path($setPath);
  219. if ($popupOptions) {
  220. switch ($popupOptions['OptionsType']) {
  221. case 'SelectPopupOptions':
  222. $msg = '(' . $popupOptions['OptionsType'] . ') ' . $publicPath . ' 경로를 확인하세요.';
  223. break;
  224. case 'HeadSelectOptions':
  225. case 'BodySelectOptions':
  226. $msg = "Parameter File Not Fount in {$popupOptions['Value']} ({$popupOptions['Caption']}) : {$publicPath}";
  227. break;
  228. }
  229. } else {
  230. $msg = 'Parameter File Not Fount (Main) : ' . $publicPath;
  231. }
  232. throw new ParameterException($msg);
  233. }
  234. if ($extension == '.json') {
  235. return json_decode($paramsFilePath, true );
  236. }
  237. return $paramsFilePath;
  238. }
  239. }