JeffreySu / WeiXinMPSDK

微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
https://weixin.senparc.com
Apache License 2.0
8.43k stars 4.35k forks source link

关于会员卡等增加和修改的功能 #205

Closed hello2008zj closed 8 years ago

hello2008zj commented 8 years ago

1、增加特性,对json格式的输出内容的控制,对枚举类型字符串输出、默认值不输出、例外属性等,如会员卡卡里面的CodeType,主要修改如下: A、 namespace Senparc.Weixin { ///

/// 当值匹配时需要忽略的属性
/// </summary>

public class IgnoreValueAttribute : Attribute
{
    public IgnoreValueAttribute(object value)
    {
        this.Value = value;
    }
    public object Value { get; set; }
}
/// <summary>
/// 例外属性,即不排除的属性值
/// </summary>
public class ExcludedAttribute : Attribute
{

}

/// <summary>
/// 枚举类型显示字符串
/// </summary>
public class EnumStringAttribute : Attribute
{

}

}

B、WeixinJsonConventer修改:

public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { var result = new Dictionary<string, object>(); if (obj == null) { return result; }

        var properties = obj.GetType().GetProperties();
        foreach (var propertyInfo in properties)
        {
            //排除的属性
            bool excludedProp = propertyInfo.IsDefined(typeof(ExcludedAttribute), true);
            if (excludedProp)
                result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
            else
            {
                if (!this._jsonSetting.PropertiesToIgnore.Contains(propertyInfo.Name))
                {
                    bool ignoreProp = propertyInfo.IsDefined(typeof(ScriptIgnoreAttribute), true);
                    if ((this._jsonSetting.IgnoreNulls || ignoreProp) && propertyInfo.GetValue(obj, null) == null)
                    {
                        continue;
                    }

                    //当值匹配时需要忽略的属性
                    IgnoreValueAttribute attri = propertyInfo.GetCustomAttribute<IgnoreValueAttribute>();
                    if (attri != null && attri.Value.Equals(propertyInfo.GetValue(obj)))
                    {
                        continue;
                    }

                    EnumStringAttribute enumStringAttri = propertyInfo.GetCustomAttribute<EnumStringAttribute>();
                    if (enumStringAttri != null)
                    {   //枚举类型显示字符串
                        result.Add(propertyInfo.Name, propertyInfo.GetValue(obj).ToString());
                    }
                    else
                        result.Add(propertyInfo.Name, propertyInfo.GetValue(obj, null));
                }
            }
        }
        return result;
    }

2、会员卡:

A、Card_MemberCardUpdateData 相关属性修改

///

/// 会员卡数据
/// </summary>

public class Card_MemberCardUpdateData : BaseUpdateInfo
{
    /// <summary>

    /// 积分清零规则
    /// 需提审
    /// </summary>

    public string bonus_cleared { get; set; }
    /// <summary>

    /// 积分规则
    /// 需提审
    /// </summary>

    public string bonus_rules { get; set; }
    /// <summary>

    /// 储值说明
    /// 需提审
    /// </summary>

    public string balance_rules { get; set; }
    /// <summary>

    /// 特权说明
    /// 需提审
    /// </summary>

    public string prerogative { get; set; }
    /// <summary>
    /// 设置为true时用户领取会员卡后系统自动将其激活,无需调用激活接口。
    /// 非必填
    /// </summary>
    [IgnoreValue(false)]
    public bool auto_activate { get; set; }
    /// <summary>
    /// 设置为true时会员卡支持一键激活,不允许同时传入activate_url字段,否则设置wx_activate失效。
    /// 非必填
    /// </summary>
    [IgnoreValue(false)]
    public bool wx_activate { get; set; }

    /// <summary>
    /// 激活会员卡的url,与“bind_old_card_url”字段二选一必填。
    /// </summary>
    public string activate_url { get; set; }
    /// <summary>
    /// 设置跳转外链查看积分详情。仅适用于积分无法通过激活接口同步的情况下使用该字段。
    /// 非必填
    /// </summary>
    public string bonus_url { get; set; }
    /// <summary>
    /// 初始设置积分 int型数据
    /// 非必填,null时显示查看
    /// </summary>
    [IgnoreValue(0)]
    public int init_increase_bonus { get; set; }

    /// <summary>
    /// 设置跳转外链查看余额详情。仅适用于余额无法通过激活接口同步的情况下使用该字段。
    /// 非必填
    /// </summary>
    public string balance_url { get; set; }
    /// <summary>
    /// 自定义会员信息类目,会员卡激活后显示。
    /// 非必填
    /// </summary>
    public CustomField custom_field1 { get; set; }
    /// <summary>
    /// 自定义会员信息类目,会员卡激活后显示。
    /// 非必填
    /// </summary>
    public CustomField custom_field2 { get; set; }
    /// <summary>
    /// 自定义会员信息类目,会员卡激活后显示。
    /// 非必填
    /// </summary>
    public CustomField custom_field3 { get; set; }
    /// <summary>
    /// 自定义会员信息类目,会员卡激活后显示
    /// 非必填
    /// </summary>
    public CustomCell custom_cell1 { get; set; }

    /// <summary>
    /// 卡背景图,非必填
    /// </summary>
    public string background_pic_url { get; set; }
}
AnnXu commented 8 years ago

已处理,谢谢 @hello2008zj