国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1. Definition of product management interface
2. Implementation of product management interface
3、商品管理接口的測試
Home WeChat Applet WeChat Development C# development of WeChat portal and application-encapsulation and testing of WeChat store product management interface

C# development of WeChat portal and application-encapsulation and testing of WeChat store product management interface

Feb 18, 2017 am 09:53 AM

In the previous article "Developing WeChat Portals and Applications in C# (22) - Development and Use of WeChat Stores", some basic knowledge of WeChat stores and the corresponding object model were introduced. This article continues the theme of WeChat stores. , introducing the encapsulation and testing use of the API interface. The relevant object models of WeChat stores basically include regular products, product groupings, shelves, inventory, and order models, as well as product classification, product classification attributes, product classification SKU, express mailing templates, image management and other functions. The interface encapsulation introduced in this article is based on these contents, and is tested and used for the implementation of the interface.

1. Definition of product management interface

The previous article introduced the object model of WeChat store, as shown below.

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

This graphic basically covers the related objects of the WeChat store and introduces the relationship between them.

We start with basic product information management. We know that the product interface includes interfaces for adding, modifying, querying, deleting, etc., as shown below.

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

#Product information is the foundation of all micro-stores, so we need to be clearer and more complete in its management operations.

To sum up the functions mentioned above, we can define the interface of WeChat products as follows.

????????#region?商品信息????????///?<summary>
????????///?創(chuàng)建商品????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="merchantJson">商品對象</param>
????????///?<returns></returns>
????????AddMerchantResult?AddMerchant(string?accessToken,?MerchantJson?merchantJson);????????///?<summary>
????????///?刪除商品????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="productId">商品ID</param>
????????///?<returns></returns>
????????CommonResult?DeleteMerchant(string?accessToken,?string?productId);????????///?<summary>
????????///?修改商品????????///?product_id表示要更新的商品的ID,其他字段說明請參考增加商品接口。????????///?從未上架的商品所有信息均可修改,否則商品的名稱(name)、商品分類(category)、商品屬性(property)這三個字段不可修改。????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="merchantJson">修改商品的信息</param>
????????///?<returns></returns>
????????CommonResult?UpdateMerchant(string?accessToken,?MerchantJson?merchantJson);????????///?<summary>
????????///?根據(jù)ID查詢商品信息,如果成功返回MerchantJson信息,否則返回null????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="productId">商品的Id</param>
????????///?<returns></returns>
????????MerchantJson?GetMerchant(string?accessToken,?string?productId);????????///?<summary>
????????///?獲取指定狀態(tài)的所有商品????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="status">商品狀態(tài)(0-全部,?1-上架,?2-下架)</param>
????????///?<returns></returns>
????????List<MerchantJson>?GetMerchantByStatus(string?accessToken,?int?status);????????///?<summary>
????????///?商品上下架????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="status">商品上下架標識(0-下架,?1-上架)</param>
????????///?<returns></returns>
????????CommonResult?UpdateMerchantStatus(string?accessToken,?string?productId,?int?status);?

????????#endregion

Of course, WeChat products also include basic management of categories, category attributes, and category SKUs, so product management also needs to add this content

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

Their functional interface definitions are as follows. Through the following interface, we can easily obtain information such as product classification (not product grouping), SKU information, and classification attributes.

????????#region?商品分類及屬性????????///?<summary>
????????///?獲取指定分類的所有子分類????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="cateId">大分類ID(根節(jié)點分類id為1)</param>
????????///?<returns></returns>
????????List<SubCategory>?GetSub(string?accessToken,?int?cate_id);????????///?<summary>
????????///?獲取指定子分類的所有SKU????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="cateId">商品子分類ID</param>
????????///?<returns></returns>
????????List<SubCategorySku>?GetSku(string?accessToken,?int?cate_id);????????///?<summary>
????????///?獲取指定分類的所有屬性????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="cateId">分類ID</param>
????????///?<returns></returns>
????????List<SubCategoryProperty>?GetProperty(string?accessToken,?int?cate_id);?

????????#endregion

2. Implementation of product management interface

The above interface defines the interface of the corresponding product.

For the implementation of the interface, we generally submit it to the URL according to the interface description of the official website, and POST the data, and then organize it into a regular processing method, obtain the result and convert it into the corresponding object That’s it. For example, the implementation code of adding product operation is as follows.

????????///?<summary>
????????///?創(chuàng)建商品????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="merchantJson">商品對象</param>
????????///?<returns></returns>
????????public?AddMerchantResult?AddMerchant(string?accessToken,?MerchantJson?merchantJson)
????????{????????????var?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????string?postData?=?merchantJson.ToJson();????????????return?JsonHelper<AddMerchantResult>.ConvertJson(url,?postData);
????????}

To return the result, this is to define an object to obtain the ID of the added product and other contents, as shown below.

????///?<summary>
????///?創(chuàng)建商品信息的返回結果????///?</summary>
????public?class?AddMerchantResult?:?ErrorJsonResult
????{????????///?<summary>
????????///?商品ID????????///?</summary>
????????public?string?product_id?{?get;?set;?}
????}

And the base class is the regular response content

????///?<summary>
????///?微信返回Json結果的錯誤數(shù)據(jù)????///?</summary>
????public?class?ErrorJsonResult?
????{????????///?<summary>
????????///?返回代碼????????///?</summary>
????????public?ReturnCode?errcode?{?get;?set;?}????????///?<summary>
????????///?錯誤消息????????///?</summary>
????????public?string?errmsg?{?get;?set;?}
????}

Through these objects Definition, after adding a product, we will know whether the operation is successful. If the addition is successful, a newly created ID will be returned for us to use. We can query specific product information or perform modification, deletion and other operations.

The modification or deletion of product information only needs to return a record of success, so we define a unified response object CommonResult. The interface implementation code for product modification and deletion is as follows.

Since I have highly improved and organized the code, it is relatively easy to understand the various processing codes.

????????///?<summary>
????????///?刪除商品????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="productId">商品ID</param>
????????///?<returns></returns>
????????public?CommonResult?DeleteMerchant(string?accessToken,?string?productId)
????????{????????????var?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????var?data?=?new
????????????{
????????????????product_id?=?productId
????????????};????????????string?postData?=?data.ToJson();????????????return?Helper.GetExecuteResult(url,?postData);
????????}????????///?<summary>
????????///?修改商品????????///?product_id表示要更新的商品的ID,其他字段說明請參考增加商品接口。????????///?從未上架的商品所有信息均可修改,否則商品的名稱(name)、商品分類(category)、商品屬性(property)這三個字段不可修改。????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="merchantJson">修改商品的信息</param>
????????///?<returns></returns>
????????public?CommonResult?UpdateMerchant(string?accessToken,?MerchantJson?merchantJson)
????????{????????????var?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????string?postData?=?merchantJson.ToJson();????????????return?Helper.GetExecuteResult(url,?postData);
????????}

In order to obtain the detailed information of the product, we need to define an entity object of the product so that we can convert the obtained information into entity class information for easy use. and processing.

The product information includes many small defined classes, which constitute the content of each part of the product. The main entity class information is as follows.

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

After defining the relatively complex product information entity, we can process it through objects.

The implementation code for obtaining product details is as follows.

????????///?<summary>
????????///?根據(jù)ID查詢商品信息,如果成功返回MerchantJson信息,否則返回null????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="productId">商品的Id</param>
????????///?<returns></returns>
????????public?MerchantJson?GetMerchant(string?accessToken,?string?productId)
????????{????????????var?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????var?data?=?new
????????????{
????????????????product_id?=?productId
????????????};????????????string?postData?=?data.ToJson();

????????????MerchantJson?merchant?=?null;
????????????GetMerchantResult?result?=?JsonHelper<GetMerchantResult>.ConvertJson(url,?postData);????????????if?(result?!=?null)
????????????{
????????????????merchant?=?result.product_info;
????????????}????????????return?merchant;
????????}

Although the entity information of the product is very complex, once we define it, it is easy for us to convert and process the results. The above code is not It's hard to understand. The main thing is to convert the data after submitting it.

Of course, we can also get the product list content in different states, as shown in the following code.

????????///?<summary>
????????///?獲取指定狀態(tài)的所有商品????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="status">商品狀態(tài)(0-全部,?1-上架,?2-下架)</param>
????????///?<returns></returns>
????????public?List<MerchantJson>?GetMerchantByStatus(string?accessToken,?int?status)
????????{????????????var?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????var?data?=?new
????????????{
????????????????status?=?status
????????????};????????????string?postData?=?data.ToJson();

????????????List<MerchantJson>?list?=?new?List<MerchantJson>();
????????????GetMerchantByStatus?result?=?JsonHelper<GetMerchantByStatus>.ConvertJson(url,?postData);????????????if?(result?!=?null)
????????????{
????????????????list?=?result.products_info;
????????????}????????????return?list;
????????}

我們添加商品的時候,商品的分類信息、分類屬性、分類SKU信息也都是很重要的內(nèi)容,我們需要指定對應商品分類才能添加到微信小店里面。

獲取商品分類的操作實現(xiàn)代碼如下所示。

????????///?<summary>
????????///?獲取指定分類的所有子分類????????///?</summary>
????????///?<param name="accessToken">調(diào)用接口憑證</param>
????????///?<param name="cateId">大分類ID(根節(jié)點分類id為1)</param>
????????///?<returns></returns>
????????public?List<SubCategory>?GetSub(string?accessToken,?int?cate_id)
????????{????????????var?url?=?string.Format("http://www.miracleart.cn/{0}",?accessToken);????????????var?data?=?new
????????????{
????????????????cate_id?=?cate_id
????????????};????????????string?postData?=?data.ToJson();

????????????List<SubCategory>?list?=?new?List<SubCategory>();
????????????GetSubResult?result?=?JsonHelper<GetSubResult>.ConvertJson(url,?postData);????????????if(result?!=?null)
????????????{
????????????????list?=?result.cate_list;
????????????}????????????return?list;
????????}

?

3、商品管理接口的測試

為了驗證我們開發(fā)的接口,我們需要增加一個測試項目,方便對我們編寫的API進行測試,測試完全成功后,我們才能正式在項目中使用。

我為了方便,創(chuàng)建了一個Winform項目,分別對各個接口進行測試。

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

本篇主要介紹商品管理方面的接口,因此下面主要介紹其中商品管理部分的接口測試代碼,以及對應的結果。

其中商品常規(guī)管理的接口測試代碼如下所示。

????????private?void?btnMerchant_Click(object?sender,?EventArgs?e)
????????{????????????//商品管理
????????????IMerchantApi?api?=?new?MerchantApi();????????????//獲取所有商品信息
????????????Console.WriteLine("獲取所有商品信息");
????????????List<MerchantJson>?list?=?api.GetMerchantByStatus(token,?0);????????????foreach(MerchantJson?json?in?list)
????????????{
????????????????Console.WriteLine(json.ToJson());
????????????????Console.WriteLine();
????????????}????????????//更新商品狀態(tài)
????????????Console.WriteLine("更新商品狀態(tài)");????????????foreach?(MerchantJson?json?in?list)
????????????{
????????????????CommonResult?result?=?api.UpdateMerchantStatus(token,?json.product_id,?1);
????????????????Console.WriteLine("商品ID:{0},商品名稱:{1},?操作:{2}",?
????????????????????json.product_id,?json.product_base.name,?result.Success???"成功"?:?"失敗");
????????????}

????????????Thread.Sleep(1000);????????????//根據(jù)商品ID獲取商品信息
????????????Console.WriteLine("根據(jù)商品ID獲取商品信息");????????????foreach?(MerchantJson?json?in?list)
????????????{
????????????????MerchantJson?getJson?=?api.GetMerchant(token,?json.product_id);????????????????if(json?!=?null)
????????????????{
????????????????????Console.WriteLine("商品ID:{0},商品名稱:{1}",?getJson.product_id,?getJson.product_base.name);
????????????????}
????????????}
????????}

測試后結果如下所示(就是返回我微店鋪里面的商品信息),一切正常。

返回的商品Json數(shù)據(jù)如下所示:

{??"product_id":?"pSiLnt6FYDuFtrRRPMlkdKbye-rE",??"product_base":?{????"category_id":?[??????"537103312"
????],????"property":?[
??????{????????"id":?"類型",????????"vid":?"軟件產(chǎn)品設計"
??????}
????],????"name":?"代碼生成工具Database2Sharp",????"sku_info":?[],????"main_img":?"http://www.miracleart.cn/",????"img":?[??????"http://www.miracleart.cn/"
????],????"detail":?[],????"buy_limit":?0,????"detail_html":?""
??},??"sku_list":?[
????{??????"sku_id":?"",??????"ori_price":?100000,??????"price":?50000,??????"icon_url":?"",??????"quantity":?1100,??????"product_code":?""
????}
??],??"attrext":?{????"location":?{??????"country":?"中國",??????"province":?"廣東",??????"city":?"廣州",??????"address":?""
????},????"isPostFree":?1,????"isHasReceipt":?0,????"isUnderGuaranty":?0,????"isSupportReplace":?0
??},??"delivery_info":?{????"delivery_type":?0,????"template_id":?175807970,????"express":?[
??????{????????"id":?10000027,????????"price":?0
??????},
??????{????????"id":?10000028,????????"price":?0
??????},
??????{????????"id":?10000029,????????"price":?0
??????}
????]
??},??"status":?1}

View Code

測試的部分結果輸出如下所示。

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

另外,“商品維護管理”的功能測試主要就是測試商品的增加、修改、刪除操作,具體代碼如下所示。

????????private?void?btnMerchantEdit_Click(object?sender,?EventArgs?e)
????????{
????????????IMerchantApi?api?=?new?MerchantApi();????????????
????????????string?img1?=?"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0";????????????
????????????string?img2?=?"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ul1UcLcwxrFdwTKYhH9Q5YZoCfX4Ncx655ZK6ibnlibCCErbKQtReySaVA/0n/";????????????
????????????string?img3?=?"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0n/";????????????
????????????//商品增刪改處理
????????????MerchantJson?merchant?=?new?MerchantJson();
????????????merchant.product_base?=?new?Merchant_base();
????????????merchant.product_base.name?=?"測試產(chǎn)品";
????????????merchant.product_base.category_id.Add("537074298");
????????????merchant.product_base.img?=?new?List<string>()?{?img1,?img2,?img3?};
????????????merchant.product_base.main_img?=?img1;
????????????merchant.product_base.detail.AddRange(new?List<MerchantDetail>()?{????????????????????new?MerchantDetail()
????????????????????{
????????????????????????text?=?"test?first"
????????????????????},????????????????????new?MerchantDetail()
????????????????????{
????????????????????????img?=?img2
????????????????????},?new?MerchantDetail()
????????????????????{
????????????????????????text?=?"test?again"
????????????????????}
????????????});
????????????merchant.product_base.property.AddRange(new?List<MerchantProperty>(){????????????????new?MerchantProperty
????????????????{
????????????????????id=?"1075741879",
????????????????????vid="1079749967"
????????????????},????????????????new?MerchantProperty{
????????????????????id=?"1075754127",
????????????????????vid=?"1079795198"
????????????????},????????????????new?MerchantProperty(){
????????????????????id=?"1075777334",
????????????????????vid=?"1079837440"
????????????????}
????????????});
????????????merchant.product_base.sku_info.AddRange(new?List<MerchantSku>(){????????????????new?MerchantSku{
????????????????????id=??"1075741873",
????????????????????vid?=?new?List<string>()?{????????????????????????"1079742386",????????????????????????"1079742363"
????????????????????}
????????????????}
????????????});
????????????merchant.product_base.buy_limit?=?10;????????????//merchant.product_base.detail_html?=?"<p class=\"item_pic_wrp\" style=\"margin-bottom:8px;font-size:0;\"><img class=\"item_pic\" style=\"width:100%;\" alt=\"\" src=\"http://www.miracleart.cn/\" ></p><p style=\"margin-bottom:11px;margin-top:11px;\">test</p><p class=\"item_pic_wrp\" style=\"margin-bottom:8px;font-size:0;\"><img class=\"item_pic\" style=\"width:100%;\" alt=\"\" src=\"http://www.miracleart.cn/\" ></p><p style=\"margin-bottom:11px;margin-top:11px;\">test?again</p>";
????????????merchant.sku_list.AddRange(new?List<MerchantSku_list>()
????????????{????????????????new?MerchantSku_list(){
????????????????sku_id="1075741873:1079742386",
????????????????price=30,
????????????????icon_url="http://mmbiz.qpic.cn/mmbiz/4whpV1VZl2iccsvYbHvnphkyGtnvjD3ulEKogfsiaua49pvLfUS8Ym0GSYjViaLic0FD3vN0V8PILcibEGb2fPfEOmw/0",
????????????????quantity=800,
????????????????product_code="testing",
????????????????ori_price=9000000
????????????????},????????????????new?MerchantSku_list(){
????????????????????sku_id="1075741873:1079742363",
????????????????????price=30,
????????????????????icon_url="http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0",
????????????????????quantity=800,
????????????????????product_code="testingtesting",
????????????????????ori_price=9000000
????????????????}
????????????});
????????????merchant.attrext?=?new?MerchantAttrext()
????????????{
????????????????location?=?new?MerchantLocation()
????????????????{
????????????????????country?=?"中國",
????????????????????province?=?"廣東省",
????????????????????city?=?"廣州市",
????????????????????address?=?"T.I.T創(chuàng)意園"
????????????????},
????????????????isPostFree?=?0,
????????????????isHasReceipt?=?1,
????????????????isUnderGuaranty?=?0,
????????????????isSupportReplace?=?0
????????????};
????????????merchant.delivery_info?=?new?MerchantDelivery()
????????????{
????????????????delivery_type?=?0,
????????????????template_id?=?0,
????????????????express?=?new?List<MerchantExpress>(){????????????????new?MerchantExpress()?{
????????????????????id=10000027,?
????????????????????price=100
????????????????},?
????????????????new?MerchantExpress(){
????????????????????id=10000028,?
????????????????????price=100
????????????????},?
????????????????new?MerchantExpress(){
????????????????????id=10000029,?
????????????????????price=100
????????????????}}
????????????};

????????????Console.WriteLine(merchant.ToJson());

????????????AddMerchantResult?result?=?api.AddMerchant(token,?merchant);
????????????Console.WriteLine("添加商品:{0}",?result.product_id);????????????if?(!string.IsNullOrEmpty(result.product_id))
????????????{????????????????//更新商品
????????????????merchant.product_id?=?result.product_id;
????????????????merchant.product_base.name?=?"測試產(chǎn)品22";
????????????????CommonResult?updateResult?=?api.UpdateMerchant(token,?merchant);
????????????????Console.WriteLine("更新商品:{0}",?updateResult.Success???"成功"?:?"失敗");


????????????????CommonResult?deleteResult?=?api.DeleteMerchant(token,?merchant.product_id);
????????????????Console.WriteLine("刪除商品:{0}",?deleteResult.Success???"成功"?:?"失敗");
????????????}
????????}

測試的輸出結果如下所示(一切成功)。

C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試

以上就是我對商品管理接口的API定義和實現(xiàn),以及對接口進行測試的闡述,基本上把所有相關的內(nèi)容都貼出來了,希望大家能夠?qū)ξ⒌觊_發(fā)部分,有更深入的了解和認識。

?更多C#開發(fā)微信門戶及應用-微信小店商品管理接口的封裝和測試?相關文章請關注PHP中文網(wǎng)!


Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276