Exemplo n.º 1
0
 def put(self, request):
     # 1. 接受参数
     token = request.GET.get('token')
     # 2. 校验参数
     if not token:
         return JsonResponse({'code': 400, 'errmsg': '缺少参数token'})
     # 3. 数据处理
     # 解密
     data_dict = SecretOauth().loads(token)
     # 数据库对比id,email
     try:
         user = User.objects.get(pk=data_dict.get('user_id'),
                                 email=data_dict.get('email'))
     except Exception as e:
         logger.error(e)
         return JsonResponse({'code': 400, 'errmsg': '参数有误'})
     try:
         # 修改激活状态
         user.email_active = True
         user.save()
     except Exception as e:
         logger.error(e)
         return JsonResponse({'code': 400, 'errmsg': '激活失败'})
     # 4. 构建响应
     return JsonResponse({'code': 0, 'errmsg': '激活成功'})
Exemplo n.º 2
0
    def get(self, reqeust, mobile):
        """
        :param reqeust: 请求对象
        :param mobile: 手机号
        :return: JSON
        """
        # 1. 接收参数
        image_code_client = reqeust.GET.get('image_code')
        uuid = reqeust.GET.get('image_code_id')

        # 2. 校验参数
        if not all([image_code_client, uuid]):
            return http.JsonResponse({'code': 400, 'errmsg': '缺少必传参数'})

        # 3. 创建连接到redis的对象
        redis_conn = get_redis_connection('verify_code')

        # 4. 提取图形验证码
        image_code_server = redis_conn.get('img_%s' % uuid)
        if image_code_server is None:
            # 图形验证码过期或者不存在
            return http.JsonResponse({'code': 400, 'errmsg': '图形验证码失效'})

        # 5. 删除图形验证码,避免恶意测试图形验证码
        try:
            redis_conn.delete('img_%s' % uuid)
        except Exception as e:
            logger.error(e)

        # 6. 对比图形验证码
        # bytes 转字符串
        image_code_server = image_code_server.decode()
        # 转小写后比较
        if image_code_client.lower() != image_code_server.lower():
            return http.JsonResponse({'code': 400, 'errmsg': '输入图形验证码有误'})

        # 7. 生成短信验证码:生成6位数验证码
        sms_code = '%06d' % random.randint(0, 999999)
        logger.info(sms_code)
        print(sms_code)

        # 8. 保存短信验证码
        # 短信验证码有效期,单位:300秒
        redis_conn.setex('sms_%s' % mobile, 300, sms_code)

        # 9. 发送短信验证码
        # 短信模板
        CCP().send_template_sms(mobile, [sms_code, 5], 1)
        print('短信验证码:', sms_code)
        # # 9. 异步发送验证码
        # ccp_send_sms_code.delay(mobile, sms_code)

        # 10. 响应结果
        return http.JsonResponse({'code': 0, 'errmsg': '发送短信成功'})
Exemplo n.º 3
0
 def put(self, request, address_id):
     # 1. 接收参数
     # 2. 校验参数
     # 3. 业务数据处理
     try:
         # 3.1 接收参数,查询地址
         address = Address.objects.get(id=address_id)
         # 3.2 设置地址为默认地址
         request.user.default_address = address
         request.user.save()
     except Exception as e:
         logger.error(e)
         return JsonResponse({
             'code': 400,
             'errmsg': '设置默认地址失败'
         },
                             status=400)
     # 4. 构建响应
     return JsonResponse({'code': 0, 'errmsg': 'ok'})
Exemplo n.º 4
0
 def delete(self, request, address_id):
     """删除收货地址"""
     # 1. 接收参数
     # 2. 校验参数
     # 3. 业务数据处理
     try:
         # 3.1 查询要删除的地址
         address = Address.objects.get(id=address_id)
         # 3.2 将逻辑删除设置位True
         address.is_deleted = True
         address.save()
     except Exception as e:
         logger.error(e)
         return JsonResponse({
             'code': 400,
             'errmsg': '删除收货地址失败'
         },
                             status=400)
     # 4. 构建响应
     return JsonResponse({'code': 0, 'errmsg': 'ok'})
Exemplo n.º 5
0
 def put(self, request):
     # 1. 接收参数
     json_dict = json.loads(request.body.decode())
     old_password = json_dict.get('old_password')
     new_password = json_dict.get('new_password')
     new_password2 = json_dict.get('new_password2')
     # 2. 校验参数
     if not all([old_password, new_password, new_password2]):
         return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}, status=400)
     result = request.user.check_password(old_password)
     if not result:
         return JsonResponse({'code': 400, 'errmsg': '原始密码不正确'}, status=400)
     if not re.match(r'^[0-9A-Za-z]{8,20}$', new_password):
         return JsonResponse({
             'code': 400,
             'errmsg': '密码最少8位,最多20位'
         },
                             status=400)
     if new_password != new_password2:
         return JsonResponse({
             'code': 400,
             'errmsg': '两次输入密码不一致'
         },
                             status=400)
     # 3. 业务数据处理
     try:
         # 3.1 修改密码
         request.user.set_password(new_password)
         request.user.save()
     except Exception as e:
         logger.error(e)
         return JsonResponse({'code': 400, 'errmsg': '没有修改密码'}, status=400)
     # 3.2 清理状态保持信息
     logout(request)
     # 4. 构建响应
     response = JsonResponse({
         'code': 0,
         'errmsg': 'ok',
     })
     response.delete_cookie('username')
     return response
Exemplo n.º 6
0
 def put(self, request, address_id):
     # 1. 接收参数
     json_dict = json.loads(request.body.decode())
     title = json_dict.get('title')
     # 2. 校验参数
     # 3. 业务数据处理
     try:
         # 3.1 查询地址
         address = Address.objects.get(id=address_id)
         # 3.2 设置新的地址标题
         address.title = title
         address.save()
     except Exception as e:
         logger.error(e)
         return JsonResponse({
             'code': 400,
             'errmsg': '修改地址标题失败'
         },
                             status=400)
     # 4. 构建响应
     return JsonResponse({'code': 0, 'errmsg': 'ok'})
Exemplo n.º 7
0
 def get(self, request):
     # 1. 接受参数
     # 2. 校验参数
     # 3. 业务数据处理
     # 3.1 查询所有地址
     try:
         addresses = Address.objects.filter(user=request.user,
                                            is_deleted=False)
         # 3.2 遍历
         address_dict_list = []
         for address in addresses:
             address_dict = {
                 'id': address.id,
                 'title': address.title,
                 'receiver': address.receiver,
                 'province': address.province.name,
                 'city': address.city.name,
                 'district': address.district.name,
                 'mobile': address.mobile,
                 'tel': address.tel,
                 'email': address.email
             }
             address_dict_list.append(address_dict)
     except Exception as e:
         logger.error(e)
         return JsonResponse({
             'code': 400,
             'errmsg': '展示收货地址失败'
         },
                             status=400)
     # 设置默认收货地址
     default_id = request.user.default_address_id
     # 4. 构建响应
     return JsonResponse({
         'code': 0,
         'errmsg': 'ok',
         'addresses': address_dict_list,
         'default_address_id': default_id
     })
Exemplo n.º 8
0
 def put(self, request, address_id):
     """修改收货地址"""
     # 1. 接收参数
     json_dict = json.loads(request.body.decode())
     title = json_dict.get('title')
     receiver = json_dict.get('receiver')
     province_id = json_dict.get('province_id')
     city_id = json_dict.get('city_id')
     district_id = json_dict.get('district_id')
     place = json_dict.get('place')
     mobile = json_dict.get('mobile')
     tel = json_dict.get('tel')
     email = json_dict.get('email')
     # 2. 校验参数
     if not all(
         [receiver, province_id, city_id, district_id, place, mobile]):
         return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}, status=400)
     if not re.match(r'^1[3-9]\d{9}$', mobile):
         return JsonResponse({
             'code': 400,
             'errmsg': '参数mobile有误'
         },
                             status=400)
     if not re.match(r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$',
                     tel):
         return JsonResponse({'code': 400, 'errmsg': '参数tel有误'}, status=400)
     if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$',
                     email):
         return JsonResponse({
             'code': 400,
             'errmsg': '参数email有误'
         },
                             status=400)
     # 3. 业务数据处理
     try:
         # 3.1 判断地址是否存在,并更新信息
         Address.objects.filter(id=address_id).update(
             user=request.user,
             title=request.title,
             receiver=request.receicer,
             province_id=request.province_id,
             city_id=request.city_id,
             district_id=request.district_id,
             place=request.place,
             mobile=request.mobile,
             tel=request.tel,
             email=request.email)
     except Exception as e:
         logger.error(e)
         return JsonResponse({
             'code': 400,
             'errmsg': '修改收货地址失败'
         },
                             status=400)
     # 构造响应数据
     address = Address.objects.get(id=address_id)
     address_dict = {
         'id': address.id,
         'title': address.title,
         'receiver': address.receiver,
         'province': address.province.name,
         'city': address.city.name,
         'district': address.district.name,
         'place': address.place,
         'mobile': address.mobile,
         'tel': address.tel,
         'email': address.email
     }
     # 4. 构建响应
     return JsonResponse({
         'code': 400,
         'errmsg': 'ok',
         'address': address_dict
     })
Exemplo n.º 9
0
 def post(self, request):
     # 1. 接受参数
     json_dict = json.loads(request.body.decode())
     title = json_dict.get('title')
     receiver = json_dict.get('receiver')
     province_id = json_dict.get('province_id')
     city_id = json_dict.get('city_id')
     district_id = json_dict.get('district_id')
     place = json_dict.get('place')
     mobile = json_dict.get('mobile')
     tel = json_dict.get('tel')
     email = json_dict.get('email')
     # 获取收货地址个数
     count = Address.objects.filter(user=request.user,
                                    is_deleted=False).count()
     # 2. 校验参数
     if count >= 20:
         return JsonResponse({
             'code': 400,
             'errmsg': '超过地址数量上限'
         },
                             status=400)
     if not all(
         [receiver, province_id, city_id, district_id, place, mobile]):
         return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}, status=400)
     if not re.match(r'^1[3-9]\d{9}$', mobile):
         return JsonResponse({
             'code': 400,
             'errmsg': '参数mobile有误'
         },
                             status=400)
     if tel:
         if not re.match(
                 r'^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$', tel):
             return JsonResponse({
                 'code': 400,
                 'errmsg': '参数tel有误'
             },
                                 status=400)
     if email:
         if not re.match(
                 r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$',
                 email):
             return JsonResponse({
                 'code': 400,
                 'errmsg': '参数email有误'
             },
                                 status=400)
     # 3. 业务数据处理
     # 3.1 保存地址信息
     try:
         address = Address.objects.create(user=request.user,
                                          title=title,
                                          receiver=receiver,
                                          province_id=province_id,
                                          city_id=city_id,
                                          district_id=district_id,
                                          place=place,
                                          mobile=mobile,
                                          tel=tel,
                                          email=email)
         # 3.2 设置默认地址
         if not request.user.default_address:
             request.user.default_address = address
     except Exception as e:
         logger.error(e)
         return JsonResponse({'code': 400, 'errmsg': '新增地址失败'}, status=400)
         # 将新增的地址响应给前段
     address_dict = {
         'id': address.id,
         'title': address.title,
         'receiver': address.receiver,
         'province': address.province.name,
         'city': address.city.name,
         'district': address.district.name,
         'place': address.place,
         'mobile': address.mobile,
         'tel': address.tel,
         'email': address.email
     }
     # 4. 构建响应
     return JsonResponse({
         'code': 0,
         'errmsg': '新增地址成功',
         'address': address_dict
     })