248 lines
9.1 KiB
C#
248 lines
9.1 KiB
C#
|
using Api.Framework;
|
|||
|
using Api.Framework.Tools;
|
|||
|
using Chat.Framework;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Drawing;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using ThoughtWorks.QRCode.Codec;
|
|||
|
using UserFission.Entitys;
|
|||
|
using UserFission.Properties;
|
|||
|
|
|||
|
namespace UserFission
|
|||
|
{
|
|||
|
public static class Tools
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 背景图片地址
|
|||
|
/// </summary>
|
|||
|
public static string PosterPathImgUrl { get; private set; }
|
|||
|
|
|||
|
public const string Rex_Code = @"^([A-Za-z0-9]{6})$";
|
|||
|
|
|||
|
static Tools()
|
|||
|
{
|
|||
|
PosterPathImgUrl = Util.MapFile("背景.jpg", "File\\Image");
|
|||
|
}
|
|||
|
|
|||
|
#region 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
|
|||
|
/// <summary>
|
|||
|
/// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="path"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Bitmap ReadImageFile(string path)
|
|||
|
{
|
|||
|
FileStream fs = File.OpenRead(path); //OpenRead
|
|||
|
int filelength = 0;
|
|||
|
filelength = (int)fs.Length; //获得文件长度
|
|||
|
Byte[] image = new Byte[filelength]; //建立一个字节数组
|
|||
|
fs.Read(image, 0, filelength); //按字节流读取
|
|||
|
Image result = Image.FromStream(fs);
|
|||
|
fs.Close();
|
|||
|
Bitmap bit = new Bitmap(result);
|
|||
|
return bit;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 获取底图的地址
|
|||
|
/// <summary>
|
|||
|
/// 获取底图的地址
|
|||
|
/// </summary>
|
|||
|
/// <param name="path"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static void GetBaseIco()
|
|||
|
{
|
|||
|
if (!File.Exists(PosterPathImgUrl))
|
|||
|
{
|
|||
|
Bitmap bit = Resources.背景;
|
|||
|
bit.Save(PosterPathImgUrl);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 生成二维码,如果有Logo,则在二维码中添加Logo
|
|||
|
/// <summary>
|
|||
|
/// 生成二维码,如果有Logo,则在二维码中添加Logo
|
|||
|
/// </summary>
|
|||
|
/// <param name="content">生成二维码的内容</param>
|
|||
|
/// <param name="logoImagepath">logo</param>
|
|||
|
/// <param name="logoSize">Logo的尺寸</param>
|
|||
|
/// <param name="version">二维码版本</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static Bitmap CreateQRCode(string content, string logoImagepath = "", int logoSize = 35, int version = 7)
|
|||
|
{
|
|||
|
string _image = string.Empty;
|
|||
|
try
|
|||
|
{
|
|||
|
QRCodeEncoder qrEncoder = new QRCodeEncoder();
|
|||
|
qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
|
|||
|
qrEncoder.QRCodeScale = 4;
|
|||
|
qrEncoder.QRCodeVersion = Convert.ToInt32(version);
|
|||
|
qrEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
|
|||
|
Bitmap qrcode = qrEncoder.Encode(content, Encoding.UTF8);
|
|||
|
if (!string.IsNullOrEmpty(logoImagepath))
|
|||
|
{
|
|||
|
using (Graphics g = Graphics.FromImage(qrcode))
|
|||
|
{
|
|||
|
if (logoImagepath.IndexOf("http") != -1)
|
|||
|
{
|
|||
|
_image = Util.MapFile(Guid.NewGuid().ToString() + ".jpg", "Cache\\Image");
|
|||
|
FileTools.DownloadImage(logoImagepath, _image);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_image = Util.MapFile(Guid.NewGuid().ToString() + ".jpg", "Cache\\Image");
|
|||
|
File.Copy(logoImagepath, _image, true);
|
|||
|
}
|
|||
|
|
|||
|
var bitmapLogo = new Bitmap(ReadImageFile(_image), new Size(logoSize, logoSize));
|
|||
|
var point = new PointF(qrcode.Width / 2.00f - logoSize / 2.00f, qrcode.Height / 2.00f - logoSize / 2.00f);
|
|||
|
g.DrawImage(bitmapLogo, point);
|
|||
|
}
|
|||
|
}
|
|||
|
return qrcode;
|
|||
|
}
|
|||
|
catch (IndexOutOfRangeException ex)
|
|||
|
{
|
|||
|
throw new Exception("超出当前二维码版本的容量上限,请选择更高的二维码版本!" + ex.Message);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw new Exception("生成二维码出错!" + ex.Message);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(_image) && File.Exists(_image))
|
|||
|
File.Delete(_image);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 判断用户数据库中,是否存在邀请码.不存在生成并返回.存在直接返回
|
|||
|
/// </summary>
|
|||
|
/// <param name="member_info_id"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string FindInvitationCode(long member_info_id)
|
|||
|
{
|
|||
|
var session = ApiClient.GetSession();
|
|||
|
var invitation_code = session.Find<fl_plugin_userfission_user_invitation_code>($"select * from fl_plugin_userfission_user_invitation_code where member_info_id = @id", new { id = member_info_id }).FirstOrDefault();
|
|||
|
if (invitation_code != null)
|
|||
|
return invitation_code.code;
|
|||
|
else
|
|||
|
{
|
|||
|
string invitationCode = string.Empty;
|
|||
|
do
|
|||
|
{
|
|||
|
//invitationCode = Util.InvitationCode(long.Parse(CsharpHttpHelper.HttpExtend.GetTimeStamp()));
|
|||
|
invitationCode = new Random().Next(100000, 999999).ToString();
|
|||
|
var num = long.Parse(session.FindTable($"select count(*) as num from fl_plugin_userfission_user_invitation_code where code = @code", new { code = invitationCode }).Rows[0]["num"].ToString());
|
|||
|
if (num == 0)
|
|||
|
{
|
|||
|
session.Insertable(new fl_plugin_userfission_user_invitation_code() { member_info_id = member_info_id, code = invitationCode, crt_time = DateTime.Now }).ExecuteCommand();
|
|||
|
return invitationCode;
|
|||
|
}
|
|||
|
}
|
|||
|
while (true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 检测邀请码是否存在,返回true邀请码没使用过,false邀请码已存在
|
|||
|
/// </summary>
|
|||
|
/// <param name="code"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static bool CheckInvitationCode(string code)
|
|||
|
{
|
|||
|
var session = ApiClient.GetSession();
|
|||
|
var num = long.Parse(session.FindTable($"select count(*) as num from fl_plugin_userfission_user_invitation_code where code = @code", new { code = code }).Rows[0]["num"].ToString());
|
|||
|
if (num != 0)
|
|||
|
return false;
|
|||
|
else
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获得下级满足条件,上级奖励列表
|
|||
|
/// </summary>
|
|||
|
/// <param name="session"></param>
|
|||
|
/// <param name="type"></param>
|
|||
|
/// <param name="refresh">是否刷新缓存</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static List<fl_plugin_userfission_superior_reward> FindRewards(bool refresh = false)
|
|||
|
{
|
|||
|
var key = $"fl_plugin_userfission_superior_reward";
|
|||
|
var fl_plugin_userfission_superior_rewards = ApiClient.Cache.Get<List<fl_plugin_userfission_superior_reward>>(key);
|
|||
|
var session = ApiClient.GetSession();
|
|||
|
if (!refresh && fl_plugin_userfission_superior_rewards != null) return fl_plugin_userfission_superior_rewards;
|
|||
|
fl_plugin_userfission_superior_rewards = session.Find<fl_plugin_userfission_superior_reward>("select * from fl_plugin_userfission_superior_reward order by subordinate_order_totle asc");
|
|||
|
ApiClient.Cache.Set(key, fl_plugin_userfission_superior_rewards, 60);
|
|||
|
return fl_plugin_userfission_superior_rewards;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 充值方式
|
|||
|
/// </summary>
|
|||
|
public enum RechargeType
|
|||
|
{
|
|||
|
增加余额 = 1,
|
|||
|
发送红包 = 2,
|
|||
|
商户付款 = 3,
|
|||
|
微信转账 = 4
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 发送红包方式
|
|||
|
/// </summary>
|
|||
|
public enum TransferType
|
|||
|
{
|
|||
|
转账 = 0,
|
|||
|
红包 = 1
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 推荐下线奖励方式
|
|||
|
/// </summary>
|
|||
|
public enum ReferralJackpotType
|
|||
|
{
|
|||
|
推荐新人立马奖励 = 1,
|
|||
|
新人完成首单奖励 = 2,
|
|||
|
新人首单满足付款金额奖励 = 3
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 推荐下线奖励方式
|
|||
|
/// </summary>
|
|||
|
public enum NewcomerAwardType
|
|||
|
{
|
|||
|
通过立即奖励 = 1,
|
|||
|
首次查券奖励 = 2
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 新用户首单奖励方式
|
|||
|
/// </summary>
|
|||
|
public enum NewcomerInitialAwardType
|
|||
|
{
|
|||
|
订单付款后奖励 = 1,
|
|||
|
订单结算后奖励 = 2
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 首单奖励新用户类型
|
|||
|
/// </summary>
|
|||
|
public enum InitialAwardNewcomerType
|
|||
|
{
|
|||
|
所有新用户 = 1,
|
|||
|
被邀请用户 = 2,
|
|||
|
非邀请用户 = 3
|
|||
|
}
|
|||
|
|
|||
|
}
|