def default_response(received_msg): user = WeixinUser.objects.get(openid=str(received_msg.from_user_name)) msg = TextMsg() msg_init(msg,received_msg) if user.is_bind: msg.content = BIND_MSG else: msg.content = DEFAULT_MSG return render_to_response('response/text_to_user.xml',locals())
def default_response(received_msg): user = WeixinUser.objects.get(openid=str(received_msg.from_user_name)) msg = TextMsg() msg_init(msg, received_msg) if user.is_bind: msg.content = BIND_MSG else: msg.content = DEFAULT_MSG return render_to_response('response/text_to_user.xml', locals())
def get_score(received_msg): '''查询成绩,状态代码 r1''' msg = TextMsg() msg_init(msg,received_msg) user = WeixinUser.objects.get(openid=str(received_msg.from_user_name)) if is_bind(received_msg): query_result = score_query.main(user.profile.student_id,user.porfile.student_pwd) if query_result == 0: #错误代码0,网络错误 msg.content = u'查询失败,目测学校服务器抽风啦~~' elif query_result == 1: #错误代码2,用户名密码错误 msg.content = u'查询失败,用户名密码错误' else: msg.content = query_result return render_to_response('response/text_to_user.xml',locals()) else: return bind(received_msg)
def get_score(received_msg): '''查询成绩,状态代码 r1''' msg = TextMsg() msg_init(msg, received_msg) user = WeixinUser.objects.get(openid=str(received_msg.from_user_name)) if is_bind(received_msg): query_result = score_query.main(user.profile.student_id, user.porfile.student_pwd) if query_result == 0: #错误代码0,网络错误 msg.content = u'查询失败,目测学校服务器抽风啦~~' elif query_result == 1: #错误代码2,用户名密码错误 msg.content = u'查询失败,用户名密码错误' else: msg.content = query_result return render_to_response('response/text_to_user.xml', locals()) else: return bind(received_msg)
def home(request): if request.method == 'GET': #get方法的时候返回验证字符串 myresponse = HttpResponse() if check_signature(request.GET): myresponse.write(request.GET.get('echostr')) return myresponse else: myresponse.write('不提供直接访问!') return myresponse #处理微信发过来的post请求 if request.method == 'POST': received_msg = UserMsg(request.body) if not user_exist( received_msg ): #检测当前微信用户是否存在,如果不存在则创建一个新的用户,用户对应的django账户为微信openid,密码为openid create_new_user(received_msg) #创建一个新用户 #为这个微信用户新增一个session状态 if not SESSION_DICT.get(received_msg.from_user_name): SESSION_DICT[received_msg.from_user_name] = WeiSession() current_session = SESSION_DICT[received_msg.from_user_name] #在用户没有会话的情况下进行正常的文字消息处理 if received_msg.msg_type == 'text': #处理文字类型的消息 #如果用户输入“退出”,则将会话状态status置空,退回主菜单 if received_msg.content == u'退出': SESSION_DICT[received_msg.from_user_name].status = u'' #验证会话,如果用户有会话存在则直接调用会话处理,如果没有则进行主菜单消息处理 if current_session.status: current_session.update() return FUNCTION_DICT[STATUS_DICT[current_session.status]]( received_msg, 1) #传入一个标志’1‘来表示为子菜单状态 #主菜单模式消息处理 if received_msg.content in FUNCTION_DICT: #使用一个dict对象进行匹配以免进行大量的if else语法,导致代码很难看 return FUNCTION_DICT[received_msg.content](received_msg) else: return default_response(received_msg) #收到事件推送的处理 elif received_msg.msg_type == 'event': if received_msg.event == u'subscribe': msg = TextMsg() msg_init(msg, received_msg) msg.content = DEFAULT_MSG return render_to_response('response/text_to_user.xml', locals()) else: return HttpResponse('成功取消关注!')
def bind(received_msg, in_menu=0): '''绑定学号密码,状态代码r0''' msg = TextMsg() msg_init(msg, received_msg) if in_menu == 1: #如果已经进入绑定状态 user = WeixinUser.objects.get(openid=str(received_msg.from_user_name)) s = received_msg.content if u'@' in s: s = s.split(u'@') length = len(s[0]) if length == 9 or length == 10: user_name = s[0] passwd = s[1] query_result = score_query.main(user_name, passwd) if query_result == 0: #错误代码0,网络错误 msg.content = u'绑定验证失败,目测学校服务器抽风啦~~' elif query_result == 1: #错误代码2,用户名密码错误 msg.content = u'绑定失败,学号密码错误,请您重新输入学号密码进行绑定' else: user.profile.student_id = user_name user.porfile.student_pwd = passwd user.is_bind = True user.save() SESSION_DICT[received_msg.from_user_name].status = u'' msg.content = u"绑定成功" + '您可以使用顶级菜单的所有功能了~~获取帮助请发送“帮助”' else: msg.content = u'输入格式有误,请重新输入,格式为“学号@密码”,不要输入空格或者在密码中包含@哦^_^' #return render_to_response('response/text_to_user.xml',locals()) else: #未进入绑定状态,提示如何进行绑定 msg.content = '您需要先绑定学号密码才能查询成绩~~请输入‘学号@密码’进行绑定。范例:031140848@123456\nK123456789@pwd,不要含有空格等字符哦,发送“退出”以退出成绩查询状态!' SESSION_DICT[received_msg.from_user_name].status = u'绑定' return render_to_response('response/text_to_user.xml', locals())
def home(request): if request.method == 'GET': #get方法的时候返回验证字符串 myresponse = HttpResponse() if check_signature(request.GET): myresponse.write(request.GET.get('echostr')) return myresponse else: myresponse.write('不提供直接访问!') return myresponse #处理微信发过来的post请求 if request.method == 'POST': received_msg = UserMsg(request.body) if not user_exist(received_msg): #检测当前微信用户是否存在,如果不存在则创建一个新的用户,用户对应的django账户为微信openid,密码为openid create_new_user(received_msg) #创建一个新用户 #为这个微信用户新增一个session状态 if not SESSION_DICT.get(received_msg.from_user_name): SESSION_DICT[received_msg.from_user_name] = WeiSession() current_session = SESSION_DICT[received_msg.from_user_name] #在用户没有会话的情况下进行正常的文字消息处理 if received_msg.msg_type == 'text': #处理文字类型的消息 #如果用户输入“退出”,则将会话状态status置空,退回主菜单 if received_msg.content == u'退出': SESSION_DICT[received_msg.from_user_name].status = u'' #验证会话,如果用户有会话存在则直接调用会话处理,如果没有则进行主菜单消息处理 if current_session.status: current_session.update() return FUNCTION_DICT[STATUS_DICT[current_session.status]](received_msg,1) #传入一个标志’1‘来表示为子菜单状态 #主菜单模式消息处理 if received_msg.content in FUNCTION_DICT: #使用一个dict对象进行匹配以免进行大量的if else语法,导致代码很难看 return FUNCTION_DICT[received_msg.content](received_msg) else: return default_response(received_msg) #收到事件推送的处理 elif received_msg.msg_type == 'event': if received_msg.event == u'subscribe': msg = TextMsg() msg_init(msg,received_msg) msg.content = DEFAULT_MSG return render_to_response('response/text_to_user.xml',locals()) else : return HttpResponse('成功取消关注!')
def bind(received_msg,in_menu=0): '''绑定学号密码,状态代码r0''' msg = TextMsg() msg_init(msg,received_msg) if in_menu == 1: #如果已经进入绑定状态 user = WeixinUser.objects.get(openid=str(received_msg.from_user_name)) s = received_msg.content if u'@' in s: s = s.split(u'@') length = len(s[0]) if length == 9 or length == 10: user_name = s[0] passwd = s[1] query_result = score_query.main(user_name,passwd) if query_result == 0: #错误代码0,网络错误 msg.content = u'绑定验证失败,目测学校服务器抽风啦~~' elif query_result == 1: #错误代码2,用户名密码错误 msg.content = u'绑定失败,学号密码错误,请您重新输入学号密码进行绑定' else: user.profile.student_id = user_name user.porfile.student_pwd = passwd user.is_bind = True user.save() SESSION_DICT[received_msg.from_user_name].status = u'' msg.content = u"绑定成功"+'您可以使用顶级菜单的所有功能了~~获取帮助请发送“帮助”' else: msg.content = u'输入格式有误,请重新输入,格式为“学号@密码”,不要输入空格或者在密码中包含@哦^_^' #return render_to_response('response/text_to_user.xml',locals()) else: #未进入绑定状态,提示如何进行绑定 msg.content = '您需要先绑定学号密码才能查询成绩~~请输入‘学号@密码’进行绑定。范例:031140848@123456\nK123456789@pwd,不要含有空格等字符哦,发送“退出”以退出成绩查询状态!' SESSION_DICT[received_msg.from_user_name].status = u'绑定' return render_to_response('response/text_to_user.xml',locals())