1. 程式人生 > >微信平臺開發——日歷服務

微信平臺開發——日歷服務

int cat href nth 服務 write pre b2c except



非常多人可能用過例如以下的功能:



技術分享



我向微信號發個字符,然後後臺去解析字符,拆出當中的keyword,然後去數據庫查詢是否開啟此項服務,假設服務開啟,則返回給用戶調用此服務的結果。


近期兩天開始做的服務有。天氣查詢,日歷。快遞。火車。黃金。。

。等六個服務做成接口。今天要分析的是這裏面唯一沒有調用外部API接口的服務。


首先,我們要寫好一個計算農歷的方法,:



#region 獲取農歷方法

        

        ///<summary>
        /// 實例化一個 ChineseLunisolarCalendar
        ///</summary>
        private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();

        ///<summary>
        /// 十天幹
        ///</summary>
        private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

        ///<summary>
        /// 十二地支
        ///</summary>
        private static string[] dz = { "子", "醜", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };

        ///<summary>
        /// 十二生肖
        ///</summary>
        private static string[] sx = { "鼠", "牛", "虎", "免", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };

        ///<summary>
        /// 返回農歷天幹地支年
        ///</summary>
        ///<param name="year">農歷年</param>
        ///<return s></return s>
        public static string GetLunisolarYear(int year)
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;

                return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
            }

            throw new ArgumentOutOfRangeException("無效的年份!");
        }

        ///<summary>
        /// 農歷月
        ///</summary>

        ///<return s></return s>
        private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(臘)" };

        ///<summary>
        /// 農歷日
        ///</summary>
        private static string[] days1 = { "初", "十", "廿", "三" };
        ///<summary>
        /// 農歷日
        ///</summary>
        private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };


        ///<summary>
        /// 返回農歷月
        ///</summary>
        ///<param name="month">月份</param>
        ///<return s></return s>
        public static string GetLunisolarMonth(int month)
        {
            if (month < 13 && month > 0)
            {
                return months[month - 1];
            }

            throw new ArgumentOutOfRangeException("無效的月份!");
        }

        ///<summary>
        /// 返回農歷日
        ///</summary>
        ///<param name="day">天</param>
        ///<return s></return s>
        public static string GetLunisolarDay(int day)
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(days[(day - 1) / 10], days1[1]);
                }
            }

            throw new ArgumentOutOfRangeException("無效的日!");
        }



        ///<summary>
        /// 依據公歷獲取農歷日期
        ///</summary>
        ///<param name="datetime">公歷日期</param>
        ///<return s></return s>
        public static string GetChineseDateTime(DateTime datetime)
        {
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            //獲取閏月。 0 則表示沒有閏月
            int leapMonth = ChineseCalendar.GetLeapMonth(year);

            bool isleap = false;

            if (leapMonth > 0)
            {
                if (leapMonth == month)
                {
                    //閏月
                    isleap = true;
                    month--;
                }
                else if (month > leapMonth)
                {
                    month--;
                }
            }

            return string.Concat(GetLunisolarYear(year), "年", isleap ? "閏" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }




        #endregion



接著,拼接好返回的字符:



  Console.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\r\n" + "農歷為:"+GetChineseDateTime(DateTime.Now));


為了測試外部調用的情況。我們能夠這樣。一個ajax過來。然後提交到handler裏面,handler調用這些接口來測試下。


須要註意的是。

1,拼接好的字符串裏面的換行符最好是HTML標簽中的<br/>,不使用\n。

2,調用百度等大型開發平臺的API接口的時候。對於返回的JSON,處理JSON轉對象的時候。要註意JSON裏面數組都要變成LIST。然後再調用自己構造的泛型方法去轉換。




以下分享一些私人手藏的接口:

http://api.ajaxsns.com/

http://www.djdkx.com/open/randxml

location=%E5%8C%97%E4%BA%AC&output=json&ak=6brZIzYakoTEW1xrDYk0Wqhd">http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%AC&output=json&ak=這裏填寫自己的AK

http://www.twototwo.cn/train/QueryTrainScheduleByNumber.aspx

http://www.chepiao100.com/my/doc/checi.html

http://www.haoservice.com/apilist/

http://www.bejson.com/go.html?u=http://www.bejson.com/webInterface.html


網上也有非常多付費的接口。另外,今天做黃金查詢的時候。發現了個聚合數據也不錯喲~






微信平臺開發——日歷服務