改版登录接口

master
liaoanqi 4 years ago
parent e2293553e7
commit bc616d3a6a

@ -8,22 +8,22 @@
onShow: function() { onShow: function() {
// //
var ua = navigator.userAgent.toLowerCase(); var ua = navigator.userAgent.toLowerCase();
if (ua.search(/MicroMessenger/i) > -1 && !uni.getStorageSync('appType')) { // if (ua.search(/MicroMessenger/i) > -1 && !uni.getStorageSync('appType')) {
// // //
uni.setStorageSync('appType', 2) // uni.setStorageSync('appType', 2)
http.mpAuthLogin() // http.mpAuthLogin()
} // }
const state = util.getUrlKey('state') // const state = util.getUrlKey('state')
const code = util.getUrlKey('code') // const code = util.getUrlKey('code')
if ((state == 'needCode' || state == 'unNeedCode') && code) { // if ((state == 'needCode' || state == 'unNeedCode') && code) {
let path = window.location.href // let path = window.location.href
if (path.indexOf('code=') > 0 && path.indexOf('&state=unNeedCode') > -1) { // if (path.indexOf('code=') > 0 && path.indexOf('&state=unNeedCode') > -1) {
http.mpLogin(null, code) // http.mpLogin(null, code)
path = path.substring(0, path.indexOf('code=') - 1) // path = path.substring(0, path.indexOf('code=') - 1)
history.replaceState({}, '', path) // history.replaceState({}, '', path)
} // }
http.getCartCount() http.getCartCount()
} // }
}, },
globalData: { globalData: {
// //

@ -14,7 +14,10 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "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"
} }
} }

@ -33,6 +33,7 @@
<script> <script>
var http = require("../../utils/http"); var http = require("../../utils/http");
var util = require('../../utils/util.js'); var util = require('../../utils/util.js');
import { encrypt } from '../../utils/crypto.js'
import { import {
AppType AppType
} from '../../utils/constant.js' } from '../../utils/constant.js'
@ -156,24 +157,18 @@
}) })
// #ifdef H5 || APP-PLUS // #ifdef H5 || APP-PLUS
var params = { var params = {
url: "/webLogin", url: "/login",
method: "post", method: "post",
data: { data: {
"principal": this.principal, "userName": this.principal,
"credentials": this.credentials "passWord": encrypt(this.credentials)
}, },
callBack: res => { callBack: res => {
console.log("login",res)
// var loginResult = '';
// uni.setStorageSync("loginResult",res);
// uni.setStorageSync('token', 'bearer' + res.access_token);
// return
http.loginSuccess(res, () => { http.loginSuccess(res, () => {
uni.showToast({ uni.showToast({
title: "登录成功", title: "登录成功",
icon: 'none', icon: 'none',
complete: () => { complete: () => {
// this.$Router.pushTab('/pages/index/index')
setTimeout(() => { setTimeout(() => {
wx.switchTab({ wx.switchTab({
url: '/pages/index/index' url: '/pages/index/index'
@ -188,25 +183,25 @@
http.request(params) http.request(params)
// #endif // #endif
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
wx.login({ // wx.login({
success: (res) => { // success: (res) => {
var params = { // var params = {
url: "/login", // url: "/login",
method: "post", // method: "post",
data: { // data: {
appType: 1, // appType: 1,
credentials: this.credentials, // credentials: this.credentials,
loginType: 0, // loginType: 0,
principal: this.principal + ':' + res.code // principal: this.principal + ':' + res.code
}, // },
callBack: result => { // callBack: result => {
http.loginSuccess(result) // http.loginSuccess(result)
this.$Router.pushTab('/pages/index/index') // this.$Router.pushTab('/pages/index/index')
}, // },
} // }
http.request(params) // http.request(params)
}, // },
}) // })
// #endif // #endif
} }
}, },

@ -39,6 +39,7 @@
// pages/register/register.js // pages/register/register.js
var http = require("../../utils/http"); var http = require("../../utils/http");
var util = require("../../utils/util.js"); var util = require("../../utils/util.js");
import { encrypt } from '../../utils/crypto.js'
export default { export default {
data() { data() {
@ -143,11 +144,10 @@
data: { data: {
// appType: 1, // appType: 1,
// 1 2 3 PC 4 H5 // 1 2 3 PC 4 H5
userMail: this.principal, userName: this.principal,
password: this.credentials, passWord: encrypt(this.credentials),
}, },
callBack: res => { callBack: res => {
console.log("1111",res)
uni.hideLoading(); uni.hideLoading();
uni.showToast({ uni.showToast({
title: "注册成功,请登录", title: "注册成功,请登录",

@ -20,7 +20,7 @@
<view class="userinfo-con"> <view class="userinfo-con">
<view class="userinfo-avatar"> <view class="userinfo-avatar">
<!-- <open-data type="userAvatarUrl"></open-data> --> <!-- <open-data type="userAvatarUrl"></open-data> -->
<image :src="loginResult.pic?loginResult.pic:'../../static/images/icon/head04.png'"></image> <image :src="loginResult.pic ? (loginResult.pic.indexOf('http') === -1 ? picDomain + loginResult.pic : loginResult.pic) : '../../static/images/icon/head04.png'"></image>
</view> </view>
<view class="userinfo-name"> <view class="userinfo-name">
<view>{{loginResult.nickName ? loginResult.nickName : "用户昵称"}}</view> <view>{{loginResult.nickName ? loginResult.nickName : "用户昵称"}}</view>
@ -139,6 +139,7 @@
// pages/user/user.js // pages/user/user.js
var http = require("../../utils/http.js"); var http = require("../../utils/http.js");
var util = require("../../utils/util.js"); var util = require("../../utils/util.js");
var config = require("../../utils/config.js");
export default { export default {
data() { data() {
@ -148,6 +149,7 @@
collectionCount: 0, collectionCount: 0,
isAuthInfo: false, isAuthInfo: false,
loginResult: '', loginResult: '',
picDomain: config.picDomain
}; };
}, },
@ -170,7 +172,7 @@
onShow: function() { onShow: function() {
// //
var ths = this; // var status = ths.data.status var ths = this; // var status = ths.data.status
console.log(uni.getStorageSync("loginResult"))
ths.setData({ ths.setData({
loginResult: uni.getStorageSync("loginResult"), loginResult: uni.getStorageSync("loginResult"),
// isAuthInfo: Boolean(wx.getStorageSync('loginResult').userId), // isAuthInfo: Boolean(wx.getStorageSync('loginResult').userId),
@ -321,8 +323,8 @@
logout: function() { logout: function() {
// 退 // 退
http.request({ http.request({
url: '/p/user/logout', url: '/logOut',
method: 'GET', method: 'post',
callBack: res => { callBack: res => {
util.removeTabBadge() util.removeTabBadge()

@ -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()
}

@ -19,9 +19,9 @@ function request(params, isGetTonken) {
params.data = params.data + ''; params.data = params.data + '';
} }
var needToken = false var needToken = false
if (params.url.indexOf("/p/") == 0 || params.url.indexOf("/user/registerOrBindUser") == 0) { // if (params.url.indexOf("/p/") == 0 || params.url.indexOf("/user/registerOrBindUser") == 0) {
needToken = true // needToken = true
} // }
wx.request({ wx.request({
// url: config.domain + params.url, // url: config.domain + params.url,
@ -31,7 +31,7 @@ function request(params, isGetTonken) {
header: { header: {
// 'content-type': params.method == "GET" ? 'application/x-www-form-urlencoded' : 'application/json;charset=utf-8', // 'content-type': params.method == "GET" ? 'application/x-www-form-urlencoded' : 'application/json;charset=utf-8',
// 'Authorization': params.login ? undefined : uni.getStorageSync('token') // '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, method: params.method == undefined ? "POST" : params.method,
dataType: 'json', dataType: 'json',
@ -247,24 +247,24 @@ var getToken = function (fn) {
* @param {Object} fn 登录成功后的回调 * @param {Object} fn 登录成功后的回调
*/ */
function loginSuccess (result, fn) { function loginSuccess (result, fn) {
if (!result.enabled) { // if (!result.enabled) {
uni.showModal({ // uni.showModal({
showCancel: false, // showCancel: false,
title: "提示", // title: "提示",
content: "您已被禁用,不能购买,请联系客服", // content: "您已被禁用,不能购买,请联系客服",
cancelText: "取消", // cancelText: "取消",
confirmText: "确定", // confirmText: "确定",
success: function (res) { // success: function (res) {
if (res.confirm) { // if (res.confirm) {
uni.switchTab({ // wx.switchTab({
url: '/pages/index/index' // url: '/pages/index/index'
}); // });
} // }
} // }
}) // })
wx.setStorageSync('token', ''); // wx.setStorageSync('token', '');
return // return
} // }
// 保存登陆信息 // 保存登陆信息
wx.setStorageSync('loginResult', result) wx.setStorageSync('loginResult', result)
@ -274,20 +274,52 @@ function loginSuccess (result, fn) {
// if (!result.pic) { // if (!result.pic) {
// updateUserInfo(); // updateUserInfo();
// } // }
const expiresTimeStamp = result.expiresIn * 1000 / 2 + new Date().getTime()
wx.setStorageSync('token', 'bearer' + result.access_token); //把token存入缓存请求接口数据时要用 // 缓存token的过期时间
uni.setStorageSync('expiresTimeStamp', expiresTimeStamp)
if (result.userId) {
wx.setStorageSync('hadBindUser', true); wx.setStorageSync('token', result.accessToken); //把token存入缓存请求接口数据时要用
getCartCount()
} else { // const routeUrlAfterLogin = uni.getStorageSync('routeUrlAfterLogin')
wx.setStorageSync('hadBindUser', false); // const pages = getCurrentPages()
} // if (pages.length === 1) {
// var globalData = getApp().globalData; // uni.reLaunch({
// globalData.isLanding = false; // url: routeUrlAfterLogin
// while (globalData.requestQueue.length) { // })
// request(globalData.requestQueue.pop()); // 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) { if (fn) {
fn() fn()

Loading…
Cancel
Save