Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to package and Test the Commodity Management Interface of Wechat Shop with C #

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to realize the encapsulation and testing of Wechat store commodity management interface by C#. It has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article. Let the editor take you to know it.

1. Definition of commodity management interface

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

This figure basically covers the related objects of Wechat stores and introduces the relationship between them.

We start with the basic commodity information management, we know that the commodity interface includes add, modify, query, delete and other interfaces, as shown below.

Commodity information is the basis of all micro-stores, so we need to be more clear and perfect in its management and operation.

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

# region Commodity Information / create Commodity / call API credential / Commodity object / AddMerchantResult AddMerchant (string accessToken, MerchantJson merchantJson) / / Delete goods / call API vouchers / goods ID / CommonResult DeleteMerchant (string accessToken, string productId); / modify goods / product_id indicates the ID of the goods to be updated. For other fields, please see add Product API. / / all the information of goods that have never been on the shelves can be modified, otherwise the three fields of commodity name (name), commodity classification (category) and commodity attribute (property) cannot be modified. / call API credential / modify product information / CommonResult UpdateMerchant (string accessToken, MerchantJson merchantJson) / / query the product information according to ID. If the MerchantJson information is returned successfully, the Id / MerchantJson GetMerchant (string accessToken, string productId) of the product will be returned if the null / call API credential / commodity is returned / get all goods in the specified status / call API credential / Product status (0-all, 1-on the shelf, 2-off the shelf) / List GetMerchantByStatus (string accessToken, int status) / goods on and off shelves / call API vouchers / signs of goods on and off shelves (0-off, 1-on shelves) / CommonResult UpdateMerchantStatus (string accessToken, string productId, int status); # endregion

Of course, Wechat's commodities also include the basic management of classification, classification attributes and classification SKU, so commodity management also needs to add this content.

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

# region merchandise classification and attributes / get all subcategories of the specified category / call API credentials / large classification ID (root node category id is 1) / List GetSub (string accessToken, int cate_id) / get all SKU / call API credentials / / goods subcategory ID / List GetSku (string accessToken, int cate_id) of the specified subcategory / get all attributes of the specified category / call the API credential / Classification ID / List GetProperty (string accessToken, int cate_id); # endregion2, implementation of the commodity management interface

The above interface defines the interface for the corresponding product.

For the implementation of the interface, we usually submit it to that URL and POST the data according to the description of the interface on the official website, and then organize it into a regular processing method to obtain the result and convert it to the corresponding object. For example, the implementation code for adding goods is shown below.

/ create goods / call API credential / Commodity object / 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.ConvertJson (url, postData);}

Instead, it returns the result, which defines an object to get the ID of the added item, and so on, as shown below.

/ / /

/ / create the returned result of product information

/ / /

Public class AddMerchantResult: ErrorJsonResult

{

/ / /

/ / Product ID

/ / /

Public string product_id {get; set;}

}

/ Wechat returned error data of Json result / public class ErrorJsonResult {/ return code / public ReturnCode errcode {get; set;} / error message / public string errmsg {get; set;}}

Through the definition of these objects, after adding goods, we will know whether the operation is successful or not. If the operation is successful, a newly created ID is returned to us for use. We can query the specific product information or modify, delete and other operations.

The operation of modifying or deleting product information is to return a record of whether it is successful or not, so we define a unified response object CommonResult. The implementation code of the interface for commodity modification and deletion is as follows.

Because the code is highly improved and sorted out, it is relatively easy to understand the code for all kinds of processing.

/ / delete goods / call API credentials / goods ID / 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);} / modify the commodity / product_id indicates the ID of the commodity to be updated. For other fields, please see add Product API. / / all the information of goods that have never been on the shelves can be modified, otherwise the three fields of commodity name (name), commodity classification (category) and commodity attribute (property) cannot be modified. / / call API credential / modify product information / 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 commodity, we need to define an entity object of the commodity, so that we can convert the obtained information into entity class information, which is easy to use and deal with.

The information of the commodity contains a lot of small defined classes, which make up the content of each part of the commodity, and the entity class information of the subject is shown below.

Once the relatively complex commodity information entities are defined, we can deal with them through objects.

The implementation code to get the product details is as follows.

/ / query the product information according to ID. If the MerchantJson information is returned successfully, the Id / public MerchantJson GetMerchant (string accessToken, string productId) {var url = string.Format ("http://www.php.cn/{0}", accessToken) of the product will be returned if the null / call API credential / product is returned. Var data = new {product_id = productId}; string postData = data.ToJson (); MerchantJson merchant = null; GetMerchantResult result = JsonHelper.ConvertJson (url, postData); if (result! = null) {merchant = result.product_info } return merchant;}

Although the physical information of the commodity is very complex, once we have defined it, it is easy for us to transform and process the results. The above code is not very difficult to understand. The main thing is to convert the data after submitting it.

Of course, we can also get the contents of the list of items in different states, as shown in the following code.

/ get all goods in the specified status / call API credential / Product status (0-all, 1-on the shelf, 2-off the shelf) / public List 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 list = new List (); GetMerchantByStatus result = JsonHelper.ConvertJson (url, postData); if (result! = null) {list = result.products_info } return list;}

When we add goods, the classification information, classification attributes, classification SKU information are also very important content, we need to specify the corresponding product classification to add to the Wechat store.

The implementation code for getting the commodity classification is as follows.

/ get all subcategories of the specified category / call API credentials / large classification ID (root node classification id is 1) / public List 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 list = new List (); GetSubResult result = JsonHelper.ConvertJson (url, postData); if (result! = null) {list = result.cate_list } return list;} 3. Test of commodity management interface

In order to verify the interface we developed, we need to add a test project to facilitate the testing of the API we wrote, and only after the test is fully successful can we officially use it in the project.

For convenience, I created a Winform project to test each interface separately.

This article mainly introduces the interface of commodity management, so the following mainly introduces the interface test code of commodity management and the corresponding results.

The interface test code for general commodity management is as follows.

Private void btnMerchant_Click (object sender, EventArgs e) {/ / Commodity Management IMerchantApi api = new MerchantApi (); / / get all commodity information Console.WriteLine ("get all commodity information"); List list = api.GetMerchantByStatus (token, 0) Foreach (MerchantJson json in list) {Console.WriteLine (json.ToJson ()); Console.WriteLine ();} / / Update commodity status Console.WriteLine ("update commodity status") Foreach (MerchantJson json in list) {CommonResult result = api.UpdateMerchantStatus (token, json.product_id, 1); Console.WriteLine ("Commodity ID: {0}, Trade name: {1}, Operation: {2}", json.product_id, json.product_base.name, result.Success? "success": "failure");} Thread.Sleep (1000); / / obtaining commodity information according to commodity ID Console.WriteLine ("obtaining commodity information according to commodity ID"); foreach (MerchantJson json in list) {MerchantJson getJson = api.GetMerchant (token, json.product_id) If (json! = null) {Console.WriteLine ("commodity ID: {0}, trade name: {1}", getJson.product_id, getJson.product_base.name);}

After the test, the results are as follows (that is, return the product information in my micro-store), everything is normal.

The returned product Json data is as follows:

{"product_id": "pSiLnt6FYDuFtrRRPMlkdKbye-rE", "product_base": {"category_id": ["537103312"], "property": [{"id": "Type", "vid": "Software Product Design"], "name": "Code Generation tool Database2Sharp", "sku_info": [] "main_img": "http://mmbiz.qpic.cn/mmbiz/mLqH9gr11Gyb2sgiaelcsxYtQENGePp0Rb3AZKbjkicnKTUNBrEdo7Dyic97ar46SoAfKRB5x2R94bDUdNpgqiaZzA/0"," img ": [" http://mmbiz.qpic.cn/mmbiz/mLqH9gr11Gyb2sgiaelcsxYtQENGePp0RiaheJmVXm7tbvTYUQV7OF3DgfGiaQVMh4WbeEcGDOQQiajQXGKK9tfoeA/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 ":" China "," province ":" Guangdong "," city ":" Guangzhou "," 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}

Some of the results of the test are output as follows.

In addition, the functional test of "Commodity maintenance Management" is mainly to test the addition, modification and deletion of goods, as shown in the following code.

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 = "Test Product"; merchant.product_base.category_id.Add ("537074298"); merchant.product_base.img = new List () {img1, img2, img3} Merchant.product_base.main_img = img1 Merchant.product_base.detail.AddRange (new List () {new MerchantDetail () {text = "test first"}, new MerchantDetail () {img = img2}) New MerchantDetail () {text = "test again"}}) Merchant.product_base.property.AddRange (new List () {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 () {new MerchantSku {id= "1075741873", vid = new List () {"1079742386", "1079742363"}) Merchant.product_base.buy_limit = 10; / / merchant.product_base.detail_html = "

\"\"

Test

\"\"

Test again

" Merchant.sku_list.AddRange (new List () {new MerchantSku_list () {sku_id= "1075741873 3VR 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= "1075741873purl 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 = "China", province = "Guangdong", city = "Guangzhou", address = "T.I.T Creative Park"} IsPostFree = 0, isHasReceipt = 1, isUnderGuaranty = 0, isSupportReplace = 0} Merchant.delivery_info = new MerchantDelivery () {delivery_type = 0, template_id = 0, express = new List () {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 ("add goods: {0}", result.product_id); if (! string.IsNullOrEmpty (result.product_id)) {/ / update goods merchant.product_id = result.product_id Merchant.product_base.name = "test product 22"; CommonResult updateResult = api.UpdateMerchant (token, merchant); Console.WriteLine ("updated product: {0}", updateResult.Success? "success": "failure"); CommonResult deleteResult = api.DeleteMerchant (token, merchant.product_id); Console.WriteLine ("Delete goods: {0}", deleteResult.Success? "success": "failure");}

The output of the test is shown below (everything is successful).

Thank you for reading this article carefully. I hope the article "how to implement the encapsulation and testing of Wechat store commodity management interface" shared by the editor will be helpful to everyone. At the same time, I also hope you can support me and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report