CommonUtils.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\common;
  3. use Ramsey\Uuid\Uuid;
  4. use think\facade\Cache;
  5. use think\helper\Str;
  6. class CommonUtils
  7. {
  8. /// 读取游戏类型
  9. public static function getGameTypeConfig($type = '2') {
  10. return $GLOBALS['gameGameTypeConfig'][$type];
  11. }
  12. /// 默认游戏图标
  13. public static function getGameDefaultImage($item = []) {
  14. $staticDomain = $GLOBALS['gameGameStaticDomain'];
  15. // 中文图标
  16. if(empty($item['image'])) {
  17. $item['image_url'] = "//{$staticDomain}/{$item['game_id']}/zh.png";
  18. }else {
  19. $item['image_url'] = $item['image'];
  20. }
  21. // 英文图标
  22. if(empty($item['image_en'])) {
  23. $item['image_en_url'] = "//{$staticDomain}/{$item['game_id']}/en.png";
  24. }else {
  25. $item['image_en_url'] = $item['image_en'];
  26. }
  27. return $item;
  28. }
  29. // 转换 分数 * 10000 or / 10000
  30. public static function convertBalance($balance = 0, $isTenThousandfold = true) {
  31. // $balance = (float)$balance;
  32. // // 分数小数点 * 10000;
  33. // // $GLOBALS['balanceTenThousandfold'] = 10000;
  34. // $balanceTenThousandfold = 10000;
  35. // if ($isTenThousandfold) {
  36. // $balance = bcmul($balance, $balanceTenThousandfold);
  37. // } else {
  38. // $balance = bcdiv($balance, $balanceTenThousandfold, 4);
  39. // }
  40. return (float)$balance;
  41. }
  42. /**
  43. * 格式化为整数
  44. */
  45. public static function convertToInt($value, int $defaultValue = 0) {
  46. if (isset($value)) {
  47. return (int)$value;
  48. }
  49. return $defaultValue;
  50. }
  51. // 生成转账流水ID
  52. public static function generateUuidSn($key = 'OD')
  53. {
  54. $uuid = Uuid::uuid4()->toString(); // 生成 UUID v4
  55. $uuid = str_replace('-', '', $uuid); // 移除连字符
  56. return 'OD' . $uuid; // 示例:OD550e8400e29b41d4a71644665544000
  57. }
  58. // 生成订单 单号
  59. public static function generateThirdOrderId($key = 'ega'): string
  60. {
  61. // 时间戳
  62. $timestamp = date('YmdHis'); // e.g., "20250319073600"
  63. // 前缀:ega + 第4位基于时间递增
  64. $currentTime = time();
  65. $startTime = strtotime(date('Y-m-d 00:00:00', $currentTime)); // 假设起始时间
  66. // 计算 15 分钟间隔
  67. $fifteenMinuteIntervals = floor(($currentTime - $startTime) / (15 * 60)); // 82
  68. // 限制在 'e' 到 'z' (101 到 122),共 22 个字符
  69. $increment = $fifteenMinuteIntervals % 22; // 82 % 22 = 16
  70. $fourthChar = chr(101 + $increment); // 101 + 16 = 117,'u'
  71. $prefix = $key . $fourthChar; // e.g., "egae", "egaf"
  72. // 8位随机字符串
  73. $random = Str::random(8,3,'0123456789'); // 生成 8 位小写字母和数字混合的随机字符串 // 组合
  74. return $timestamp . $prefix . $random;
  75. }
  76. // 生成局ID $prefix = '19'; // 服务器标识 $ext // 备用
  77. public static function generateFlowId($prefix = '19', $ext = '000'): string
  78. {
  79. $prefix = $prefix ?? '19';
  80. // 获取微秒级时间戳
  81. $microtime = microtime(true); // e.g., 1743088050.123456
  82. $timestamp = date('YmdHis', (int)$microtime) . sprintf('%06d', floor(($microtime - floor($microtime)) * 1000000));
  83. $time_part = substr($timestamp, 2, 14); // 14 位时间,例如 "25032516073012"
  84. // Redis 键名:基于毫秒时间戳(精确到毫秒)
  85. $redisKey = 'order:counter:' . substr($timestamp, 0, 14); // e.g., "order:counter:20250325160730"
  86. // 使用 Redis INCR 获取计数器值,并在首次设置时设置过期时间
  87. $redis = Cache::store('redis')->handler(); // 获取 Redis 实例
  88. $counter = $redis->incr($redisKey); // 计数器从 1 开始递增
  89. // 如果是第一次递增,设置过期时间(1 秒)
  90. if ($counter == 1) {
  91. $redis->expire($redisKey, 1); // 1 秒后过期
  92. }
  93. // 转换为 3 位字符串,减 1 使其从 000 开始
  94. $suffix = sprintf('%03d', $counter - 1); // e.g., "000", "001", "002"
  95. // 拼接 19 位流水 ID
  96. $flow_id = $time_part . $prefix . $suffix . $ext;
  97. return $flow_id;
  98. }
  99. /**
  100. * 获取游戏玩法名称
  101. * @param $type
  102. * @return string
  103. */
  104. public static function getGamePlayType($type = 0) {
  105. return $GLOBALS['gamePgGameConfig']['play_type'][$type] ?? "";
  106. }
  107. /**
  108. * 统一游戏成功返回字段
  109. * @param $data
  110. * @return \think\response\Json
  111. */
  112. public static function gameJsonSuccess($data = []) {
  113. return json([
  114. 'dt' => $data,
  115. 'err' => null,
  116. ]);
  117. }
  118. /**
  119. * 统一游戏失败字段
  120. * @param $code
  121. * @param $msg
  122. * @param $data
  123. */
  124. public static function gameJsonError($code = "1004", $msg = "", $data =[], $is_json = true) {
  125. /**
  126. * cd: '1201',
  127. * msg: 'GameStateNotFoundException',
  128. * tid: 'HQIIMN11',
  129. *
  130. * { cd: '1200', msg: 'Internal server error.', tid: 'LAAUWY01', at: 1 }
  131. * * * cd: '1403',
  132. * * msg: 'We are undergoing maintenance, please try again later.',
  133. * * tid: 'SH8Q14K6'
  134. * }
  135. * cd: '50038',
  136. * msg: '游戏不存在:%!d(string=cocktail-nite)',
  137. * tid: 'S2VAFQQ9'
  138. * { cd: '500', msg: 'protobuf data too short', tid: '4UOFS6OA' }
  139. * err: { cd: '2', msg: 'is small game ont fb', tid: 'AFJGTF6B' }
  140. *
  141. * {cd: "1302", msg: "Invalid player session", tid: "PGTVSW16", at: null}
  142. *
  143. * {"cd":"1308","msg":"Player session is expired","tid":"PBSMUF18","at":null}
  144. * Access denied.
  145. * err: {
  146. * cd: '3202',
  147. * msg: 'OERR:Not enough cash.',
  148. * tid: 'JSXSWO82',
  149. * at: null
  150. * }
  151. */
  152. $temp = [
  153. 'dt' => null,
  154. 'err' => array_merge([
  155. "cd" => $code,
  156. "msg" => $msg
  157. ]),
  158. 'td' => time(),
  159. ];
  160. if(!empty($data)) {
  161. $temp = array_merge($temp, $data);
  162. }
  163. if(!$is_json) {
  164. return $temp;
  165. }
  166. return json($temp);
  167. }
  168. /**
  169. * 格式化数据
  170. * @param $item
  171. * @param $change
  172. * @return mixed
  173. */
  174. public static function formatResultData($item, $change) {
  175. // $item['tb'] = CommonUtils::convertBalance($item['tb'], $change);
  176. // $item['tbb'] = CommonUtils::convertBalance($item['tbb'],$change);
  177. // $item['np'] = CommonUtils::convertBalance($item['np'],$change);
  178. // $item['ctw'] = CommonUtils::convertBalance($item['ctw'],$change);
  179. // $keysList = ['cptw','atw', 'tb', 'tbb','np', 'ctw'];
  180. // foreach ($keysList as $key) {
  181. // if(isset($item[$key])) {
  182. // $item[$key] = CommonUtils::convertBalance($item[$key],$change);
  183. // }
  184. // }
  185. // $item['aw'] = CommonUtils::convertBalance($item['aw'],$change);
  186. // if(!empty($item['lw'])) {
  187. // foreach ($item['lw'] as $key => $lw) {
  188. // if(is_array($lw)) {
  189. // foreach ($lw as $k => $templw) {
  190. // $lw[$k] = CommonUtils::convertBalance($templw,$change);
  191. // }
  192. // $item['lw'][$key] = $lw;
  193. // }else {
  194. // $item['lw'][$key] = CommonUtils::convertBalance($lw,$change);
  195. // }
  196. // }
  197. // }
  198. return $item;
  199. }
  200. }