index.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="content">
  3. <view class="one">
  4. <image class="logo" :src="logoUrl||'/static/logo.png'" />
  5. </view>
  6. </view>
  7. </template>
  8. <script setup lang="ts">
  9. import { getCurrentInstance, computed, ref } from 'vue';
  10. //该依赖已内置不需要单独安装
  11. import { onShow } from "@dcloudio/uni-app";
  12. // 请求接口
  13. const $api = getCurrentInstance()?.appContext.config.globalProperties.$api;
  14. const $app = getCurrentInstance()?.appContext.config.globalProperties.$app;
  15. const $config = getCurrentInstance()?.appContext.config.globalProperties.$config;
  16. // openid
  17. const openid = computed(() => {
  18. return uni.getStorageSync('openid');
  19. })
  20. // logo图片
  21. const logoUrl = ref('');
  22. onShow(() => {
  23. search();
  24. searchConfig();
  25. })
  26. // 用户信息
  27. const search = async () => {
  28. uni.login({
  29. success: async function (result) {
  30. if (!result.code) {
  31. uni.showToast({
  32. title: '登录失败请重进!',
  33. icon: 'fail',
  34. });
  35. return false;
  36. }
  37. if (!openid.value) {
  38. const res = await login(result.code)
  39. if (res) uni.setStorageSync('openid', res);
  40. }
  41. uni.redirectTo({
  42. url: '/pages/home/index'
  43. })
  44. }
  45. })
  46. };
  47. // 获取openid
  48. const login = async (js_code : string) => {
  49. const res = await $app('/wechat/api/login/app', 'GET', {
  50. js_code: js_code,
  51. config: $config.wx_projectkey
  52. })
  53. if (res.errcode === 0) return res.data.openid;
  54. else {
  55. uni.showToast({
  56. title: '登录失败请重进!',
  57. icon: 'fail',
  58. });
  59. return false;
  60. }
  61. };
  62. // config信息
  63. const searchConfig = async () => {
  64. const res = await $api('matchconfig/findOne', 'GET', {});
  65. if (res.code === 200) {
  66. if (res.data) {
  67. logoUrl.value = res.data.logoUrl
  68. uni.setStorageSync('config', res.data);
  69. }
  70. } else {
  71. uni.showToast({
  72. title: res.msg || '',
  73. icon: 'error',
  74. });
  75. }
  76. };
  77. </script>
  78. <style lang="scss" scoped>
  79. .content {
  80. display: flex;
  81. flex-direction: column;
  82. width: 100vw;
  83. height: 100vh;
  84. .one {
  85. text-align: center;
  86. margin: 50vw 0 0 0;
  87. .logo {
  88. width: 50vw;
  89. height: 50vw;
  90. border-radius: 90px;
  91. box-shadow: 0 0 5px #f1f1f1;
  92. }
  93. }
  94. }
  95. </style>