wx-starlock/pages/feature/createCard.vue

421 lines
11 KiB
Vue
Raw Permalink Normal View History

2025-02-07 16:28:45 +08:00
<template>
<view>
<view class="tabs">
<up-tabs
:list="tabs"
lineWidth="40rpx"
lineHeight="5rpx"
:current="currentIndex"
2025-07-29 11:07:43 +08:00
lineColor="#4777ee"
2025-02-07 16:28:45 +08:00
@click="clickTab"
:inactiveStyle="{ color: '#a3a3a3', fontSize: '32rpx', fontWeight: 'bold' }"
2025-07-29 11:07:43 +08:00
:activeStyle="{ color: '#4777ee', fontSize: '32rpx', fontWeight: 'bold' }"
2025-02-07 16:28:45 +08:00
>
</up-tabs>
</view>
<swiper
:style="{ height: deviceInfo.windowHeight - deviceInfo.safeArea.top - 44 + 'px' }"
2025-02-07 16:28:45 +08:00
v-if="deviceInfo"
:list="tabs"
:autoplay="false"
:circular="true"
:current="currentIndex"
@change="changeSwiper"
>
<swiper-item>
<LockInput
:value="permanentName"
title="姓名"
placeholder="请输入"
2025-02-08 18:31:14 +08:00
@change-input="changeName('permanent', $event)"
2025-02-07 16:28:45 +08:00
></LockInput>
2025-02-21 14:01:36 +08:00
<view class="mt-3" v-if="$bluetooth.currentLockInfo.userType === 110301">
2025-02-07 16:28:45 +08:00
<LockSwitch
2025-02-08 18:31:14 +08:00
:value="permanentAdmin"
2025-02-07 16:28:45 +08:00
title="是否为管理员"
2025-02-08 18:31:14 +08:00
@change="changeAdmin('permanent', $event)"
2025-02-07 16:28:45 +08:00
></LockSwitch>
</view>
<view class="mt-3 mb-3">
<LockSwitch
2025-02-08 18:31:14 +08:00
:value="permanentCoerced"
2025-02-07 16:28:45 +08:00
title="胁迫卡"
2025-02-08 18:31:14 +08:00
@change="changeCoerced('permanent', $event)"
2025-02-07 16:28:45 +08:00
:placeholder="placeholder"
></LockSwitch>
</view>
2025-02-08 18:31:14 +08:00
<view class="button" @click="create('permanent')">下一步</view>
2025-02-07 16:28:45 +08:00
</swiper-item>
2025-02-12 17:26:21 +08:00
<swiper-item>
2025-02-07 16:28:45 +08:00
<LockInput
:value="temporaryName"
title="姓名"
placeholder="请输入"
2025-02-08 18:31:14 +08:00
@change-input="changeName('temporary', $event)"
2025-02-07 16:28:45 +08:00
></LockInput>
<view class="mt-3">
<LockDatetimePicker
title="生效时间"
2025-02-08 18:31:14 +08:00
:value="temporaryStartTime"
2025-02-07 16:28:45 +08:00
:minDate="minDate"
:maxDate="maxDate"
2025-02-08 18:31:14 +08:00
@change-time="changeDate('temporaryStartTime', $event)"
2025-02-07 16:28:45 +08:00
></LockDatetimePicker>
</view>
<view class="mt-3">
<LockDatetimePicker
title="失效时间"
2025-02-08 18:31:14 +08:00
:value="temporaryEndTime"
2025-02-07 16:28:45 +08:00
:minDate="minDate"
:maxDate="maxDate"
2025-02-08 18:31:14 +08:00
@change-time="changeDate('temporaryEndTime', $event)"
2025-02-07 16:28:45 +08:00
></LockDatetimePicker>
</view>
2025-02-21 14:01:36 +08:00
<view class="mt-3" v-if="$bluetooth.currentLockInfo.userType === 110301">
2025-02-07 16:28:45 +08:00
<LockSwitch
2025-02-08 18:31:14 +08:00
:value="temporaryAdmin"
2025-02-07 16:28:45 +08:00
title="是否为管理员"
2025-02-08 18:31:14 +08:00
@change="changeAdmin('temporary', $event)"
2025-02-07 16:28:45 +08:00
></LockSwitch>
</view>
<view class="mt-3 mb-3">
<LockSwitch
2025-02-08 18:31:14 +08:00
:value="temporaryCoerced"
2025-02-07 16:28:45 +08:00
title="胁迫卡"
2025-02-08 18:31:14 +08:00
@change="changeCoerced('temporary', $event)"
2025-02-07 16:28:45 +08:00
:placeholder="placeholder"
></LockSwitch>
</view>
2025-02-08 18:31:14 +08:00
<view class="button" @click="create('temporary')">下一步</view>
2025-02-07 16:28:45 +08:00
</swiper-item>
2025-02-12 17:26:21 +08:00
<swiper-item>
<LockInput
:value="cycleName"
title="姓名"
placeholder="请输入"
@change-input="changeName('cycle', $event)"
></LockInput>
<view class="mt-3">
<LockCycle @change="changeCycle"></LockCycle>
</view>
2025-02-21 14:01:36 +08:00
<view class="mt-3" v-if="$bluetooth.currentLockInfo.userType === 110301">
2025-02-12 17:26:21 +08:00
<LockSwitch
:value="cycleAdmin"
title="是否为管理员"
@change="changeAdmin('cycle', $event)"
></LockSwitch>
</view>
<view class="mt-3 mb-3">
<LockSwitch
:value="cycleCoerced"
title="胁迫卡"
@change="changeCoerced('cycle', $event)"
:placeholder="placeholder"
></LockSwitch>
</view>
<view class="button" @click="create('cycle')">下一步</view>
</swiper-item>
2025-02-07 16:28:45 +08:00
</swiper>
</view>
</template>
<script setup>
2025-02-08 18:31:14 +08:00
import { getCurrentInstance, ref } from 'vue'
2025-02-07 16:28:45 +08:00
import { onLoad } from '@dcloudio/uni-app'
2025-02-12 17:26:21 +08:00
import { timeFormat } from 'uview-plus'
2025-02-07 16:28:45 +08:00
import { useBasicStore } from '@/stores/basic'
2025-02-08 18:31:14 +08:00
import { useUserStore } from '@/stores/user'
import { useBluetoothStore } from '@/stores/bluetooth'
import { checkCardNameRequest } from '@/api/card'
import { transportType } from '@/constant/transportType'
2025-02-08 18:31:14 +08:00
const instance = getCurrentInstance().proxy
const eventChannel = instance.getOpenerEventChannel()
2025-02-07 16:28:45 +08:00
const $basic = useBasicStore()
2025-02-08 18:31:14 +08:00
const $bluetooth = useBluetoothStore()
const $user = useUserStore()
2025-02-07 16:28:45 +08:00
const tabs = [
{
name: '永久'
},
{
name: '限时'
2025-02-12 17:26:21 +08:00
},
{
name: '循环'
2025-02-07 16:28:45 +08:00
}
]
2025-02-08 18:31:14 +08:00
2025-02-07 16:28:45 +08:00
const permanentName = ref('')
2025-02-08 18:31:14 +08:00
const permanentAdmin = ref(false)
const permanentCoerced = ref(false)
2025-02-07 16:28:45 +08:00
const temporaryName = ref('')
2025-02-08 18:31:14 +08:00
const temporaryStartTime = ref(Number(new Date()))
const temporaryEndTime = ref(Number(new Date()))
const temporaryAdmin = ref(false)
const temporaryCoerced = ref(false)
2025-02-12 17:26:21 +08:00
const cycleName = ref('')
const cycleStartTime = ref(null)
const cycleEndTime = ref(null)
const weekDays = ref([])
const cycleAdmin = ref(false)
const cycleCoerced = ref(false)
2025-02-07 16:28:45 +08:00
const minDate = ref(Number(new Date()))
const maxDate = ref(Number(4133951940000))
const currentIndex = ref(0)
const deviceInfo = ref(null)
const placeholder =
'当被胁迫要求强行开锁时,使用胁迫卡会触发报警,报警信息会推送给管理员,该功能需要锁联网。\n请不要将胁迫卡用于日常开锁'
onLoad(async () => {
deviceInfo.value = await $basic.getDeviceInfo()
2025-02-08 18:31:14 +08:00
temporaryStartTime.value = setTime()
temporaryEndTime.value = setTime()
2025-02-07 16:28:45 +08:00
minDate.value = Number(getNextFullHour())
maxDate.value = Number(getFutureTimestamp())
})
2025-02-12 17:26:21 +08:00
const changeCycle = data => {
cycleStartTime.value = data.startDate
cycleEndTime.value = data.endDate
weekDays.value = data.weekDays
}
2025-02-07 16:28:45 +08:00
const getNextFullHour = () => {
const now = new Date()
const currentHour = now.getHours()
now.setHours(currentHour)
now.setMinutes(0)
now.setSeconds(0)
now.setMilliseconds(0)
return now
}
const getFutureTimestamp = () => {
const currentDate = new Date()
const year = currentDate.getFullYear()
const month = currentDate.getMonth()
const day = currentDate.getDate()
const futureDate = new Date(year + 3, month, day, 23, 0, 0)
return futureDate.getTime()
}
const setTime = () => {
const now = new Date()
return now.getTime()
}
2025-02-08 18:31:14 +08:00
const create = async type => {
2025-02-07 16:28:45 +08:00
if (
(type === 'temporary' && temporaryName.value === '') ||
2025-02-12 17:26:21 +08:00
(type === 'permanent' && permanentName.value === '') ||
(type === 'cycle' && cycleName.value === '')
2025-02-07 16:28:45 +08:00
) {
uni.showToast({
title: '名称不能为空',
icon: 'none'
})
return
}
2025-02-17 15:41:41 +08:00
if (type === 'temporary' && temporaryStartTime.value >= temporaryEndTime.value) {
2025-02-08 18:31:14 +08:00
uni.showToast({
title: '失效时间要大于生效时间',
icon: 'none'
})
2025-02-07 16:28:45 +08:00
return
}
2025-02-12 17:26:21 +08:00
if (type === 'cycle' && weekDays.value.length === 0) {
uni.showToast({
title: '请选择有效期',
icon: 'none'
})
return
}
2025-02-08 18:31:14 +08:00
const netWork = await $basic.getNetworkType()
if (!netWork) {
2025-02-07 16:28:45 +08:00
return
}
let params = {
2025-02-09 14:38:52 +08:00
type: 'card',
2025-02-08 18:31:14 +08:00
keyId: $bluetooth.keyId.toString(),
uid: $user.userInfo.uid.toString(),
2025-02-09 14:38:52 +08:00
no: 0,
2025-02-08 18:31:14 +08:00
operate: 0,
userCountLimit: 0xffff
2025-02-07 16:28:45 +08:00
}
2025-02-08 18:31:14 +08:00
if (type === 'permanent') {
params = {
...params,
cardName: permanentName.value,
isAdmin: permanentAdmin.value ? 1 : 0,
isForce: permanentCoerced.value ? 1 : 0,
weekDays: [],
isRound: 0,
startDate: 0,
cardType: 1,
endDate: 0,
startTime: '00:00',
endTime: '00:00'
}
2025-02-12 17:26:21 +08:00
} else if (type === 'temporary') {
2025-02-08 18:31:14 +08:00
params = {
...params,
cardName: temporaryName.value,
isAdmin: temporaryAdmin.value ? 1 : 0,
isForce: temporaryCoerced.value ? 1 : 0,
weekDays: [],
cardType: 2,
isRound: 0,
startDate: temporaryStartTime.value,
endDate: temporaryEndTime.value,
startTime: '00:00',
endTime: '00:00'
}
2025-02-12 17:26:21 +08:00
} else {
params = {
...params,
cardName: cycleName.value,
isAdmin: cycleAdmin.value ? 1 : 0,
isForce: cycleCoerced.value ? 1 : 0,
weekDays: weekDays.value,
cardType: 4,
isRound: 1,
startDate: cycleStartTime.value,
endDate: cycleEndTime.value,
startTime: timeFormat(new Date(cycleStartTime.value), 'h:M'),
endTime: timeFormat(new Date(cycleEndTime.value), 'h:M')
}
2025-02-07 16:28:45 +08:00
}
2025-02-08 18:31:14 +08:00
const { code, message } = await checkCardNameRequest({
lockId: $bluetooth.currentLockInfo.lockId,
cardName: params.cardName
})
2025-02-07 16:28:45 +08:00
if (code === 0) {
2025-04-29 16:43:12 +08:00
if ($bluetooth.currentLockInfo.transportType === transportType.TRANSPORT_TENCENT_YUN) {
const { code } = await $bluetooth.registerAuthentication(params)
if (code === 0) {
$basic.backAndToast('请在锁端添加卡片')
} else {
uni.showToast({
title: '添加卡片命令下达失败,请重试',
icon: 'none'
})
2025-02-07 16:28:45 +08:00
}
} else {
$basic.routeJump({
name: 'bindCard',
params: {
card: JSON.stringify(params)
},
events: {
refresherList() {
eventChannel.emit('refresherList', {})
}
}
})
}
2025-02-07 16:28:45 +08:00
} else {
uni.showToast({
title: message,
icon: 'none'
})
}
}
2025-02-08 18:31:14 +08:00
const changeName = (type, event) => {
if (type === 'permanent') {
permanentName.value = event
2025-02-12 17:26:21 +08:00
} else if (type === 'temporary') {
2025-02-08 18:31:14 +08:00
temporaryName.value = event
2025-02-12 17:26:21 +08:00
} else {
cycleName.value = event
2025-02-08 18:31:14 +08:00
}
}
const changeDate = (type, event) => {
if (type === 'temporaryStartTime') {
temporaryStartTime.value = event
} else {
temporaryEndTime.value = event
}
2025-02-07 16:28:45 +08:00
}
2025-02-08 18:31:14 +08:00
const changeAdmin = (type, event) => {
if (type === 'permanent') {
permanentAdmin.value = event.detail.value
2025-02-12 17:26:21 +08:00
} else if (type === 'temporary') {
2025-02-08 18:31:14 +08:00
temporaryAdmin.value = event.detail.value
2025-02-12 17:26:21 +08:00
} else {
cycleAdmin.value = event.detail.value
2025-02-08 18:31:14 +08:00
}
2025-02-07 16:28:45 +08:00
}
2025-02-08 18:31:14 +08:00
const changeCoerced = (type, event) => {
if (type === 'permanent') {
permanentCoerced.value = event.detail.value
2025-02-12 17:26:21 +08:00
} else if (type === 'temporary') {
2025-02-08 18:31:14 +08:00
temporaryCoerced.value = event.detail.value
2025-02-12 17:26:21 +08:00
} else {
cycleCoerced.value = event.detail.value
2025-02-08 18:31:14 +08:00
}
2025-02-07 16:28:45 +08:00
}
const clickTab = data => {
currentIndex.value = data.index
}
const changeSwiper = e => {
currentIndex.value = e.detail.current
}
</script>
<style lang="scss">
page {
background-color: $uni-bg-color-grey;
}
</style>
<style lang="scss" scoped>
.tabs {
display: flex;
justify-content: center;
}
.text {
padding: 0 32rpx;
2025-02-07 16:28:45 +08:00
margin-top: 40rpx;
margin-bottom: 50rpx;
font-size: 26rpx;
color: #262626;
2025-02-07 16:28:45 +08:00
}
.button {
width: 686rpx;
height: 100rpx;
margin-left: 32rpx;
font-size: 32rpx;
font-weight: bold;
2025-02-07 16:28:45 +08:00
line-height: 100rpx;
color: #fff;
2025-02-07 16:28:45 +08:00
text-align: center;
2025-07-29 11:07:43 +08:00
background-color: #4777ee;
border-radius: 64rpx;
2025-02-07 16:28:45 +08:00
}
</style>