From 6bd5ec12d3620c6cc56a33cc9e27d54ee4e0a25c Mon Sep 17 00:00:00 2001 From: Junling Bu Date: Mon, 26 Mar 2018 22:05:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E9=99=86=E7=9B=B8=E5=85=B3=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E4=BB=8Eutil.js=E9=87=8D=E6=9E=84=E5=88=B0user.js?= =?UTF-8?q?=EF=BC=9B=20=E6=AD=A4=E5=A4=96=E7=94=A8=E6=88=B7=E5=A6=82?= =?UTF-8?q?=E6=9E=9C=E8=AF=B7=E6=B1=82=E5=90=8E=E5=8F=B0=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E7=99=BB=E9=99=86=E6=97=B6=EF=BC=8C=E5=88=99=E8=B7=B3=E8=BD=AC?= =?UTF-8?q?=E5=88=B0=E6=88=91=E7=9A=84=E9=A1=B5=E9=9D=A2=E7=99=BB=E9=99=86?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- litemall-wx/services/user.js | 60 +++++++++++++++++++++++-- litemall-wx/utils/util.js | 86 ++---------------------------------- 2 files changed, 60 insertions(+), 86 deletions(-) diff --git a/litemall-wx/services/user.js b/litemall-wx/services/user.js index 29a062cf..8bbf98fb 100644 --- a/litemall-wx/services/user.js +++ b/litemall-wx/services/user.js @@ -1,11 +1,63 @@ /** * 用户相关服务 */ - const util = require('../utils/util.js'); const api = require('../config/api.js'); +/** + * Promise封装wx.checkSession + */ +function checkSession() { + return new Promise(function (resolve, reject) { + wx.checkSession({ + success: function () { + resolve(true); + }, + fail: function () { + reject(false); + } + }) + }); +} + +/** + * Promise封装wx.login + */ +function login() { + return new Promise(function (resolve, reject) { + wx.login({ + success: function (res) { + if (res.code) { + resolve(res); + } else { + reject(res); + } + }, + fail: function (err) { + reject(err); + } + }); + }); +} + +/** + * Promise封装wx.getUserInfo + */ +function getUserInfo() { + return new Promise(function (resolve, reject) { + wx.getUserInfo({ + withCredentials: true, + success: function (res) { + resolve(res); + }, + fail: function (err) { + reject(err); + } + }) + }); +} + /** * 调用微信登录 */ @@ -13,9 +65,9 @@ function loginByWeixin() { let code = null; return new Promise(function (resolve, reject) { - return util.login().then((res) => { + return login().then((res) => { code = res.code; - return util.getUserInfo(); + return getUserInfo(); }).then((userInfo) => { //登录远程服务器 util.request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => { @@ -44,7 +96,7 @@ function checkLogin() { return new Promise(function (resolve, reject) { if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) { - util.checkSession().then(() => { + checkSession().then(() => { resolve(true); }).catch(() => { reject(false); diff --git a/litemall-wx/utils/util.js b/litemall-wx/utils/util.js index 799b39ba..0d61329d 100644 --- a/litemall-wx/utils/util.js +++ b/litemall-wx/utils/util.js @@ -32,35 +32,14 @@ function request(url, data = {}, method = "GET") { 'X-Litemall-Token': wx.getStorageSync('token') }, success: function (res) { - // console.log("success"); if (res.statusCode == 200) { if (res.data.errno == 401) { //需要登录后才可以操作 - - let code = null; - return login().then((res) => { - code = res.code; - return getUserInfo(); - }).then((userInfo) => { - //登录远程服务器 - request(api.AuthLoginByWeixin, { code: code, userInfo: userInfo }, 'POST').then(res => { - if (res.errno === 0) { - //存储用户信息 - wx.setStorageSync('userInfo', res.data.userInfo); - wx.setStorageSync('token', res.data.token); - - resolve(res); - } else { - reject(res); - } - }).catch((err) => { - reject(err); - }); - }).catch((err) => { - reject(err); - }) + wx.switchTab({ + url: '/pages/ucenter/index/index' + }); } else { resolve(res.data); } @@ -71,60 +50,6 @@ function request(url, data = {}, method = "GET") { }, fail: function (err) { reject(err) - // console.log("failed") - } - }) - }); -} - -/** - * 检查微信会话是否过期 - */ -function checkSession() { - return new Promise(function (resolve, reject) { - wx.checkSession({ - success: function () { - resolve(true); - }, - fail: function () { - reject(false); - } - }) - }); -} - -/** - * 调用微信登录 - */ -function login() { - return new Promise(function (resolve, reject) { - wx.login({ - success: function (res) { - if (res.code) { - //登录远程服务器 - // console.log(res) - resolve(res); - } else { - reject(res); - } - }, - fail: function (err) { - reject(err); - } - }); - }); -} - -function getUserInfo() { - return new Promise(function (resolve, reject) { - wx.getUserInfo({ - withCredentials: true, - success: function (res) { - // console.log(res) - resolve(res); - }, - fail: function (err) { - reject(err); } }) }); @@ -156,10 +81,7 @@ module.exports = { formatTime, request, redirect, - showErrorToast, - checkSession, - login, - getUserInfo, + showErrorToast }