


C# develops WeChat portal and applies encapsulation and testing of WeChat store product management interface
Mar 01, 2017 am 10:51 AM1. Definition of product management interface
The previous article introduced the object model of WeChat store, as shown below.
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.
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>調(diào)用接口憑證 ????????///?<param>商品對(duì)象 ????????///?<returns></returns> ????????AddMerchantResult?AddMerchant(string?accessToken,?MerchantJson?merchantJson); ????????///?<summary> ????????///?刪除商品 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品ID ????????///?<returns></returns> ????????CommonResult?DeleteMerchant(string?accessToken,?string?productId); ????????///?<summary> ????????///?修改商品 ????????///?product_id表示要更新的商品的ID,其他字段說(shuō)明請(qǐng)參考增加商品接口。 ????????///?從未上架的商品所有信息均可修改,否則商品的名稱(name)、商品分類(category)、商品屬性(property)這三個(gè)字段不可修改。 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>修改商品的信息 ????????///?<returns></returns> ????????CommonResult?UpdateMerchant(string?accessToken,?MerchantJson?merchantJson); ????????///?<summary> ????????///?根據(jù)ID查詢商品信息,如果成功返回MerchantJson信息,否則返回null ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品的Id ????????///?<returns></returns> ????????MerchantJson?GetMerchant(string?accessToken,?string?productId); ????????///?<summary> ????????///?獲取指定狀態(tài)的所有商品 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品狀態(tài)(0-全部,?1-上架,?2-下架) ????????///?<returns></returns> ????????List<merchantjson>?GetMerchantByStatus(string?accessToken,?int?status); ????????///?<summary> ????????///?商品上下架 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品上下架標(biāo)識(shí)(0-下架,?1-上架) ????????///?<returns></returns> ????????CommonResult?UpdateMerchantStatus(string?accessToken,?string?productId,?int?status);? ????????#endregion</merchantjson>
Of course, WeChat products also include basic management of categories, category attributes, and category SKUs, so product management also needs to add this content
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>調(diào)用接口憑證 ????????///?<param>大分類ID(根節(jié)點(diǎn)分類id為1) ????????///?<returns></returns> ????????List<subcategory>?GetSub(string?accessToken,?int?cate_id); ????????///?<summary> ????????///?獲取指定子分類的所有SKU ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品子分類ID ????????///?<returns></returns> ????????List<subcategorysku>?GetSku(string?accessToken,?int?cate_id); ????????///?<summary> ????????///?獲取指定分類的所有屬性 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>分類ID ????????///?<returns></returns> ????????List<subcategoryproperty>?GetProperty(string?accessToken,?int?cate_id);? ????????#endregion</subcategoryproperty></subcategorysku></subcategory>
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>調(diào)用接口憑證 ????????///?<param>商品對(duì)象 ????????///?<returns></returns> ????????public?AddMerchantResult?AddMerchant(string?accessToken,?MerchantJson?merchantJson) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/merchant/create?access_token={0}",?accessToken); ????????????string?postData?=?merchantJson.ToJson(); ????????????return?JsonHelper<addmerchantresult>.ConvertJson(url,?postData); ????????}</addmerchantresult>
To return the result, this is to define an object to obtain the ID of the added product and other contents, as shown below.
////// Return result of creating product information
///
public class AddMerchantResult: ErrorJsonResult
{
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
///?<summary> ????///?微信返回Json結(jié)果的錯(cuò)誤數(shù)據(jù) ????///?</summary> ????public?class?ErrorJsonResult? ????{ ????????///?<summary> ????????///?返回代碼 ????????///?</summary> ????????public?ReturnCode?errcode?{?get;?set;?} ????????///?<summary> ????????///?錯(cuò)誤消息 ????????///?</summary> ????????public?string?errmsg?{?get;?set;?} ????}
Through the definition of these objects, after adding the 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 operations such as modification and deletion. 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>調(diào)用接口憑證 ????????///?<param>商品ID ????????///?<returns></returns> ????????public?CommonResult?DeleteMerchant(string?accessToken,?string?productId) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/merchant/del?access_token={0}",?accessToken); ????????????var?data?=?new ????????????{ ????????????????product_id?=?productId ????????????}; ????????????string?postData?=?data.ToJson(); ????????????return?Helper.GetExecuteResult(url,?postData); ????????} ????????///?<summary> ????????///?修改商品 ????????///?product_id表示要更新的商品的ID,其他字段說(shuō)明請(qǐng)參考增加商品接口。 ????????///?從未上架的商品所有信息均可修改,否則商品的名稱(name)、商品分類(category)、商品屬性(property)這三個(gè)字段不可修改。 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>修改商品的信息 ????????///?<returns></returns> ????????public?CommonResult?UpdateMerchant(string?accessToken,?MerchantJson?merchantJson) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/merchant/update?access_token={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.
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>調(diào)用接口憑證 ????????///?<param>商品的Id ????????///?<returns></returns> ????????public?MerchantJson?GetMerchant(string?accessToken,?string?productId) ????????{????????????var?url?=?string.Format("http://ipnx.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; ????????}</getmerchantresult>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>調(diào)用接口憑證 ????????///?<param>商品狀態(tài)(0-全部,?1-上架,?2-下架) ????????///?<returns></returns> ????????public?List<merchantjson>?GetMerchantByStatus(string?accessToken,?int?status) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/merchant/getbystatus?access_token={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; ????????}</getmerchantbystatus></merchantjson></merchantjson></merchantjson>When we add products, the product classification information, classification attributes, and classification SKU information are also very important. We need to specify the corresponding product classification. Only then can it be added to the WeChat store.
獲取商品分類的操作實(shí)現(xiàn)代碼如下所示。
///?<summary> ????????///?獲取指定分類的所有子分類 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>大分類ID(根節(jié)點(diǎn)分類id為1) ????????///?<returns></returns> ????????public?List<subcategory>?GetSub(string?accessToken,?int?cate_id) ????????{ ????????????var?url?=?string.Format("https://api.weixin.qq.com/merchant/category/getsub?access_token={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; ????????}</getsubresult></subcategory></subcategory></subcategory>
?
3、商品管理接口的測(cè)試
為了驗(yàn)證我們開發(fā)的接口,我們需要增加一個(gè)測(cè)試項(xiàng)目,方便對(duì)我們編寫的API進(jìn)行測(cè)試,測(cè)試完全成功后,我們才能正式在項(xiàng)目中使用。
我為了方便,創(chuàng)建了一個(gè)Winform項(xiàng)目,分別對(duì)各個(gè)接口進(jìn)行測(cè)試。
本篇主要介紹商品管理方面的接口,因此下面主要介紹其中商品管理部分的接口測(cè)試代碼,以及對(duì)應(yīng)的結(jié)果。
其中商品常規(guī)管理的接口測(cè)試代碼如下所示。
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); ????????????????} ????????????} ????????}</merchantjson>
測(cè)試后結(jié)果如下所示(就是返回我微店鋪里面的商品信息),一切正常。
返回的商品Json數(shù)據(jù)如下所示:
{ ??"product_id":?"pSiLnt6FYDuFtrRRPMlkdKbye-rE", ??"product_base":?{ ????"category_id":?[ ??????"537103312" ????], ????"property":?[ ??????{ ????????"id":?"類型", ????????"vid":?"軟件產(chǎn)品設(shè)計(jì)" ??????} ????], ????"name":?"代碼生成工具Database2Sharp", ????"sku_info":?[], ????"main_img":?"http://mmbiz.qpic.cn/mmbiz/mLqH9gr11Gyb2sgiaelcsxYtQENGePp0Rb3AZKbjkicnKTUNBrEdo7Dyic97ar46SoAfKRB5x2R94bDUdNpgqiaZzA/0", ????"img":?[ ??????"http://mmbiz.qpic.cn/mmbiz/mLqH9gr11Gyb2sgiaelcsxYtQENGePp0RiaheJmVXm7tbvTYUQV7OF3DgfGiaQVMh3WbeEcGDOQQiajQXGKK9tfoeA/0" ????], ????"detail":?[], ????"buy_limit":?0, ????"detail_html":?"" ??}, ??"sku_list":?[ ????{ ??????"sku_id":?"", ??????"ori_price":?100000, ??????"price":?50000, ??????"icon_url":?"", ??????"quantity":?1100, ??????"product_code":?"" ????} ??], ??"attrext":?{ ????"location":?{ ??????"country":?"中國(guó)", ??????"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 }
測(cè)試的部分結(jié)果輸出如下所示。
另外,“商品維護(hù)管理”的功能測(cè)試主要就是測(cè)試商品的增加、修改、刪除操作,具體代碼如下所示。
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/0"; ????????????string?img3?=?"http://mmbiz.qpic.cn/mmbiz/4whpV1VZl28bJj62XgfHPibY3ORKicN1oJ4CcoIr4BMbfA8LqyyjzOZzqrOGz3f5KWq1QGP3fo6TOTSYD3TBQjuw/0"; ????????????//商品增刪改處理 ????????????MerchantJson?merchant?=?new?MerchantJson(); ????????????merchant.product_base?=?new?Merchant_base(); ????????????merchant.product_base.name?=?"測(cè)試產(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?=?"<div><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/013/e15f9625115203e4434fdbad670bb402-8.png" class="lazy" alt="C# develops WeChat portal and applies encapsulation and testing of WeChat store product management interface" ></div> <p>test</p> <div><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/013/e15f9625115203e4434fdbad670bb402-8.png" class="lazy" alt="C# develops WeChat portal and applies encapsulation and testing of WeChat store product management interface" ></div> <p>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?=?"中國(guó)", ????????????????????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?=?"測(cè)試產(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???"成功"?:?"失敗"); ????????????} ????????}</merchantexpress></merchantsku_list></string></merchantsku></merchantproperty></merchantdetail></string>
測(cè)試的輸出結(jié)果如下所示(一切成功)。
以上就是我對(duì)商品管理接口的API定義和實(shí)現(xiàn),以及對(duì)接口進(jìn)行測(cè)試的闡述,基本上把所有相關(guān)的內(nèi)容都貼出來(lái)了,希望大家能夠?qū)ξ⒌觊_發(fā)部分,有更深入的了解和認(rèn)識(shí)。
?更多C# develops WeChat portal and applies encapsulation and testing of WeChat store product management interface相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)