<script setup lang="ts">
import { createGlobalLoadingMiddleware } from '@/api/core/middleware'
import router from '@/router'

definePage({
  name: 'smqjh-login',
  islogin: false,
  style: {
    navigationBarTitleText: '市民请集合',
  },
})
const { token, redirectName } = storeToRefs(useUserStore())
const { data, send } = useRequest((code, phoneCode) => Apis.sys.auth({ params: {
  grant_type: 'wechat',
  code,
  phoneCode,
} }), { middleware: createGlobalLoadingMiddleware({ loadingText: '微信授权登录中' }), immediate: false })
const tabList = ['/pages/my/index', '/pages/index/index', '/pages/cart/index', '/pages/classfiy/index']
async function handleGetPhone(e: UniHelper.ButtonOnGetphonenumberDetail) {
  console.log(e, '手机号')
  uni.showLoading({ mask: true })
  uni.login({
    provider: 'weixin',
    success: async (res) => {
      uni.hideLoading()
      await send(res.code, e.code)
      token.value = `Bearer ${data.value.data.access_token}`
      useGlobalToast().show({ msg: '登录成功' })
      useUserStore().getUserInfo()

      setTimeout(() => {
        if (tabList.includes(redirectName.value)) {
          router.pushTab({ path: redirectName.value })
        }
        else {
          console.log(redirectName.value, 'JSON.parse(path[1])')

          router.replace({ path: redirectName.value })
        }
      }, 2000)
    },
  })
}
function goBack() {
  uni.switchTab({
    url: '/pages/index/index',
  })
}
</script>

<template>
  <view class="h80vh px24rpx">
    <view class="mt300rpx">
      <wd-button block size="large" open-type="getPhoneNumber" @getphonenumber="handleGetPhone">
        手机号快捷登录
      </wd-button>
      <view class="mt20rpx">
        <wd-button block size="large" type="info" @click="goBack">
          暂不登录
        </wd-button>
      </view>
    </view>
  </view>
</template>

<style scoped>

</style>
