def craft_boolean_response(boolean: bool, error_code=StopCodes.ServerError.InternalServerError, **kwargs): ret_data = {"state": boolean, "status": 200 if boolean else error_code} for arg in kwargs: ret_data[arg] = kwargs[arg] ret = make_response(ret_data, 200 if boolean else error_code) ret.headers["Content-Type"] = "application/json" return ret
def craft_response(data, status_code, **kwargs): ret_data = {"data": data, "status": status_code} for arg in kwargs: ret_data[arg] = kwargs[arg] ret = make_response(ret_data, status_code) ret.headers["Content-Type"] = "application/json" return ret
def pay_accept(): # 导入: if request.method == 'POST': xml_recv = ET.fromstring(request.data) return_code = xml_recv.find("return_code").text # openid=xml_recv.find("openid").text # total_fee=xml_recv.find("total_fee").text # out_trade_no = xml_recv.find("out_trade_no").text if return_code == 'SUCCESS': reply = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>" response = make_response(reply) response.content_type = 'application/xml' res = '订单信息支付完成,调用更新数据的方法' if res == 'success': return response else: return 'error' else: return 'whoareyou'
def craft_complex_response(data: list, status_code, **kwargs): ret_data = {"status": status_code} for dat in data: ret_data[dat] = data[dat] for arg in kwargs: ret_data[arg] = kwargs[arg] ret = make_response(ret_data, status_code) ret.headers["Content-Type"] = "application/json" return ret
def chatcore(): # 导入: if request.method == 'GET': #为get请求是时是配置校验 signature = request.args.get('signature') timestamp = request.args.get('timestamp') nonce = request.args.get('nonce') echostr = request.args.get('echostr') token = '公众号配置的token' #公众号配置的token check_dict = [token, timestamp, nonce] check_dict.sort() check_str = str_encrypt(check_dict[0].encode('utf-8') + check_dict[1].encode('utf-8') + check_dict[2].encode('utf-8')) if check_str == signature: return echostr else: return 'error' else: #为POST请求时候是数据传递,微信公众号采用的是XML数据格式 # Get the infomations from the recv_xml. try: xml_recv = ET.fromstring(request.data) Msg_Type = xml_recv.find("MsgType").text if Msg_Type == 'image': #当接受到图片的时候,进行对应回复 ToUserName = xml_recv.find("ToUserName").text FromUserName = xml_recv.find("FromUserName").text PicUrl = xml_recv.find("PicUrl").text res_content = '我收到了你发的照片' if res_content == 'error': return 'success' else: reply = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>" response = make_response( reply % (FromUserName, ToUserName, str(int( time.time())), res_content)) response.content_type = 'application/xml' return response elif Msg_Type == 'text': #这里接受到文字信息 ToUserName = xml_recv.find("ToUserName").text FromUserName = xml_recv.find("FromUserName").text Content = xml_recv.find("Content").text res_content = '我收到了你发的文字' if res_content == 'success': return 'success' else: reply = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>" response = make_response( reply % (FromUserName, ToUserName, str(int( time.time())), res_content)) response.content_type = 'application/xml' return response elif Msg_Type == 'event': #这里是事件监听 Event = xml_recv.find("Event").text if Event == 'subscribe': #用户关注公众号 reply = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image><FuncFlag>0</FuncFlag></xml>" ToUserName = xml_recv.find("ToUserName").text #openid FromUserName = xml_recv.find("FromUserName").text res_image_id = '素材id' response = make_response( reply % (FromUserName, ToUserName, str(int( time.time())), res_image_id)) response.content_type = 'application/xml' #用户注册事件在这里补充 return response if Event == 'unsubscribe': #用户取关 # 用户删除事件 return 'success' EventKey = xml_recv.find("EventKey").text if Event == 'CLICK' and EventKey == 'shop': #点击菜单栏时间 reply = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>" ToUserName = xml_recv.find("ToUserName").text FromUserName = xml_recv.find("FromUserName").text res_content = '小店装修中。' response = make_response( reply % (FromUserName, ToUserName, str(int( time.time())), res_content)) response.content_type = 'application/xml' return response return 'success' else: return 'success' except Exception as err: print('服务器异常' + str(err)) return 'success'
def empty_success(): return make_response({}, StopCodes.Success.NoContent)