index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. console.log(result)
  56. uni.showToast({
  57. title: result.errmsg || '',
  58. icon: 'error',
  59. });
  60. return false;
  61. }
  62. uni.redirectTo({
  63. url: '/pages/list/index'
  64. })
  65. }
  66. }
  67. })
  68. };
  69. initUser();
  70. </script>
  71. <style>
  72. .content {
  73. display: flex;
  74. flex-direction: column;
  75. align-items: center;
  76. justify-content: center;
  77. }
  78. .logo {
  79. height: 200rpx;
  80. width: 200rpx;
  81. margin-top: 200rpx;
  82. margin-left: auto;
  83. margin-right: auto;
  84. margin-bottom: 50rpx;
  85. }
  86. .text-area {
  87. display: flex;
  88. justify-content: center;
  89. }
  90. .title {
  91. font-size: 36rpx;
  92. color: #8f8f94;
  93. }
  94. </style>