示例#1
0
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"])
示例#2
0
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
示例#3
0
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
示例#4
0
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
示例#5
0
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
示例#6
0
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
示例#7
0
def init_response_result():
    result = {}
    result["ret"] = Status.OK
    result["info"] = Status().getReason(result["ret"])
    result["data"] = {}
    return result