微信些许方法


/**
        * 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数
         * @return 时间戳
        */
        public static string GenerateTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

/**
        * 生成随机串,随机串包含字母或数字
        * @return 随机串
        */
        public static string GenerateNonceStr()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }

/**
        * 根据当前系统时间加随机序列来生成订单号
         * @return 订单号
        */
        public static string GenerateOutTradeNo()
        {
            var ran = new Random();
            return string.Format("{0}{1}{2}", WxPayConfig.MCHID, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
        }

/// <summary>
        /// 自定义菜单创建接口
        /// </summary>
        /// <param name="list">菜单集合</param>
        /// <param name="token">微信令牌</param>
        public static void CreateMenu(List<MenuButton> list, string token = null)
        {
            token = token ?? GetToken();

            if (list == null)
                throw new ArgumentException("创建菜单的List为null");
            if (list.Count > 3)
                throw new Exception("一级菜单不能超过三个");

            var obj = new JsonObject();
            obj["button"] = new JsonProperty();
            foreach (var menuButton in list)
            {
                var jobj = new JsonObject();
                jobj["name"].SetValue(menuButton.Name);
                if (menuButton.SubButton != null && menuButton.SubButton.Count > 0)
                {
                    if (menuButton.SubButton.Count > 5)
                        throw new Exception("一级菜单【" + menuButton.Name + "】的子菜单超过了5个!");
                    jobj["sub_button"] = new JsonProperty();
                    foreach (var button in menuButton.SubButton)
                    {
                        var jobjButton = new JsonObject();
                        jobjButton["name"].SetValue(button.Name);
                        jobjButton["type"].SetValue(EnumHelper.GetDesc(button.Type));
                        if (button.Type == EnumMenuButtonType.View)
                            jobjButton["url"].SetValue(button.Url);
                        else
                            jobjButton["key"].SetValue(button.Key);
                        jobj["sub_button"].Add(jobjButton);
                    }
                }
                else
                {
                    jobj["type"].SetValue(EnumHelper.GetDesc(menuButton.Type));
                    if (menuButton.Type == EnumMenuButtonType.View)
                        jobj["url"].SetValue(menuButton.Url);
                    else
                        jobj["key"].SetValue(menuButton.Key);
                }
                obj["button"].Add(jobj);
            }

            var json = WebHelper.Post(string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", token),
                "application/json", obj.ToString());
            var result = IsSuccess(json);
            if (result.errcode != 0)
            {
                LogHelper.Error("WeChatApi.CreateMenu.json:" + json + ".");
                //throw new Exception("生成微信菜单失败:[" + result.ErrCode + "]" + result.ErrMsg);
            }
        }

/// <summary>
        /// 自定义菜单查询接口
        /// </summary>
        /// <param name="token">密令</param>
        /// <returns>菜单列表</returns>
        public static List<MenuButton> GetMenu(string token = null)
        {
            var list = new List<MenuButton>();
            token = token ?? GetToken();
            var json =
                WebHelper.Get(string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", token));
            var jobj = new JsonObject(json);
            json = jobj["menu"]["button"].ToString();
            list = JsonHelper.ParseJson<List<MenuButton>>(json);
            return list;
        }

/// <summary>
        /// 自定义菜单查询接口
        /// </summary>
        /// <param name="token">密令</param>
        /// <returns>菜单列表</returns>
        public static List<MenuButton> DeleteMenu(string token = null)
        {
            var list = new List<MenuButton>();
            token = token ?? GetToken();
            var json =
                WebHelper.Get(string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", token));
            var result = IsSuccess(json);
            if (result.errcode != 0)
            {
                throw new Exception("自定义菜单删除失败:[" + result.errcode + "]" + result.errmsg);
            }
            return list;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值