<1> CharField
#字符串字段, 用于较短的字符串.
#CharField 要求必须有一个参数 maxlength,
用于从数据库层和Django校验层限制该字段所允许的最大字符数.

根据GUID+DateTime.Now.Ticks生产唯一订单号

       //获取时间戳 .net framework
            /*
            DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
            int time = (int)(DateTime.Now - dt).TotalSeconds;
            String Timestamp = time.ToString();
            */
            // .net core 获取时间戳
            DateTime dt = new DateTime(1970, 1, 1,0, 0, 0, DateTimeKind.Utc);
            int time = (int)(DateTime.Now.AddHours(-8) - dt).TotalSeconds;
            String Timestamp = time.ToString();
            //随机正整数,用于防止重放攻击
            Random rd = new Random();
            int rd_i = rd.Next();
            String nonce = Convert.ToString(rd_i);
            //SecretId
            String SecretId = "";
            //参数(用于编码)
            String PostStr = string.Format("Action=DescribeLVBChannelList&Nonce={0}&Region=bj&SecretId={1}&Timestamp={2}", nonce, SecretId, Timestamp);
            //地址
            String url = "https://live.api.qcloud.com/v2/index.php";
            //编码
            UTF8Encoding enc = new UTF8Encoding();
            String qm = "POSTlive.api.qcloud.com/v2/index.php"  + "?" + PostStr;
            byte[] dataToHash = enc.GetBytes(qm);
            /*
            var sha1 = SHA1.Create();
            var result = sha1.ComputeHash(dataToHash);
            */  
            HMACSHA1 hmac = new HMACSHA1()
            {
                Key = enc.GetBytes("")
            };
            var result = hmac.ComputeHash(dataToHash);
            string Signature = Convert.ToBase64String(result);
            //完整参数
            var completeUrl = string.Format("Action=DescribeLVBChannelList&Nonce={0}&Region=bj&SecretId={1}&Signature={2}&Timestamp={3}", nonce, SecretId, Signature, Timestamp);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ProtocolVersion = HttpVersion.Version10;
            byte[] data = Encoding.UTF8.GetBytes(completeUrl);
            request.ContentLength = data.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse response = null;
            int httpStatus = 200;
            string content;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                httpStatus = (int)response.StatusCode;
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                content = reader.ReadToEnd();
            }
            catch (WebException e)
            {
                response = (HttpWebResponse)e.Response;
                httpStatus = (int)response.StatusCode;
                using (Stream errData = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(errData))
                    {
                        content = reader.ReadToEnd();
                    }
                }
            }

<2> IntegerField
#用于保存一个整数.

 1     /// <summary>
 2     /// 生成唯一数
 3     /// </summary>
 4     public class UniqueData
 5     {
 6         private static object obj = new object();
 7         private static int GuidInt { get { return Guid.NewGuid().GetHashCode(); } }
 8         private static string GuidIntStr { get { return Math.Abs(GuidInt).ToString(); } }
 9 
10         /// <summary>
11         /// 生成
12         /// </summary>
13         /// <param name="mark">前缀</param>
14         /// <param name="timeType">时间精确类型  1 日,2 时,3 分,4 秒(默认) </param>
15         /// <param name="id">id 小于或等于0则随机生成id</param>
16         /// <returns></returns>
17         public static string Gener(string mark, int timeType = 4, int id = 0)
18         {
19             lock (obj)
20             {
21                 var number = mark;
22                 var ticks = (DateTime.Now.Ticks - GuidInt).ToString();
23                 int fillCount = 0;//填充位数
24 
25                 number += GetTimeStr(timeType, out fillCount);
26                 if (id > 0)
27                 {
28                     number += ticks.Substring(ticks.Length - (fillCount + 3)) + id.ToString().PadLeft(10, '0');
29                 }
30                 else
31                 {
32                     number += ticks.Substring(ticks.Length - (fillCount + 3)) + GuidIntStr.PadLeft(10, '0');
33                 }
34                 return number;
35             }
36         }
37 
38         /// <summary>
39         /// 生成
40         /// </summary>
41         /// <param name="mark">前缀</param>
42         /// <param name="timeType">时间精确类型  1 日,2 时,3 分,4 秒(默认)</param>
43         /// <param name="id">id 小于或等于0则随机生成id</param>
44         /// <returns></returns>
45         public static string GenerLong(string mark, int timeType = 4, long id = 0)
46         {
47             lock (obj)
48             {
49                 var number = mark;
50                 var ticks = (DateTime.Now.Ticks - GuidInt).ToString();
51                 int fillCount = 0;//填充位数
52 
53                 number += GetTimeStr(timeType, out fillCount);
54                 if (id > 0)
55                 {
56                     number += ticks.Substring(ticks.Length - fillCount) + id.ToString().PadLeft(19, '0');
57                 }
58                 else
59                 {
60                     number += GuidIntStr.PadLeft(10, '0') + ticks.Substring(ticks.Length - (9 + fillCount));
61                 }
62                 return number;
63             }
64         }
65 
66         /// <summary>
67         /// 获取时间字符串
68         /// </summary>
69         /// <param name="timeType">时间精确类型  1 日,2 时,3 分,4 秒(默认)</param>
70         /// <param name="fillCount">填充位数</param>
71         /// <returns></returns>
72         private static string GetTimeStr(int timeType, out int fillCount)
73         {
74             var time = DateTime.Now;
75             if (timeType == 1)
76             {
77                 fillCount = 6;
78                 return time.ToString("yyyyMMdd");
79             }
80             else if (timeType == 2)
81             {
82                 fillCount = 4;
83                 return time.ToString("yyyyMMddHH");
84             }
85             else if (timeType == 3)
86             {
87                 fillCount = 2;
88                 return time.ToString("yyyyMMddHHmm");
89             }
90             else
91             {
92                 fillCount = 0;
93                 return time.ToString("yyyyMMddHHmmss");
94             }
95         }
96     }

 

<3> FloatField
# 一个浮点数. 必须 提供两个参数:
#
# 参数 描述
# max_digits 总位数(不包括小数点和符号)
# decimal_places 小数位数
# 举例来说, 要保存最大值为 999 (小数点后保存2位),你要这样定义字段:
#
# models.FloatField(…, max_digits=5, decimal_places=2)
# 要保存最大值一百万(小数点后保存10位)的话,你要这样定义:
#
# models.FloatField(…, max_digits=19, decimal_places=10)
# admin 用一个文本框(<input type=”text”>)表示该字段保存的数据.

 

<4> AutoField
# 一个 IntegerField, 添加记录时它会自动增长.
你通常不需要直接使用这个字段;
# 自定义一个主键:my_id=models.AutoField(primary_key=True)
# 如果你不指定主键的话,系统会自动添加一个主键字段到你的 model.

<5> BooleanField
# A true/false field. admin 用 checkbox 来表示此类字段.

<6> TextField
# 一个容量很大的文本字段.
# admin 用一个 <textarea>
(文本区域)表示该字段数据.(一个多行编辑框).

发表评论

电子邮件地址不会被公开。 必填项已用*标注