完成登录注册与切换账号相关功能
This commit is contained in:
parent
f4d6130615
commit
6bcda7adb9
18
api/user.js
18
api/user.js
@ -55,3 +55,21 @@ export function getEmailCodeRequest(data) {
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
// 登录
|
||||
export function loginRequest(data) {
|
||||
return request({
|
||||
url: '/wechat/mini/login',
|
||||
method: 'POST',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
// 注册
|
||||
export function phoneLoginRequest(data) {
|
||||
return request({
|
||||
url: '/wechat/mini/phone/login ',
|
||||
method: 'POST',
|
||||
data
|
||||
})
|
||||
}
|
||||
|
||||
@ -22,4 +22,4 @@ const PROD = {
|
||||
}
|
||||
|
||||
// 更换环境的时候 切换导出就行
|
||||
export default PRE
|
||||
export default DEV
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
"transformPx" : false,
|
||||
/* 小程序特有相关 */
|
||||
"mp-weixin" : {
|
||||
"appid" : "wx9829a39e65550757",
|
||||
"appid" : "wx10c16cfd90808097",
|
||||
"setting" : {
|
||||
"urlCheck" : true,
|
||||
"minified" : true
|
||||
|
||||
@ -50,15 +50,18 @@
|
||||
class="button-add" @click="toSearchDevice"></image>
|
||||
</view>
|
||||
<view v-else>
|
||||
<view class="button-login" @click="login">登录</view>
|
||||
<label for="phone">
|
||||
<view class="button-login">登录</view>
|
||||
</label>
|
||||
</view>
|
||||
</view>
|
||||
<button open-type="getPhoneNumber" style="display:none" id="phone" @getphonenumber="getphonenumber"></button>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { timeFormat } from 'uview-plus'
|
||||
import { getUserInfoRequest } from '@/api/user'
|
||||
import { getUserInfoRequest, loginRequest } from '@/api/user'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import { useLockStore } from '@/stores/lock'
|
||||
import { useBluetoothStore } from '@/stores/bluetooth'
|
||||
@ -89,25 +92,81 @@
|
||||
title: '加载中'
|
||||
})
|
||||
this.deviceInfo = await this.getDeviceInfo()
|
||||
console.log(this.deviceInfo)
|
||||
|
||||
const token = uni.getStorageSync('token')
|
||||
if(token) {
|
||||
this.updateLoginStatus(true)
|
||||
const code = await this.getUserInfo()
|
||||
if(code === 0) {
|
||||
await this.getLockList(this.search)
|
||||
}
|
||||
await this.getUserInfo()
|
||||
await this.getLockList(this.search)
|
||||
} else {
|
||||
await this.homeLogin()
|
||||
}
|
||||
this.penging = false
|
||||
uni.hideLoading()
|
||||
},
|
||||
methods: {
|
||||
timeFormat,
|
||||
...mapActions(useUserStore, ['updateUserInfo', 'updateLoginStatus', 'login']),
|
||||
...mapActions(useUserStore, ['updateUserInfo', 'updateLoginStatus', 'getPhone']),
|
||||
...mapActions(useLockStore, ['getLockList', 'getRole', 'getTimeLimit']),
|
||||
...mapActions(useBluetoothStore, ['getBluetoothStatus', 'initAndListenBluetooth', 'updateCurrentLockInfo',
|
||||
'checkSetting', 'updateKeyId']),
|
||||
...mapActions(useBasicStore, ['routeJump', 'getDeviceInfo']),
|
||||
homeLogin() {
|
||||
const that = this
|
||||
return new Promise((resolve) => {
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: async function (loginRes) {
|
||||
const { code, data } = await loginRequest({
|
||||
js_code: loginRes.code
|
||||
})
|
||||
if(code === 0) {
|
||||
uni.setStorageSync('openid', data.openid)
|
||||
if(data.accessToken) {
|
||||
uni.setStorageSync('token', data.accessToken)
|
||||
that.getUserInfo()
|
||||
that.getLockList(that.search)
|
||||
that.updateLoginStatus(true)
|
||||
resolve(true)
|
||||
} else {
|
||||
that.updateLoginStatus(false)
|
||||
resolve(false)
|
||||
}
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '登录失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
resolve(false)
|
||||
}
|
||||
},
|
||||
fail: function () {
|
||||
uni.showToast({
|
||||
title: '登录失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
async getphonenumber(data) {
|
||||
if(data.detail.errMsg === 'getPhoneNumber:fail user deny') {
|
||||
return
|
||||
}
|
||||
const result = await this.getPhone({
|
||||
encryptedData: data.detail.encryptedData,
|
||||
iv: data.detail.iv
|
||||
})
|
||||
if(result) {
|
||||
this.getUserInfo()
|
||||
this.getLockList(this.search)
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '登录失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
async nextPage() {
|
||||
if(this.lockList.length < this.lockTotal) {
|
||||
this.search.pageNo++
|
||||
@ -135,7 +194,7 @@
|
||||
const { code, data } = await getUserInfoRequest()
|
||||
if(code === 0) {
|
||||
this.updateUserInfo(data)
|
||||
console.log(this.userInfo)
|
||||
this.updateLoginStatus(true)
|
||||
}
|
||||
return code
|
||||
},
|
||||
|
||||
@ -93,13 +93,17 @@ export default {
|
||||
})
|
||||
},
|
||||
async getNetToken(){
|
||||
const { code, data } = await getLockNetTokenRequest({
|
||||
const { code, data, message } = await getLockNetTokenRequest({
|
||||
lockId: this.currentLockInfo.keyId
|
||||
})
|
||||
if(code === 0) {
|
||||
this.onlineToken = data.token
|
||||
return true
|
||||
} else {
|
||||
uni.toast({
|
||||
title: message,
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
},
|
||||
@ -121,10 +125,6 @@ export default {
|
||||
if(!result) {
|
||||
this.$refs.loading.close()
|
||||
this.pending = false
|
||||
uni.showToast({
|
||||
title: '联网开锁失败,请保证网络畅通',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -143,12 +143,12 @@ export default {
|
||||
})
|
||||
if(code === 0) {
|
||||
uni.showToast({
|
||||
title: '开锁成功',
|
||||
title: `${type === 'close' ? '关' : '开'}锁成功`,
|
||||
icon: 'none'
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '开锁失败,请保证在锁附近',
|
||||
title: `${type === 'close' ? '关' : '开'}锁失败,请保证在锁附近`,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
|
||||
@ -31,19 +31,28 @@
|
||||
<image class="icon-arrow" src="/static/images/icon_arrow.png" mode="aspectFill"></image>
|
||||
</view>
|
||||
</view>
|
||||
<label for="changePhone">
|
||||
<view class="switch-account">切换账号</view>
|
||||
</label>
|
||||
<view class="button-logout" @click="logout">退出</view>
|
||||
</view>
|
||||
<view v-else>
|
||||
<view class="button-login" @click="login">登录</view>
|
||||
<label for="phone">
|
||||
<view class="button-login">登录</view>
|
||||
</label>
|
||||
</view>
|
||||
</view>
|
||||
<button open-type="contact" id="contact"></button>
|
||||
<button open-type="contact" style="display:none" id="contact"></button>
|
||||
<button open-type="getPhoneNumber" style="display:none" id="phone" @getphonenumber="getphonenumber"></button>
|
||||
<button open-type="getPhoneNumber" style="display:none" id="changePhone" @getphonenumber="changePhone"></button>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { useBasicStore } from '@/stores/basic'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import { useLockStore } from '@/stores/lock'
|
||||
import { mapState, mapActions } from 'pinia'
|
||||
import { getUserInfoRequest, phoneLoginRequest } from '@/api/user'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@ -59,7 +68,67 @@
|
||||
},
|
||||
methods: {
|
||||
...mapActions(useBasicStore, ['getButtonInfo', 'routeJump']),
|
||||
...mapActions(useUserStore, ['updateLoginStatus', 'login']),
|
||||
...mapActions(useLockStore, ['getLockList']),
|
||||
...mapActions(useUserStore, ['updateLoginStatus', 'getPhone', 'updateUserInfo']),
|
||||
async changePhone(res) {
|
||||
if(res.detail.errMsg === 'getPhoneNumber:fail user deny') {
|
||||
return
|
||||
}
|
||||
const openid = uni.getStorageSync('openid')
|
||||
const { code, data, message } = await phoneLoginRequest({
|
||||
encryptedData: res.detail.encryptedData,
|
||||
iv: res.detail.iv,
|
||||
rebind: true,
|
||||
openid
|
||||
})
|
||||
if(code === 0) {
|
||||
uni.setStorageSync('token', data.accessToken)
|
||||
console.log('token', data.accessToken,uni.getStorageSync('token'))
|
||||
this.getUserInfo()
|
||||
this.getLockList({
|
||||
pageNo: 1,
|
||||
pageSize: 50
|
||||
})
|
||||
uni.showToast({
|
||||
title: '账号切换成功',
|
||||
icon: 'none'
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: message,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
async getphonenumber(data) {
|
||||
if(data.detail.errMsg === 'getPhoneNumber:fail user deny') {
|
||||
return
|
||||
}
|
||||
const result = await this.getPhone({
|
||||
encryptedData: data.detail.encryptedData,
|
||||
iv: data.detail.iv
|
||||
})
|
||||
if(result) {
|
||||
this.getUserInfo()
|
||||
this.getLockList({
|
||||
pageNo: 1,
|
||||
pageSize: 50
|
||||
})
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '登录失败,请重试',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
},
|
||||
async getUserInfo() {
|
||||
const { code, data } = await getUserInfoRequest()
|
||||
if(code === 0) {
|
||||
this.updateUserInfo(data)
|
||||
this.updateLoginStatus(true)
|
||||
}
|
||||
return code
|
||||
},
|
||||
toUsereInfo() {
|
||||
this.routeJump({
|
||||
name: 'userInfo'
|
||||
@ -140,17 +209,32 @@ page {
|
||||
position: absolute;
|
||||
border-radius: 46rpx;
|
||||
bottom: 60rpx;
|
||||
width: 710rpx;
|
||||
width: 600rpx;
|
||||
height: 80rpx;
|
||||
line-height: 80rpx;
|
||||
text-align: center;
|
||||
margin-left: 20rpx;
|
||||
margin-left: 75rpx;
|
||||
background: #ec433c;
|
||||
color: #ffffff;
|
||||
font-size: 40rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.switch-account {
|
||||
position: absolute;
|
||||
border-radius: 46rpx;
|
||||
bottom: 180rpx;
|
||||
width: 600rpx;
|
||||
height: 80rpx;
|
||||
line-height: 80rpx;
|
||||
text-align: center;
|
||||
margin-left: 75rpx;
|
||||
background: #63b8af;
|
||||
color: #ffffff;
|
||||
font-size: 40rpx;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.button-login {
|
||||
margin-top: 40vh;
|
||||
border-radius: 46rpx;
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
* @description 用户信息数据持久化
|
||||
*/
|
||||
import { defineStore } from 'pinia'
|
||||
import { getUserInfoRequest } from '@/api/user'
|
||||
import { getUserInfoRequest, loginRequest, phoneLoginRequest } from '@/api/user'
|
||||
import { useLockStore } from '@/stores/lock'
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
@ -21,18 +21,40 @@ export const useUserStore = defineStore('user', {
|
||||
updateLoginStatus(status) {
|
||||
this.isLogin = status
|
||||
},
|
||||
async login() {
|
||||
uni.setStorageSync('token', '3028|6WkZCHj5yzLlXW3z3ylc1TDhtKYvF3jHB6e4eTWr22681e3e')
|
||||
const { code, data } = await getUserInfoRequest()
|
||||
await useLockStore().getLockList({
|
||||
pageNo: 1,
|
||||
pageSize: 50
|
||||
async getPhone(data) {
|
||||
const { iv, encryptedData } = data
|
||||
const that = this
|
||||
return new Promise((resolve) => {
|
||||
uni.checkSession({
|
||||
async success(){
|
||||
const openid = uni.getStorageSync('openid')
|
||||
const { code, data, message } = await phoneLoginRequest({ iv, encryptedData, openid })
|
||||
if(code === 0) {
|
||||
uni.setStorageSync('token', data.accessToken)
|
||||
resolve(true)
|
||||
} else {
|
||||
resolve(false)
|
||||
}
|
||||
},
|
||||
fail() {
|
||||
uni.login({
|
||||
provider: 'weixin',
|
||||
success: async function (loginRes) {
|
||||
const { code, data } = await loginRequest({
|
||||
js_code: loginRes.code
|
||||
})
|
||||
if (code === 0) {
|
||||
uni.setStorageSync('openid', data.openid)
|
||||
}
|
||||
resolve(false)
|
||||
},
|
||||
fail() {
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
if(code === 0) {
|
||||
this.updateUserInfo(data)
|
||||
}
|
||||
this.isLogin = true
|
||||
return this.isLogin
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user