index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view class="content" style="height:100vh">
  3. <u-loading-icon mode="circle" size="64" :vertical="true" color="#2b85e4" textColor="#2b85e4"
  4. text="正在登录"></u-loading-icon>
  5. </view>
  6. </template>
  7. <script setup>
  8. import {
  9. getCurrentInstance,
  10. reactive,
  11. computed
  12. } from 'vue'
  13. const api = getCurrentInstance()?.appContext.config.globalProperties.$api;
  14. const configSign = getCurrentInstance()?.appContext.config.globalProperties.$configSign;
  15. const openid = computed(() => {
  16. return uni.getStorageSync('openid');
  17. })
  18. const login = async (js_code) => {
  19. const result = await api('https://broadcast.waityou24.cn/wechat/api/login/app', 'GET', {
  20. js_code,
  21. config: configSign
  22. });
  23. if (result.errcode === 0) return result.data.openid;
  24. else {
  25. uni.showToast({
  26. title: '登录失败请重进',
  27. icon: 'fail',
  28. });
  29. return false;
  30. }
  31. };
  32. const initUser = async () => {
  33. uni.login({
  34. success: async function(result) {
  35. if (!result.code) {
  36. uni.showToast({
  37. title: '登录失败请重进',
  38. icon: 'fail',
  39. });
  40. return false;
  41. }
  42. let openid = uni.getStorageSync('openid');
  43. if (!openid) {
  44. const res = await login(result.code)
  45. if (res) {
  46. uni.setStorageSync('openid', res);
  47. openid = res;
  48. }
  49. }
  50. if (openid) {
  51. const result = await api('/user', 'POST', {
  52. openid
  53. });
  54. if(result.errcode!=0) {
  55. uni.showToast({
  56. title: result.errmsg,
  57. icon: 'error',
  58. });
  59. return false;
  60. }
  61. uni.navigateTo({
  62. url: '/pages/list/index'
  63. })
  64. }
  65. }
  66. })
  67. };
  68. initUser();
  69. </script>
  70. <style>
  71. .content {
  72. display: flex;
  73. flex-direction: column;
  74. align-items: center;
  75. justify-content: center;
  76. }
  77. .logo {
  78. height: 200rpx;
  79. width: 200rpx;
  80. margin-top: 200rpx;
  81. margin-left: auto;
  82. margin-right: auto;
  83. margin-bottom: 50rpx;
  84. }
  85. .text-area {
  86. display: flex;
  87. justify-content: center;
  88. }
  89. .title {
  90. font-size: 36rpx;
  91. color: #8f8f94;
  92. }
  93. </style>