提交代码

This commit is contained in:
zpc 2024-09-18 17:55:10 +08:00
parent 50999c7f5d
commit f2d219897f
10 changed files with 380 additions and 12 deletions

View File

@ -7,13 +7,19 @@ using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using HuanMeng.MiaoYu.Code.Chat.Contract;
using Org.BouncyCastle.Utilities;
namespace HuanMeng.MiaoYu.Code.Music
{
/// <summary>
/// ai音乐逻辑类
/// ai音乐逻辑类
/// </summary>
public class MusicBLL : MiaoYuBase
{
@ -78,5 +84,105 @@ namespace HuanMeng.MiaoYu.Code.Music
musicUserGenresDtos = musicUserGenresDtos.OrderBy(it => it.GenreType).ThenBy(it => it.OrderId).ToList();
return new BaseResponse<List<MusicUserGenresDto>>(ResonseCode.Success, "", musicUserGenresDtos);
}
/// <summary>
///
/// </summary>
/// <param name="musicCreateRequest"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BaseResponse<bool>> CreateMusic(MusicCreateRequest musicCreateRequest)
{
if (_UserId == 0)
{
throw new Exception("请先登录");
}
UserInfoBLL userInfoBLL = new UserInfoBLL(Dao, _UserId);
if (!userInfoBLL.IsCheckingSufficient(UserCurrencyType., 10))
{
throw new Exception("音乐点数不足");
}
Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
keyValuePairs.Add("gpt_description_prompt", $"歌曲风格:{musicCreateRequest.GenreName}");
keyValuePairs.Add("make_instrumental", false);
keyValuePairs.Add("mv", "chirp-v3-5");
keyValuePairs.Add("prompt", musicCreateRequest.Prompt);
var requestBody = JsonConvert.SerializeObject(keyValuePairs);
var httpClient = HttpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("authorization", "Bearer hk-ylg6c31000042000fef54b80aec96b39e83b36f6e551440b");
var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
var url = "https://api.openai-hk.com/sunoapi/generate/description-mode";
var response = await httpClient.PostAsync(url, content);
if (response == null || !response.IsSuccessStatusCode)
{
throw new Exception("创建音乐失败");
}
var responseContent = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(responseContent))
{
throw new Exception("创建音乐失败!");
}
var obj = JsonConvert.DeserializeObject<JObject>(responseContent);
var id = obj?["id"]?.ToString() ?? "";
var musicId1 = obj?.SelectToken("clips[0].id")?.ToString();
var musicId2 = obj?.SelectToken("clips[1].id")?.ToString();
M_SongInfo m_SongInfo = new M_SongInfo()
{
CompleteAt = DateTime.Now,
CreateAt = DateTime.Now,
GetUrl = url,
RequestInfo = requestBody,
ResonseInfo = responseContent,
Status = "已完成",
UpdateAt = DateTime.Now,
};
Dao.daoDbMiaoYu.context.Add(m_SongInfo);
Dao.daoDbMiaoYu.context.SaveChanges();
if (string.IsNullOrEmpty(musicId1) && string.IsNullOrEmpty(musicId2))
{
throw new Exception("创建音乐失败!!!");
}
if (!string.IsNullOrEmpty(musicId1))
{
var song1 = GetSong(musicCreateRequest.GenreName, musicCreateRequest.Name, musicId1, m_SongInfo.Id);
Dao.daoDbMiaoYu.context.Add(song1);
}
if (!string.IsNullOrEmpty(musicId2))
{
var song2 = GetSong(musicCreateRequest.GenreName, musicCreateRequest.Name, musicId2, m_SongInfo.Id);
Dao.daoDbMiaoYu.context.Add(song2);
}
//扣除货币
userInfoBLL[UserCurrencyType.].ConsumeMoneyNoWork(-10, Dao);
return new BaseResponse<bool>(ResonseCode.Success, "音乐正在生成", true) { };
}
private M_Songs GetSong(string genreName, string name, string? musicId1, int songInfoId)
{
M_Songs m_Songs = new M_Songs()
{
AuthorId = _UserId,
CoverImage = "https://cos.shhuanmeng.com/yinyue/fengmian.png",
CreationTimestamp = DateTime.Now,
DownloadCount = 0,
LikeCount = 0,
PlayCount = 0,
Duration = TimeOnly.MinValue,
Genre = genreName,
GenreId = 0,
ImageLargeUrl = "https://cos.shhuanmeng.com/yinyue/fengmian.png",
IsPublic = false,
Lyrics = "",
MusicAddress = "",
SongInfoId = songInfoId,
SpecialId = musicId1,
State = 1,
Title = name
};
return m_Songs;
}
}
}

View File

@ -0,0 +1,114 @@
using HuanMeng.DotNetCore.MultiTenant.Contract;
using HuanMeng.DotNetCore.Processors;
using HuanMeng.MiaoYu.Code.AppExtend;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace HuanMeng.MiaoYu.Code.Music
{
/// <summary>
///
/// </summary>
public class MusicService(IServiceProvider serviceProvider) : IHostedService, IDisposable
{
MusicProcessor? musicProcessor = null;
public Task StartAsync(CancellationToken cancellationToken)
{
musicProcessor = new MusicProcessor(serviceProvider);
musicProcessor.Run();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
if (musicProcessor != null)
{
musicProcessor.Stop();
musicProcessor.Dispose();
}
return Task.CompletedTask;
}
public void Dispose()
{
musicProcessor?.Dispose();
}
}
public class MusicProcessor(IServiceProvider serviceProvider) : ThreadProcessor
{
protected override async void Proc_Do()
{
while (true)
{
foreach (var item in AppConfigurationExtend.AppConfigs.Values)
{
var service = serviceProvider.CreateScope();
var temantInfo = serviceProvider.GetRequiredService<ITenantInfo>();
item.ToITenantInfo(temantInfo);
DAO dao = new DAO(serviceProvider);
var list = dao.daoDbMiaoYu.context.M_Songs.Where(it => it.State == 1).ToList();
if (list.Count > 0)
{
var httpFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();
foreach (var songs in list)
{
var httpClient = httpFactory.CreateClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("authorization", "Bearer hk-ylg6c31000042000fef54b80aec96b39e83b36f6e551440b");
var url = $"https://api.openai-hk.com/suno/feed/{songs.SpecialId}";
var response = await httpClient.GetAsync(url);
if (response == null || !response.IsSuccessStatusCode)
{
//throw new Exception("创建音乐失败");
continue;
}
var responseContent = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(responseContent))
{
continue;
}
var obj = JsonConvert.DeserializeObject<JArray>(responseContent);
//status
var status = obj?.SelectToken("[0].status")?.ToString();
if (status == "complete")
{
//obj.SelectToken("[0].video_url")?.ToString();
songs.MusicAddress = obj?.SelectToken("[0].audio_url")?.ToString() ?? "";
songs.CoverImage = obj?.SelectToken("[0].image_url")?.ToString() ?? "";
songs.ImageLargeUrl = obj?.SelectToken("[0].image_large_url")?.ToString() ?? "";
songs.Lyrics = obj?.SelectToken("[0].metadata.prompt")?.ToString() ?? "";
var duration = obj?.SelectToken("[0].metadata.duration")?.ToString() ?? "";
if (!string.IsNullOrEmpty(duration))
{
if (int.TryParse(duration, out var d))
{
songs.Duration = new TimeOnly(0, 0, d);
}
}
dao.daoDbMiaoYu.context.SaveChanges();
}
}
}
}
Thread.Sleep(1000 * 5);
}
}
}
}

View File

@ -116,7 +116,7 @@ namespace HuanMeng.MiaoYu.Code.Users
public async Task<BaseResponse<ResponseUserInfo>> GetUserInfo()
{
var user = await Dao.daoDbMiaoYu.context.T_User.FirstOrDefaultAsync(it => it.Id == _UserId);
var userData = await Dao.daoDbMiaoYu.context.T_User_Data.FirstOrDefaultAsync(it => it.Id == _UserId);
var userData = await Dao.daoDbMiaoYu.context.T_User_Data.FirstOrDefaultAsync(it => it.UserId == _UserId);
//获取用户余额
UserInfoBLL userInfoBLL = new UserInfoBLL(Dao, _UserId);
var Currency = userInfoBLL[UserCurrencyType.]?.CurrencyMoney;
@ -134,7 +134,7 @@ namespace HuanMeng.MiaoYu.Code.Users
NickName = user.NickName,
UserId = user.Id,
Currency = (int)Currency,
UserIconUrl = string.IsNullOrEmpty(userData.UserIconUrl) ? "https://cos.shhuanmeng.com/default.png" : userData.UserIconUrl,
UserIconUrl = string.IsNullOrEmpty(userData?.UserIconUrl) ? "https://cos.shhuanmeng.com/default.png" : userData?.UserIconUrl,
RemainingChatCount = (int)RemainingChatCount,//这里先写1我不会decimal转int
HasTalked = hasTalked,
Photographs = 0,

View File

@ -0,0 +1,48 @@
using System;
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
/// <summary>
/// 音乐任务表
/// </summary>
public partial class M_SongInfo: MultiTenantEntity
{
public virtual int Id { get; set; }
public override Guid TenantId { get; set; }
/// <summary>
/// 请求数据
/// </summary>
public virtual string? RequestInfo { get; set; }
/// <summary>
/// 返回数据
/// </summary>
public virtual string? ResonseInfo { get; set; }
/// <summary>
/// 请求地址
/// </summary>
public virtual string? GetUrl { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public virtual DateTime CreateAt { get; set; }
/// <summary>
/// 任务完成时间
/// </summary>
public virtual DateTime? CompleteAt { get; set; }
/// <summary>
/// 任务状态
/// </summary>
public virtual string Status { get; set; } = null!;
/// <summary>
/// 修改时间
/// </summary>
public virtual DateTime UpdateAt { get; set; }
}

View File

@ -3,7 +3,7 @@
namespace HuanMeng.MiaoYu.Model.DbSqlServer.Db_MiaoYu;
/// <summary>
/// 存储所有生成的歌曲的信息
/// 存储所有生成的歌曲的信息
/// </summary>
public partial class M_Songs: MultiTenantEntity
{
@ -78,4 +78,24 @@ public partial class M_Songs: MultiTenantEntity
/// 音乐风格Id
/// </summary>
public virtual int? GenreId { get; set; }
/// <summary>
/// 封面缩略图
/// </summary>
public virtual string? ImageLargeUrl { get; set; }
/// <summary>
/// 所属任务Id
/// </summary>
public virtual int SongInfoId { get; set; }
/// <summary>
/// 音乐状态0生成功1生成中
/// </summary>
public virtual int State { get; set; }
/// <summary>
/// 特殊Id
/// </summary>
public virtual string SpecialId { get; set; } = null!;
}

View File

@ -74,7 +74,12 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
public virtual DbSet<M_SongComments> M_SongComments { get; set; }
/// <summary>
/// 存储所有生成的歌曲的信息。
/// 音乐任务表
/// </summary>
public virtual DbSet<M_SongInfo> M_SongInfo { get; set; }
/// <summary>
/// 存储所有生成的歌曲的信息
/// </summary>
public virtual DbSet<M_Songs> M_Songs { get; set; }
@ -379,11 +384,39 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
}
});
modelBuilder.Entity<M_SongInfo>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__M_SongIn__3214EC070CFDD5C9");
entity.ToTable(tb => tb.HasComment("音乐任务表"));
entity.Property(e => e.CompleteAt)
.HasComment("任务完成时间")
.HasColumnType("datetime");
entity.Property(e => e.CreateAt)
.HasComment("创建时间")
.HasColumnType("datetime");
entity.Property(e => e.GetUrl).HasComment("请求地址");
entity.Property(e => e.RequestInfo).HasComment("请求数据");
entity.Property(e => e.ResonseInfo).HasComment("返回数据");
entity.Property(e => e.Status)
.HasMaxLength(100)
.HasComment("任务状态");
entity.Property(e => e.UpdateAt)
.HasComment("修改时间")
.HasColumnType("datetime");
//添加全局筛选器
if (this.TenantInfo != null)
{
entity.HasQueryFilter(it => it.TenantId == this.TenantInfo.TenantId);
}
});
modelBuilder.Entity<M_Songs>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Songs__3214EC0728D42B46");
entity.ToTable(tb => tb.HasComment("存储所有生成的歌曲的信息。"));
entity.ToTable(tb => tb.HasComment("存储所有生成的歌曲的信息"));
entity.Property(e => e.Id).HasComment("歌曲唯一标识");
entity.Property(e => e.AuthorId).HasComment("歌曲作者ID");
@ -395,14 +428,18 @@ public partial class MiaoYuContext : MultiTenantDbContext//DbContext
entity.Property(e => e.DownloadCount).HasComment("下载次数");
entity.Property(e => e.Duration).HasComment("歌曲时长");
entity.Property(e => e.Genre)
.HasMaxLength(50)
.HasMaxLength(200)
.HasComment("音乐风格");
entity.Property(e => e.GenreId).HasComment("音乐风格Id");
entity.Property(e => e.ImageLargeUrl).HasComment("封面缩略图");
entity.Property(e => e.IsPublic).HasComment("歌曲是否公开展示");
entity.Property(e => e.LikeCount).HasComment("点赞次数");
entity.Property(e => e.Lyrics).HasComment("歌词内容");
entity.Property(e => e.MusicAddress).HasComment("音乐下载地址");
entity.Property(e => e.PlayCount).HasComment("播放次数");
entity.Property(e => e.SongInfoId).HasComment("所属任务Id");
entity.Property(e => e.SpecialId).HasComment("特殊Id");
entity.Property(e => e.State).HasComment("音乐状态0生成功1生成中");
entity.Property(e => e.Title)
.HasMaxLength(200)
.HasComment("歌曲名称");

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.MiaoYu.Model.Dto.Music
{
/// <summary>
/// 创建音乐
/// </summary>
public class MusicCreateRequest
{
/// <summary>
/// 音乐风格
/// </summary>
public string GenreName { get; set; }
/// <summary>
/// 音乐描述
/// </summary>
public string Prompt { get; set; }
/// <summary>
/// 歌曲名称
/// </summary>
public string Name { get; set; }
}
}

View File

@ -29,6 +29,10 @@ namespace HuanMeng.MiaoYu.Model.EnumModel.User
/// </summary>
= 3,
/// <summary>
/// 生成音乐次数
/// </summary>
= 4,
/// <summary>
/// 初级记忆卡
/// </summary>

View File

@ -24,7 +24,7 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<BaseResponse<List<MusicGenresDto>>> GetMusicGenresList()
public async Task<BaseResponse<List<MusicGenresDto>>> GetMusicGenresList()
{
MusicBLL musicBLL = new MusicBLL(ServiceProvider);
return await musicBLL.GetMusicGenresList();
@ -51,5 +51,16 @@ namespace HuanMeng.MiaoYu.WebApi.Controllers
MusicBLL musicBLL = new MusicBLL(ServiceProvider);
return await musicBLL.GetUserMusicGenresList();
}
/// <summary>
/// 创作音乐
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<BaseResponse<bool>> CreateMusic([FromBody] MusicCreateRequest musicCreateRequest)
{
MusicBLL musicBLL = new MusicBLL(ServiceProvider);
return await musicBLL.CreateMusic(musicCreateRequest); ;
}
}
}

View File

@ -1,8 +1,7 @@
using AgileConfig.Client;
using HuanMeng.DotNetCore.CustomExtension;
using HuanMeng.DotNetCore.MiddlewareExtend;
using HuanMeng.DotNetCore.Utility.AssemblyHelper;
using HuanMeng.MiaoYu.Code.AppExtend;
using HuanMeng.MiaoYu.Code.Base;
using HuanMeng.MiaoYu.Code.JwtUtil;
using HuanMeng.MiaoYu.Code.MultiTenantUtil;
@ -13,9 +12,8 @@ using HuanMeng.MiaoYu.Code.TencentUtile;
using HuanMeng.MiaoYu.Code.Users.UserAccount.VerificationCodeManager;
using HuanMeng.MiaoYu.Model.Dto;
using HuanMeng.MiaoYu.WebApi.Base;
using HuanMeng.MiaoYu.Code.AppExtend;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Serialization;