Home  >  Article  >  WeChat Applet  >  C# development of WeChat portal and application of WeChat store shelf information management

C# development of WeChat portal and application of WeChat store shelf information management

高洛峰
高洛峰Original
2017-03-01 10:43:482017browse

1. Introduction to WeChat store shelves

In the background of the WeChat official account, shelf information can be maintained, and the interface is as follows. The concept of a shelf is to display products in categories to customers. A shelf is similar to a well-layed showcase. We can define different shelves and then publish different URLs for experience.

C# development of WeChat portal and application of WeChat store shelf information management

In addition, we generally create shelves based on the shelf template library. The shelf template allows us to quickly build a shelf and provides a visual reference interface. The shelf template interface is shown below.

C# development of WeChat portal and application of WeChat store shelf information management

2. Development model of shelf management

For the development of WeChat stores using APIs, the shelf management operation interface of WeChat stores, It is similar to a regular module and has the following functional operations.

C# development of WeChat portal and application of WeChat store shelf information management

Although it looks similar to the previous object model, the shelf information is very complex, so if you need to restore it to an entity object based on Json data, you need to repeat it. Consider it carefully, otherwise it is easy to model errors.

Corresponds to the shelf template of the WeChat store management interface. The object information of the shelf includes 5 different control models, and some of them can be used in combination.

C# development of WeChat portal and application of WeChat store shelf information management

The model display of several shelves is shown below.

C# development of WeChat portal and application of WeChat store shelf information managementC# development of WeChat portal and application of WeChat store shelf information management

C# development of WeChat portal and application of WeChat store shelf information managementC# development of WeChat portal and application of WeChat store shelf information management

C# development of WeChat portal and application of WeChat store shelf information management

##Through the above 5 control models, we can see They represent different layout effects respectively, and they can be used in combination on the shelves.

3. Object modeling of shelf information

According to the interface description of WeChat store, the shelf entity object information we finally defined is very rich and flexible in content.

C# development of WeChat portal and application of WeChat store shelf information management

By referring to the API description of the WeChat store, we can see that the shelf information JSON data is very complex, and the specific definition is as follows.

{
    "shelf_data": {
      "module_infos": [
        {
          "group_info": {
            "filter": {
              "count": 2
            },
            "group_id": 50
          },
          "eid": 1
        },
        {
            "group_infos": {
                "groups": [
                  {
                    "group_id": 49
                  },
                  {
                    "group_id": 50
                  },
                  {
                    "group_id": 51
                  }
                ]
          },
          "eid": 2
        },
        {
          "group_info": {
            "group_id": 52,
            "img": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5Jm64z4I0TTicv0TjN7Vl9bykUUibYKIOjicAwIt6Oy0Y6a1Rjp5Tos8tg/0"
          },
          "eid": 3
        },
        {
          "group_infos": {  
            "groups": [
              {
                "group_id": 49,
                "img": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
              },
              {
                "group_id": 50,
                "img": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5G1kdy3ViblHrR54gbCmbiaMnl5HpLGm5JFeENyO9FEZAy6mPypEpLibLA/0"
              },
              {
                "group_id": 52,
                "img": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
              }
            ]
          },
          "eid": 4
        }, 
        {
          "group_infos": {
            "groups": [
              {
                "group_id": 43
              },
              {
                "group_id": 44
              },
              {
                "group_id": 45
              },
              {
                "group_id": 46
              }
            ],
            "img_background": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl29nqqObBwFwnIX3licVPnFV5uUQx7TLx4tB9qZfbe3JmqR4NkkEmpb5LUWoXF1ek9nga0IkeSSFZ8g/0"
          },
          "eid": 5
        }
      ]
    }, 
    "shelf_banner": "http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2ibrWQn8zWFUh1YznsMV0XEiavFfLzDWYyvQOBBszXlMaiabGWzz5B2KhNn2IDemHa3iarmCyribYlZYyw/0", 
    "shelf_name": "测试货架"
}

We have defined several shelf control objects based on the definition of JSON data. Their relationship is as follows.

C# development of WeChat portal and application of WeChat store shelf information management

We can model entity objects based on JSON data, and then with these objects, we can further define the relevant operation interfaces of the shelves. The interface definition is as follows Show.

#region 货架管理

        /// <summary>
        /// 增加货架
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架招牌图片Url
        /// <param>货架名称
        /// <param>货架控件1,2,3,4,5类型的集合
        /// <returns></returns>
        AddShelfResult AddShelf(string accessToken, string shelfBanner, string shelfName, List<shelfcontrolbase> controls);

        /// <summary>
        /// 删除货架
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架Id
        /// <returns></returns>
        CommonResult DeleteShelf(string accessToken, int shelfId);

        /// <summary>
        /// 修改货架
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架Id
        /// <param>货架招牌图片Url
        /// <param>货架名称
        /// <param>货架控件1,2,3,4,5类型的集合
        /// <returns></returns>
        CommonResult UpdateShelf(string accessToken, int shelfId, string shelfBanner, string shelfName, List<shelfcontrolbase> controls);

        /// <summary>
        /// 获取所有货架
        /// </summary>
        /// <param>调用接口凭证
        /// <returns></returns>
        List<shelfjson> GetAllShelf(string accessToken);

        /// <summary>
        /// 根据货架ID获取货架信息
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架Id
        /// <returns></returns>
        ShelfJson GetShelfById(string accessToken, int shelfId); 

        #endregion</shelfjson></shelfcontrolbase></shelfcontrolbase>

有了这些接口的定义,我们就需要实现对应的接口,从而实现我们向微信API的封装处理了。

微信小店的货架管理实现内容如下所示(部分内容,增删改)。

/// <summary>
        /// 增加货架
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架招牌图片Url
        /// <param>货架名称
        /// <param>货架控件1,2,3,4,5类型的集合
        /// <returns></returns>
        public AddShelfResult AddShelf(string accessToken, string shelfBanner, string shelfName, List<shelfcontrolbase> controls)
        {
            var url = string.Format("https://api.weixin.qq.com/merchant/shelf/add?access_token={0}", accessToken);
            var data = new
            {
                shelf_data = new
                {
                    module_infos = controls
                },
                shelf_banner = shelfBanner,
                shelf_name = shelfName
            };

            string postData = data.ToJson();
            return JsonHelper<addshelfresult>.ConvertJson(url, postData);
        }

        /// <summary>
        /// 删除货架
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架Id
        /// <returns></returns>
        public CommonResult DeleteShelf(string accessToken, int shelfId)
        {
            var url = string.Format("https://api.weixin.qq.com/merchant/shelf/del?access_token={0}", accessToken);
            var data = new
            {
                shelf_id = shelfId
            };
            string postData = data.ToJson();

            return Helper.GetExecuteResult(url, postData);
        }

        /// <summary>
        /// 修改货架
        /// </summary>
        /// <param>调用接口凭证
        /// <param>货架Id
        /// <param>货架招牌图片Url
        /// <param>货架名称
        /// <param>货架控件1,2,3,4,5类型的集合
        /// <returns></returns>
        public CommonResult UpdateShelf(string accessToken, int shelfId, string shelfBanner, string shelfName, List<shelfcontrolbase> controls)
        {
            var url = string.Format("https://api.weixin.qq.com/merchant/shelf/mod?access_token={0}", accessToken);
            var data = new
            {
                shelf_id = shelfId,
                shelf_data = new
                {
                    module_infos = controls
                },
                shelf_banner = shelfBanner,
                shelf_name = shelfName
            };
            string postData = data.ToJson();

            return Helper.GetExecuteResult(url, postData);
        }</shelfcontrolbase></addshelfresult></shelfcontrolbase>

4、微信小店货架管理的接口测试

由于货架管理的对象和接口定义比较复杂一些,一定需要进行反复的测试才能正式使用,如果不注意有可能你定义的实体类,获取不到某个字段信息。

我为了方便,创建了一个Winform项目,分别对各个接口进行测试。

C# development of WeChat portal and application of WeChat store shelf information management

对于货架管理内容的接口测试,测试代码如下所示。

private void btnShelf_Click(object sender, EventArgs e)
        {
            IMerchantApi api = new MerchantApi();
            List<shelfjson> list = api.GetAllShelf(token);
            Console.WriteLine(list.ToJson());

            foreach(ShelfJson json in list)
            {
                Console.WriteLine("货架信息:");
                ShelfJson getJson = api.GetShelfById(token, json.shelf_id.Value);
                Console.WriteLine(getJson.ToJson());
            }

            string shelf_banner = "http://mmbiz.qpic.cn/mmbiz/mLqH9gr11Gyb2sgiaelcsxYtQENGePp0RgeNlAQicfZQokjbJMUq4h8MHtjpekJNEWKuMN3gdRz5RxfkYb7NlIrw/0";
            string shelf_name = "测试货架";
            ShelfControl1 c11 = new ShelfControl1(6, 202797386);            
            ShelfControl1 c12 = new ShelfControl1(4, 202797397);
            List<shelfcontrolbase> controlList = new List<shelfcontrolbase>(){c11, c12};
            AddShelfResult result = api.AddShelf(token, shelf_banner, shelf_name, controlList);
            if (result != null && result.shelf_id > 0)
            {
                Console.WriteLine("增加的货架信息:");
                ShelfJson getJson = api.GetShelfById(token, result.shelf_id);
                Console.WriteLine(getJson.ToJson());

                shelf_name = "测试货架-修改";
                controlList = new List<shelfcontrolbase>(){c11};
                CommonResult updateReuslt = api.UpdateShelf(token, result.shelf_id, shelf_banner, shelf_name, controlList);
                Console.WriteLine("修改货架操作:{0}", updateReuslt.Success ? "成功" : "失败");

                CommonResult deleteResult = api.DeleteShelf(token, result.shelf_id);
                Console.WriteLine("删除货架操作:{0}", deleteResult.Success ? "成功" : "失败");
            }
        }</shelfcontrolbase></shelfcontrolbase></shelfcontrolbase></shelfjson>

C# development of WeChat portal and application of WeChat store shelf information management

 

更多C# development of WeChat portal and application of WeChat store shelf information management相关文章请关注PHP中文网!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn