DataConverter.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 get_status($value)
  163. {
  164. if (isset(self::$codeTitle['status'][$value])) {
  165. return;
  166. }
  167. try {
  168. self::$codeTitle['status'][$value] = collect(Utils::getParamFile("/etc/code-title/status/{$value}"))->keyBy('Code')->toArray();
  169. } catch (ParameterException $e) {
  170. self::$codeTitle['status'][$value] = null;
  171. }
  172. }
  173. public static function get_shop_status($value)
  174. {
  175. if (isset(self::$codeTitle['shop-status'][$value])) {
  176. return;
  177. }
  178. try {
  179. self::$codeTitle['shop-status'][$value] = collect(Utils::getParamFile("/etc/code-title/shop-status/{$value}"))->keyBy('Code')->toArray();
  180. } catch (ParameterException $e) {
  181. self::$codeTitle['shop-status'][$value] = null;
  182. }
  183. }
  184. public static function get_sort($value)
  185. {
  186. if (isset(self::$codeTitle['sort'][$value])) {
  187. return;
  188. }
  189. try {
  190. self::$codeTitle['sort'][$value] = collect(Utils::getParamFile("/etc/code-title/sort/{$value}"))->keyBy('Code')->toArray();
  191. } catch (ParameterException $e) {
  192. self::$codeTitle['sort'][$value] = null;
  193. }
  194. }
  195. public static function get_deal_type($value)
  196. {
  197. if (isset(self::$codeTitle['deal-type'][$value])) {
  198. return;
  199. }
  200. try {
  201. self::$codeTitle['deal-type'][$value] = collect(Utils::getParamFile("/etc/code-title/deal-type/{$value}"))->keyBy('Code')->toArray();
  202. } catch (ParameterException $e) {
  203. self::$codeTitle['deal-type'][$value] = null;
  204. }
  205. }
  206. public static function get_paymethod($value)
  207. {
  208. if (isset(self::$codeTitle['paymethod'][$value])) {
  209. return;
  210. }
  211. try {
  212. self::$codeTitle['paymethod'][$value] = collect(Utils::getParamFile("/etc/code-title/paymethod/{$value}"))->keyBy('Code')->toArray();
  213. } catch (ParameterException $e) {
  214. self::$codeTitle['paymethod'][$value] = null;
  215. }
  216. }
  217. public static function get_situation($value)
  218. {
  219. if (isset(self::$codeTitle['situation'][$value])) {
  220. return;
  221. }
  222. try {
  223. self::$codeTitle['situation'][$value] = collect(Utils::getParamFile("/etc/code-title/situation/{$value}"))->keyBy('Code')->toArray();
  224. } catch (ParameterException $e) {
  225. self::$codeTitle['situation'][$value] = null;
  226. }
  227. }
  228. public static function get_bill_type($value)
  229. {
  230. if (isset(self::$codeTitle['bill-type'][$value])) {
  231. return;
  232. }
  233. try {
  234. self::$codeTitle['bill-type'][$value] = collect(Utils::getParamFile("/etc/code-title/bill-type/{$value}"))->keyBy('Code')->toArray();
  235. } catch (ParameterException $e) {
  236. self::$codeTitle['bill-type'][$value] = null;
  237. }
  238. }
  239. public static function get_device_type($value)
  240. {
  241. if (isset(self::$codeTitle['device-type'][$value])) {
  242. return;
  243. }
  244. try {
  245. self::$codeTitle['device-type'][$value] = collect(Utils::getParamFile("/etc/code-title/device-type/{$value}"))->keyBy('Code')->toArray();
  246. } catch (ParameterException $e) {
  247. self::$codeTitle['device-type'][$value] = null;
  248. }
  249. }
  250. public static function get_lang_type($value)
  251. {
  252. if (isset(self::$codeTitle['lang-type'][$value])) {
  253. return;
  254. }
  255. try {
  256. self::$codeTitle['lang-type'][$value] = collect(Utils::getParamFile("/etc/code-title/lang-type/{$value}"))->keyBy('Code')->toArray();
  257. } catch (ParameterException $e) {
  258. self::$codeTitle['lang-type'][$value] = null;
  259. }
  260. }
  261. public static function get_sort_type($value)
  262. {
  263. if (isset(self::$codeTitle['sort-type'][$value])) {
  264. return;
  265. }
  266. try {
  267. self::$codeTitle['sort-type'][$value] = collect(Utils::getParamFile("/etc/code-title/sort-type/{$value}"))->keyBy('Code')->toArray();
  268. } catch (ParameterException $e) {
  269. self::$codeTitle['sort-type'][$value] = null;
  270. }
  271. }
  272. public static function get_setup_code($value)
  273. {
  274. if (isset(self::$codeTitle['setup-code'][$value])) {
  275. return;
  276. }
  277. try {
  278. self::$codeTitle['setup-code'][$value] = collect(Utils::getParamFile("/etc/code-title/setup-code/{$value}"))->keyBy('Code')->toArray();
  279. } catch (ParameterException $e) {
  280. self::$codeTitle['setup-code'][$value] = null;
  281. }
  282. }
  283. public static function get_expose_type($value)
  284. {
  285. if (isset(self::$codeTitle['expose-type'][$value])) {
  286. return;
  287. }
  288. try {
  289. self::$codeTitle['expose-type'][$value] = collect(Utils::getParamFile("/etc/code-title/expose-type/{$value}"))->keyBy('Code')->toArray();
  290. } catch (ParameterException $e) {
  291. self::$codeTitle['expose-type'][$value] = null;
  292. }
  293. }
  294. public static function get_ship_type($value)
  295. {
  296. if (isset(self::$codeTitle['ship-type'][$value])) {
  297. return;
  298. }
  299. try {
  300. self::$codeTitle['ship-type'][$value] = collect(Utils::getParamFile("/etc/code-title/ship-type/{$value}"))->keyBy('Code')->toArray();
  301. } catch (ParameterException $e) {
  302. self::$codeTitle['ship-type'][$value] = null;
  303. }
  304. }
  305. public static function get_delay_type($value)
  306. {
  307. if (isset(self::$codeTitle['delay-type'][$value])) {
  308. return;
  309. }
  310. try {
  311. self::$codeTitle['delay-type'][$value] = collect(Utils::getParamFile("/etc/code-title/delay-type/{$value}"))->keyBy('Code')->toArray();
  312. } catch (ParameterException $e) {
  313. self::$codeTitle['delay-type'][$value] = null;
  314. }
  315. }
  316. public static function get_cargo_type($value)
  317. {
  318. if (isset(self::$codeTitle['cargo-type'][$value])) {
  319. return;
  320. }
  321. try {
  322. self::$codeTitle['cargo-type'][$value] = collect(Utils::getParamFile("/etc/code-title/cargo-type/{$value}"))->keyBy('Code')->toArray();
  323. } catch (ParameterException $e) {
  324. self::$codeTitle['cargo-type'][$value] = null;
  325. }
  326. }
  327. public static function get_condition_type($value)
  328. {
  329. if (isset(self::$codeTitle['condition-type'][$value])) {
  330. return;
  331. }
  332. try {
  333. self::$codeTitle['condition-type'][$value] = collect(Utils::getParamFile("/etc/code-title/condition-type/{$value}"))->keyBy('Code')->toArray();
  334. } catch (ParameterException $e) {
  335. self::$codeTitle['condition-type'][$value] = null;
  336. }
  337. }
  338. }