梁朋杰

  • 主页
  • 所有文章
所有文章 友链 关于我

梁朋杰

  • 主页
  • 所有文章

用户登录注册(一)

2018-08-10

最近一直想尝试一下前后台都由自己来写的项目,但是由于刚接触到Mongdb,所以先尝试做一个小项目,思来想去,就拿前端最基本的注册登录来试一下手吧。环境的搭建和Mongdb数据库的可视化软件配置,我这里就不做多的描述了。

服务端语言的选择

因为前期对node.js学习了一段时间,在选择后台框架时,就选择了Koa,因为使用起来比较方便,文档很全面。

首先来介绍一下Koa,Koa 是一个新的 web 框架,由 Express 幕后的原班人马打造, 致力于成为 web 应用和 API 开发领域中的一个更小、更富有表现力、更健壮的基石。 通过利用 async 函数,Koa 帮你丢弃回调函数,并有力地增强错误处理。 Koa 并没有捆绑任何中间件, 而是提供了一套优雅的方法,帮助您快速而愉快地编写服务端应用程序。

话不多说,先看前台页面的搭建

前台页面的搭建

采用基本的Vue-cli进行基本结构的搭建,因为只是一个登陆注册的页面,所以页面结构很简单,一个首页,注册页、登陆页,以及一个下单页面。
先来看一下首页的效果图
首页的效果图
首页这里点击立即下单,判断用户如果有没有登陆,如果vuex中存在用户的token,那么页面直接跳转到下单页面,如果vuex中没有存在用户的token,或者存储用户的token已经过期,那么提示用户你还没有登陆,请先登录再进行下单。

提示用户登录

1
2
3
4
5
6
7
8
9
10
11
order(){
//获取store里面的token
let token = store.state.user.token;
if(token){
//如果存在token,说明已经登录,跳往下单页面
this.$router.push('/Order');
}else{
this.modalShowOneBtn=true;
this.modalOneBody='您还没有登录,请先登录,再进行下单操作!'
}
}

用户登录
验证码由后台直接生成base64的地址,然后前台进行获取,点击图片进行验证码的切换

1
2
3
4
5
6
7
8
9
10
11
12
async get_check_code() {
let res = await this.$http.api_get_checkcode();
let {
code,
msg,
data = {}
} = res.data;
if (code == 200) {
this.img_base64 = data.img;
this.user.code_token = data.token;
}
}

用户如果已经有账号,那么在输入账号的时候,可以异步获取用户的头像,提升用户的使用体验。
用户头像获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
async get_user_avatar(id) {
let res = await this.$http.api_getAvatar(id);
// console.log(res.data);
let {
code,
msg,
data
} = res.data;
if (code == 200) {
if(data.userAva.length!==0){
this.userAvatar = data.userAva[0].avatar;
}else{
this.userAvatar = require('../assets/logo.png');
}
}
},

如果没有账号可以随时进行注册,这里用户可以上传头像,我采用了vue-cropper插件进行用户头像的裁剪。
用户注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
upload_img(e) {
let obj = e.target;
let file = obj.files[0];
let temArr = file.name.split(".");
let file_suffix = temArr[temArr.length - 1];
if (file_suffix != 'jpg' && file_suffix != 'png' && file_suffix != 'jpeg') {
this.$toast.center('上传图片失败,目前只支持jpg,png,jpeg的图片!');
return;
}
let reader = new FileReader();
let _self = this;
reader.onload = function(ev) {
_self.cropperData.img = ev.target.result;
_self.cropper_box_mark = true;
}
reader.readAsDataURL(file);
}
赏

谢谢你请我吃糖果

  • Vue Mongdb Koa

扫一扫,分享到微信

微信分享二维码
Vue的世界
'用户注册登录(二)'
© 2018 梁朋杰
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • JS
  • React
  • React表单
  • Vue px rem
  • Vuex
  • Vue axios vuex vue-router
  • Vue
  • http
  • node nvm
  • node utility
  • Vue Mongdb Koa

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • 'Vue旅行小账本(二)'

    2018-10-03

  • Vue旅行小账本 (一)

    2018-09-25

    #Vue axios vuex vue-router

  • Vuex

    2018-09-20

    #Vuex

  • JS节流防抖函数

    2018-09-19

    #JS

  • Vue:将px转化为rem

    2018-08-15

    #Vue px rem

  • '用户注册登录(二)'

    2018-08-11

    #Vue Mongdb Koa

  • 用户登录注册(一)

    2018-08-10

    #Vue Mongdb Koa

  • Vue的世界

    2018-03-18

    #Vue

  • React井字小游戏

    2017-07-05

    #React

  • node第二天

    2017-07-04

    #node utility

  • node第一天

    2017-07-04

    #node nvm

  • http知识点

    2017-05-20

    #http

  • sass语法

    2017-05-19

  • React Router

    2017-05-12

  • React练习Todolist

    2017-05-08

  • Demo练习

    2017-04-25

  • React组件生命周期

    2017-04-24

  • React表单组件

    2017-04-21

    #React表单

  • React历程

    2017-04-20

  • Hello World

    2017-04-18

  • React教程
  • React菜鸟教程
  • React入门实例(阮一峰)
  • React Router使用教程(阮一峰)
  • React介绍及实例教程
乐于分享,喜欢环境音乐,爱看动漫,偶尔写写技术博客的小程一枚...