| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace app\common;
- use Ramsey\Uuid\Uuid;
- use think\facade\Cache;
- use think\helper\Str;
- class CommonUtils
- {
- /// 读取游戏类型
- public static function getGameTypeConfig($type = '2') {
- return $GLOBALS['gameGameTypeConfig'][$type];
- }
- /// 默认游戏图标
- public static function getGameDefaultImage($item = []) {
- $staticDomain = $GLOBALS['gameGameStaticDomain'];
- // 中文图标
- if(empty($item['image'])) {
- $item['image_url'] = "//{$staticDomain}/{$item['game_id']}/zh.png";
- }else {
- $item['image_url'] = $item['image'];
- }
- // 英文图标
- if(empty($item['image_en'])) {
- $item['image_en_url'] = "//{$staticDomain}/{$item['game_id']}/en.png";
- }else {
- $item['image_en_url'] = $item['image_en'];
- }
- return $item;
- }
- // 转换 分数 * 10000 or / 10000
- public static function convertBalance($balance = 0, $isTenThousandfold = true) {
- // $balance = (float)$balance;
- // // 分数小数点 * 10000;
- // // $GLOBALS['balanceTenThousandfold'] = 10000;
- // $balanceTenThousandfold = 10000;
- // if ($isTenThousandfold) {
- // $balance = bcmul($balance, $balanceTenThousandfold);
- // } else {
- // $balance = bcdiv($balance, $balanceTenThousandfold, 4);
- // }
- return (float)$balance;
- }
- // 生成转账流水ID
- public static function generateUuidSn($key = 'OD')
- {
- $uuid = Uuid::uuid4()->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;
- }
- }
|