MD5, js-sha1加密,js-cookie 使用

顾靖
2023-12-01
 

第一: js-md5

npm install --save js-md5

方法一:在需要的项目文件中引入

import md5 from 'js-md5';

使用例子:md5('hello world')

方法二:全局引入,在main.js文件中将md5转换成vue原型

import md5 from 'js-md5';

Vue.prototype.$md5 = md5;

使用例子:

this.$md5('hello world')

第二:js-sha1

npm install js-sha1

sha1 = require('js-sha1') || import sha1 from 'js-sha1';

const reqData = {

username:this.form.username,

newPassword:sha1(this.form.newPassword),

oldPassword:sha1(this.form.oldPassword),

code:this.form.code

}

第三:js-cookie

npm install js-cookie --save

import Cookie from 'js-cookie'

Cookie.set(‘token',token)

Cookie.get('token')

实例:

import Cookies from "js-cookie";

// 变量

const tokenKey = "tokenAdmin";

const userNameKey = "username";

// 获取token

export function getToken(){ return Cookies.get(tokenKey); }

// 写入token

export function setToken(value){ return Cookies.set(tokenKey, value,{expires:8}); }

// 删除token

export function removeToken(){ return Cookies.remove(tokenKey); }

// 写入userName

export function setUsername(value){ return Cookies.set(userNameKey, value, {expires:7}); }

// 获取userName

export function getUsername(){ return Cookies.get(userNameKey); }

// 删除userName

export function removeUsername(){ return Cookies.remove(userNameKey); }

import { removeToken, removeUsername,setToken, setUsername} from "@/utils/cookies"

setToken(reqData.code);//设置setToken

setUsername(reqData.username);//设置

export default {

name:'User',

data(){

return{}

},

methods:{

logout(){

removeToken(); //删除token

removeUsername();//删除userName

}

}

}

 类似资料: