亚洲一区精品自拍_2021年国内精品久久_男同十八禁gv在线观看_免费观看a级性爱黄片

當(dāng)前位置:文章中心>技術(shù)教程
公告通知 新聞快遞 技術(shù)教程 產(chǎn)品展示

Egg.js中復(fù)用靜態(tài)頁面邏輯、局部刷新架構(gòu)、生成驗(yàn)證碼

發(fā)布時(shí)間:2021-11-17 點(diǎn)擊數(shù):758


配置ejs模板引擎


  • 安裝插件

npm i egg-view-js復(fù)制代碼

  • plugin.js中添加下列代碼

module.exports = {  // had enabled by egg  static: {    enable: true  },  ejs: {    enable: true,    package: 'egg-view-ejs'  }}復(fù)制代碼

  • config.default.js中添加下列代碼

const userConfig = {    view: {      mapping: {        '.html': 'ejs'      }    }  }復(fù)制代碼

對(duì)視圖進(jìn)行分組

將所有的頁面都放在view文件夾中是不合理的,所以可以將view文件夾進(jìn)行分組。

復(fù)用靜態(tài)邏輯

  1. 把需要復(fù)用的靜態(tài)頁面抽離到一個(gè)文件夾中。

  2. 在需要復(fù)用的地方,通過下面的方式進(jìn)行引入。

<% include ../public/page_header.html %>復(fù)制代碼

配置局部刷新架構(gòu)

之所以要配置局部刷新架構(gòu),就是希望當(dāng)我們點(diǎn)擊某個(gè)鏈接的時(shí)候,能夠保存原有的狀態(tài),例如瀏覽器導(dǎo)航欄的狀態(tài)。

配置局部刷新的核心在于跳轉(zhuǎn)路由的時(shí)候,通過target指向iframe。

  • 增加角色
  • 復(fù)制代碼


    基于Serverless的Egg.js后臺(tái)管理系統(tǒng)配置Session

    騰訊云的Serverless云函數(shù)如果30分鐘內(nèi)沒有訪問的話就會(huì)銷毀容器,所以如果session要保存30分鐘以上的話,就需要把session存儲(chǔ)到數(shù)據(jù)庫中。

    配置session

    • config.default.js

    config.session = {    key: 'session_id_test',    maxAge: 30*60*1000;    httpOnly: true,    encrypt: true,    renew: true  }復(fù)制代碼

    • 在控制器中設(shè)置session

    this.ctx.session.username = "張三";復(fù)制代碼

    • 在控制器中讀取session

    this.ctx.body = this.ctx.session.username;復(fù)制代碼

    生成圖形驗(yàn)證碼

    1. 安裝依賴

    npm install --save svg-captcha復(fù)制代碼

    1. 在控制器中進(jìn)行下面的定義

    const svgCaptcha = require('svg-captcha');  // 驗(yàn)證碼模塊  async captcha() {    const captcha = svgCaptcha.create({      size: 4,      fontSize: 50,      width: 100,      height: 40,      background: "#cc9966"    });    console.log(captcha.text);    this.ctx.response.type = 'image/svg+xml';    this.ctx.body = captcha.data;  }復(fù)制代碼

    1. 靜態(tài)頁面獲取驗(yàn)證碼

    靜態(tài)驗(yàn)證碼獲取驗(yàn)證碼是通過訪問路由,讓控制器返回圖片來實(shí)現(xiàn)的。

    dd>驗(yàn) 證 碼:復(fù)制代碼

    1. 將驗(yàn)證碼保存在session中

    this.ctx.session.code = captcha.text;復(fù)制代碼

    如果你想復(fù)用上述的驗(yàn)證碼邏輯,可以通過下面的方式進(jìn)行封裝。

    • 首先在app文件夾下創(chuàng)建service文件夾,并在service文件夾下創(chuàng)建tools.js

    'use strict';const Service = require('egg').Service;const svgCaptcha = require('svg-captcha');class ToolsService extends Service {  async getCaptcha() {    const captcha = svgCaptcha.create({      size: 4,      fontSize: 50,      width: 100,      height: 40,      background: "#cc9966"    });    return captcha;  }}module.exports = ToolsService;復(fù)制代碼

    • 在控制器中調(diào)用即可

    async captcha() {    let captcha = await this.service.tools.getCaptcha();    console.log(captcha.text);    // 將驗(yàn)證碼保存在session中    this.ctx.session.code = captcha.text;    this.ctx.response.type = 'image/svg+xml';    this.ctx.body = captcha.data;  }




    版權(quán)聲明:本文內(nèi)容轉(zhuǎn)發(fā)自阿里云社區(qū),由阿里云實(shí)名注冊(cè)用戶自發(fā)貢獻(xiàn)版權(quán)歸原作者所有。本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本中有涉嫌抄襲的內(nèi)容,請(qǐng)聯(lián)系站內(nèi)客服,本將立刻刪除涉嫌侵權(quán)內(nèi)容。