index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <route lang="json5" type="page">
  2. {
  3. layout: 'default',
  4. style: {
  5. navigationBarTitleText: '登录',
  6. },
  7. }
  8. </route>
  9. <template>
  10. <view class="flex flex-col items-center justify-center h-full">
  11. <!-- <image src="/static/wechat.png" class="w-16 h-16 mb-8" /> -->
  12. <view class="text-2xl font-bold mb-8 mt-20">达州农机优惠劵</view>
  13. <view class="text-gray-500 text-sm mb-8">申请获取以下权限</view>
  14. <view class="text-gray-500 text-sm mb-8">获取您的公开信息(昵称、头像等)</view>
  15. <wd-button type="success" round block @click="handleLogin" :loading="loading">
  16. 点击授权
  17. </wd-button>
  18. </view>
  19. </template>
  20. <script lang="ts" setup>
  21. import { getAuthCode } from '@/service/auth'
  22. import { onMounted } from 'vue'
  23. import { until } from '@vueuse/core'
  24. import { useAppStore } from '@/store/app'
  25. import { useUserStore } from '@/store/user'
  26. const userStore = useUserStore()
  27. const appStore = useAppStore()
  28. const loading = ref(false)
  29. // 微信登录
  30. const handleLogin = async () => {
  31. loading.value = true
  32. wx.getUserProfile({
  33. desc: '用于完善用户信息',
  34. success: ({ userInfo }) => {
  35. // console.log('用户信息:', userInfo)
  36. // avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
  37. // city: ""
  38. // country: ""
  39. // gender: 0
  40. // is_demote: true
  41. // language: ""
  42. // nickName: "微信用户"
  43. // province: ""
  44. userStore.setUserInfo(userInfo)
  45. // 获取token
  46. uni.login({
  47. provider: 'weixin',
  48. onlyAuthorize: true,
  49. success: async function (event) {
  50. try {
  51. const { loading, error, data } = useRequest(() => getAuthCode(event.code), {
  52. immediate: true,
  53. })
  54. // 等待数据加载完成
  55. await until(loading).toBe(false)
  56. if (!error.value && data.value) {
  57. const { token, userid, openid } = data.value as any
  58. appStore.setAppInfo({ token, userid, openid })
  59. uni.switchTab({ url: '/pages/index/index' })
  60. } else {
  61. uni.showToast({
  62. title: '授权失败,请重试',
  63. icon: 'none',
  64. })
  65. }
  66. } catch (err) {
  67. console.error('授权失败:', err)
  68. uni.showToast({
  69. title: '授权失败,请重试',
  70. icon: 'none',
  71. })
  72. }
  73. },
  74. fail: function (err) {
  75. console.error('登录失败:', err)
  76. uni.showToast({
  77. title: '登录失败,请重试',
  78. icon: 'none',
  79. })
  80. },
  81. complete: () => {
  82. loading.value = false
  83. },
  84. })
  85. },
  86. fail: function (err) {
  87. console.error('获取用户信息失败:', err)
  88. loading.value = false
  89. uni.showToast({
  90. title: '获取用户信息失败,请重试',
  91. icon: 'none',
  92. })
  93. },
  94. })
  95. }
  96. onMounted(() => {
  97. // 检查是否已经授权
  98. // uni.getSetting({
  99. // success: (res) => {
  100. // if (res.authSetting['scope.userInfo']) {
  101. // uni.navigateBack()
  102. // }
  103. // },
  104. // })
  105. })
  106. </script>
  107. <style lang="scss" scoped>
  108. //
  109. </style>