Skip to content

Commit

Permalink
改版登录接口
Browse files Browse the repository at this point in the history
  • Loading branch information
412102420ts committed Mar 31, 2022
1 parent b918fad commit 1c85f4c
Show file tree
Hide file tree
Showing 19 changed files with 1,418 additions and 108 deletions.
30 changes: 15 additions & 15 deletions mall4uni/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
onShow: function() {
// 判断浏览器环境
var ua = navigator.userAgent.toLowerCase();
if (ua.search(/MicroMessenger/i) > -1 && !uni.getStorageSync('appType')) {
// 微信环境
uni.setStorageSync('appType', 2)
http.mpAuthLogin()
}
const state = util.getUrlKey('state')
const code = util.getUrlKey('code')
if ((state == 'needCode' || state == 'unNeedCode') && code) {
let path = window.location.href
if (path.indexOf('code=') > 0 && path.indexOf('&state=unNeedCode') > -1) {
http.mpLogin(null, code)
path = path.substring(0, path.indexOf('code=') - 1)
history.replaceState({}, '', path)
}
// if (ua.search(/MicroMessenger/i) > -1 && !uni.getStorageSync('appType')) {
// // 微信环境
// uni.setStorageSync('appType', 2)
// http.mpAuthLogin()
// }
// const state = util.getUrlKey('state')
// const code = util.getUrlKey('code')
// if ((state == 'needCode' || state == 'unNeedCode') && code) {
// let path = window.location.href
// if (path.indexOf('code=') > 0 && path.indexOf('&state=unNeedCode') > -1) {
// http.mpLogin(null, code)
// path = path.substring(0, path.indexOf('code=') - 1)
// history.replaceState({}, '', path)
// }
http.getCartCount()
}
// }
},
globalData: {
// 定义全局请求队列
Expand Down
7 changes: 5 additions & 2 deletions mall4uni/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"author": "",
"license": "ISC",
"devDependencies": {
"uni-read-pages": "^1.0.5",
"query-string": "^6.13.1"
"query-string": "^6.13.1",
"uni-read-pages": "^1.0.5"
},
"dependencies": {
"crypto-js": "^4.1.1"
}
}
51 changes: 23 additions & 28 deletions mall4uni/pages/accountLogin/accountLogin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<script>
var http = require("../../utils/http");
var util = require('../../utils/util.js');
import { encrypt } from '../../utils/crypto.js'
import {
AppType
} from '../../utils/constant.js'
Expand Down Expand Up @@ -156,24 +157,18 @@
})
// #ifdef H5 || APP-PLUS
var params = {
url: "/webLogin",
url: "/login",
method: "post",
data: {
"principal": this.principal,
"credentials": this.credentials
"userName": this.principal,
"passWord": encrypt(this.credentials)
},
callBack: res => {
console.log("login",res)
// var loginResult = '';
// uni.setStorageSync("loginResult",res);
// uni.setStorageSync('token', 'bearer' + res.access_token);
// return
http.loginSuccess(res, () => {
uni.showToast({
title: "登录成功",
icon: 'none',
complete: () => {
// this.$Router.pushTab('/pages/index/index')
setTimeout(() => {
wx.switchTab({
url: '/pages/index/index'
Expand All @@ -188,25 +183,25 @@
http.request(params)
// #endif
// #ifdef MP-WEIXIN
wx.login({
success: (res) => {
var params = {
url: "/login",
method: "post",
data: {
appType: 1,
credentials: this.credentials,
loginType: 0,
principal: this.principal + ':' + res.code
},
callBack: result => {
http.loginSuccess(result)
this.$Router.pushTab('/pages/index/index')
},
}
http.request(params)
},
})
// wx.login({
// success: (res) => {
// var params = {
// url: "/login",
// method: "post",
// data: {
// appType: 1,
// credentials: this.credentials,
// loginType: 0,
// principal: this.principal + ':' + res.code
// },
// callBack: result => {
// http.loginSuccess(result)
// this.$Router.pushTab('/pages/index/index')
// },
// }
// http.request(params)
// },
// })
// #endif
}
},
Expand Down
6 changes: 3 additions & 3 deletions mall4uni/pages/register/register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
// pages/register/register.js
var http = require("../../utils/http");
var util = require("../../utils/util.js");
import { encrypt } from '../../utils/crypto.js'
export default {
data() {
Expand Down Expand Up @@ -143,11 +144,10 @@
data: {
// appType: 1,
// 应用类型 1小程序 2微信公众号 3 PC 4 H5
userMail: this.principal,
password: this.credentials,
userName: this.principal,
passWord: encrypt(this.credentials),
},
callBack: res => {
console.log("1111",res)
uni.hideLoading();
uni.showToast({
title: "注册成功,请登录",
Expand Down
6 changes: 3 additions & 3 deletions mall4uni/pages/user/user.vue
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@
logout: function() {
// 请求退出登陆接口
http.request({
url: '/p/user/logout',
method: 'GET',
url: '/logOut',
method: 'post',
callBack: res => {
util.removeTabBadge()
Expand All @@ -336,7 +336,7 @@
title: "退出成功",
icon: "none"
})
this.setData({
orderAmount: ''
});
Expand Down
11 changes: 11 additions & 0 deletions mall4uni/utils/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import CryptoJS from 'crypto-js'
// 加密
const keyStr = '-mall4j-password' // 解密用的key
export function encrypt (word) {
const time = Date.now()

const key = CryptoJS.enc.Utf8.parse(keyStr)
const srcs = CryptoJS.enc.Utf8.parse(time + word) // 加密方式: 时间戳 + 密文
const encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7})
return encrypted.toString()
}
106 changes: 69 additions & 37 deletions mall4uni/utils/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function request(params, isGetTonken) {
params.data = params.data + '';
}
var needToken = false
if (params.url.indexOf("/p/") == 0 || params.url.indexOf("/user/registerOrBindUser") == 0) {
needToken = true
}
// if (params.url.indexOf("/p/") == 0 || params.url.indexOf("/user/registerOrBindUser") == 0) {
// needToken = true
// }

wx.request({
// url: config.domain + params.url,
Expand All @@ -31,7 +31,7 @@ function request(params, isGetTonken) {
header: {
// 'content-type': params.method == "GET" ? 'application/x-www-form-urlencoded' : 'application/json;charset=utf-8',
// 'Authorization': params.login ? undefined : uni.getStorageSync('token')
'Authorization': !needToken ? undefined : uni.getStorageSync('token') || uni.getStorageSync('tempToken'),
'Authorization': uni.getStorageSync('token') ,
},
method: params.method == undefined ? "POST" : params.method,
dataType: 'json',
Expand Down Expand Up @@ -247,24 +247,24 @@ var getToken = function (fn) {
* @param {Object} fn 登录成功后的回调
*/
function loginSuccess (result, fn) {
if (!result.enabled) {
uni.showModal({
showCancel: false,
title: "提示",
content: "您已被禁用,不能购买,请联系客服",
cancelText: "取消",
confirmText: "确定",
success: function (res) {
if (res.confirm) {
wx.switchTab({
url: '/pages/index/index'
});
}
}
})
wx.setStorageSync('token', '');
return
}
// if (!result.enabled) {
// uni.showModal({
// showCancel: false,
// title: "提示",
// content: "您已被禁用,不能购买,请联系客服",
// cancelText: "取消",
// confirmText: "确定",
// success: function (res) {
// if (res.confirm) {
// wx.switchTab({
// url: '/pages/index/index'
// });
// }
// }
// })
// wx.setStorageSync('token', '');
// return
// }

// 保存登陆信息
wx.setStorageSync('loginResult', result)
Expand All @@ -273,21 +273,53 @@ function loginSuccess (result, fn) {
// 没有获取到用户昵称,说明服务器没有保存用户的昵称,也就是用户授权的信息并没有传到服务器
// if (!result.pic) {
// updateUserInfo();
// }

wx.setStorageSync('token', 'bearer' + result.access_token); //把token存入缓存,请求接口数据时要用

if (result.userId) {
wx.setStorageSync('hadBindUser', true);
getCartCount()
} else {
wx.setStorageSync('hadBindUser', false);
}
// var globalData = getApp().globalData;
// globalData.isLanding = false;
// while (globalData.requestQueue.length) {
// request(globalData.requestQueue.pop());
// }
// }
const expiresTimeStamp = result.expiresIn * 1000 / 2 + new Date().getTime()
// 缓存token的过期时间
uni.setStorageSync('expiresTimeStamp', expiresTimeStamp)

wx.setStorageSync('token', result.accessToken); //把token存入缓存,请求接口数据时要用

// const routeUrlAfterLogin = uni.getStorageSync('routeUrlAfterLogin')
// const pages = getCurrentPages()
// if (pages.length === 1) {
// uni.reLaunch({
// url: routeUrlAfterLogin
// })
// uni.removeStorageSync('routeUrlAfterLogin')
// return
// }
// const prevPage = pages[pages.length - 2]
// if (!prevPage) {
// wx.switchTab({
// url: '/pages/index/index'
// });
// return
// }
// // 判断上一页面是否为tabbar页面 (首页和分类页无需登录接口)
// const isTabbar = prevPage.route === 'pages/user/user' || prevPage.route === 'pages/basket/basket'
// if (isTabbar) {
// wx.switchTab({
// url: '/' + prevPage.route
// });
// } else {
// // 非tabbar页面
// let backDelata = 0
// pages.forEach((page, index) => {
// if (page.$page.fullPath === routeUrlAfterLogin) {
// backDelata = pages.length - index - 1
// }
// })
// if (backDelata) {
// uni.navigateBack({
// delta: backDelata
// })
// } else {
// wx.switchTab({
// url: '/pages/index/index'
// });
// }
// }

if (fn) {
fn()
Expand Down
Binary file added mall4v/src/assets/img/default-verify-error.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1c85f4c

Please sign in to comment.