Browse Source

(*)登录+登出+基本信息

master
Archer_cdm 2 years ago
parent
commit
ac40727ed3
  1. 18
      weapp/app.js
  2. 73
      weapp/common.js
  3. 6
      weapp/package/basic/basic.js
  4. 10
      weapp/package/basic/basic.wxml
  5. 16
      weapp/pages/index/index.js
  6. 28
      weapp/pages/login/login.js
  7. 32
      weapp/pages/myInfo/myInfo.js
  8. 2
      weapp/pages/myInfo/myInfo.wxml
  9. 9
      weapp/utils/getApiUrl.js

18
weapp/app.js

@ -1,20 +1,10 @@
// app.js // app.js
App({ App({
onLaunch() { onLaunch() { },
// // 展示本地存储能力
// const logs = wx.getStorageSync('logs') || []
// logs.unshift(Date.now())
// wx.setStorageSync('logs', logs)
// // 登录
// wx.login({
// success: res => {
// // 发送 res.code 到后台换取 openId, sessionKey, unionId
// }
// })
},
globalData: { globalData: {
userInfo: null userInfo: null,
baseUrl: 'http://10.8.16.221:4900', //api 本地环境
// imgUrl: 'http://10.8.16.221:5000/_file-server/', //本地环境
}, },
onShow(e) { onShow(e) {
// 检查是否有更新 // 检查是否有更新

73
weapp/common.js

@ -0,0 +1,73 @@
const app = getApp();
const { baseUrl } = app.globalData;
// 全局配置 请求拦截, 长时间
const buildRequest = (type, url, data) => {
return new Promise((resolve, reject) => {
if (url.indexOf('token') == -1) {
let token = wx.getStorageSync('token');
if (token) {
url += url.indexOf('?') == -1 ? '?' : '&';
url += `token=${token}`;
}
}
if (url.indexOf('http') != 0) {
url = baseUrl + url;
}
wx.request({
url,
data,
method: type,
success: res => {
if (res.statusCode == 200 || res.statusCode == 204) {
resolve(res.data);
return;
}
if (res.statusCode == 400) {
console.error("400报错" + url, res);
wx.showToast({
title: res.data.message,
icon: "none",
duration: 1500
});
// reject(res);
}
if (res.statusCode == 401) {
wx.clearStorageSync();
wx.reLaunch({
url: '/pages/login/login'
});
}
},
fail: err => {
wx.showToast({
title: '网络异常',
icon: 'none',
duration: 1500
})
console.error('网络异常' + url, err);
reject(err);
}
});
});
}
// 请求拦截
let Request = {};
Request.get = (url, query) => {
return buildRequest('GET', url, query);
}
Request.post = (url, data) => {
return buildRequest('POST', url, data);
}
Request.put = (url, data) => {
return buildRequest('PUT', url, data);
}
Request.del = (url, data) => {
return buildRequest('DELETE', url, data);
}
module.exports = {
Request
}

6
weapp/package/basic/basic.js

@ -12,7 +12,11 @@ Page({
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad(options) { onLoad(options) {
let that = this;
let userInfo = wx.getStorageSync('userInfo');
that.setData({
userInfo
})
}, },
/** /**

10
weapp/package/basic/basic.wxml

@ -3,20 +3,20 @@
<view class='header-item-container'> <view class='header-item-container'>
<image class='logo' src='../../images/avatar.svg' /> <image class='logo' src='../../images/avatar.svg' />
<view> <view>
<text class='userName'>超级管理</text> <text class='userName'>{{userInfo.name || '--'}}</text>
</view> </view>
</view> </view>
<view class='list'> <view class='list'>
<view class='content'> <view class='content'>
<view class='title'>姓名:</view> <view class='title'>姓名:</view>
<view class='value'> <view class='value'>
管理员 {{userInfo.name || '--'}}
</view> </view>
</view> </view>
<view class='content'> <view class='content'>
<view class='title'>电话:</view> <view class='title'>电话:</view>
<view class='value'> <view class='value'>
18965662365 {{userInfo.phone || '--'}}
</view> </view>
</view> </view>
<view class='content'> <view class='content'>
@ -28,13 +28,13 @@
<view class='content'> <view class='content'>
<view class='title'>职务:</view> <view class='title'>职务:</view>
<view class='value'> <view class='value'>
系统管理员 {{userInfo.post || '--'}}
</view> </view>
</view> </view>
<view class='content'> <view class='content'>
<view class='title'>邮箱:</view> <view class='title'>邮箱:</view>
<view class='value'> <view class='value'>
chendingming@free-sun.com.cn {{userInfo.email || '--'}}
</view> </view>
</view> </view>
</view> </view>

16
weapp/pages/index/index.js

@ -5,14 +5,19 @@ Page({
* 页面的初始数据 * 页面的初始数据
*/ */
data: { data: {
dataList:[1,2,3,4] dataList: [1, 2, 3, 4]
}, },
/** /**
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad(options) { onLoad(options) {
const userInfo = wx.getStorageSync('userInfo');
if (!userInfo || !userInfo.id) {
wx.reLaunch({
url: '/pages/login/login'
});
}
}, },
/** /**
@ -26,7 +31,12 @@ Page({
* 生命周期函数--监听页面显示 * 生命周期函数--监听页面显示
*/ */
onShow() { onShow() {
const userInfo = wx.getStorageSync('userInfo');
if (!userInfo || !userInfo.id) {
wx.reLaunch({
url: '/pages/login/login'
});
}
}, },
/** /**

28
weapp/pages/login/login.js

@ -1,11 +1,15 @@
// pages/login/login.js // pages/login/login.js
import { loginUrl } from "../../utils/getApiUrl";
import { Request } from "../../common";
Page({ Page({
/** /**
* 页面的初始数据 * 页面的初始数据
*/ */
data: { data: {
userNameCan: false,
passwordCan: false,
}, },
setColor: function () { setColor: function () {
@ -27,10 +31,32 @@ Page({
this.setColor(); this.setColor();
}, },
// 登录
getLogin: function (e) { getLogin: function (e) {
if (e.detail.value.username.length == 0 || e.detail.value.password == 0) {
wx.showToast({
title: '请输入用户名密码',
icon: 'none',
})
return
}
wx.showLoading()
Request.post(loginUrl(), {
"username": e.detail.value.username,
"password": e.detail.value.password
}).then((res) => {
if (!res.authorized) {
wx.showToast({ title: "登录失败", icon: "none" });
return;
}
wx.setStorageSync('token', res.token);
wx.setStorageSync("userInfo", res);
getApp().globalData.userInfo = res
wx.switchTab({ wx.switchTab({
url: '/pages/index/index', url: '/pages/index/index',
}) })
wx.hideLoading()
})
}, },
/** /**

32
weapp/pages/myInfo/myInfo.js

@ -1,4 +1,7 @@
// pages/myInfo/myInfo.js // pages/myInfo/myInfo.js
import { logoutUrl } from "../../utils/getApiUrl";
import { Request } from "../../common";
Page({ Page({
/** /**
@ -14,11 +17,38 @@ Page({
}) })
}, },
// 登出
logout() {
wx.showModal({
title: '切换用户',
content: '将退出当前用户,重新登录',
confirmText: '切换用户',
confirmColor: '#1979ff',
success: res => {
if (res.confirm) {
wx.showLoading()
Request.put(logoutUrl(), {}).then((res) => {
wx.clearStorage();
getApp().globalData.userInfo = null;
wx.reLaunch({
url: '../login/login',
})
wx.hideLoading()
})
}
}
})
},
/** /**
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad(options) { onLoad(options) {
let that = this;
let userInfo = wx.getStorageSync('userInfo');
that.setData({
userInfo
})
}, },
/** /**

2
weapp/pages/myInfo/myInfo.wxml

@ -3,7 +3,7 @@
<view class='header-item-container'> <view class='header-item-container'>
<image class='logo' src='../../images/avatar.svg' /> <image class='logo' src='../../images/avatar.svg' />
<view> <view>
<text class='userName'>超级管理</text> <text class='userName'>{{userInfo.name}}</text>
</view> </view>
</view> </view>

9
weapp/utils/getApiUrl.js

@ -0,0 +1,9 @@
// 登录
exports.loginUrl = () => {
return `/login`
}
// 登出
exports.logoutUrl = () => {
return `/logout`
}
Loading…
Cancel
Save