From fc6ae600a4392e75730f8fc7d8a66d1c268026f0 Mon Sep 17 00:00:00 2001 From: xk_guohonglei <2057494155@qq.com> Date: Tue, 1 Sep 2020 15:30:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=AF=86=E6=9C=89=E9=97=AE=E6=8F=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 2 +- package-lock.json | 5 +++++ package.json | 1 + utils/encode.js | 51 +++++++++++++++++++++++++++++------------------ utils/request.js | 45 ++++++++++++++++++++++------------------- 5 files changed, 63 insertions(+), 41 deletions(-) diff --git a/App.vue b/App.vue index f315d6f..d3ee885 100644 --- a/App.vue +++ b/App.vue @@ -32,7 +32,7 @@ import utils from '@/utils/encode' console.log('App Show') const JSESSIONID = utils.uuid() console.log(JSESSIONID) - console.log(utils.bcrypt(JSESSIONID)) + // console.log(utils.bcrypt(JSESSIONID)) uni.getLocation({ type: 'wgs84', success: function (res) { diff --git a/package-lock.json b/package-lock.json index b98ba03..24bc608 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53,6 +53,11 @@ "resolved": "https://registry.npm.taobao.org/js-md5/download/js-md5-0.7.3.tgz", "integrity": "sha1-tPL7sLMnRV9ZjWcn447Ccs0Jw/I=" }, + "jsencrypt": { + "version": "3.0.0-rc.1", + "resolved": "https://registry.npm.taobao.org/jsencrypt/download/jsencrypt-3.0.0-rc.1.tgz", + "integrity": "sha1-DgpHRLpDzFV/tc9i/oZGvOtWGxw=" + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npm.taobao.org/ms/download/ms-2.0.0.tgz?cache=0&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fms%2Fdownload%2Fms-2.0.0.tgz", diff --git a/package.json b/package.json index 9b40994..54813bf 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "js-base64": "2.6.1", "js-cookie": "2.2.1", "js-md5": "0.7.3", + "jsencrypt": "^3.0.0-rc.1", "react-native-bcrypt": "^2.4.0", "tki-qrcode": "^0.1.6" } diff --git a/utils/encode.js b/utils/encode.js index 66b7bb0..834d6ae 100644 --- a/utils/encode.js +++ b/utils/encode.js @@ -1,20 +1,9 @@ import CryptoJS from 'crypto-js' import md5 from 'js-md5' -// import dcodeIO from 'bcryptjs' -require.config({ - paths: { - "bcrypt": "../node_modules/react-native-bcrypt/dist/bcrypt.js" - } -}); -require(["bcrypt"], function(bcrypt) { - console.log(bcrypt) -}) -let Base64 = require('js-base64').Base64 -// var bcrypt = dcodeIO.bcrypt; +// let Base64 = require('js-base64').Base64 +// import { JSEncrypt } from 'jsencrypt' var keyStr = 'qDfajQ*v@W1mCruZ' -// var dcodeIO = bcrypt.setRandomFallback -// var bcrypt = dcodeIO.bcrypt export default { /** @@ -32,14 +21,43 @@ export default { }, decrypt(word) { // 解密 var key = CryptoJS.enc.Utf8.parse(keyStr) + // var srcs = CryptoJS.enc.Utf8.parse(word) + if(word) { + word = word.replace(/[\r\n]/g,"") + } var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) return CryptoJS.enc.Utf8.stringify(decrypt).toString() }, + // decrypt (word) { + // var key = CryptoJS.enc.Utf8.parse(keyStr) + // var encryptedHexStr = CryptoJS.enc.Hex.parse(word); + // var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); + // var decrypt = CryptoJS.AES.decrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) + // return decrypt.toString(CryptoJS.enc.Utf8).toString(); + // }, + // encrypt(data) { + // // 新建JSEncrypt对象 + // let encryptor = new JSEncrypt(); + // // 设置公钥 + // encryptor.setPublicKey(keyStr); + // // 加密数据 + // return encryptor.encrypt(data); + // }, + // // 解密 + // decrypt(data) { + // // 新建JSEncrypt对象 + // let decrypt = new JSEncrypt(); + // // 设置私钥 + // decrypt.setPrivateKey(keyStr); + // // 解密数据 + // return decrypt.decrypt(secretWord); + // }, md5Salt(str) { - return Base64.encode(md5(str + 'Do&9hY%l8e')) + console.log('hahahahaha',str, str + "Do&9hY%l8e", md5(str + 'Do&9hY%l8e')) + return md5(str + 'Do&9hY%l8e') }, md5NoSalt(str) { return md5(str) @@ -56,11 +74,6 @@ export default { var uuid = s.join('') return uuid - }, - bcrypt(str) { - var salt = bcrypt.genSaltSync(10) // 定义密码加密的计算强度,默认10 - var hash = bcrypt.hashSync(this.md5Salt(str), salt) // 把要加密的内容带进去,变量hash就是加密后的密码 - return hash } } diff --git a/utils/request.js b/utils/request.js index 4797e41..38fafbc 100644 --- a/utils/request.js +++ b/utils/request.js @@ -20,25 +20,28 @@ service.interceptors.request.use( config.headers['Authorization'] = token config.headers['device'] = openid - config.headers['dataSource'] = 'MP' + config.headers['dataSources'] = 'MP' - if (env === 'production') { + // if (env === 'production') { // 生产环境,加密,不输出任何东西 // 设置jsessionid和token const JSESSIONID = utils.uuid() config.headers['JSESSIONID'] = JSESSIONID - config.headers['token'] = utils.bcrypt(JSESSIONID) - if (!notEncrypt && encryptWhite.indexOf(config.url) < 0) { + config.headers['token'] = utils.md5Salt(JSESSIONID) + // if ( encryptWhite.indexOf(config.url) < 0) { + // if (!notEncrypt && encryptWhite.indexOf(config.url) < 0) { + // 这是定义在哪的 const data = { // 用于存储加密 params: '' // 加密后的密文 } // 要加密 data.params = utils.encrypt(JSON.stringify(config.data)) + console.log(data.params) config.data = data - } - } else { + // } + // } else { console.log('请求路径', config.url, '参数加密前', config.data) - } + // } return config }, @@ -57,17 +60,17 @@ service.interceptors.response.use( const res = response.data const env = process.env.NODE_ENV - if (env === 'production') { + // if (env === 'production') { // 生产环境,进行加密解密,不输出日志 - if (res.encrypt === 1) { + // if (res.encrypt === 1) { // 加密的数据,需要解密 const dataParam = JSON.parse(utils.decrypt(res.data)) res.data = JSON.stringify(dataParam) === '{}' ? null : dataParam - } - } else { + // } + // } else { console.log('请求路径', response.config.url, '返回结果未加密', res) console.log('-------------------------------------------') - } + // } uni.hideLoading() if (res.code != 20000) { @@ -77,15 +80,15 @@ service.interceptors.response.use( }) if(res.code===42011){ console.log(res.code) - uni.navigateTo({ - url:'/pages/login/login', - success: () => { - console.log('跳转成功') - }, - fail: (err) => { - console.log(err) - } - }) + // uni.navigateTo({ + // url:'/pages/login/login', + // success: () => { + // console.log('跳转成功') + // }, + // fail: (err) => { + // console.log(err) + // } + // }) } } return res