﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml;

namespace weixin.model
{
    public class ReqMsgType
    {
      
        /// <summary>
        /// 文本类型
        /// </summary>
        public static  string Text = "text";

        /// <summary>
        /// 事件类型
        /// </summary>
        public static  string Event = "event";

        /// <summary>
        /// 语音类型
        /// </summary>
        public static  string Voice = "voice";

        /// <summary>
        /// 图片消息
        /// </summary>
        public static  string Image = "image";

        /// <summary>
        /// 视频消息
        /// </summary>
        public static  string Video = "video";

        /// <summary>
        /// 位置消息
        /// </summary>
        public static  string Location = "location";

        /// <summary>
        /// 链接消息
        /// </summary>
        public static  string Link = "link";   
        
        /// <summary>
        /// 图文信息
        /// </summary>
        public static string News = "news";
    }
    public enum ReqEventType
    {
        /// <summary>
        /// 未关注事件
        /// </summary>
        subscribe,
        /// <summary>
        /// 取消关注事件
        /// </summary>
         unsubscribe,
        /// <summary>
        /// 已关注事件
        /// </summary>
         scan,
        /// <summary>
        /// 点击菜单拉取消息事件
        /// </summary>
         click,
        /// <summary>
        /// 点击菜单跳转链接时的事件
        /// </summary>
         view ,
        /// <summary>
        /// 获取地址信息事件
        /// </summary>
         location,
        /// <summary>
        /// 扫一扫
        /// </summary>
         scancode_push
       
    }


    public enum QRSceneType
    {         
        Merchant = 1,
        Table = 2,
        Order = 3,
        Waiter = 4

    }


    public class WeiXinServer
    {
        private const string token = "icaiban";  //考虑以后为每个商户设置自己独立的微信token , 这样可以有效验证微信公众号请求
        /// <summary>
        /// 将xml文件转换成Hashtable
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static Hashtable ParseXml(String xml)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);
            XmlNode bodyNode = xmlDocument.ChildNodes[0];
            Hashtable ht = new Hashtable();
            if (bodyNode.ChildNodes.Count > 0)
            {
                foreach (XmlNode xn in bodyNode.ChildNodes)
                {
                    ht.Add(xn.Name, xn.InnerText);
                }
            }
            return ht;
        }

        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <returns></returns>
        public static bool CheckSignature( String signature, String timestamp, String nonce)
        {
            String[] arr = new String[] { token, timestamp, nonce };
            Array.Sort<String>(arr);
            StringBuilder content = new StringBuilder();
            for (int i = 0; i < arr.Length; i++)
            {
                content.Append(arr[i]);
            }
            String tmpStr = SHA1_Encrypt(content.ToString());
            return tmpStr != null ? tmpStr.Equals(signature) : false;
        }
        /// <summary>
        /// 使用缺省密匙给字符串加密
        /// </summary>
        /// <param name="Source_String"></param>
        /// <returns></returns>
        public static string SHA1_Encrypt(string Source_String)
        {
            byte[] StrRes = Encoding.Default.GetBytes(Source_String);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            StrRes = iSHA.ComputeHash(StrRes);
            StringBuilder EnText = new StringBuilder();
            foreach (byte iByte in StrRes)
            {
                EnText.AppendFormat("{0:x2}", iByte);
            }
            return EnText.ToString();
        }
    }
}