Exemplo n.º 1
0
def verifyTelephone(request):
    response = {}
    response['data'] = {}
    response['errorMsg'] = ""
    logger = logging.getLogger('Pack.app')
    logger.info('-----------------------verifyTelephone')
    _telephone = request.REQUEST.get('telephone')
    _verify_code = request.REQUEST.get('verifyCode')
    _clientID = request.REQUEST.get('clientID')
    _deviceToken = request.REQUEST.get('deviceToken')
    _deviceInfo = request.REQUEST.get('deviceInfo')
    logger.info(str(_telephone))
    cache.set(str(_telephone),str(_verify_code),5*60)
    if _telephone == None or _telephone == '':
        response['code'] = -1
        response['errorMsg'] = '请输入手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if  _verify_code == None or _verify_code == '':
        response['code'] = -1
        response['errorMsg'] = '请输入验证码'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _deviceInfo == None or _deviceInfo == '':
        response['code'] = -1
        response['errorMsg'] = u'请输入设备信息'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    if len(_telephone) != 11:
        response['code'] = -1
        response['errorMsg'] = '请输入11位手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _telephone.isdigit() == False:
        response['code'] = -1
        response['errorMsg'] = '请输入有效手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if len(_verify_code) != 4:
        response['code'] = -1
        response['errorMsg'] = '请输入4位验证码'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _clientID == None:
        _clientID = ''
    else:
        _clientID = str(_clientID)
    if _deviceToken == None:
        _deviceToken = ''
    else:
        _deviceToken = str(_deviceToken)
    _deviceInfo = str(_deviceInfo)

    MOBILE_prog = re.compile(r"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$")
    CM_prog = re.compile(r"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)")
    CU_prog = re.compile(r"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)")
    CT_prog = re.compile(r"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)")

    telephone_match_MOBILE = MOBILE_prog.match(str(_telephone))
    telephone_match_CM = CM_prog.match(str(_telephone))
    telephone_match_CU = CU_prog.match(str(_telephone))
    telephone_match_CT = CT_prog.match(str(_telephone))

    if not telephone_match_MOBILE and not telephone_match_CM and not telephone_match_CT and not telephone_match_CU:
        response['code'] =  -1
        response['errorMsg'] = '请输入有效的手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    saved_verify_code = cache.get(str(_telephone))
    print('saved_verify_code:'+str(saved_verify_code))
    if not saved_verify_code:
        response['code'] = -1
        response['errorMsg'] = '请重新发送验证码'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if saved_verify_code != _verify_code:
        response['code'] = -1
        response['errorMsg'] = '验证码错误,请重新输入'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    query_telephone = User.objects.filter(telephone = str(_telephone))
    if not query_telephone.exists():
        _name = '****'+_telephone[7:11]
        currentTime = time.time()
        currentTime = int(currentTime)
        currentTime = str(currentTime)
        new_user = User(telephone=str(_telephone),name = _name,lastLoginTime = currentTime,clientID =
        _clientID,deviceToken = _deviceToken,deviceInfo = _deviceInfo)

        new_user.save()
        request.session['lastLoginTime'] = currentTime
        response_data = {'userId':new_user.id,'telephone':new_user.telephone,'name':new_user.name,'headImage':new_user.headImage}
        response['code'] = 0
        request.session['userId'] = new_user.id
        response['data'] = response_data
        print('get the new user with id :'+str(new_user.id)+'withtelephone:'+new_user.telephone+'with choice:False')
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    query_telephone.update(clientID=_clientID,deviceToken = _deviceToken,deviceInfo = _deviceInfo)
    currentTime = time.time()*1000
    currentTime = int(currentTime)
    currentTime = str(currentTime)
    logger.info('--------currentTime-----')
    logger.info(currentTime)
    query_telephone.update(lastLoginTime = currentTime)
    request.session['lastLoginTime'] = currentTime
    request.session['userId'] = query_telephone[0].id
    response_data = {'userId':query_telephone[0].id,'telephone':query_telephone[0].telephone,'name':query_telephone[0].name,'headImage':query_telephone[0].headImage}
    response['code'] = 0
    response['data'] = response_data
    return HttpResponse(json.dumps(response),content_type="application/json")
Exemplo n.º 2
0
def verifyTelephone(request):
    response = {}
    response['data'] = {}
    response['errorMsg'] = ""
    logger = logging.getLogger('Pack.app')
    logger.info('-----------------------verifyTelephone')
    _telephone = request.REQUEST.get('telephone')
    _verify_code = request.REQUEST.get('verifyCode')
    _clientID = request.REQUEST.get('clientID')
    _deviceToken = request.REQUEST.get('deviceToken')
    _deviceInfo = request.REQUEST.get('deviceInfo')
    logger.info(str(_telephone))
    cache.set(str(_telephone),str(_verify_code),1800)
    if _telephone == None or _telephone == '':
        response['code'] = -1
        response['errorMsg'] = '请输入手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if  _verify_code == None or _verify_code == '':
        response['code'] = -1
        response['errorMsg'] = '请输入验证码'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _deviceInfo == None or _deviceInfo == '':
        response['code'] = -1
        response['errorMsg'] = u'请输入设备信息'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    if len(_telephone) != 11:
        response['code'] = -1
        response['errorMsg'] = '请输入11位手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _telephone.isdigit() == False:
        response['code'] = -1
        response['errorMsg'] = '请输入有效手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if len(_verify_code) != 4:
        response['code'] = -1
        response['errorMsg'] = '请输入4位验证码'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if _clientID == None:
        _clientID = ''
    else:
        _clientID = str(_clientID)
    if _deviceToken == None:
        _deviceToken = ''
    else:
        _deviceToken = str(_deviceToken)
    _deviceInfo = str(_deviceInfo)

    #中国移动:China Mobile
    # 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
    CM_prog = re.compile(r"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\d)\d{7}$")
    #CM = r"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
    #中国联通:China Unicom
    # 130,131,132,152,155,156,185,186
    CU_prog = re.compile(r"^1(3[0-2]|5[256]|8[56])\d{8}$")
    #CU = r"^1(3[0-2]|5[256]|8[56])\\d{8}$"
    # 中国电信:China Telecom
    # 133,1349,153,180,189
    CT_prog = re.compile(r"^1((33|53|8[09])[0-9]|349)\d{7}$")
    #CT = r"^1((33|53|8[09])[0-9]|349)\\d{7}$"
    telephone_match_CM = CM_prog.match(str(_telephone))
    telephone_match_CU = CU_prog.match(str(_telephone))
    telephone_match_CT = CT_prog.match(str(_telephone))
    #print telephone_match_CM
    #print telephone_match_CU
    #print telephone_match_CT
    if not telephone_match_CM and not telephone_match_CT and not telephone_match_CU:
        response['code'] =  -1
        response['errorMsg'] = '请输入有效的手机号'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    saved_verify_code = cache.get(str(_telephone))
    print('saved_verify_code:'+str(saved_verify_code))
    if not saved_verify_code:
        response['code'] = -1
        response['errorMsg'] = '请重新发送验证码'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")
    if saved_verify_code != _verify_code:
        response['code'] = -1
        response['errorMsg'] = '验证码错误,请重新输入'
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    query_telephone = User.objects.filter(telephone = str(_telephone))
    if not query_telephone.exists():
        _name = '****'+_telephone[7:11]
        currentTime = time.time()
        currentTime = int(currentTime)
        currentTime = str(currentTime)
        new_user = User(telephone=str(_telephone),name = _name,lastLoginTime = currentTime,clientID =
        _clientID,deviceToken = _deviceToken,deviceInfo = _deviceInfo)

        new_user.save()
        request.session['lastLoginTime'] = currentTime
        response_data = {'userId':new_user.id,'telephone':new_user.telephone,'name':new_user.name,'headImage':new_user.headImage}
        response['code'] = 0
        request.session['userId'] = new_user.id
        response['data'] = response_data
        print('get the new user with id :'+str(new_user.id)+'withtelephone:'+new_user.telephone+'with choice:False')
        return HttpResponse(json.dumps(response,ensure_ascii=False),content_type="application/json")

    query_telephone.update(clientID=_clientID,deviceToken = _deviceToken,deviceInfo = _deviceInfo)
    currentTime = time.time()*1000
    currentTime = int(currentTime)
    currentTime = str(currentTime)
    logger.info('--------currentTime-----')
    logger.info(currentTime)
    query_telephone.update(lastLoginTime = currentTime)
    request.session['lastLoginTime'] = currentTime
    request.session['userId'] = query_telephone[0].id
    response_data = {'userId':query_telephone[0].id,'telephone':query_telephone[0].telephone,'name':query_telephone[0].name,'headImage':query_telephone[0].headImage}
    response['code'] = 0
    response['data'] = response_data
    return HttpResponse(json.dumps(response),content_type="application/json")