322 lines
14 KiB
Python
322 lines
14 KiB
Python
# !/usr/bin/python
|
||
# -*- coding: utf-8 -*-
|
||
# @Author:: Arthur Wu
|
||
# @Date:: 2024/11/14-14:35
|
||
# @Description::
|
||
import requests,json, logging
|
||
from commons.SignatureYM import SignatureYM2
|
||
|
||
|
||
class YMClientApi(object):
|
||
def __init__(self):
|
||
self.Domain = "https://api.test.yimaogo.com/cart"
|
||
self.headerss = SignatureYM2(Mac="b8:2d:28:04:c7:5c")._headers()
|
||
|
||
''' 1- without sessionid '''
|
||
def get_ads_list(self):
|
||
logging.info("========== [获取广告列表] get_ads_list ==========")
|
||
''' method 1 '''
|
||
payload = {}
|
||
url = self.Domain + "/v1/ads/list?areaCode&userId&barcode&adsAreaIds=1,2,3,4,5,6"
|
||
logging.info(f"---url: {url}---")
|
||
logging.info(f"---headers: {self.headerss}---")
|
||
response = requests.request("GET", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def query_ad_detail(self, ADsId: str):
|
||
logging.info("========== [查询广告详情] query_ad_detail ==========")
|
||
payload = {}
|
||
url = self.Domain + "/v1/ads/{adsId}".replace("{adsId}", str(ADsId))
|
||
response = requests.request("GET", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
''' 2- sessionid must be started first '''
|
||
def session_start(self):
|
||
logging.info("========== [前置] session_start ==========")
|
||
url = self.Domain+"/v1/session/start"
|
||
payload = ""
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
|
||
def session_end(self):
|
||
logging.info("========== [后置] session_end ==========")
|
||
url = self.Domain+"/v1/session/end?reason"
|
||
payload = {}
|
||
response = requests.request("PUT", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
|
||
def login_app_v2(self, Payload):
|
||
'''
|
||
1-匿名登录
|
||
{"action": 0, "isAnon": True}
|
||
2-会员登录
|
||
{"action":1,"code":"18052753212","isAnon":False}
|
||
:param Payload:
|
||
:return:
|
||
'''
|
||
logging.info("========== [登录] login_app_v2 ==========")
|
||
payload = json.dumps(Payload)
|
||
url = self.Domain+"/v2/login"
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def get_login_type(self):
|
||
logging.info("========== [获取登录方式] get_login_type ==========")
|
||
payload = {}
|
||
url = self.Domain+"/v2/login/type?action=1"
|
||
response = requests.request("GET", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
|
||
def get_goods_info(self, InputCode):
|
||
logging.info("========== [获取商品信息] get_goods_info ==========")
|
||
payload = {}
|
||
url = self.Domain+"/v2/shopping/{inputCode}".replace("{inputCode}", str(InputCode))
|
||
response = requests.request("GET", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def add_cart_goods(self, GoodsInfoData, AddPurchaseQuantity, LoginData):
|
||
logging.info("========== [加入购物车] add_retire_purchase ==========")
|
||
payload = json.dumps({
|
||
"addGoods":[{
|
||
"inputCode": GoodsInfoData["data"]["inputCode"],
|
||
"isNormalAddPurchase": True,
|
||
"qty": int(AddPurchaseQuantity), # AddPurchaseQuantity,GoodsInfoData["data"]["qty"]
|
||
"weight": int(GoodsInfoData["data"]["weight"]),
|
||
}],
|
||
"autoSelectCoupon": True,
|
||
"coupons": [],
|
||
"deleteGoods": [],
|
||
"existGoods": [],
|
||
"orderNo": LoginData["data"][0]["orderNo"]
|
||
})
|
||
url = self.Domain+"/v2/shopping/add/retire/purchase"
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def delete_cart_goods(self, GoodsInfoData, LoginData, AddGoodsResponse):
|
||
''' 退购
|
||
|
||
:param GoodsInfoData:
|
||
:return:
|
||
'''
|
||
logging.info("========== [删除购物车商品] delete_cart_goods ==========")
|
||
GoodsInfoData = ''
|
||
LoginData = ''
|
||
AddGoodsResponse = ''
|
||
|
||
existGoods = []
|
||
for goodsinfo in AddGoodsResponse["data"]["orderItemList"]:
|
||
good_dict = {}
|
||
good_dict["inputCode"] = goodsinfo["inputCode"]
|
||
good_dict["isNormalAddPurchase"] = True
|
||
good_dict["qty"] = goodsinfo["qty"]
|
||
good_dict["uuid"] = goodsinfo["uuid"]
|
||
# good_dict["weight"] = goodsinfo["weight"]
|
||
good_dict["weight"] = GoodsInfoData["data"]["weight"]
|
||
existGoods.append(good_dict)
|
||
|
||
payload = json.dumps({
|
||
"addGoods": [],
|
||
"autoSelectCoupon": True,
|
||
"coupons": [],
|
||
"deleteGoods": [{
|
||
"inputCode": "6924743915848",
|
||
"isNormalAddPurchase": True,
|
||
"qty": 1,
|
||
"uuid": ["23B2363A224E4933942992F84B937D7D"],
|
||
"weight": 0
|
||
}],
|
||
"existGoods": [{
|
||
"inputCode": "6924743915848",
|
||
"isNormalAddPurchase": True,
|
||
"qty": 1,
|
||
"uuid": ["23B2363A224E4933942992F84B937D7D"],
|
||
"weight": 155
|
||
}],
|
||
"orderNo": "1858386778027515904"
|
||
})
|
||
|
||
add_2_result = {
|
||
"code": 0,
|
||
"msg": "成功",
|
||
"data": {
|
||
"remainAmount": "13.8",
|
||
"totalDisc": "0.0",
|
||
"orderItemList": [{
|
||
"uuid": ["2BEDF9C6DD954E1FB3DCD9A9F3C57D29"], # 下传
|
||
"qty": 1, # 下传
|
||
"marketPrice": "5",
|
||
"salePrice": "5.00",
|
||
"totalSalePrice": "5",
|
||
"minWeight": 0,
|
||
"weight": 0,
|
||
"maxWeight": 0,
|
||
"goodsPic": "https://ieemoo-storage.obs.cn-east-3.myhuaweicloud.com/lhpic/6925303796426.jpg",
|
||
"barcode": "6925303796426",
|
||
"inputCode": "6925303796426",
|
||
"goodsName": "统一茄皇蕃茄牛肉面",
|
||
"goodsPromotionTags": None,
|
||
"measureProperty": 0,
|
||
"shoppingBagFlag": False,
|
||
"activityId": None,
|
||
"activityDescription": None,
|
||
"totalPromotionPrice": 0,
|
||
"categoryCode": None,
|
||
"isOneBarcodeMore": False,
|
||
"pkgFlag": False
|
||
},
|
||
{
|
||
"uuid": ["F9BBA7C9AF7944FF91C717E3D18BD682"],
|
||
"qty": 1,
|
||
"marketPrice": "8.8",
|
||
"salePrice": "8.80",
|
||
"totalSalePrice": "8.8",
|
||
"minWeight": 0,
|
||
"weight": 0,
|
||
"maxWeight": 0,
|
||
"goodsPic": "https://ieemoo-storage.obs.cn-east-3.myhuaweicloud.com/lhpic/6924743915848.jpg",
|
||
"barcode": "6924743915848",
|
||
"inputCode": "6924743915848",
|
||
"goodsName": "乐事无限翡翠黄瓜味薯片",
|
||
"goodsPromotionTags": None,
|
||
"measureProperty": 0,
|
||
"shoppingBagFlag": False,
|
||
"activityId": None,
|
||
"activityDescription": None,
|
||
"totalPromotionPrice": 0,
|
||
"categoryCode": None,
|
||
"isOneBarcodeMore": False,
|
||
"pkgFlag": False
|
||
}],
|
||
"bagInfoList": None,
|
||
"couponMap": None,
|
||
"activityGoodsList": None
|
||
}
|
||
}
|
||
|
||
del_201 = {
|
||
"addGoods": [],
|
||
"autoSelectCoupon": True,
|
||
"coupons": [],
|
||
"deleteGoods": [{
|
||
"inputCode": "6924743915848",
|
||
"isNormalAddPurchase": True,
|
||
"qty": 1,
|
||
"uuid": ["F9BBA7C9AF7944FF91C717E3D18BD682"],
|
||
"weight": 0
|
||
}],
|
||
"existGoods": [{
|
||
"inputCode": "6925303796426", # 取自 add_2_result["data"]["orderItemList"][0]["inputCode"]
|
||
"isNormalAddPurchase": True,
|
||
"qty": 1, # 取自 add_2_result["data"]["orderItemList"][0]["qty"]
|
||
"uuid": ["2BEDF9C6DD954E1FB3DCD9A9F3C57D29"], # 取自 add_2_result["data"]["orderItemList"][1]["uuid"]
|
||
"weight": 165 # 取自 GoodsInfoData["data"]["weight"]
|
||
}, {
|
||
"inputCode": "6924743915848",
|
||
"isNormalAddPurchase": True,
|
||
"qty": 1,
|
||
"uuid": ["F9BBA7C9AF7944FF91C717E3D18BD682"],
|
||
"weight": 160 # GoodsInfoData["data"]["weight"]
|
||
}],
|
||
"orderNo": "1858386778027515904" # 取自 LoginData["data"][0]["orderNo"]
|
||
}
|
||
|
||
|
||
def get_coupon_list(self):
|
||
logging.info("========== [获取优惠券列表] get_coupon_list ==========")
|
||
payload = json.dumps({
|
||
"code":"4",
|
||
"value":""
|
||
})
|
||
url = self.Domain+"/v1/coupon/list"
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def receive_coupon(self, CouponListData):
|
||
logging.info("========== [领取优惠券] receive_coupon ==========")
|
||
CouponList = []
|
||
for i in CouponListData["data"]:
|
||
CouponList.append(i["name"])
|
||
payload = json.dumps({"activityIds": CouponList})
|
||
url = self.Domain+"/v1/coupon/receive"
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def query_coupon_list_user(self):
|
||
logging.info("========== [查询优惠券列表] query_coupon_list_user ==========")
|
||
payload = {}
|
||
url = self.Domain+"/v1/coupon/list/user?state=2&page=0&pageSize=10"
|
||
response = requests.request("GET", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def get_cart_goods_info(self, GoodsDataList):
|
||
logging.info("========== [获取购物车商品信息] get_cart_goods_info ==========")
|
||
goods_param_lList = []
|
||
for goods_data in GoodsDataList:
|
||
goods_dict = {}
|
||
goods_dict["inputCode"] = goods_data["data"]["inputCode"]
|
||
goods_dict["qty"] = goods_data["data"]["qty"]
|
||
goods_dict["weight"] = int(goods_data["data"]["weight"])
|
||
goods_param_lList.append(goods_dict)
|
||
payload = json.dumps({"goodsParamList": goods_param_lList})
|
||
url = self.Domain+"/v2/shopping/cart/goods/info"
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
def request_order_settlement(self, LoginData):
|
||
logging.info("========== [请求订单结算] request_order_settlement ==========")
|
||
payload = json.dumps({
|
||
"orderNo": LoginData["data"][0]["orderNo"],
|
||
"setCouponFlag": False,
|
||
"setPayMethodFlag": False
|
||
})
|
||
url = self.Domain+"/v2/shopping/order/settle"
|
||
response = requests.request("POST", url, headers=self.headerss, data=payload)
|
||
logging.info(f"-----------接口返回状态码:{response.status_code}")
|
||
logging.info(f"-----------接口返回数据:{response.json()}\n\n")
|
||
return response.json()
|
||
|
||
|
||
|
||
if __name__ == '__main__':
|
||
ymc = YMClientApi()
|
||
''' 01- '''
|
||
ymc.session_start() # step1:session开始
|
||
Payload01 = {"action": 0, "isAnon": True}
|
||
LoginData = ymc.login_app_v2(Payload01) # step2:匿名登录
|
||
|
||
ymc.get_login_type() # step3:获取登录方式
|
||
Payload02 = {"action": 1, "code": "18052753212", "isAnon": False}
|
||
ymc.login_app_v2(Payload02) # step4:切换会员登录
|
||
GoodsInfoData = ymc.get_goods_info(InputCode=6924882486100) # step5:获取商品信息 ------ 6924882486100
|
||
CouponListData = ymc.get_coupon_list() # step6:获取优惠券列表
|
||
ymc.receive_coupon(CouponListData) # step7:领取优惠券
|
||
ymc.query_coupon_list_user() # step8:查询用户持有的优惠券列表
|
||
ymc.add_cart_goods(GoodsInfoData, 1, LoginData) # step9:加购商品 ------ 6924882486100
|
||
ymc.get_cart_goods_info(GoodsInfoData) # step10:获取购物车商品信息
|
||
ymc.request_order_settlement(LoginData) # step11:请求订单结算
|
||
|
||
ymc.session_end()
|
||
|