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