CommonUtils.php 7.1 KB

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