async def delete_menu(self): token = get_cache(ACCESS) url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={}".format( token) try: result = await requests.get(url) if 300 > result.status_code >= 200: content = result.json() if content.get('errcode') == 0: return { 'status': True, 'msg': '删除菜单成功', 'code': StatusCode.success.value } else: return { 'status': False, 'msg': f'删除菜单失败 {content.get("errmsg")}', 'code': StatusCode.error.value } else: return { 'status': False, 'msg': '删除菜单失败', 'code': StatusCode.third_api_error.value } except Exception as e: logger.error(f"Create Menu Request Error : {e}") return { 'status': False, 'msg': '删除菜单失败, 请求异常', 'code': StatusCode.third_api_error.value }
async def get_menus(self): token = get_cache(ACCESS) url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={}".format( token) try: result = await requests.get(url) if 300 > result.status_code >= 200: content = result.json() return { 'status': True, 'msg': '获取菜单成功', 'code': StatusCode.success.value, "data": content } else: return { 'status': False, 'msg': '获取菜单失败', 'code': StatusCode.third_api_error.value } except Exception as e: logger.error(f"Create Menu Request Error : {e}") return { 'status': False, 'msg': '获取菜单失败,请求异常', 'code': StatusCode.third_api_error.value }
async def gen_signature(self, **payload): state, msg = validate(['url'], payload) if not state: return { 'status': False, 'msg': '数据入参验证失败', "code": StatusCode.params_error.value } else: sort_data = dict(noncestr=str(uuid4()), ticket=get_cache(TICKET), timestamp=str(int(time.time())), url=payload.get('url').replace('#', '')) sorted(sort_data.keys()) str1 = "" for k, v in sort_data.items(): str1 += f"{k}={v}&" if str1.endswith('&'): str1 = str1[:-1] sha1 = hashlib.sha1(str1.encode()) hashcode = sha1.hexdigest() content = { "timestamp": sort_data.get('timestamp'), "nonceStr": sort_data.get('noncestr'), "signature": hashcode, "appId": wx_app_id } return { "status": True, 'msg': "业务处理成功", "code": StatusCode.success.value, "data": content }
async def add_menu(self, **kwargs): token = get_cache(ACCESS) url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={}".format( token) body = json.dumps(kwargs, ensure_ascii=False).encode('utf-8') try: result = await requests.post(url, data=body) if 300 > result.status_code >= 200: content = result.json() if content.get('errcode') == 0: return { 'status': True, 'msg': '创建菜单成功', 'code': StatusCode.success.value } else: return { 'status': False, 'msg': f'创建菜单成失败 {content.get("errmsg")}', 'code': StatusCode.error.value } else: return { 'status': False, 'msg': '创建菜单成失败', 'code': StatusCode.third_api_error.value } except Exception as e: logger.error(f"Create Menu Request Error : {e}") return { 'status': False, 'msg': '创建菜单成失败,请求异常', 'code': StatusCode.third_api_error.value }
async def gen_signature(self, **payload): state, msg = validate(['url'], payload) if not state: return { 'status': False, 'msg': '数据入参验证失败', "code": StatusCode.params_error.value } else: sort_data = dict(nonceStr=str(uuid4()), jsapi_ticket=get_cache(TICKET), timestamp=str(int(time.time())), url=payload.get('url').replace('#', '')) str1 = '&'.join([ '%s=%s' % (key.lower(), sort_data[key]) for key in sorted(sort_data) ]) hashcode = hashlib.sha1(str1.encode('utf-8')).hexdigest() content = { "timestamp": sort_data.get('timestamp'), "nonceStr": sort_data.get('nonceStr'), "signature": hashcode, "appId": wx_app_id } return { "status": True, 'msg': "业务处理成功", "code": StatusCode.success.value, "data": content }