def api_router(request): result = {} try: if request.method == 'POST': func_name = request.POST["action"] params = json.loads( request.POST["data"]) if request.POST.has_key("data") else {} access_token = params.get('access_token', None) if access_token: user = api_tools.get_user_with_access_token(access_token) if not user: result['ret'] = Status.ACCESSTOKENERROR result['info'] = Status().getReason(result['ret']) result['data'] = {} return HttpResponse(json.dumps(result)) else: user.last_active_time = datetime.datetime.now() user.save() result = getattr(manage_api, func_name, None)(request, params) elif request.method == 'GET': func_name = request.GET["action"] if func_name != "get_token": result = init_response_result() result["ret"] = Status.REQUESTMETHODERROR result["info"] = Status().getReason(result["ret"]) else: params = {} result = getattr(manage_api, func_name, None)(request, params) else: return HttpResponse(json.dumps({})) except Exception, e: print(e) result = init_response_result() result["ret"] = Status.REQUESTPARAMSERROR result["info"] = Status().getReason(result["ret"])
def check_phone(phone): result = init_response_result() # 检查手机号是否存在 if not db_tools.check_phone(phone): result['ret'] = Status.PHONEEXISTS result['info'] = Status().getReason(result['ret']) return result
def dowith_error(error, result): print(error) if isinstance(error, ZGError): result['ret'] = error.ret result['info'] = error.info else: result['ret'] = Status.REQUESTPARAMSERROR result['info'] = Status().getReason(result['ret']) return result
def login(phone, pwd): pwd = tools.md5_pwd(pwd) user_info = db_tools.check_login(phone, pwd) result = init_response_result() if user_info: result['data'] = user_info else: result['ret'] = Status.PHONEORPWDERROR result['info'] = Status().getReason(result['ret']) return result
def register(nick, phone, pwd, photo, sex, tag_list, university): result = init_response_result() # 检查手机号是否存在 if not db_tools.check_phone(phone): result['ret'] = Status.PHONEEXISTS result['info'] = Status().getReason(result['ret']) return result pwd = tools.md5_pwd(pwd) result['data'] = db_tools.create_user(nick, phone, pwd, photo, sex, tag_list, university) return result
def switch_active_university(access_token, university): result = init_response_result() if not db_tools.switch_active_university(access_token, university): result['ret'] = Status.UNKNOWNERR result['info'] = Status().getReason(result['ret']) return result
def init_response_result(): result = {} result["ret"] = Status.OK result["info"] = Status().getReason(result["ret"]) result["data"] = {} return result