DataConverter.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace App\Helpers;
  3. use Carbon\Carbon;
  4. use App\Exceptions\ParameterException;
  5. use App\Helpers\ProUtils;
  6. use App\Helpers\ParameterUtils;
  7. class DataConverter
  8. {
  9. public static $codeTitle = [];
  10. public static $para = [];
  11. public static function createFromTimestamp($timestamp, $format = 'Y-m-d H:i')
  12. {
  13. return Carbon::createFromTimestamp($timestamp)->timezone('Asia/Seoul')->format($format);
  14. }
  15. public static function tranxnValue($val)
  16. {
  17. if (empty($val) || isHex($val)) { return 0; }
  18. return $val / 10000000000 / 1000000;
  19. }
  20. public static function tokenValueRev($balance, $unitPoint)
  21. {
  22. $value = $balance * (10 ** $unitPoint);
  23. return sprintf("%.0f", $value);
  24. }
  25. public static function tokenValue($balance, $unitPoint, $decimalPoint, $number_format = true)
  26. {
  27. if ($unitPoint === '' || $decimalPoint === '') {
  28. return 'API Error';
  29. }
  30. if (! is_numeric($balance)) { return 0; }
  31. if ($decimalPoint === -1) {
  32. $result = bcdiv($balance, pow(10, $unitPoint), 50);
  33. $result = rtrim($result, '0');
  34. $index = strlen($result) - strpos($result, '.') - 1;
  35. if ($index === 0) {
  36. $result = rtrim($result, '.');
  37. }
  38. $value = number_format($result, $index);
  39. if (! $number_format) {
  40. $value = convNum($value);
  41. }
  42. return $value;
  43. }
  44. else {
  45. $format = '%.f';
  46. $result = sprintf($format, $balance / pow(10, $unitPoint));
  47. $result = rtrim($result, '0');
  48. if ($unitPoint < 0) {
  49. return number_format($result * 0.001, $decimalPoint);
  50. }
  51. return number_format($result, $decimalPoint);
  52. }
  53. }
  54. public static function readParameter($path)
  55. {
  56. $parameter = ProUtils::getParamFile($path);
  57. $parameter['IncludeList'] = [];
  58. foreach (['FormVars', 'ListVars', 'FooterVars', 'ListType1RangeVars'] as $varsName) {
  59. if (array_key_exists($varsName, $parameter)) {
  60. ParameterUtils::separateAlignAndFormat($parameter, $varsName);
  61. ParameterUtils::checkDisplayAndCount($parameter, $varsName);
  62. ParameterUtils::mappingKeys($parameter, $varsName);
  63. if (array_key_exists('Target', $parameter[$varsName])) {
  64. foreach ($parameter[$varsName]['Target'] as $key => $target) {
  65. if ($target) {
  66. $parameter['IncludeList'][] = self::execute('', $target);
  67. }
  68. }
  69. // dd($parameter[$varsName]['Target']);
  70. }
  71. }
  72. }
  73. return $parameter;
  74. }
  75. public static function execute($data, $format)
  76. {
  77. switch ($format) {
  78. case 'YY-MM-DD': case 'YY.MM.DD': case 'YYMMDD':
  79. case 'yy-mm-dd': case 'yy.mm.dd': case 'yymmdd':
  80. $result = self::createFromTimestamp($data, $format);
  81. break;
  82. case 'diffForHumans':
  83. $result = Carbon::parse((int)$data)->diffForHumans();
  84. break;
  85. case 'check':
  86. if ($data == 1 || $data == true) {
  87. $result = '✓';
  88. } else {
  89. $result = '';
  90. }
  91. break;
  92. default:
  93. if (preg_match("/[A-Za-z]+\s*\(\s*'(.*?)\'\s*\)\s*/", $format)) {
  94. if (! isset($data)) {
  95. $funcName = $format;
  96. } else {
  97. $funcName = str_replace(')', ", '${data}')", $format);
  98. }
  99. eval('$result = ' . "self::format_func_{$funcName};");
  100. } else {
  101. $result = $data;
  102. }
  103. break;
  104. }
  105. return $result;
  106. }
  107. public static function format_func_target_modal($paraPath, $component)
  108. {
  109. return [
  110. 'Parameter' => self::readParameter($paraPath),
  111. 'Component' => $component,
  112. ];
  113. }
  114. public static function format_func_decimal($value, $data)
  115. {
  116. return number_format($value, $data);
  117. }
  118. public static function format_func_status($value, $data)
  119. {
  120. return self::$codeTitle['status'][$value][$data]['Title'] ?? 'Invalid';
  121. }
  122. public static function format_func_sort($value, $data)
  123. {
  124. return self::$codeTitle['sort'][$value][$data]['Title'] ?? 'Invalid';
  125. }
  126. public static function format_func_deal_type($value, $data)
  127. {
  128. return self::$codeTitle['deal-type'][$value][$data]['Title'] ?? 'Invalid';
  129. }
  130. public static function format_func_paymethod($value, $data)
  131. {
  132. return self::$codeTitle['paymethod'][$value][$data]['Title'] ?? 'Invalid';
  133. }
  134. public static function format_func_situation($value, $data)
  135. {
  136. return self::$codeTitle['situation'][$value][$data]['Title'] ?? 'Invalid';
  137. }
  138. public static function format_func_bill_type($value, $data)
  139. {
  140. return self::$codeTitle['bill-type'][$value][$data]['Title'] ?? 'Invalid';
  141. }
  142. public static function format_func_device_type($value, $data)
  143. {
  144. return self::$codeTitle['device-type'][$value][$data]['Title'] ?? 'Invalid';
  145. }
  146. public static function format_func_lang_type($value, $data)
  147. {
  148. return self::$codeTitle['lang-type'][$value][$data]['Title'] ?? 'Invalid';
  149. }
  150. public static function format_func_sort_type($value, $data)
  151. {
  152. return self::$codeTitle['sort-type'][$value][$data]['Title'] ?? 'Invalid';
  153. }
  154. public static function format_func_setup_code($value, $data)
  155. {
  156. return self::$codeTitle['setup-code'][$value][$data]['Title'] ?? 'Invalid';
  157. }
  158. public static function format_func_ship_type($value, $data)
  159. {
  160. return self::$codeTitle['ship-type'][$value][$data]['Title'] ?? 'Invalid';
  161. }
  162. public static function format_func_body_situation($value, $data)
  163. {
  164. return self::$codeTitle['body-situation'][$value][$data]['Title'] ?? 'Invalid';
  165. }
  166. public static function get_status($value)
  167. {
  168. if (isset(self::$codeTitle['status'][$value])) {
  169. return;
  170. }
  171. try {
  172. self::$codeTitle['status'][$value] = collect(Utils::getParamFile("/etc/code-title/status/{$value}"))->keyBy('Code')->toArray();
  173. } catch (ParameterException $e) {
  174. self::$codeTitle['status'][$value] = null;
  175. }
  176. }
  177. public static function get_shop_status($value)
  178. {
  179. if (isset(self::$codeTitle['shop-status'][$value])) {
  180. return;
  181. }
  182. try {
  183. self::$codeTitle['shop-status'][$value] = collect(Utils::getParamFile("/etc/code-title/shop-status/{$value}"))->keyBy('Code')->toArray();
  184. } catch (ParameterException $e) {
  185. self::$codeTitle['shop-status'][$value] = null;
  186. }
  187. }
  188. public static function get_sort($value)
  189. {
  190. if (isset(self::$codeTitle['sort'][$value])) {
  191. return;
  192. }
  193. try {
  194. self::$codeTitle['sort'][$value] = collect(Utils::getParamFile("/etc/code-title/sort/{$value}"))->keyBy('Code')->toArray();
  195. } catch (ParameterException $e) {
  196. self::$codeTitle['sort'][$value] = null;
  197. }
  198. }
  199. public static function get_deal_type($value)
  200. {
  201. if (isset(self::$codeTitle['deal-type'][$value])) {
  202. return;
  203. }
  204. try {
  205. self::$codeTitle['deal-type'][$value] = collect(Utils::getParamFile("/etc/code-title/deal-type/{$value}"))->keyBy('Code')->toArray();
  206. } catch (ParameterException $e) {
  207. self::$codeTitle['deal-type'][$value] = null;
  208. }
  209. }
  210. public static function get_paymethod($value)
  211. {
  212. if (isset(self::$codeTitle['paymethod'][$value])) {
  213. return;
  214. }
  215. try {
  216. self::$codeTitle['paymethod'][$value] = collect(Utils::getParamFile("/etc/code-title/paymethod/{$value}"))->keyBy('Code')->toArray();
  217. } catch (ParameterException $e) {
  218. self::$codeTitle['paymethod'][$value] = null;
  219. }
  220. }
  221. public static function get_situation($value)
  222. {
  223. if (isset(self::$codeTitle['situation'][$value])) {
  224. return;
  225. }
  226. try {
  227. self::$codeTitle['situation'][$value] = collect(Utils::getParamFile("/etc/code-title/situation/{$value}"))->keyBy('Code')->toArray();
  228. } catch (ParameterException $e) {
  229. self::$codeTitle['situation'][$value] = null;
  230. }
  231. }
  232. public static function get_bill_type($value)
  233. {
  234. if (isset(self::$codeTitle['bill-type'][$value])) {
  235. return;
  236. }
  237. try {
  238. self::$codeTitle['bill-type'][$value] = collect(Utils::getParamFile("/etc/code-title/bill-type/{$value}"))->keyBy('Code')->toArray();
  239. } catch (ParameterException $e) {
  240. self::$codeTitle['bill-type'][$value] = null;
  241. }
  242. }
  243. public static function get_device_type($value)
  244. {
  245. if (isset(self::$codeTitle['device-type'][$value])) {
  246. return;
  247. }
  248. try {
  249. self::$codeTitle['device-type'][$value] = collect(Utils::getParamFile("/etc/code-title/device-type/{$value}"))->keyBy('Code')->toArray();
  250. } catch (ParameterException $e) {
  251. self::$codeTitle['device-type'][$value] = null;
  252. }
  253. }
  254. public static function get_lang_type($value)
  255. {
  256. if (isset(self::$codeTitle['lang-type'][$value])) {
  257. return;
  258. }
  259. try {
  260. self::$codeTitle['lang-type'][$value] = collect(Utils::getParamFile("/etc/code-title/lang-type/{$value}"))->keyBy('Code')->toArray();
  261. } catch (ParameterException $e) {
  262. self::$codeTitle['lang-type'][$value] = null;
  263. }
  264. }
  265. public static function get_sort_type($value)
  266. {
  267. if (isset(self::$codeTitle['sort-type'][$value])) {
  268. return;
  269. }
  270. try {
  271. self::$codeTitle['sort-type'][$value] = collect(Utils::getParamFile("/etc/code-title/sort-type/{$value}"))->keyBy('Code')->toArray();
  272. } catch (ParameterException $e) {
  273. self::$codeTitle['sort-type'][$value] = null;
  274. }
  275. }
  276. public static function get_setup_code($value)
  277. {
  278. if (isset(self::$codeTitle['setup-code'][$value])) {
  279. return;
  280. }
  281. try {
  282. self::$codeTitle['setup-code'][$value] = collect(Utils::getParamFile("/etc/code-title/setup-code/{$value}"))->keyBy('Code')->toArray();
  283. } catch (ParameterException $e) {
  284. self::$codeTitle['setup-code'][$value] = null;
  285. }
  286. }
  287. public static function get_expose_type($value)
  288. {
  289. if (isset(self::$codeTitle['expose-type'][$value])) {
  290. return;
  291. }
  292. try {
  293. self::$codeTitle['expose-type'][$value] = collect(Utils::getParamFile("/etc/code-title/expose-type/{$value}"))->keyBy('Code')->toArray();
  294. } catch (ParameterException $e) {
  295. self::$codeTitle['expose-type'][$value] = null;
  296. }
  297. }
  298. public static function get_ship_type($value)
  299. {
  300. if (isset(self::$codeTitle['ship-type'][$value])) {
  301. return;
  302. }
  303. try {
  304. self::$codeTitle['ship-type'][$value] = collect(Utils::getParamFile("/etc/code-title/ship-type/{$value}"))->keyBy('Code')->toArray();
  305. } catch (ParameterException $e) {
  306. self::$codeTitle['ship-type'][$value] = null;
  307. }
  308. }
  309. public static function get_delay_type($value)
  310. {
  311. if (isset(self::$codeTitle['delay-type'][$value])) {
  312. return;
  313. }
  314. try {
  315. self::$codeTitle['delay-type'][$value] = collect(Utils::getParamFile("/etc/code-title/delay-type/{$value}"))->keyBy('Code')->toArray();
  316. } catch (ParameterException $e) {
  317. self::$codeTitle['delay-type'][$value] = null;
  318. }
  319. }
  320. public static function get_cargo_type($value)
  321. {
  322. if (isset(self::$codeTitle['cargo-type'][$value])) {
  323. return;
  324. }
  325. try {
  326. self::$codeTitle['cargo-type'][$value] = collect(Utils::getParamFile("/etc/code-title/cargo-type/{$value}"))->keyBy('Code')->toArray();
  327. } catch (ParameterException $e) {
  328. self::$codeTitle['cargo-type'][$value] = null;
  329. }
  330. }
  331. public static function get_condition_type($value)
  332. {
  333. if (isset(self::$codeTitle['condition-type'][$value])) {
  334. return;
  335. }
  336. try {
  337. self::$codeTitle['condition-type'][$value] = collect(Utils::getParamFile("/etc/code-title/condition-type/{$value}"))->keyBy('Code')->toArray();
  338. } catch (ParameterException $e) {
  339. self::$codeTitle['condition-type'][$value] = null;
  340. }
  341. }
  342. public static function get_body_situation($value)
  343. {
  344. if (isset(self::$codeTitle['body-situation'][$value])) {
  345. return;
  346. }
  347. try {
  348. self::$codeTitle['body-situation'][$value] = collect(Utils::getParamFile("/etc/code-title/body-situation/{$value}"))->keyBy('Code')->toArray();
  349. } catch (ParameterException $e) {
  350. self::$codeTitle['body-situation'][$value] = null;
  351. }
  352. }
  353. }