第二章 建議學習時間8小時 總項目預計10章
學習方式:詳細閱讀,並手動實現相關代碼(如果沒有node和vue基礎,請學習前面的vue和node基礎博客【共10章】
演示地址:后台:demoback.lalalaweb.com 前台:demo.lalalaweb.com
演示過程中可能會發現bug,希望即時留言反饋,謝謝
源碼下載:https://github.com/sutianbinde/classweb //不是全部的代碼,每次更新博客才更新代碼
學習目標:此教程將教會大家 如何一步一步實現一個完整的課程學習系統(包括課程管理后台/Node服務器/學習門戶三個模塊)。
上次node基礎課程博客大家反響很好,時隔3個月,才更新項目部分,預計2~3天更新一章,我盡量20天更新完畢,學完這個項目Nodejs和vue就基本熟悉了,如發現教程有誤的地方,請及時留言反饋
視頻教程地址:www.lalalaweb.com,后期會上傳教學視頻,大家可前往視頻學習(暫時還沒有視頻)
首頁導航 路由配置
上一篇博客中,我們已經實現了登錄功能,那么接着我就需要寫登錄完成后跳轉的頁面
我們先把項目運行起來 Node端 npm start ,vue端npm run dev
首先我們需要使用一個字體圖標庫 fontawesome,如果沒有用過的自行百度啊,這里就不扯其他知識了
下載地址:http://fontawesome.dashgame.com/ 或者 到github下載的本項目中對應路徑去找也可以
下載好以后把css和font放到static中,然后我們在index.html中引入
<link rel="stylesheet" type="text/css" href="../static/css/font-awesome.min.css"/>
注:1。我沒有找到可以通過import引入的fontawesome包,所以就直接引入文件了,(網上有 vue-awesome,但貌似不好用,就沒有用)
這里為什么是 ../static 這樣去找static,而不是 ./ ,因為當進入二級路由以后,在路由內部index和static就不在被認為是同一級,就找不到了,所以就通過 ../往上再找了一級。
我們要設置一些統一的全局樣式,我們就直接寫在 index.html中,這里本來不是一次就寫完這些樣式,但為了避免以后再回來添加樣式,這里就一起寫了,首先清楚了全局的margin等,然后定義了 .btn按鈕樣式 .myinput輸入框樣式,以后再使用
<style> *{ margin: 0; padding: 0; } body{ font-size: 14px; font-family: arial "microsoft yahei"; background: #f0f2f5; } ul,li{ list-style: none; } /*按鈕*/ .btn{ border:1px solid #4187db; color: #4187db; background: #fff; padding: 6px 14px 7px; border-radius: 3px; transition: all 0.5s ease; outline: none; margin-top: 14px; cursor: pointer; } .btn i{ margin-right: 4px; } .btn:hover{ background: #4187db; color: #fff; } /*輸入框*/ .myinput{ width: 65%; border: 1px solid #cad3de; height: 35px; line-height: 35px; margin: 5px 0 10px; border-radius: 3px; padding: 0 10px; outline: none; box-sizing: border-box; } .myinput:focus{ border: 1px solid #4289dc; } </style>
在assets文件夾中創建 images文件夾,放入我們backIndex.vue中需要的圖片 (圖片請到github下載的項目中對應路徑去找)
然后我們去修改 路由文件 index.js的路由,在其中添加后台首頁框架的路由,如下圖
並且在components中創建 backIndex.vue組件
在backIndex.vue組件中寫入后面代碼
基本功能如下圖,左側導航,頂部搜索欄和個人頭像 退出等操作
代碼解釋:中間大部分是Html+css代碼,代碼中注釋已經可以幫助大家理解
這里着重說一下路由部分:router-link表示點擊的時候url需要跳轉的地址,這些地址對應的路由文件還沒有寫,這里先寫上,下下步就配置這個,頁面不多,我們就完成 首頁 用戶/學員管理 課程列表/課程編輯 幾個頁面,這樣就可以算是一個基礎版的后台管理系統了。
當點擊對應的link的時候,vue內部會自動添加router-link-active 類,我們css中對這個類做了樣式設置。
<template> <div class="backlogin"> <div class="header"> <div class="search_box" :class="{search_box_fouce:search_box_fouce}"> <i class="fa fa-search" aria-hidden="true"></i> <input @focus="focusFn" @blur="blurFn" type="text" name="" id="" value="" placeholder="搜索 . . . " /> </div> <div class="handler"> <div class="more" @click="toggleSlide"> <i class="fa fa-bars" aria-hidden="true"></i> <ul :class="{showul:showExit}"> <li><a href="javascript:;" @click="logout"><i class="fa fa-sign-out" aria-hidden="true"></i>退出</a></li> <li><a href="javascript:;" @click="">修改密碼</a></li> <li><a href="javascript:;">意見反饋</a></li> </ul> </div> <img src="../assets/images/teacherimg01.png" alt="" /> </div> </div> <!--側面導航--> <div class="sidenav_box"> <img class="logo" src="../assets/images/logo03.png" alt="" /> <ul class="sidenav"> <li class="now"> <router-link to="/backIndex/indexContent"> <i class="fa fa-home" aria-hidden="true"></i> <span>網站首頁</span> </router-link> </li> <li> <router-link to="/backIndex/adminList"> <i class="fa fa-user-o" aria-hidden="true"></i> <span>后台人員</span> </router-link> </li> <li> <router-link to="/backIndex/studentList"> <i class="fa fa-user-circle-o" aria-hidden="true"></i> <span>學員管理</span> </router-link> </li> <li> <router-link to="/backIndex/courseList"> <i class="fa fa-book" aria-hidden="true"></i> <span>課程管理</span> </router-link> </li> </ul> </div> <div class="content"> <ul class="breadcrumb"> <li><a href="#/backIndex/">首頁</a></li> <li>{{pageTitle}}</li> </ul> <router-view></router-view> </div> </div> </template> <script> var pageTitleObj = { indexContent:"網站首頁", adminList:"后台人員", studentList:"學員管理", courseList:"課程管理", courseEdit:"課程編輯" }; export default { name: 'backlogin', data () { return { search_box_fouce:false, showExit:false, pageTitle: pageTitleObj[ this.$route.path.substr( this.$route.path.lastIndexOf("/")+1 ) ] || "網站首頁" } }, methods:{ focusFn(){ //搜索框獲取焦點,添加class this.search_box_fouce = true; }, blurFn(){ //搜索框失去焦點,去掉class this.search_box_fouce = false; }, toggleSlide(){ //這個是用來顯示和隱藏頭像旁的退出下拉框 this.showExit = !this.showExit }, logout(){ //退出系統 } }, watch:{ //監控路徑變化 當路徑發送變化的時候,改變面包屑導航的顯示 $route: { handler: function (val, oldVal) { var path = val.path; this.pageTitle = pageTitleObj[ path.substr( path.lastIndexOf("/")+1 ) ] || "網站首頁"; } } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> ul, li{ list-style: none; } .header{ height: 60px; box-shadow: 0 1px 5px rgba(13,62,73,0.2) ; background: #fff; margin-left: 80px; min-width: 740px; } .sidenav_box{ width: 80px; box-shadow: 0 1px 5px rgba(13,62,73,0.2) ; position: fixed; left: 0; top: 0; bottom: 0; background: #fff; z-index: 99; } .sidenav_box .logo{ width: 46px; margin: 20px 0 0 17px; } .sidenav{ margin-top: 30px; } .sidenav li{ margin-bottom: 20px; } .sidenav a{ display: block; width: 56px; height: 56px; margin: 0 auto; position: relative; cursor: pointer; opacity: 0.6; transition:all 0.5s ease; text-decoration: none; } .sidenav a i{ font-size: 20px; line-height: 56px; text-align: center; display: block; color: #566a80; } .sidenav a:hover{ background: #f0f2f5; opacity: 1; } .sidenav a span{ position: absolute; left: 55px; top: 22px; background: #000; color: #fff; width: 0px; padding: 5px 0; border-radius: 3px; font-size: 12px; opacity: 0; } .sidenav a:hover span{ opacity: 1; left: 65px; width: 60px; padding: 5px 20px; transition:none 0.5s ease-out; transition-property: opacity,left; } .sidenav a span:after{ content: ""; position: absolute; top: 8px; left: -10px; border:5px solid transparent; border-right-color: #000; } .sidenav .router-link-active:after{ content: ""; position: absolute; left: -16px; top: 8px; height: 40px; width: 8px; border-radius: 3px; background: #566a80; } .sidenav .router-link-active{ opacity: 1; background: #f0f2f5; } /*頂部欄*/ .search_box{ color: #979fa8; padding-top: 20px; float: left; } .search_box i{ margin: 0 12px 0 70px; transition: all 0.5s ease; } .search_box input{ border:none; outline: none; } .search_box_fouce i{ margin-left: 55px; color: #2C3D50; } .handler > *{ float: right; margin-right: 20px; cursor: pointer; } .handler .more{ font-size: 20px; color: #566A80; margin: 15px 30px 0 0; position: relative; } .handler .more:hover{ color: #2C3D50; } .handler .more ul{ font-size: 14px; position: absolute; right: 0; top: 55px; width: 120px; box-shadow: 0 1px 5px rgba(13,62,73,0.2); transition: all 0.3s ease-out; height: 0; opacity: 0; overflow: hidden; text-align: center; } .handler .more .showul{ height: auto; top: 45px; opacity: 1; border-top: 1px solid #979FA8; } .handler .more a{ display: block; padding: 8px 10px; background: #fff; color: #566A80; text-decoration: none; } .handler .more a:hover{ background: #f8f9fb; } .handler > img{ width: 50px; border-radius: 50%; margin-top: 5px; margin-right: 30px; } .content{ margin: 20px 30px 0px 100px; min-height: 300px; min-width: 700px; } .breadcrumb{ border-radius: 4px; padding: 10px 15px; background: #fff; } .breadcrumb > li{ display: inline-block; color: #777777; } .breadcrumb > li+li:before{ padding: 0 5px; color: #ccc; content: "/\00a0"; } .breadcrumb > li > a{ color: #32475f; text-decoration: none; } .main{ border-radius: 4px; background: #fff; margin-top: 10px; } </style>
然后在地址欄輸入 http://localhost:8080/#/backIndex 就可以看到首頁框架的效果了。 (這時候內部頁面還沒有,所以點擊左側導航會找不到頁面,先不要點)
常見錯誤: 如果前面的圖片路徑沒放對,就會出現這個錯誤
然后我們繼續將所有的路由配置其他頁面的路由
一共有 課程列表 / 編輯課程 / 首頁統計 / 后台用戶 / 學員用戶 這些二級頁面,我們都加入到 路由文件 index.js中
import Vue from 'vue' import Router from 'vue-router' import Login from '@/components/login' import backIndex from '@/components/backIndex' import courseList from '@/components/courseList' import indexContent from '@/components/indexContent' import adminList from '@/components/adminList' import studentList from '@/components/studentList' import courseEdit from '@/components/courseEdit' Vue.use(Router) export default new Router({ routes: [ { path: '/', name: 'Login', component: Login },{ path: '/backIndex', //首頁框架 name: 'backIndex', component: backIndex, children:[ { path: 'courseList', //課程列表 component: courseList },{ path: 'courseEdit/:sysId', //編輯課程 component: courseEdit },{ path: 'indexContent', //首頁統計 component: indexContent },{ path: 'adminList', //后台用戶 component: adminList },{ path: 'studentList', //學員用戶 component: studentList },{ path: '*', //其他路徑都跳轉到首頁 redirect: 'indexContent' } ] } ] })
添加這些路由后的需要建立對應的 vue 組件文件,在componets中建立上面路由對應的空vue文件
這些新建的vue文件中需要先放入一個空的template標簽,以免報錯
<template> </template>
再刷新頁面的時候,左側導航就可以點擊了(這些router-link對應的路徑和路由對應的path一致),可以看到面包屑導航對應的名字就修改了(這個修改的邏輯在backIndex.vue的97行左右的 watch方法中)。
首頁統計頁面
然后我們為 indexContent.vue 添加中間顯示的統計圖表,代碼在后面
代碼如下:由於前台頁面的統計還沒開始寫,當下我們就只放一個靜態頁面在此,
canvas繪制提標使用的是原生js寫的,具體的原理步驟請查看我以前的博客 http://www.cnblogs.com/chengduxiaoc/p/7678967.html
<template> <div class="indexContent main"> <h4>最新數據</h4> <ul class="number"> <li> <div class="title">今日訪問</div> <p>12000</p> <a href="javascript:;">查看詳情<i class="fa fa-angle-right" aria-hidden="true"></i></a> </li> <li> <div class="title">學員總數</div> <p>3000000</p> <a href="javascript:;">查看詳情<i class="fa fa-angle-right" aria-hidden="true"></i></a> </li> <li> <div class="title">在學人數</div> <p>2000</p> <a href="javascript:;">查看詳情<i class="fa fa-angle-right" aria-hidden="true"></i></a> </li> </ul> <canvas id="barChart" height="400" width="600" style="margin:10px 0"> 你的瀏覽器不支持HTML5 canvas </canvas> </div> </template> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .main{ border-radius: 4px; background: #fff; margin-top: 10px; overflow: hidden; } .main > h4{ color: #51555a; padding:10px; border-bottom: 1px solid #DFE3EA; } .number{ width: 30%; float: right; margin-right: 10%; margin-top: 10px; color: #566A80; } .number li{ padding: 20px; border-top:1px solid #F0F2F5; } .number li:first-child{ border: none 0; } .number p{ font-size: 20px; font-family: arial; margin: 10px 0; } .number a{ text-decoration: none; color: #4187db; font-size: 12px; } .number li:hover{ color: #173859; } .number a:hover{ } .number i{ transition: all 0.3s ease-out; padding-left: 10px; } .number a:hover i{ padding-left: 20px; } .number:hover li{ border-color:#DFE3EA } canvas{ max-width: 55%; min-width: 45%; } </style> <script> export default { name: 'indexContent', data () { return { } }, methods:{ }, mounted:function(){ var chartData = [["2017/01", 50], ["2017/02", 60], ["2017/03", 100], ["2017/04",200], ["2017/05",350], ["2017/06",600]]; goBarChart(chartData); } } function goBarChart(dataArr){ // 聲明所需變量 var canvas,ctx; // 圖表屬性 var cWidth, cHeight, cMargin, cSpace; var originX, originY; // 折線圖屬性 var tobalDots, dotSpace, maxValue; var totalYNomber; // 運動相關變量 var ctr, numctr, speed; // 獲得canvas上下文 canvas = document.getElementById("barChart"); if(canvas && canvas.getContext){ ctx = canvas.getContext("2d"); } initChart(); // 圖表初始化 drawLineLabelMarkers(); // 繪制圖表軸、標簽和標記 drawBarAnimate(); // 繪制折線圖的動畫 //點擊刷新圖表 canvas.onclick = function(){ initChart(); // 圖表初始化 drawLineLabelMarkers(); // 繪制圖表軸、標簽和標記 drawBarAnimate(); // 繪制折線圖的動畫 }; // 圖表初始化 function initChart(){ // 圖表信息 cMargin = 60; cSpace = 80; canvas.width = Math.floor( (window.innerWidth-100)/2 ) * 2 ; canvas.height = 740; canvas.style.height = canvas.height/2 + "px"; canvas.style.width = canvas.width/2 + "px"; cHeight = canvas.height - cMargin - cSpace; cWidth = canvas.width - cMargin - cSpace; originX = cMargin + cSpace; originY = cMargin + cHeight; // 折線圖信息 tobalDots = dataArr.length; dotSpace = parseInt( cWidth/tobalDots ); maxValue = 0; for(var i=0; i<dataArr.length; i++){ var dotVal = parseInt( dataArr[i][1] ); if( dotVal > maxValue ){ maxValue = dotVal; } } maxValue += 50; totalYNomber = 10; // 運動相關 ctr = 1; numctr = 100; speed = 6; ctx.translate(0.5,0.5); // 當只繪制1像素的線的時候,坐標點需要偏移,這樣才能畫出1像素實線 } // 繪制圖表軸、標簽和標記 function drawLineLabelMarkers(){ ctx.font = "24px Arial"; ctx.lineWidth = 2; ctx.fillStyle = "#566a80"; ctx.strokeStyle = "#566a80"; // y軸 drawLine(originX, originY, originX, cMargin); // x軸 drawLine(originX, originY, originX+cWidth, originY); // 繪制標記 drawMarkers(); } // 畫線的方法 function drawLine(x, y, X, Y){ ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(X, Y); ctx.stroke(); ctx.closePath(); } // 繪制標記 function drawMarkers(){ ctx.strokeStyle = "#E0E0E0"; // 繪制 y 軸 及中間橫線 var oneVal = parseInt(maxValue/totalYNomber); ctx.textAlign = "right"; for(var i=0; i<=totalYNomber; i++){ var markerVal = i*oneVal; var xMarker = originX-5; var yMarker = parseInt( cHeight*(1-markerVal/maxValue) ) + cMargin; //console.log(xMarker, yMarker+3,markerVal/maxValue,originY); ctx.fillText(markerVal, xMarker, yMarker+3, cSpace); // 文字 if(i>0){ drawLine(originX+2, yMarker, originX+cWidth, yMarker); } } // 繪制 x 軸 及中間豎線 ctx.textAlign = "center"; for(var i=0; i<tobalDots; i++){ var markerVal = dataArr[i][0]; var xMarker = originX+i*dotSpace; var yMarker = originY+30; ctx.fillText(markerVal, xMarker, yMarker, cSpace); // 文字 if(i>0){ drawLine(xMarker, originY-2, xMarker, cMargin ); } } // 繪制標題 y ctx.save(); ctx.rotate(-Math.PI/2); ctx.fillText("訪問量", -canvas.height/2, cSpace-10); ctx.restore(); // 繪制標題 x ctx.fillText("月份", originX+cWidth/2, originY+cSpace/2+20); }; //繪制折線圖 function drawBarAnimate(){ ctx.strokeStyle = "#566a80"; //"#49FE79"; //連線 ctx.beginPath(); for(var i=0; i<tobalDots; i++){ var dotVal = dataArr[i][1]; var barH = parseInt( cHeight*dotVal/maxValue* ctr/numctr );// var y = originY - barH; var x = originX + dotSpace*i; if(i==0){ ctx.moveTo( x, y ); }else{ ctx.lineTo( x, y ); } } ctx.stroke(); //背景 ctx.lineTo( originX+dotSpace*(tobalDots-1), originY); ctx.lineTo( originX, originY); //背景漸變色 //柱狀圖漸變色 var gradient = ctx.createLinearGradient(0, 0, 0, 300); gradient.addColorStop(0, 'rgba(133,171,212,0.6)'); gradient.addColorStop(1, 'rgba(133,171,212,0.1)'); ctx.fillStyle = gradient; ctx.fill(); ctx.closePath(); ctx.fillStyle = "#566a80"; //繪制點 for(var i=0; i<tobalDots; i++){ var dotVal = dataArr[i][1]; var barH = parseInt( cHeight*dotVal/maxValue * ctr/numctr ); var y = originY - barH; var x = originX + dotSpace*i; drawArc( x, y ); //繪制點 ctx.fillText(parseInt(dotVal*ctr/numctr), x+15, y-8); // 文字 } if(ctr<numctr){ ctr++; setTimeout(function(){ ctx.clearRect(0,0,canvas.width, canvas.height); drawLineLabelMarkers(); drawBarAnimate(); }, speed); } } //繪制圓點 function drawArc( x, y, X, Y ){ ctx.beginPath(); ctx.arc( x, y, 3, 0, Math.PI*2 ); ctx.fill(); ctx.closePath(); } } </script>
然后我們可以看到,首頁的效果就出來啦
登錄功能完善:
當登錄請求完成以后,如果出錯,就彈出錯誤(我們本項目沒有封裝模態框,就直接用alert吧),如果正確,就跳轉到首頁
修改后的 login.vue中的 ajax請求代碼如下:
this.$reqs.post("/users/login",{ username:this.username, password:this.password }).then(function(result){ //成功 if(result.data.err){ alert(result.data.err); }else{ _this.$router.push({path:'/backIndex/indexContent'}); } _this.disablebtn = false; _this.loginText = "登錄"; }).catch(function (error) { //失敗 _this.disablebtn = false; _this.loginText = "登錄" });
注:我們通過 router.push去修改url,作用和原生js的 window.location.href基本一致。
退出系統
功能點:點擊退出,實現退出功能
在backIndex.vue中我們有一個退出登錄的空方法,我們在里面寫退出登錄的請求的代碼,退出成功后跳轉到根目錄(以就算登錄頁面)
logout(){ //退出系統 var _this = this; this.$reqs.post("/users/logout",{ }).then(function(result){ //成功 _this.$router.push({path:'/'}); }).catch(function (error) { //失敗 console.log(error) }); }
然后在后台寫接口,在user.js中 登錄的方法后面寫(修改完成后需要重啟node服務)
注:這里直接清除登錄中設置的 session 就可以了,(我們后面會對所有的請求設置攔截,如果session中的用戶信息沒有,再提示用戶未登錄,跳轉到登錄頁面就可以了)
//退出 router.post('/logout', function(req, res, next) { req.session.username = ""; //清除session req.session.password = ""; res.end('{"success":"true"}'); });
到此,我們就實現了登錄,顯示首頁,退出的基本功能
好啦,今天就講到這里。下一篇將講解 用戶添加/修改/刪除,表格分頁
關注公眾號,博客更新即可收到推送