首页 > 其他分享 >【2022-11-14】luffy项目实战(七)

【2022-11-14】luffy项目实战(七)

时间:2022-11-14 18:22:42浏览次数:37  
标签:11 14 mobile res sms color 2022 message data

一、短信注册接口

user/views.py

class UserView(ViewSet):
    @action(methods=['POST'], detail=False)
    def register(self, request):
        info = UserRegisterSerializer(data=request.data)
        info.is_valid(raise_exception=True)
        info.save()
        return APIResponse(msg='您已注册成功')

user/serializer.py

class UserRegisterSerializer(serializers.ModelSerializer):  # 用于做数据校验和反序列化
    code = serializers.CharField(max_length=4, min_length=4)

    class Meta:
        model = UserInfo
        fields = ['mobile', 'code', 'password']

    def validate(self, attrs):
        mobile = attrs.get('mobile')
        code = attrs.get('code')
        old_code = cache.get('sms_code_%s' % mobile)
        if not (old_code == code or code == '6666'):
            raise APIException('验证码错误')
        attrs['username'] = mobile
        attrs.pop('code')
        return attrs

    def create(self, validated_data):
        user = UserInfo.objects.create_user(**validated_data)
        return user

接口测试

二、登录功能前端

login.vue(多方式登录+短信验证登录)

<template>
  <div class="login">
    <div class="box">
      <i class="el-icon-close" @click="close_login"></i>
      <div class="content">
        <div class="nav">
                    <span :class="{active: login_method === 'is_pwd'}"
                          @click="change_login_method('is_pwd')">密码登录</span>
          <span :class="{active: login_method === 'is_sms'}"
                @click="change_login_method('is_sms')">短信登录</span>
        </div>
        <el-form v-if="login_method === 'is_pwd'">
          <el-input
              placeholder="用户名/手机号/邮箱"
              prefix-icon="el-icon-user"
              v-model="username"
              clearable>
          </el-input>
          <el-input
              placeholder="密码"
              prefix-icon="el-icon-key"
              v-model="password"
              clearable
              show-password>
          </el-input>
          <el-button type="primary" @click="handlerMullLogin">登录</el-button>
        </el-form>
        <el-form v-if="login_method === 'is_sms'">
          <el-input
              placeholder="手机号"
              prefix-icon="el-icon-phone-outline"
              v-model="mobile"
              clearable
              @blur="check_mobile">
          </el-input>
          <el-input
              placeholder="验证码"
              prefix-icon="el-icon-chat-line-round"
              v-model="sms"
              clearable>
            <template slot="append">
              <span class="sms" @click="send_sms">{{ sms_interval }}</span>
            </template>
          </el-input>
          <el-button type="primary" @click="handlerSmsLogin">登录</el-button>
        </el-form>
        <div class="foot">
          <span @click="go_register">立即注册</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      username: '',
      password: '',
      mobile: '',
      sms: '',
      login_method: 'is_pwd',
      sms_interval: '获取验证码',
      is_send: false,
    }
  },
  methods: {
    close_login() {
      this.$emit('close')
    },
    go_register() {
      this.$emit('go')
    },
    change_login_method(method) {
      this.login_method = method;
    },
    check_mobile() {
      if (!this.mobile) return;
      if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
        this.$message({
          message: '手机号有误',
          type: 'warning',
          duration: 1000,
          onClose: () => {
            this.mobile = '';
          }
        });
        return false;
      }
      // 校验手机号是否被注册
      this.$axios.get(this.$settings.BASE_URL + 'userlogin/user/mobile/?mobile=' + this.mobile).then(res => {
            if (res.data.code != 200) {
              this.mobile = ''
              this.$message({
                message: '该手机号未被注册,请先进行注册',
                type: 'error'
              });
              return // 结束函数
            }
          }
      )
      this.is_send = true;
    },
    send_sms() {

      if (!this.is_send) return;
      this.is_send = false;
      let sms_interval_time = 60;
      this.sms_interval = "发送中...";
      let timer = setInterval(() => {
        if (sms_interval_time <= 1) {
          clearInterval(timer);
          this.sms_interval = "获取验证码";
          this.is_send = true; // 重新回复点击发送功能的条件
        } else {
          sms_interval_time -= 1;
          this.sms_interval = `${sms_interval_time}秒后再发`;
        }
      }, 1000);
      // 发送短信
      this.$axios.get(this.$settings.BASE_URL + 'userlogin/user/send_sms/?mobile=' + this.mobile).then(
          res => {
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
          }
      )
    },

    // 多方式登录
    handlerMullLogin() {
      if (this.username && this.password) {
        this.$axios.post(this.$settings.BASE_URL + 'userlogin/user/mul_login/', {
          username: this.username,
          password: this.password,
        }).then(res => {
          console.log(res.data)
          if (res.data.code == 200) {
            // 使用cookie存储用户名
            this.$cookies.set('token', res.data.token)
            this.$cookies.set('username', res.data.username)
            this.$cookies.set('icon', res.data.icon)
            // 登录成功后,取消登录框的显示
            this.$emit('close')
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
        })
      } else {
        this.$message({
          message: '用户名密码不能为空',
          type: 'warning'
        });
      }
    },
    // 短信验证登录
    handlerSmsLogin() {
      if (this.mobile && this.sms) {
        this.$axios.post(this.$settings.BASE_URL + 'userlogin/user/mobile_login/', {
          mobile: this.mobile,
          code: this.sms
        }).then(res => {
          if (res.data.code == 200) {
            // 使用cookie存储用户名
            this.$cookies.set('token', res.data.token)
            this.$cookies.set('username', res.data.username)
            this.$cookies.set('icon', res.data.icon)
            // 登录成功后,取消登录框的显示
            this.$emit('close')
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.login {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  background-color: rgba(0, 0, 0, 0.3);
}

.box {
  width: 400px;
  height: 420px;
  background-color: white;
  border-radius: 10px;
  position: relative;
  top: calc(50vh - 210px);
  left: calc(50vw - 200px);
}

.el-icon-close {
  position: absolute;
  font-weight: bold;
  font-size: 20px;
  top: 10px;
  right: 10px;
  cursor: pointer;
}

.el-icon-close:hover {
  color: darkred;
}

.content {
  position: absolute;
  top: 40px;
  width: 280px;
  left: 60px;
}

.nav {
  font-size: 20px;
  height: 38px;
  border-bottom: 2px solid darkgrey;
}

.nav > span {
  margin: 0 20px 0 35px;
  color: darkgrey;
  user-select: none;
  cursor: pointer;
  padding-bottom: 10px;
  border-bottom: 2px solid darkgrey;
}

.nav > span.active {
  color: black;
  border-bottom: 3px solid black;
  padding-bottom: 9px;
}

.el-input, .el-button {
  margin-top: 40px;
}

.el-button {
  width: 100%;
  font-size: 18px;
}

.foot > span {
  float: right;
  margin-top: 20px;
  color: orange;
  cursor: pointer;
}

.sms {
  color: orange;
  cursor: pointer;
  display: inline-block;
  width: 70px;
  text-align: center;
  user-select: none;
}
</style>

Header.vue

<template>
  <div class="header">
    <div class="slogan">
      <p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</p>
    </div>
    <div class="nav">
      <ul class="left-part">
        <li class="logo">
          <router-link to="/">
            <img src="../assets/img/head-logo.svg" alt="">
          </router-link>
        </li>
        <li class="ele">
          <span @click="goPage('/free-course')" :class="{active: url_path === '/free-course'}">免费课</span>
        </li>
        <li class="ele">
          <span @click="goPage('/actual-course')" :class="{active: url_path === '/actual-course'}">实战课</span>
        </li>
        <li class="ele">
          <span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">轻课</span>
        </li>
      </ul>

      <div class="right-part">
        <div v-if="!username">
          <span @click="put_login">登录</span>
          <span class="line">|</span>
          <span @click="put_register">注册</span>
        </div>
        <div v-else>
          <span>{{ username }}</span>
          <span class="line">|</span>
          <span @click="logout">退出</span>
        </div>
        <login v-if="is_login" @close="close_login" @go="put_register"></login>
        <Register v-if="is_register" @close="close_register" @go="put_login"></Register>
      </div>
    </div>
  </div>

</template>

<script>
import login from "@/components/login";
import Register from "@/components/Register";

export default {
  name: "Header",
  data() {
    return {
      url_path: sessionStorage.url_path || '/',
      is_login: false,
      is_register: false,
      username: '',
      token: '',
    }
  },
  methods: {
    goPage(url_path) {
      // 已经是当前路由就没有必要重新跳转
      if (this.url_path !== url_path) {
        // 传入的参数,如果不等于当前路径,就跳转
        this.$router.push(url_path)
      }
      sessionStorage.url_path = url_path;
    },
    goLogin() {
      this.loginShow = true
    },
    put_login() {
      this.is_login = true;
      this.is_register = false;
    },
    put_register() {
      this.is_login = false;
      this.is_register = true;
    },
    close_login() {
      this.is_login = false;
      this.username = this.$cookies.get('username')
    },
    close_register() {
      this.is_register = false;
    },
    // 退出功能
    logout() {
      this.$cookies.remove('token')
      this.$cookies.remove('username')
      this.$cookies.remove('icon')
      this.username = ''
    }
  },
  created() {
    sessionStorage.url_path = this.$route.path
    this.url_path = this.$route.path
    // 取出cookie中的用户名和token
    this.username = this.$cookies.get('username')
  },
  components: {
    login,
    Register
  }
}
</script>

<style scoped>
.header {
  background-color: white;
  box-shadow: 0 0 5px 0 #aaa;
}

.header:after {
  content: "";
  display: block;
  clear: both;
}

.slogan {
  background-color: #eee;
  height: 40px;
}

.slogan p {
  width: 1200px;
  margin: 0 auto;
  color: #aaa;
  font-size: 13px;
  line-height: 40px;
}

.nav {
  background-color: white;
  user-select: none;
  width: 1200px;
  margin: 0 auto;

}

.nav ul {
  padding: 15px 0;
  float: left;
}

.nav ul:after {
  clear: both;
  content: '';
  display: block;
}

.nav ul li {
  float: left;
}

.logo {
  margin-right: 20px;
}

.ele {
  margin: 0 20px;
}

.ele span {
  display: block;
  font: 15px/36px '微软雅黑';
  border-bottom: 2px solid transparent;
  cursor: pointer;
}

.ele span:hover {
  border-bottom-color: orange;
}

.ele span.active {
  color: orange;
  border-bottom-color: orange;
}

.right-part {
  float: right;
}

.right-part .line {
  margin: 0 10px;
}

.right-part span {
  line-height: 68px;
  cursor: pointer;
}
</style>

三、注册功能前端

Register.vue

<template>
  <div class="register">
    <div class="box">
      <i class="el-icon-close" @click="close_register"></i>
      <div class="content">
        <div class="nav">
          <span class="active">新用户注册</span>
        </div>
        <el-form>
          <el-input
              placeholder="手机号"
              prefix-icon="el-icon-phone-outline"
              v-model="mobile"
              clearable
              @blur="check_mobile">
          </el-input>
          <el-input
              placeholder="密码"
              prefix-icon="el-icon-key"
              v-model="password"
              clearable
              show-password>
          </el-input>
          <el-input
              placeholder="验证码"
              prefix-icon="el-icon-chat-line-round"
              v-model="sms"
              clearable>
            <template slot="append">
              <span class="sms" @click="send_sms">{{ sms_interval }}</span>
            </template>
          </el-input>
          <el-button type="primary" @click="handlerRegister">注册</el-button>
        </el-form>
        <div class="foot">
          <span @click="go_login">立即登录</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      mobile: '',
      password: '',
      sms: '',
      sms_interval: '获取验证码',
      is_send: false,
    }
  },
  methods: {
    close_register() {
      this.$emit('close', false)
    },
    go_login() {
      this.$emit('go')
    },
    check_mobile() {
      if (!this.mobile) return;
      if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
        this.$message({
          message: '手机号有误',
          type: 'warning',
          duration: 1000,
          onClose: () => {
            this.mobile = '';
          }
        });
        return false;
      }
      // 判断手机号是否存在
      this.$axios.get(this.$settings.BASE_URL + 'userlogin/user/mobile/?mobile=' + this.mobile).then(res => {
            if (res.data.code == 200) {
              this.mobile = ''
              this.$message({
                message: '该手机号已被注册,请进行登录',
                type: 'error'
              });
              return // 结束函数
            }
          }
      )
      this.is_send = true;
    },
    send_sms() {
      if (!this.is_send) return;
      this.is_send = false;
      let sms_interval_time = 60;
      this.sms_interval = "发送中...";
      let timer = setInterval(() => {
        if (sms_interval_time <= 1) {
          clearInterval(timer);
          this.sms_interval = "获取验证码";
          this.is_send = true; // 重新回复点击发送功能的条件
        } else {
          sms_interval_time -= 1;
          this.sms_interval = `${sms_interval_time}秒后再发`;
        }
      }, 1000);
      // 发送短信
      this.$axios.get(this.$settings.BASE_URL + 'userlogin/user/send_sms/?mobile=' + this.mobile).then(
          res => {
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
          }
      )
    },
    handlerRegister() {
      if (this.mobile && this.sms && this.password) {
        this.$axios.post(this.$settings.BASE_URL + 'userlogin/user/register/', {
          mobile: this.mobile,
          code: this.sms,
          password: this.password,
        }).then(res => {
          if (res.data.code == 200) {
            // 注册成功跳转到登录
            this.$emit('go')
          } else {
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
        })
      } else {
        this.$message({
          message: '手机号不能为空',
          type: 'error'
        });
      }
    }
  }
}
</script>

<style scoped>
.register {
  width: 100vw;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10;
  background-color: rgba(0, 0, 0, 0.3);
}

.box {
  width: 400px;
  height: 480px;
  background-color: white;
  border-radius: 10px;
  position: relative;
  top: calc(50vh - 240px);
  left: calc(50vw - 200px);
}

.el-icon-close {
  position: absolute;
  font-weight: bold;
  font-size: 20px;
  top: 10px;
  right: 10px;
  cursor: pointer;
}

.el-icon-close:hover {
  color: darkred;
}

.content {
  position: absolute;
  top: 40px;
  width: 280px;
  left: 60px;
}

.nav {
  font-size: 20px;
  height: 38px;
  border-bottom: 2px solid darkgrey;
}

.nav > span {
  margin-left: 90px;
  color: darkgrey;
  user-select: none;
  cursor: pointer;
  padding-bottom: 10px;
  border-bottom: 2px solid darkgrey;
}

.nav > span.active {
  color: black;
  border-bottom: 3px solid black;
  padding-bottom: 9px;
}

.el-input, .el-button {
  margin-top: 40px;
}

.el-button {
  width: 100%;
  font-size: 18px;
}

.foot > span {
  float: right;
  margin-top: 20px;
  color: orange;
  cursor: pointer;
}

.sms {
  color: orange;
  cursor: pointer;
  display: inline-block;
  width: 70px;
  text-align: center;
  user-select: none;
}
</style>

四、Redis介绍使用

4.1 什么是Redis

Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型有:字符串类型 散列类型 列表类型 集合类型,端口默认为6379。

4.2 Redis的特性

1. 高性能:支持超过 100K+ 每秒的读写频率。
2. 支持多种数据类型:除了key-value类型的数据,同时还提供String,List,Set,hash,以及Ordered Set等数据结构的存储。
3. 原子性:Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
4. 数据持久化:可以将内存中的数据保存在磁盘中,在服务器宕机或者重启时,可以重新读取使用。
5. 单线程:避免了不必要的上下文切换以及加锁导致的一系列性能问题。
6. 功能丰富:Redis还支持 publish/subscribe, 通知, key 过期等等特性。

4.3 Redis的应用场景

1. 热点数据缓存
	redis访问速度快,所以对于那些经常查询且不常修改的数据首选使用redis缓存,像新闻内容、商品内容等。
2. 计数器
	对于视频播放,文章阅读,排行榜等数据计数的情况,并发量较大且对实时性要求比较高,Redis可以极大减轻服务器的压力。
3. 消息队列
	像秒杀,抢购这种活动,可以用于流量削峰,异步处理实时性低的业务(但效果比RabbitMQ,kafka这种专门的消息队列中间件要差点)。
4. 分布式session
	分布式集群架构中的session分离。

4.4 Redis单线程

1. 命令基于内存操作,一条命令在内存的操作也就是几十纳秒
2. 命令是单线程操作没有上下文切换开销
3. 基于I/O多路复用机制提升redis的I/O利用率
4. 高效的数据存储结构:全局hash表以及跳表、压缩表、链表等数据结构。

五、python操作Redis

# 前提,已经安装好了Redis

# 官方网:https://redis.io/
    下载完是源代码:c语言源码 :https://redis.io/download/
    最稳定:6.x
    最新7.x

# 中文网:http://redis.cn/download.html
	上面最新只到5.x
    
    
# win版本下载地址
    # 最新5.x版本 https://github.com/tporadowski/redis/releases/
    # 最新3.x版本 https://github.com/microsoftarchive/redis/releases
	下载完一路下一步即可,具体可参照:https://www.cnblogs.com/liuqingzheng/p/9831331.html
# pycharm安装redis
	pip install redis
    
# 创建一个测试脚本,代码如下:
    from redis import Redis

    connent = Redis(host="localhost", port=6379)
    # connent.set('name', 'tony')
    print(connent.get('name'))
    connent.close()

六、Redis连接池

pool.py

import redis

pool = redis.ConnectionPool(max_connections=200, host='127.0.0.1', port=6379)

redis_pool_test.py

from redis import Redis
from threading import Thread

# 直接连接
# def get_name_from_redis():
#     conn = Redis(host="localhost", port=6379)
#     print(conn.get('name'))
#     conn.close()
#
#
# for i in range(100):
#     t=Thread(target=get_name_from_redis)
#     t.start()
#
#
# import time
# time.sleep(10)


# 使用连接池链接
import redis
from POOL import pool
def get_name_from_redis():
    # 创建一个连接池,保证它是单例,全局只有一个pool对象:使用模块导入方式实现单例

    conn = redis.Redis(connection_pool=pool) # 每执行一次会从池中取一个链接,如果没有则等待
    res=conn.get('name')
    print(res)
    conn.close()


for i in range(100):
    t=Thread(target=get_name_from_redis)
    t.start()


import time
time.sleep(10)

标签:11,14,mobile,res,sms,color,2022,message,data
From: https://www.cnblogs.com/dy12138/p/16889914.html

相关文章

  • Python实验报告(第11周)
      实验11:模块一、实验目的和要求1、学会自定义模块;2、学会引用其他模块;3、学会创建和使用包。二、实验环境软件版本:Python3.1064_bit三、实验过程1、实例1:......
  • 【ECCV2022】AMixer: Adaptive Weight Mixing for Self-Attention Free Vision Transf
    1、Motivation这个论文来自于清华大学鲁继文老师团队,核心是attention和MLP-mixer思想的结合。建议用2分钟时间学习一下谷歌公司的MLP-Mixer「MLP-Mixer:Anall-ML......
  • Win11右键菜单改为Win10设计【自用】
    出处:【Win11右键菜单改为Win10设计】https://www.bilibili.com/video/BV1gB4y1L7Uj?vd_source=5e9bda91f690b8bf5340dc78c84bccbd修改:reg add HKCU\Software\Classes\C......
  • [已满分在线评测] cmu15445 2022 PROJECT #2 B+Tree Index
    CMU154452022PROJECT#2B+TreeIndex前前言本地测试通过是真的比较简单,因为有数据可以单步debug,很快就能定位错误。但是要通过在线评测还是比较痛苦的,没有数据,没办法......
  • 11-Go语言进阶-01
    包的使用引入时,用.做前缀,使用时可以省略包名,不建议这么使用可以前缀别名"_"下划线操作,可以执行包里面对应的init函数首字母大写的字段和实体,才能被外部引用init函数......
  • 2022-找工准备-面经积累
    请使用Python实现对数据重复文本的去除,数据是dict的形式,key总计N个,value是字符串,对value中重复的(完全重叠10个字以上)内容进行识别,保留第一次出现该重复文本的内容,去除掉......
  • 2022云栖现场|体验阿里巴巴工作数字化实践
    简介: 2022云栖大会,阿里巴巴企业智能带来阿里数字化工作方法与企业IT解决方案,展示着阿里内部在办公协同与IT管理上的实际应用场景。越来越多的企业主动拥抱数字......
  • Java学习——11.14
    将近4天没更新啊,为什么呢,主要是面向过程太难太抽象了,不过好在我用四天还是将他理解了。1.封装(关键字:private)保护私有的方法和属性。set直接修改private  ......
  • 11.14.8
    #include<stdio.h>intmain(){ intn,m,i,j,sum=0,count=0; scanf("%d%d",&m,&n); for(i=m;i<=n;i++) {for(j=1;j<i;j++) {if(i%j==0){sum+=j; } } if(sum==i){p......
  • 11.14.9
    #include<stdio.h>intmain(){ intn,m,i,j,count=0; inta[100][100],b[100][100],c[100][100]; scanf("%d%d",&m,&n); for(i=0;i<m;i++) {for(j=0;j<n;j++) {scan......