Browse Source

新增一键登录

master
两千亩小少爷 3 years ago
parent
commit
121b70738b
9 changed files with 100 additions and 131 deletions
  1. +8
    -7
      nsgk_entry/app.js
  2. +17
    -11
      nsgk_entry/pages/index/index.js
  3. +3
    -3
      nsgk_entry/pages/index/index.wxml
  4. +0
    -1
      nsgk_entry/pages/show/show.js
  5. +56
    -87
      nsgk_entry/pages/user/login/login.js
  6. +1
    -1
      nsgk_entry/project.config.json
  7. +8
    -3
      nsgk_entry/utils/API.js
  8. +1
    -15
      nsgk_entry/utils/storage.js
  9. +6
    -3
      nsgk_entry/utils/util.js

+ 8
- 7
nsgk_entry/app.js View File

@@ -1,6 +1,8 @@
import * as STORAGE from './utils/storage' import * as STORAGE from './utils/storage'
import * as UTIL from './utils/util' import * as UTIL from './utils/util'
import * as API from './utils/API' import * as API from './utils/API'
let APP = getApp();

App({ App({
onLaunch() { onLaunch() {
var that = this; var that = this;
@@ -10,7 +12,6 @@ App({
//获取code //获取code
UTIL.getCOdeFromWX({ UTIL.getCOdeFromWX({
complate: (code) => { complate: (code) => {
console.log('app:微信code,' + code);
// //获取openId // //获取openId
that.getOpenIdFromFW(code); that.getOpenIdFromFW(code);
} }
@@ -32,7 +33,6 @@ App({
globalData: { globalData: {
// 系统用户登录信息(用户id、token) // 系统用户登录信息(用户id、token)
userInfo: { userInfo: {
memberId: '',
token: '', token: '',
toastTimeout:null toastTimeout:null
}, },
@@ -68,14 +68,16 @@ App({
} }
UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", { UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
success: (res) => { success: (res) => {
if (res._code == API.SUCCESS_CODE) {
UTIL.showToastNoneIcon("openId:" + res._data.openid);
if (res.code == API.SUCCESS_CODE) {
// UTIL.showToastNoneIcon("openId:" + res._data.openid);
STORAGE.setToken(res.token)
STORAGE.setOpenId(res.data.openId)
STORAGE.setSessionKey(res.data.sessionKey)
getApp().globalData.userInfo.token = res.token;
} else { } else {
//未获取到openId //未获取到openId
// console.log("失败,获取到openId:" + res.msg);
STORAGE.setOpenId(res.data.openId) STORAGE.setOpenId(res.data.openId)
STORAGE.setSessionKey(res.data.sessionKey) STORAGE.setSessionKey(res.data.sessionKey)
// UTIL.showToastNoneIcon("openId:失败");
} }
} }
}) })
@@ -85,7 +87,6 @@ App({
*/ */
initGlobalData() { initGlobalData() {
var userInfo = { var userInfo = {
memberId: STORAGE.getMemberId(),
token: STORAGE.getToken() token: STORAGE.getToken()
} }
console.log(userInfo) console.log(userInfo)


+ 17
- 11
nsgk_entry/pages/index/index.js View File

@@ -1,22 +1,18 @@
import * as UTIL from '../../utils/util.js'; import * as UTIL from '../../utils/util.js';
import * as API from '../../utils/API.js';
Page({ Page({
data: { data: {
//顶部胶囊按钮位置信息rect //顶部胶囊按钮位置信息rect
CustomMenuButton: null, CustomMenuButton: null,
wrokScrollHeight:0
wrokScrollHeight:0,
userInfoObj:{} //用户信息
}, },
onLoad: function (options) { onLoad: function (options) {
this.computeBarLocation()
this.computeBarLocation();
//获取用户信息
this.getUserInfo()
}, },
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
/**
* 计算bar 高度
*/
/* 计算bar 高度*/
computeBarLocation() { computeBarLocation() {
var that = this; var that = this;
let CustomMenuButton = wx.getMenuButtonBoundingClientRect(); let CustomMenuButton = wx.getMenuButtonBoundingClientRect();
@@ -43,4 +39,14 @@ Page({
CustomMenuButton: CustomMenuButton, CustomMenuButton: CustomMenuButton,
}); });
}, },
/* 获取用户信息*/
getUserInfo(){
UTIL.httpRequest(API.URL_GET_GETINFO, {method:'GET'}, {
success: (res) => {
if (res.code == API.SUCCESS_CODE) {
this.setData({userInfoObj:res.user})
}
}
})
}
}) })

+ 3
- 3
nsgk_entry/pages/index/index.wxml View File

@@ -7,10 +7,10 @@
<view class="portrait_head"></view> <view class="portrait_head"></view>
<view class="information_main"> <view class="information_main">
<view class="name_wrap"> <view class="name_wrap">
<text class="name">张没事</text>
<text class="name">{{userInfoObj.nickName}}</text>
<view class="jobs"> <view class="jobs">
<view class="icon" style="background:url('../../image/index/header_job.png') no-repeat; background-size: 100% 100%;"></view> <view class="icon" style="background:url('../../image/index/header_job.png') no-repeat; background-size: 100% 100%;"></view>
报账员
{{userInfoObj.remark}}
</view> </view>
</view> </view>
<view class="task_wrap "> <view class="task_wrap ">
@@ -25,7 +25,7 @@
</view> </view>
<view class="address_wrap"> <view class="address_wrap">
<view class="icon"></view> <view class="icon"></view>
山东省威海市环翠区李
{{userInfoObj.allDeptName}}
</view> </view>


</view> </view>


+ 0
- 1
nsgk_entry/pages/show/show.js View File

@@ -217,7 +217,6 @@ Page({
centerWarehouseId: 10051, centerWarehouseId: 10051,
channel: 220, channel: 220,
channelType: 22, channelType: 22,
memberId: 24892,
rows: 40, rows: 40,
shopId: 10005, shopId: 10005,
token: "LWXAPP1636599316684iv6qkhyqhr4izg", token: "LWXAPP1636599316684iv6qkhyqhr4izg",


+ 56
- 87
nsgk_entry/pages/user/login/login.js View File

@@ -4,72 +4,44 @@ import * as API from '../../../utils/API.js';
import * as STORAGE from '../../../utils/storage' import * as STORAGE from '../../../utils/storage'
const APP = getApp(); const APP = getApp();
Page({ Page({

/**
* 页面的初始数据
*/
data: { data: {
isIPhoneX:false, isIPhoneX:false,
privacyCheck:true //用户协议 privacyCheck:true //用户协议
}, },

/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) { onLoad: function (options) {
this.setData({ this.setData({
isIPhoneX:UTIL.isIPhoneX() isIPhoneX:UTIL.isIPhoneX()
}) })
this.automaticLogin()
}, },

/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {

},

/**
* 生命周期函数--监听页面显示
*/
onShow: function () {

},

/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {

},

/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {

},

/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {

//自动登录
automaticLogin(){
let automatic = STORAGE.getToken();
let getOpenId = STORAGE.getOpenId();
if(automatic == '' && getOpenId ==''){
UTIL.getCOdeFromWX({
complate: (code) => {
let sendData = {
code: code
}
UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
success: (res) => {
if (res.code == API.SUCCESS_CODE) {
wx.navigateTo({
url: '/pages/index/index',
})
}
}
})
}
});
}else{
wx.navigateTo({
url: '/pages/index/index',
})
}
}, },
//用户隐私协议选项
checkboxChange: function(res) { checkboxChange: function(res) {
let checkStatus = false; let checkStatus = false;
if(res.detail.value.length!=0){ if(res.detail.value.length!=0){
@@ -81,6 +53,7 @@ Page({
privacyCheck:checkStatus privacyCheck:checkStatus
}) })
}, },
//微信一键登录授权
getPhoneNumber: function(res) { getPhoneNumber: function(res) {
let that = this; let that = this;
let { let {
@@ -101,47 +74,43 @@ Page({
} }
UTIL.httpRequest(API.URL_POST_DECRYPTEDWXDATA, sendData,{ UTIL.httpRequest(API.URL_POST_DECRYPTEDWXDATA, sendData,{
success: (res) => { success: (res) => {
if (res._code == API.SUCCESS_CODE) {
if (res.code == API.SUCCESS_CODE) {
let phoneNumber = res.data.phoneNumber;
//微信手机号码绑定
that.wxUserBand(phoneNumber)
// UTIL.showToastNoneIcon("数据共:" + res._data.length + "条"); // UTIL.showToastNoneIcon("数据共:" + res._data.length + "条");
} else { } else {
//待删 //待删
wx.navigateTo({
url: '/pages/index/index',
})
UTIL.showToastNoneIcon(res.msg) UTIL.showToastNoneIcon(res.msg)
} }
}, },
fail: (res) => { fail: (res) => {
UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP) UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
},
complete: (res) => {
} }
}); });
//     if (res.detail.userInfo) {
//       //用户按了允许授权按钮
//       var that = this;
//       // 获取到用户的信息了,打印到控制台上看下
//       console.log("用户的信息如下:");
//       console.log(res.detail);
//       //授权成功后,通过改变 isHide 的值,让实现页面显示出来,把授权页面隐藏起来
//       that.setData({
//         isHide: false
//       });
//     } else {
//       //用户按了拒绝按钮
//       wx.showModal({
//         title: '警告',
//         content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
//         showCancel: false,
//         confirmText: '返回授权',
//         success: function(res) {
//           // 用户没有授权成功,不需要改变 isHide 的值
//           if (res.confirm) {
//             console.log('用户点击了“返回授权”');
//           }
//         }
//       });
//     }
},
wxUserBand(phone){
let sendData = {
openId:STORAGE.getOpenId(),
phonenumber:phone
}
UTIL.httpRequest(API.URL_POST_USERBAND, sendData,{
success: (res) => {
if (res.code == API.SUCCESS_CODE) {
wx.navigateTo({
url: '/pages/index/index',
})
}else{
UTIL.showToastNoneIcon(res.msg)
}
},
fail: (res) => {
UTIL.showToastNoneIcon(API.MSG_FAIL_HTTP)
}
});
console.log(sendData)
//
} }
}) })

+ 1
- 1
nsgk_entry/project.config.json View File

@@ -48,7 +48,7 @@
}, },
"compileType": "miniprogram", "compileType": "miniprogram",
"libVersion": "2.21.1", "libVersion": "2.21.1",
"appid": "wx90a68c6fdd650f48",
"appid": "wxaace54cc2cf8924b",
"projectname": "WXMB", "projectname": "WXMB",
"debugOptions": { "debugOptions": {
"hidedInDevtools": [] "hidedInDevtools": []


+ 8
- 3
nsgk_entry/utils/API.js View File

@@ -8,7 +8,7 @@ let {
//用户登录页面,接口检测用户token失效,需跳转重新登录 //用户登录页面,接口检测用户token失效,需跳转重新登录
const USER_LOGIN_PAGE_PATH='/pages/user/login/login'; const USER_LOGIN_PAGE_PATH='/pages/user/login/login';
//接口成功 //接口成功
const SUCCESS_CODE = '200';
const SUCCESS_CODE = 200;
//微信登陆失效 //微信登陆失效
const INVALID_USER_TOKEN_CODE = '001007'; const INVALID_USER_TOKEN_CODE = '001007';


@@ -29,7 +29,10 @@ const MSG_INVALID_USER_TOKEN='登陆信息失效,请重新登陆';
const URL_GET_OPENID=`${URL_PREFIX}/wechat/codeLogin`; const URL_GET_OPENID=`${URL_PREFIX}/wechat/codeLogin`;
// 获取手机号解密接口 // 获取手机号解密接口
const URL_POST_DECRYPTEDWXDATA = `${URL_PREFIX}/wechat/decryptedWXData`; const URL_POST_DECRYPTEDWXDATA = `${URL_PREFIX}/wechat/decryptedWXData`;

// 微信绑定手机号
const URL_POST_USERBAND = `${URL_PREFIX}/register/wechat/band`;
//获取用户信息
const URL_GET_GETINFO = `${URL_PREFIX}/getInfo`;


/****************接口地址end****************/ /****************接口地址end****************/


@@ -45,5 +48,7 @@ export {
MSG_ERROR_TO, MSG_ERROR_TO,
MSG_INVALID_USER_TOKEN, MSG_INVALID_USER_TOKEN,
URL_GET_OPENID, URL_GET_OPENID,
URL_POST_DECRYPTEDWXDATA
URL_POST_DECRYPTEDWXDATA,
URL_POST_USERBAND,
URL_GET_GETINFO
} }

+ 1
- 15
nsgk_entry/utils/storage.js View File

@@ -1,16 +1,4 @@
/**
* 获取当前登录用户的 ID
*/
function getMemberId() {
return wx.getStorageSync('memberId');
}
/**
* 存储用户ID
* @param {用户id} memberId
*/
function setMemberId(memberId) {
wx.setStorageSync('memberId', memberId);
}

/** /**
* 获取当前登录用户的 token * 获取当前登录用户的 token
*/ */
@@ -54,8 +42,6 @@ function setSessionKey(sessionKey) {




export { export {
getMemberId,
setMemberId,
getToken, getToken,
setToken, setToken,
getOpenId, getOpenId,


+ 6
- 3
nsgk_entry/utils/util.js View File

@@ -113,13 +113,16 @@ function httpRequest(url, data, {
wx.showNavigationBarLoading(); wx.showNavigationBarLoading();
let finalData = {}; let finalData = {};
Object.assign(finalData, data); Object.assign(finalData, data);
finalData.memberId = getApp().globalData.userInfo.memberId;
finalData.token = getApp().globalData.userInfo.token;
// finalData.token = getApp().globalData.userInfo.token;
wx.request({ wx.request({
url, url,
data: finalData, data: finalData,
method: data.method || 'POST', method: data.method || 'POST',
timeout: 60000, timeout: 60000,
header: {
'Authorization':'Bearer '+getApp().globalData.userInfo.token
// 'Authorization':'1111111111'
},
success: function (response) { success: function (response) {
if (response.data && response.data._code == API.INVALID_USER_TOKEN_CODE) { if (response.data && response.data._code == API.INVALID_USER_TOKEN_CODE) {
//微信登陆失效 //微信登陆失效
@@ -257,7 +260,7 @@ function handleFail(data = '') {
* 获取微信Code * 获取微信Code
*/ */
function getCOdeFromWX({ complate }) { function getCOdeFromWX({ complate }) {
showLoadingHaveMask('正在检测账号..');
showLoadingHaveMask('正在加载数据..');
wx.login({ wx.login({
success: function (data) { success: function (data) {
console.log(data) console.log(data)


Loading…
Cancel
Save