toString(); // 生成 UUID v4 $uuid = str_replace('-', '', $uuid); // 移除连字符 return 'OD' . $uuid; // 示例:OD550e8400e29b41d4a71644665544000 } // 生成订单 单号 public static function generateThirdOrderId($key = 'ega'): string { // 时间戳 $timestamp = date('YmdHis'); // e.g., "20250319073600" // 前缀:ega + 第4位基于时间递增 $currentTime = time(); $startTime = strtotime(date('Y-m-d 00:00:00', $currentTime)); // 假设起始时间 // 计算 15 分钟间隔 $fifteenMinuteIntervals = floor(($currentTime - $startTime) / (15 * 60)); // 82 // 限制在 'e' 到 'z' (101 到 122),共 22 个字符 $increment = $fifteenMinuteIntervals % 22; // 82 % 22 = 16 $fourthChar = chr(101 + $increment); // 101 + 16 = 117,'u' $prefix = $key . $fourthChar; // e.g., "egae", "egaf" // 8位随机字符串 $random = Str::random(8,3,'0123456789'); // 生成 8 位小写字母和数字混合的随机字符串 // 组合 return $timestamp . $prefix . $random; } // 生成局ID $prefix = '19'; // 服务器标识 $ext // 备用 public static function generateFlowId($prefix = '19', $ext = '000'): string { $prefix = $prefix ?? '19'; // 获取微秒级时间戳 $microtime = microtime(true); // e.g., 1743088050.123456 $timestamp = date('YmdHis', (int)$microtime) . sprintf('%06d', floor(($microtime - floor($microtime)) * 1000000)); $time_part = substr($timestamp, 2, 14); // 14 位时间,例如 "25032516073012" // Redis 键名:基于毫秒时间戳(精确到毫秒) $redisKey = 'order:counter:' . substr($timestamp, 0, 14); // e.g., "order:counter:20250325160730" // 使用 Redis INCR 获取计数器值,并在首次设置时设置过期时间 $redis = Cache::store('redis')->handler(); // 获取 Redis 实例 $counter = $redis->incr($redisKey); // 计数器从 1 开始递增 // 如果是第一次递增,设置过期时间(1 秒) if ($counter == 1) { $redis->expire($redisKey, 1); // 1 秒后过期 } // 转换为 3 位字符串,减 1 使其从 000 开始 $suffix = sprintf('%03d', $counter - 1); // e.g., "000", "001", "002" // 拼接 19 位流水 ID $flow_id = $time_part . $prefix . $suffix . $ext; return $flow_id; } /** * 获取游戏玩法名称 * @param $type * @return string */ public static function getGamePlayType($type = 0) { return $GLOBALS['gamePgGameConfig']['play_type'][$type] ?? ""; } /** * 统一游戏成功返回字段 * @param $data * @return \think\response\Json */ public static function gameJsonSuccess($data = []) { return json([ 'dt' => $data, 'err' => null, ]); } /** * 统一游戏失败字段 * @param $code * @param $msg * @param $data */ public static function gameJsonError($code = "1004", $msg = "", $data =[], $is_json = true) { /** * cd: '1201', * msg: 'GameStateNotFoundException', * tid: 'HQIIMN11', * * { cd: '1200', msg: 'Internal server error.', tid: 'LAAUWY01', at: 1 } * * * cd: '1403', * * msg: 'We are undergoing maintenance, please try again later.', * * tid: 'SH8Q14K6' * } * cd: '50038', * msg: '游戏不存在:%!d(string=cocktail-nite)', * tid: 'S2VAFQQ9' * { cd: '500', msg: 'protobuf data too short', tid: '4UOFS6OA' } * err: { cd: '2', msg: 'is small game ont fb', tid: 'AFJGTF6B' } * * {cd: "1302", msg: "Invalid player session", tid: "PGTVSW16", at: null} * * {"cd":"1308","msg":"Player session is expired","tid":"PBSMUF18","at":null} * Access denied. * err: { * cd: '3202', * msg: 'OERR:Not enough cash.', * tid: 'JSXSWO82', * at: null * } */ $temp = [ 'dt' => null, 'err' => array_merge([ "cd" => $code, "msg" => $msg ]), 'td' => time(), ]; if(!empty($data)) { $temp = array_merge($temp, $data); } if(!$is_json) { return $temp; } return json($temp); } /** * 格式化数据 * @param $item * @param $change * @return mixed */ public static function formatResultData($item, $change) { // $item['tb'] = CommonUtils::convertBalance($item['tb'], $change); // $item['tbb'] = CommonUtils::convertBalance($item['tbb'],$change); // $item['np'] = CommonUtils::convertBalance($item['np'],$change); // $item['ctw'] = CommonUtils::convertBalance($item['ctw'],$change); $keysList = ['cptw','atw', 'tb', 'tbb','np', 'ctw']; foreach ($keysList as $key) { if(isset($item[$key])) { $item[$key] = CommonUtils::convertBalance($item[$key],$change); } } $item['aw'] = CommonUtils::convertBalance($item['aw'],$change); if(!empty($item['lw'])) { foreach ($item['lw'] as $key => $lw) { if(is_array($lw)) { foreach ($lw as $k => $templw) { $lw[$k] = CommonUtils::convertBalance($templw,$change); } $item['lw'][$key] = $lw; }else { $item['lw'][$key] = CommonUtils::convertBalance($lw,$change); } } } return $item; } }