小程序登录逻辑跑通;登陆密码MD5存储;小程序首页添加调试输出
This commit is contained in:
@@ -10,7 +10,8 @@ const App = createApp({
|
||||
// 全局变量 refer: https://docs.taro.zone/docs/come-from-miniapp#%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F
|
||||
App.use(setGlobalDataPlugin, {
|
||||
globalData: {
|
||||
baseUrl: "http://localhost:8080", // 不带最后的 /
|
||||
debugMode: true, // 是否展示调试内容
|
||||
baseUrl: "http://localhost", // 不带最后的 /
|
||||
}
|
||||
})
|
||||
|
||||
|
@@ -4,6 +4,24 @@
|
||||
<view style="display: block; padding-left: 30px; padding-top: 12px;">
|
||||
<text>欢迎你,{{ displayUsername }}!</text>
|
||||
</view>
|
||||
|
||||
<view style="display: block;" v-if="isVisitor">
|
||||
<view style="display: flex;text-align: center;margin-top: 20px;">
|
||||
<view style="width: 25%;">
|
||||
<image src="../../image/icon/code.png" style="width: 40px;height: 40px;" @tap='goCode'></image>
|
||||
<view>进出码</view>
|
||||
</view>
|
||||
<view style="width: 25%;">
|
||||
<image src="../../image/icon/feedback.png" style="width: 40px;height: 40px;" bindtap='goFeedback'></image>
|
||||
<view>反馈查看</view>
|
||||
</view>
|
||||
<view style="width: 25%;">
|
||||
<image src="../../image/icon/apply.png" style="width: 40px;height: 40px;" bindtap='goApply'></image>
|
||||
<view>申请记录</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view style="display: block;" v-if="isUser">
|
||||
<view style="display: flex;text-align: center;margin-top: 20px;">
|
||||
<view style="width: 25%;">
|
||||
@@ -39,6 +57,7 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view style="display: block;" v-if="isAdmin">
|
||||
<view style="display: flex;text-align: center;margin-top: 20px;">
|
||||
<view style="width: 25%;">
|
||||
@@ -76,28 +95,39 @@
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-if="debugMode">
|
||||
<button @tap='debugCleanCache'>清除缓存</button>
|
||||
<text decode="nbsp">{{ debugText }}</text>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import Taro from '@tarojs/taro'
|
||||
import { eventCenter, getCurrentInstance } from '@tarojs/taro'
|
||||
import ENUM from '../../utils/const.js'
|
||||
|
||||
import './index.css'
|
||||
|
||||
|
||||
export default {
|
||||
setup() {
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
userInfo: null,
|
||||
displayUsername: "",
|
||||
isVisitor: false,
|
||||
isUser: false,
|
||||
isAdmin: false,
|
||||
debugMode: Taro.getApp().globalData.debugMode,
|
||||
debugText: "",
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
eventCenter.once(getCurrentInstance().router.onShow, () => {
|
||||
console.log('onShow')
|
||||
console.log("ENUM", ENUM)
|
||||
console.log("debugMode", this.debugMode)
|
||||
|
||||
this.userInfo = Taro.getStorageSync("userInfo");
|
||||
this.displayUsername = this.userInfo?.username ?? "请登录";
|
||||
if (!this.userInfo) {
|
||||
@@ -106,8 +136,30 @@ export default {
|
||||
url: '/pages/index/login'
|
||||
})
|
||||
} else {
|
||||
this.isUser = this.userInfo.userType == "user";
|
||||
this.isAdmin = this.userInfo.userType == "admin";
|
||||
const role = ENUM.user.role;
|
||||
this.isVisitor = [
|
||||
role.VISITOR,
|
||||
].includes(this.userInfo.role);
|
||||
this.isUser = [
|
||||
role.RESIDENTS_OWNER,
|
||||
role.RESIDENTS_MEMBER,
|
||||
role.RESIDENTS_TENENT,
|
||||
].includes(this.userInfo.role);
|
||||
this.isAdmin = [
|
||||
role.ADMIN,
|
||||
role.STAFF,
|
||||
].includes(this.userInfo.role);
|
||||
console.log(
|
||||
"isVisitor", this.isVisitor,
|
||||
"isUser", this.isUser,
|
||||
"isAdmin", this.isAdmin
|
||||
)
|
||||
this.debugText = JSON.stringify({
|
||||
"isVisitor": this.isVisitor,
|
||||
"isUser": this.isUser,
|
||||
"isAdmin": this.isAdmin,
|
||||
"userInfo": this.userInfo
|
||||
}, null, 4).replace(/[ ]/g, ' ')
|
||||
}
|
||||
})
|
||||
},
|
||||
@@ -119,6 +171,19 @@ export default {
|
||||
Taro.switchTab({
|
||||
url: '/pages/residents/code'
|
||||
})
|
||||
},
|
||||
debugCleanCache() {
|
||||
Taro.clearStorage({
|
||||
success: function () {
|
||||
Taro.showToast({
|
||||
title: "成功",
|
||||
icon: 'success'
|
||||
})
|
||||
Taro.reLaunch({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,7 @@
|
||||
|
||||
<script>
|
||||
import Taro from '@tarojs/taro'
|
||||
import md5 from 'blueimp-md5'
|
||||
|
||||
import './login.css'
|
||||
|
||||
@@ -42,40 +43,45 @@ export default {
|
||||
duration: 2000
|
||||
})
|
||||
} else {
|
||||
let passwordHash = md5(this.password);
|
||||
Taro.request({
|
||||
url: `${this.baseUrl}/user/login`,
|
||||
method: "POST",
|
||||
header: {
|
||||
"Content-Type": "application/x-www-form-urlencoded" //用于post
|
||||
},
|
||||
data: {
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
password: passwordHash,
|
||||
},
|
||||
success: function (d) {
|
||||
let data = d.data;
|
||||
if (data.code == 200) {
|
||||
if (data.status == "success") {
|
||||
Taro.setStorageSync("userInfo", data.userInfo);
|
||||
console.log("userInfo", Taro.getStorageSync("userInfo"))
|
||||
Taro.showToast({
|
||||
title: "登录成功",
|
||||
icon: 'success',
|
||||
success: function () {
|
||||
Taro.redirectTo({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: data.msg,
|
||||
icon: 'error'
|
||||
})
|
||||
}
|
||||
let result = d.data;
|
||||
if (result.success) {
|
||||
Taro.setStorageSync("userInfo", result.data.userInfo);
|
||||
console.log("userInfo", Taro.getStorageSync("userInfo"))
|
||||
Taro.showToast({
|
||||
title: "登录成功",
|
||||
icon: 'success',
|
||||
success: function () {
|
||||
Taro.redirectTo({
|
||||
url: '/pages/index/index'
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Taro.showToast({
|
||||
title: data.msg,
|
||||
title: result.msg,
|
||||
icon: 'error',
|
||||
duration: 2000
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: function () {
|
||||
Taro.showToast({
|
||||
title: "请求失败",
|
||||
icon: 'error',
|
||||
duration: 2000
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
12
miniprogram/src/utils/const.js
Normal file
12
miniprogram/src/utils/const.js
Normal file
@@ -0,0 +1,12 @@
|
||||
module.exports = {
|
||||
user: {
|
||||
role: {
|
||||
ADMIN: 0,
|
||||
STAFF: 1,
|
||||
RESIDENTS_OWNER: 2,
|
||||
RESIDENTS_MEMBER: 3,
|
||||
RESIDENTS_TENENT: 4,
|
||||
VISITOR: 5
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user