def post(self, request): """ Cautions: verify_url :此处发送前端的地址。 """ data = json.loads(request.body) if len(data) != 4: return JsonResponse({'code': 10123, 'error': 'Miss parameters!'}) checked_data = self.check_args(data) if type(checked_data) == str: return JsonResponse({ 'code': 10123, 'error': 'Missing %s parameters' % checked_data }) username = data.get('uname') email = data.get('email') password = data.get('password') phone = data.get('phone') # 优先查询当前用户名是否已存在 old_user = UserProfile.objects.filter(username=username) if old_user: result = {'code': 206, 'error': 'Your username is already existed'} return JsonResponse(result) m = hashlib.md5() m.update(password.encode()) try: UserProfile.objects.create(username=username, password=m.hexdigest(), email=email, phone=phone) except Exception as e: return JsonResponse({'code': 208, 'error': 'Server is busy'}) # 发送用户激活链接 # 此链接是通过用户名和邮箱中间拼接了/ code = "%d" % random.randint(1000, 9999) code_str = code + '/' + username # 生成激活链接: active_code = base64.urlsafe_b64encode( code_str.encode(encoding='utf-8')).decode('utf-8') redis_conn = get_redis_connection('verify_email') # TODO : 用户激活链接永久有效,可以根据自己的喜好去设置。 redis_conn.set("email_active_%s" % email, active_code) verify_url = settings.ACTIVE_HOST + '/html/templates/active.html?code=%s' % ( active_code) token = make_token(username) result = { 'code': 200, 'username': username, 'data': { 'token': token.decode() } } send_verify.delay(email=email, verify_url=verify_url, sendtype=1) return JsonResponse(result)
def post(self, request): json_str = request.body if not json_str: result = {'code': 10132, 'error': 'No data found'} return JsonResponse(result) json_obj = json.loads(json_str) username = json_obj.get('uname') if not username: result = {'code': 202, 'error': 'Please give me username'} return JsonResponse(result) email = json_obj.get('email') if not email: result = {'code': 203, 'error': 'Please give me email'} return JsonResponse(result) # 优先查询当前用户名是否已存在 old_user = UserProfile.objects.filter(username=username) if old_user: result = {'code': 206, 'error': 'Your username is already existed'} return JsonResponse(result) password = json_obj.get('password') m = hashlib.md5() m.update(password.encode()) phone = json_obj.get('phone') if not phone: result = {'code': 207, 'error': 'Please give me phone'} return JsonResponse(result) try: UserProfile.objects.create(username=username, password=m.hexdigest(), email=email, phone=phone) except Exception as e: result = {'code': 208, 'error': 'Server is busy'} return JsonResponse(result) # 发送用户激活链接 code_str = username + '.' + email # 生成激活链接: active_code = base64.b64encode(code_str.encode(encoding='utf-8')).decode('utf-8') redis_conn = get_redis_connection('verify_email') ### todo : 用户激活链接永久有效 redis_conn.set("email_active_%s" % email, active_code) verify_url = 'http://114.116.244.115:7000/dadashop/templates/active.html?code=%s&username=%s' % (active_code, username) token = make_token(username) result = {'code': 200, 'username': username, 'data': {'token': token.decode()}} send_verify.delay(email=email, verify_url=verify_url, sendtype=1) return JsonResponse(result)
def post(self, request): data = json.loads(request.body) if len(data) != 1: return JsonResponse({'code': 10105, 'error': 'please input email'}) checked_data = self.check_args(data) if type(checked_data) == str: return JsonResponse({'code': 10106, 'error': 'email can not kong'}) email = data.get('email') try: user = UserProfile.objects.filter(email=email)[0] except Exception: return JsonResponse({ 'code': 10107, 'error': 'email user is not exist' }) # 链接到redis redis_con = get_redis_connection('verify_email') try: # 在redis中查找验证码 email_code = redis_con.get('email_code_%s' % email) except Exception: return JsonResponse({'code': 10108, 'error': 'verify_email error'}) if email_code: # 十分钟内该邮箱已经发送过验证码 return JsonResponse({ 'code': 10109, 'error': 'please enter your code' }) email_code = '%06d' % random.randint(0, 999999) try: # redis 保存 邮箱 验证码 有效时间 redis_con.setex('email_code_%s' % email, 10 * 60, email_code) except Exception: return JsonResponse({ 'code': 10110, 'error': 'redis email code set error' }) send_verify.delay(email=email, email_code=email_code, sendtype=0) return JsonResponse({'code': 200, 'data': 'OK'})
def post(self, request): """ 短信测试: :param request: :return: """ data = json.loads(request.body) if not data: return JsonResponse({'code': 10131, 'error': {'message': 'Invalid phone number!'}}) phone = data.get('phone', None) code = "%06d" % random.randint(0, 999999) try: redis_conn = get_redis_connection('verify_email') redis_conn.setex("sms_code_%s" % phone, 3 * 60, code) except Exception as e: return JsonResponse({'code': 10105, 'error': {'message': 'Storage authentication code failed'}}) send_verify.delay(phone=phone, code=code, sendtype=2) return JsonResponse({'code': 200, 'data': {'message': 'OK'}})
def post(self, request): data = json.loads(request.body) if len(data) != 1: return JsonResponse({'code': 10123, 'error': 'Missing parameters'}) check_data = self.check_args(data) if type(check_data) == str: return JsonResponse({ 'code': 10123, 'error': 'Missing %s parameters' % checked_data }) # 验证用户是否是已经注册用户 email = data.get('email') try: user = UserProfile.objects.get(email=email) except Exception as e: return JsonResponse({'code': 10104, 'error': 'User query error'}) # 先去查询该用户是否在时效时间内发送过验证码 redis_conn = get_redis_connection('verify_email') try: email_code = redis_conn.get('email_code_%s' % email) except Exception as e: return JsonResponse({'code': 10132, 'error': 'Verify Code Error'}) if email_code: return JsonResponse({ 'code': 202, 'error': 'please enter your code!' }) email_code = "%06d" % random.randint(0, 999999) try: redis_conn.setex("email_code_%s" % email, 10 * 60, email_code) except Exception as e: return JsonResponse({ 'code': 10105, 'error': 'Storage authentication code failed' }) send_verify.delay(email=email, email_code=email_code, sendtype=0) return JsonResponse({'code': 200, 'data': 'OK'})