qrCode.vue 10.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
<!--
 * @Date: 2024-01-16 10:06:47
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-01-24 14:12:30
 * @FilePath: /xyxBooking-weapp/src/components/qrCode.vue
 * @Description: 预约码卡组件
-->
<template>
  <view class="qr-code-page">
    <view v-if="userList.length" class="show-qrcode">
      <view class="qrcode-content">
        <view class="user-info">{{ userinfo.name }}&nbsp;{{ userinfo.id }}</view>
        <view class="user-qrcode">
          <view class="left" @tap="prevCode">
            <image src="https://cdn.ipadbiz.cn/xys/booking/%E5%B7%A6@2x.png" />
          </view>
          <view class="center">
            <image :src="currentQrCodeUrl" mode="aspectFit" />
            <view
              v-if="useStatus === STATUS_CODE.CANCELED || useStatus === STATUS_CODE.USED"
              class="qrcode-used"
            >
              <view class="overlay"></view>
              <text class="status-text">二维码{{ get_qrcode_status_text(useStatus) }}</text>
            </view>
          </view>
          <view class="right" @tap="nextCode">
            <image src="https://cdn.ipadbiz.cn/xys/booking/%E5%8F%B3@2x.png" />
          </view>
        </view>
        <view style="color: red; margin-top: 32rpx">{{ userinfo.datetime }}</view>
      </view>
      <view class="user-list">
        <view
          @tap="selectUser(index)"
          v-for="(item, index) in userList"
          :key="index"
          :class="[
            'user-item',
            select_index === index ? 'checked' : '',
            userList.length > 1 && item.sort ? 'border' : ''
          ]"
        >
          {{ item.name }}
        </view>
      </view>
    </view>
    <view v-else class="no-qrcode">
      <image
        src="https://cdn.ipadbiz.cn/xys/booking/%E6%9A%82%E6%97%A0@2x.png"
        style="width: 320rpx; height: 320rpx"
      />
      <view class="no-qrcode-title">今天没有预约记录</view>
      <view style="text-align: center; color: #a67939; margin-top: 16rpx"
        >查看我的“<text @tap="toRecord" style="text-decoration: underline; color: #ed9820"
          >预约记录</text
        >”</view
      >
    </view>
  </view>
</template>

<script setup>
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'
import { formatDatetime, mask_id_number, get_qrcode_status_text } from '@/utils/tools'
import { qrcodeListAPI, qrcodeStatusAPI, billPersonAPI } from '@/api/index'
import { useGo } from '@/hooks/useGo'
import BASE_URL from '@/utils/config'

const go = useGo()

const props = defineProps({
  status: {
    type: String,
    default: ''
  },
  type: {
    type: String,
    default: ''
  },
  payId: {
    // 接收 payId
    type: String,
    default: ''
  }
})

const emit = defineEmits(['status-change'])

const select_index = ref(0)
const userList = ref([])

/**
 * @description 切换到上一张二维码(循环)
 * @returns {void} 无返回值
 */
const prevCode = () => {
  select_index.value = select_index.value - 1
  if (select_index.value < 0) {
    select_index.value = userList.value.length - 1
  }
}

/**
 * @description 切换到下一张二维码(循环)
 * @returns {void} 无返回值
 */
const nextCode = () => {
  select_index.value = select_index.value + 1
  if (select_index.value > userList.value.length - 1) {
    select_index.value = 0
  }
}

watch(
  () => select_index.value,
  () => {
    refreshBtn()
  }
)

watch(
  () => props.payId,
  val => {
    if (val) {
      init()
    }
  },
  { immediate: true }
)

const formatId = id => mask_id_number(id)

const userinfo = computed(() => {
  return {
    name: userList.value[select_index.value]?.name,
    id: formatId(userList.value[select_index.value]?.id_number),
    datetime: userList.value[select_index.value]?.datetime
  }
})

const currentQrCodeUrl = computed(() => {
  const url = userList.value[select_index.value]?.qr_code_url
  if (url && url.startsWith('/')) {
    return BASE_URL + url
  }
  return url
})

const useStatus = ref('0')

const STATUS_CODE = {
  APPLY: '1',
  SUCCESS: '3',
  CANCELED: '5',
  USED: '7'
}

/**
 * @description 刷新当前选中二维码状态
 * - 仅在当前选中用户存在时请求
 * - 状态改变时通知父组件刷新订单信息
 * @returns {Promise<void>} 无返回值
 */
const refreshBtn = async () => {
  if (!userList.value[select_index.value]) {
    return
  }
  const { code, data } = await qrcodeStatusAPI({
    qr_code: userList.value[select_index.value].qr_code
  })
  if (code) {
    const oldStatus = useStatus.value
    useStatus.value = data.status

    // 当状态发生改变时,通知父组件刷新订单信息
    if (oldStatus !== data.status) {
      emit('status-change', data.status)
    }
  }
}

/**
 * @description 选择指定参观者的二维码
 * @param {number} index 下标
 * @returns {void} 无返回值
 */
const selectUser = index => {
  select_index.value = index
}

/**
 * @description 按 pay_id 分组标记(用于展示分隔线)
 * - 首个 pay_id 出现处标记 sort=1,其余为 sort=0
 * @param {Array<Object>} data 二维码列表
 * @returns {Array<Object>} 处理后的列表
 */
const formatGroup = data => {
  let lastPayId = null
  for (let i = 0; i < data.length; i++) {
    if (data[i].pay_id !== lastPayId) {
      data[i].sort = 1
      lastPayId = data[i].pay_id
    } else {
      data[i].sort = 0
    }
  }
  return data
}

/**
 * @description 初始化二维码列表
 * - 不传 type:拉取“我的当日二维码列表”
 * - 传入 type + payId:按订单查询二维码人员列表
 * @returns {Promise<void>} 无返回值
 */
const init = async () => {
  if (!props.type) {
    try {
      const { code, data } = await qrcodeListAPI()

      if (code) {
        data.forEach(item => {
          item.qr_code_url = `/admin?m=srv&a=get_qrcode&key=${item.qr_code}`
          item.datetime = formatDatetime({ begin_time: item.begin_time, end_time: item.end_time })
          item.sort = 0
        })
        // 剔除qr_code为空的二维码
        const validData = data.filter(item => item.qr_code !== '')

        if (validData.length > 0) {
          userList.value = formatGroup(validData)
          refreshBtn()
        } else {
          userList.value = []
        }
      }
    } catch (err) {
      console.error('Fetch QR List Failed:', err)
    }
  } else {
    if (props.payId) {
      const { code, data } = await billPersonAPI({ pay_id: props.payId })
      if (code) {
        data.forEach(item => {
          item.qr_code_url = `/admin?m=srv&a=get_qrcode&key=${item.qr_code}`
          item.sort = 0
          // billPersonAPI 返回的数据可能没有 datetime 字段,需要检查
          // 如果没有,可能需要从外部传入或者假设是当天的?
          // H5 代码没有处理 datetime,但在 template 里用了。
          // 这里暂且不做处理,如果没有 datetime 就不显示
        })
        const validData = data.filter(item => item.qr_code !== '')
        if (validData.length > 0) {
          userList.value = validData
          refreshBtn()
        } else {
          userList.value = []
        }
      }
    }
  }
}

onMounted(() => {
  init()
  start_polling()
})

/**
 * @description 轮询刷新二维码状态
 * - 仅在“待使用”状态下轮询,避免无意义请求
 * @returns {Promise<void>} 无返回值
 */
const poll = async () => {
  if (userList.value.length && useStatus.value === STATUS_CODE.SUCCESS) {
    if (userList.value[select_index.value]) {
      const { code, data } = await qrcodeStatusAPI({
        qr_code: userList.value[select_index.value].qr_code
      })
      if (code) {
        useStatus.value = data.status
      }
    }
  }
}

const interval_id = ref(null)
/**
 * @description 启动轮询
 * - 仅在当前选中用户存在时轮询
 * @returns {void} 无返回值
 */

const start_polling = () => {
  if (interval_id.value) {
    return
  }
  interval_id.value = setInterval(poll, 3000)
}

/**
 * @description 停止轮询
 * - 组件卸载时调用,避免内存泄漏
 * @returns {void} 无返回值
 */

const stop_polling = () => {
  if (!interval_id.value) {
    return
  }
  clearInterval(interval_id.value)
  interval_id.value = null
}

onUnmounted(() => {
  stop_polling()
})

defineExpose({
  start_polling,
  stop_polling
})

/**
 * @description 跳转预约记录列表页
 * @returns {void} 无返回值
 */
const toRecord = () => {
  go('/bookingList')
}
</script>

<style lang="less">
.qr-code-page {
  .qrcode-content {
    padding: 32rpx 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    border-radius: 16rpx;
    box-shadow: 0 0 29rpx 0 rgba(106, 106, 106, 0.27);

    .user-info {
      color: #a6a6a6;
      font-size: 37rpx;
      margin-top: 16rpx;
      margin-bottom: 16rpx;
    }
    .user-qrcode {
      display: flex;
      align-items: center;
      .left {
        image {
          width: 56rpx;
          height: 56rpx;
          margin-right: 16rpx;
        }
      }
      .center {
        border: 2rpx solid #d1d1d1;
        border-radius: 40rpx;
        padding: 46rpx;
        position: relative;
        image {
          width: 400rpx;
          height: 400rpx;
        }
        .qrcode-used {
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          border-radius: 40rpx;
          overflow: hidden;

          .overlay {
            width: 100%;
            height: 100%;
            background-image: url('https://cdn.ipadbiz.cn/xys/booking/southeast.jpeg');
            background-size: contain;
            opacity: 0.9;
          }

          .status-text {
            color: #a67939;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 38rpx;
            white-space: nowrap;
            font-weight: bold;
            z-index: 10;
          }
        }
      }
      .right {
        image {
          width: 56rpx;
          height: 56rpx;
          margin-left: 16rpx;
        }
      }
    }
  }
  .user-list {
    display: flex;
    padding: 32rpx;
    align-items: center;
    flex-wrap: wrap;
    .user-item {
      position: relative;
      padding: 8rpx 16rpx;
      border: 2rpx solid #a67939;
      margin: 8rpx;
      border-radius: 10rpx;
      color: #a67939;
      &.checked {
        color: #fff;
        background-color: #a67939;
      }
      &.border {
        margin-right: 16rpx;
        &::after {
          position: absolute;
          right: -16rpx;
          top: calc(50% - 16rpx);
          content: '';
          height: 32rpx;
          border-right: 2rpx solid #a67939;
        }
      }
    }
  }

  .no-qrcode {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    margin-bottom: 32rpx;

    .no-qrcode-title {
      color: #a67939;
      font-size: 34rpx;
    }
  }
}
</style>