index.vue 791 B

123456789101112131415161718192021222324252627282930313233343536
  1. <route lang="json5">
  2. {
  3. style: { navigationBarTitleText: '我的' },
  4. }
  5. </route>
  6. <template>
  7. <view class="ml-4">wx的openid: </view>
  8. <view class="ml-4">{{ openId }}</view>
  9. <wx-login />
  10. </template>
  11. <script lang="ts" setup>
  12. import { useUserStore } from '@/store'
  13. import { http } from '@/utils/http'
  14. import WxLogin from './components/wx-login.vue'
  15. const userStore = useUserStore()
  16. const openId = ref('')
  17. // 用户登录,获取openId
  18. uni.login({
  19. provider: 'weixin',
  20. success: async ({ code }) => {
  21. const res = await http<{ session_key: string; openid: string }>({
  22. method: 'GET',
  23. url: '/weixin/jscode2session',
  24. data: {
  25. code,
  26. },
  27. })
  28. openId.value = res.result.openid
  29. userStore.setUserInfo({ openid: res.result.openid })
  30. },
  31. })
  32. </script>