$userId, 'type' => $type, 'num' => $num, 'balance' => $balance, 'remark' => $remark, 'relate_id' => $relateId, 'create_time' => time(), ]); } /** * 获取用户变动记录 * @param int $userId 用户ID * @param int $page 页码 * @param int $limit 每页数量 * @return array */ public static function getUserLogs(int $userId, int $page = 1, int $limit = 10): array { $data = self::where('user_id', $userId) ->order('id desc') ->page($page, $limit) ->select(); $total = self::where('user_id', $userId)->count(); $list = []; foreach ($data ?: [] as $item) { $itemArr = $item->toArray(); $itemArr['type_text'] = self::getTypeText((int)$itemArr['type']); $itemArr['num_text'] = (int)$itemArr['num'] > 0 ? '+' . $itemArr['num'] : (string)$itemArr['num']; $list[] = $itemArr; } return [ 'list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit, ]; } /** * 获取类型文本 * @param int $type 类型 * @return string */ public static function getTypeText(int $type): string { $typeMap = [ self::TYPE_GIFT => '新用户赠送', self::TYPE_BUY => '积分购买', self::TYPE_FIRST_CHAT => '首次聊天消耗', self::TYPE_ADMIN_ADD => '管理员赠送', self::TYPE_ADMIN_SUB => '管理员扣除', ]; return $typeMap[$type] ?? '未知'; } }