ProUtils.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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')['MemberPermId'] ?? 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')['MemberPermId'] ?? 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. 'tabbed_menu_hash' => $menuPage['TabbedMenuHash'],
  79. ];
  80. self::bpaDelete($menuPage);
  81. return array_merge($menuPage, ['bpa' => base64_encode(json_encode($bpa))]);
  82. });
  83. }
  84. public static function bpaDelete(array &$array): void
  85. {
  86. foreach (['IsMymenu', 'IsList',
  87. 'IsRead', 'IsCreate', 'IsUpdate', 'IsDelete',
  88. 'ParaName'] as $item) {
  89. unset($array[$item]);
  90. }
  91. }
  92. public static function formatMenuList($menuPages)
  93. {
  94. $menuList = [];
  95. foreach ($menuPages as $key => $value) {
  96. if ($value['MenuCode'] % 10000 == 0) {
  97. $menuList[] = $value;
  98. unset($menuPages[$key]);
  99. }
  100. }
  101. $menuPages = collect($menuPages)->sortBy('MenuCode');
  102. foreach ($menuPages as $key => $value) {
  103. foreach ($menuList as $k => $v) {
  104. $valArr = str_split($value['MenuCode'], 2);
  105. $vArr = str_split($v['MenuCode'], 2);
  106. if ($valArr[0] == $vArr[0] && $valArr[1] != '00' && $valArr[2] == '00') {
  107. $menuList[$k]['child'][$value['MenuCode']] = $value;
  108. unset($menuPages[$key]);
  109. }
  110. }
  111. }
  112. foreach ($menuPages as $key => $value) {
  113. foreach ($menuList as $k => $v) {
  114. if (empty($v['child'])) { continue; }
  115. foreach ($v['child'] as $i => $j) {
  116. $valArr = str_split($value['MenuCode'], 2);
  117. $jArr = str_split($i, 2);
  118. if ($valArr[0] == $jArr[0] && $valArr[1] == $jArr[1]) {
  119. $menuList[$k]['child'][$i]['child'][$value['MenuCode']] = $value;
  120. }
  121. }
  122. }
  123. }
  124. // dump($menuPages);
  125. // dd($menuList);
  126. return $menuList;
  127. }
  128. public static function getSortMenu()
  129. {
  130. $memberPermId = session('member')['MemberPermId'] ?? null;
  131. if (empty($memberPermId)) { return []; }
  132. $cacheName = "member-sort-menu";
  133. $menuCacheData = self::getDirectParamCache($cacheName);
  134. if ($menuCacheData) {
  135. return json_decode($menuCacheData, true);
  136. }
  137. $response = app(CallApiService::class)->callApi([
  138. 'url' => 'list-type1-page',
  139. 'data' => [
  140. 'QueryVars' => [
  141. 'QueryName' => 'menu-perm/sort',
  142. 'SimpleFilter' => "is_enabled = '1' and mgt_type = 'member'",
  143. 'IsntPagination' => true
  144. ],
  145. 'PageVars' => [
  146. 'Limit' => 9999999,
  147. 'Offset' => 0
  148. ]
  149. ],
  150. ]);
  151. if (! isset($response['apiStatus'])) {
  152. self::putDirectParamCache($cacheName, json_encode($response));
  153. }
  154. return $response;
  155. }
  156. public static function getMainMenu($sortType)
  157. {
  158. $memberPermId = session('member')['MemberPermId'] ?? null;
  159. if (empty($memberPermId)) { return []; }
  160. $cacheName = "member-menu/$sortType";
  161. $menuCacheData = self::getDirectParamCache($cacheName);
  162. if ($menuCacheData) {
  163. return json_decode($menuCacheData, true);
  164. }
  165. $simpleFilter = "mb.member_perm_id = $memberPermId";
  166. if ($sortType !== 'all') {
  167. $simpleFilter .= " and sort_type = '$sortType'";
  168. }
  169. $response = app(CallApiService::class)->callApi([
  170. 'url' => 'list-type1-page',
  171. 'data' => [
  172. 'QueryVars' => [
  173. 'QueryName' => 'menu-perm/member',
  174. 'SimpleFilter' => $simpleFilter,
  175. 'IsntPagination' => true
  176. ],
  177. 'PageVars' => [
  178. 'Limit' => 9999999,
  179. 'Offset' => 0
  180. ]
  181. ],
  182. ]);
  183. if (! isset($response['apiStatus'])) {
  184. $response['Page'] = self::parseApiResponse($response['Page']);
  185. self::putDirectParamCache($cacheName, json_encode($response));
  186. }
  187. return $response;
  188. }
  189. public static function parseApiResponse($data): array
  190. {
  191. return collect($data)->map(function ($menu) {
  192. return [
  193. 'MemberMenuId' => $menu['Id'],
  194. 'SortType' => $menu['C1'],
  195. 'IsMymenu' => $menu['C2'],
  196. 'IsList' => $menu['C3'],
  197. 'IsRead' => $menu['C4'],
  198. 'IsCreate' => $menu['C5'],
  199. 'IsUpdate' => $menu['C6'],
  200. 'IsDelete' => $menu['C7'],
  201. 'IsNewtab' => $menu['C8'],
  202. 'MenuCode' => $menu['C9'],
  203. 'MenuName' => $menu['C10'],
  204. 'PageUri' => $menu['C11'],
  205. 'ParaName' => $menu['C12'],
  206. 'ThemeDir' => $menu['C13'],
  207. 'Icon' => $menu['C14'],
  208. 'DisableLMenu' => $menu['C15'],
  209. 'EnableRMenu' => $menu['C16'],
  210. 'MainAppId' => (int)$menu['C17'],
  211. 'GuestAppId' => (int)$menu['C18'],
  212. 'CustomVar' => $menu['C19'],
  213. 'TabbedMenuHash' => $menu['C20'],
  214. ];
  215. })->toArray();
  216. }
  217. public static function getParamFile(string $settingFileName = '', $extension = '.json', $popupOptions = false)
  218. {
  219. $countryCode = session('user')['CountryCode'] ?? config('constants.countries')[0];
  220. $setPath = "para/pro/{$countryCode}{$settingFileName}{$extension}";
  221. if (file_exists(public_path($setPath))) {
  222. $paramsFilePath = file_get_contents(public_path($setPath));
  223. } else {
  224. $publicPath = public_path($setPath);
  225. if ($popupOptions) {
  226. switch ($popupOptions['OptionsType']) {
  227. case 'SelectPopupOptions':
  228. $msg = '(' . $popupOptions['OptionsType'] . ') ' . $publicPath . ' 경로를 확인하세요.';
  229. break;
  230. case 'HeadSelectOptions':
  231. case 'BodySelectOptions':
  232. $msg = "Parameter File Not Fount in {$popupOptions['Value']} ({$popupOptions['Caption']}) : {$publicPath}";
  233. break;
  234. }
  235. } else {
  236. $msg = 'Parameter File Not Fount (Main) : ' . $publicPath;
  237. }
  238. throw new ParameterException($msg);
  239. }
  240. if ($extension == '.json') {
  241. return json_decode($paramsFilePath, true );
  242. }
  243. return $paramsFilePath;
  244. }
  245. }