zoukankan      html  css  js  c++  java
  • 17.注册数据合法性接口

    1.django添加检查用户名和手机号数量

    1.1 在user/views.py中添加视图函数

    class RegCountView(APIView):
        # 注册时需要验证的用户名和手机号是否使用
    
        # 自定义权限类
        permission_classes = (AllowAny,)
    
        def post(self, request):
            # 接收参数:  验证的内容type: username/phone,  data: '用户名' 或者 '手机号',
            datatype = request.data.get('type')
            data = request.data.get('data')
            if not all([data, datatype]):
                return Response({'code': 999, 'msg': '参数不完整'})
            if datatype == 'username':
                count = User.objects.filter(username=data).count()
            if datatype == 'phone':
                count = User.objects.filter(phone=data).count()
            return Response({'code': 0, 'msg': '查询成功', 'data': {'type': datatype, 'count': count}})
    
    

    1.2 在user/urls.py中添加路由

    urlpatterns = [
        path('count/', views.RegCountView.as_view()),
    ]
    

    2. 测试接口

    3. vue检查用户名和手机号是否重复

    • 组件:componentscommonlab_header.vue
        // 检查用户名 是否使用
        check_username() {
          console.log('判断用户名')
          console.log(this.username == '')
          var reg = new RegExp(/^[a-zA-Z0-9_-]{3,16}$/); //字符串正则表达式 4到14位(字母,数字,下划线,减号)
          if (this.username == '') {
            this.username_message = '用户名不能为空'
            this.username_error = true
            return false
          }
          if (!reg.test(this.username)) {
            this.username_message = '用户名格式不正确'
            this.username_error = true
            return false
          } else {
            // 去后端检查用户名使用数量
            user_count({ type: 'username', data: this.username }).then((res) => {
              console.log(res)
              if (res.data.count > 0) {
                this.username_message = '用户名已存在'
                this.username_error = true
              } else {
                this.username_message = ''
                this.username_error = false
              }
            })
          }
        },
    
            
        // 检查手机号是否使用
        check_phone() {
          console.log('检查手机号')
          var reg = new RegExp(/^[1]([3-9])[0-9]{9}$/)
          if (this.phone == '') {
            this.phone_message = '手机号不能为空'
            this.phone_error = true
          }
    
          if (!reg.test(this.phone)) {
            this.phone_message = '手机号格式不正确'
            this.phone_error = true
            return false
          } else {
            // 去后端查用户数量
            user_count({ type: 'phone', data: this.phone }).then((res) => {
              console.log(res)
              if (res.data.count > 0) {
                this.phone_message = '手机号已存在'
                this.phone_error = true
              } else {
                this.phone_message = ''
                this.phone_error = false
              }
            })
          }
        },
    
  • 相关阅读:
    KVM WEB管理工具——WebVirtMgr(二)日常配置
    在阿里云上遇见更好的Oracle(四)
    Django源码分析之权限系统_擒贼先擒王
    Django源码分析之server
    Django源码分析之执行入口
    HDFS常用文件操作
    排查实时tail功能cpu占用过高问题
    ZooKeeper完全分布式安装与配置
    Hadoop2.5.2集群部署(完全分布式)
    构造器
  • 原文地址:https://www.cnblogs.com/fiee/p/13782616.html
Copyright © 2011-2022 走看看