1. ?? ?? ????? ??
?? ???? ?? ???? ?? ??? ??? ?? ??????.
? ???? ????? WeChat ???? ?? ??? ??? ?? ?? ??? ?????.
?? ??????? ??? ?? ??, ??, ??, ?? ?? ?????? ???? ??? ?? ?? ????.
?? ?? ??? ??? ?? ???? ??? ?? ???? ???? ???? ???.
??? ??? ??? ???? WeChat ??? ?????? ??? ?? ??? ? ????.
#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)參考增加商品接口。 ????????///?從未上架的商品所有信息均可修改,否則商品的名稱(chēng)(name)、商品分類(lèi)(category)、商品屬性(property)這三個(gè)字段不可修改。 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>修改商品的信息 ????????///?<returns></returns> ????????CommonResult?UpdateMerchant(string?accessToken,?MerchantJson?merchantJson); ????????///?<summary> ????????///?根據(jù)ID查詢(xún)商品信息,如果成功返回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>
?? WeChat ???? ????, ???? ??, ???? SKU? ?? ?? ??? ???? ???? ?? ???? ? ???? ???? ???
??? ????? ??? ??? ????. ?? ?????? ?? ?? ??(?? ?? ??), SKU ??, ?? ?? ?? ??? ?? ?? ? ????.
#region?商品分類(lèi)及屬性 ????????///?<summary> ????????///?獲取指定分類(lèi)的所有子分類(lèi) ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>大分類(lèi)ID(根節(jié)點(diǎn)分類(lèi)id為1) ????????///?<returns></returns> ????????List<subcategory>?GetSub(string?accessToken,?int?cate_id); ????????///?<summary> ????????///?獲取指定子分類(lèi)的所有SKU ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品子分類(lèi)ID ????????///?<returns></returns> ????????List<subcategorysku>?GetSku(string?accessToken,?int?cate_id); ????????///?<summary> ????????///?獲取指定分類(lèi)的所有屬性 ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>分類(lèi)ID ????????///?<returns></returns> ????????List<subcategoryproperty>?GetProperty(string?accessToken,?int?cate_id);? ????????#endregion</subcategoryproperty></subcategorysku></subcategory>
2. ?? ?? ????? ??
? ?????? ?? ??? ?????? ?????.
????? ??? ?? ????? ?? ????? ????? ??? ?? URL? ???? ???? POST? ? ?? ???? ?? ???? ???? ??? ?? ?????. ?? ?? ?? ??? ???? ?? ??? ??? ????.
///?<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>
??? ???? ???? ??? ?? ??? ??? ?? ???? ID? ???? ??? ???? ???.
////// ???? ?? ?? ??
///
public class AddMerchantResult: ErrorJsonResult
{
?????????????????????????????????????????????????>
rreee
??? ??? ??? ?? ??? ??? ? ??? ????? ??? ? ? ????. ??? ????? ???? ?? ??? ID? ???? ??? ? ???, ?? ?? ??? ????? ?? ? ??? ?? ??? ??? ? ????. ?? ?? ???? ??? ?? ??? ???? ??? ??? ?? ??? CommonResult? ?????. ?? ?? ? ??? ?? ????? ?? ??? ??? ????. ??? ?? ???? ???? ??? ??? ?? ??? ??? ?? ??? ? ????.
///?<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;?} ????}??? ????? ?? ???? ??? ??? ???? ??? ? ??? ??? ??? ??? ???? ???. ????? ?? ???? ??? ? ????. ?? ???? ??? ? ??? ???? ???? ???? ??? ???? ?? ???? ????.
??? ??? ???? ??? ??? ? ??? ?? ??? ? ????.
?? ?? ??? ?? ?? ?? ??? ??? ????.
///?<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)參考增加商品接口。 ????????///?從未上架的商品所有信息均可修改,否則商品的名稱(chēng)(name)、商品分類(lèi)(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); ????????}??? ??? ??? ?? ????? ?? ???? ?? ?? ??? ???? ???? ?? ????. ????. ?? ??? ?? ???? ??? ? ???? ????. ?? ?? ??? ?? ??? ??? ?? ?? ???? ??? ?? ????.
????????///?<summary> ????????///?根據(jù)ID查詢(xún)商品信息,如果成功返回MerchantJson信息,否則返回null????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>商品的Id ????????///?<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; ????????}</getmerchantresult>??? ??? ? ???? ??, ???? ??, ???? SKU ??? ?? ?????. ?? ?? WeChat ???? ??? ? ????.
獲取商品分類(lèi)的操作實(shí)現(xiàn)代碼如下所示。
///?<summary> ????????///?獲取指定分類(lèi)的所有子分類(lèi) ????????///?</summary> ????????///?<param>調(diào)用接口憑證 ????????///?<param>大分類(lèi)ID(根節(jié)點(diǎn)分類(lèi)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)證我們開(kāi)發(fā)的接口,我們需要增加一個(gè)測(cè)試項(xiàng)目,方便對(duì)我們編寫(xiě)的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},商品名稱(chēng):{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},商品名稱(chēng):{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":?"類(lèi)型", ????????"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#? WeChat ??? ???? WeChat ?? ?? ?? ?????? ??? ? ???? ?????." ></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#? WeChat ??? ???? WeChat ?? ?? ?? ?????? ??? ? ???? ?????." ></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ū)ξ⒌觊_(kāi)發(fā)部分,有更深入的了解和認(rèn)識(shí)。
?更多C#? WeChat ??? ???? WeChat ?? ?? ?? ?????? ??? ? ???? ?????.相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)