login.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="p-3">
  3. <view v-if="type == 'mobile'" class="d-flex flex-column align-center justify-center">
  4. <image class="width-300 m-5" src="https://mncdn.wenlvti.net/app_static/minnan/logo.png" mode="widthFix"></image>
  5. <view class="w-100 mt-3 mb-3">
  6. <u-form
  7. labelPosition="top"
  8. labelWidth="180"
  9. :model="loginFormModel"
  10. :rules="loginFormRules"
  11. ref="uForm"
  12. >
  13. <u-form-item
  14. label="手机号"
  15. prop="mobile"
  16. borderBottom
  17. >
  18. <u-input
  19. v-model="loginFormModel.mobile"
  20. placeholder="请输入手机号"
  21. />
  22. </u-form-item>
  23. <u-form-item
  24. label="密码"
  25. prop="password"
  26. borderBottom
  27. >
  28. <u-input
  29. v-model="loginFormModel.password"
  30. :password="true"
  31. placeholder="请输入密码(6-16位)"
  32. ></u-input>
  33. </u-form-item>
  34. </u-form>
  35. </view>
  36. <u-button type="primary" @click="loginMobile">
  37. 登录
  38. </u-button>
  39. <view class="mt-3" />
  40. <u-button class="mt-3" type="primary" :plain="true" @click="type='wechat'">
  41. 微信登录
  42. </u-button>
  43. </view>
  44. <view v-if="type == 'wechat'" class="d-flex flex-column align-center justify-center">
  45. <image class="width-300 m-5" src="https://mncdn.wenlvti.net/app_static/minnan/logo.png" mode="widthFix"></image>
  46. <view class="mt-3 mb-3">
  47. <text class="text-align-center">请点击微信登录,并授权获取公开信息, 登录后您将获得更多权益</text>
  48. </view>
  49. <u-button type="primary" @click="loginWechat">
  50. 微信登录
  51. </u-button>
  52. <!-- <view class="mt-3" />
  53. <u-button type="primary" :plain="true" @click="type='mobile'">
  54. 手机号登录
  55. </u-button> -->
  56. </view>
  57. </view>
  58. </template>
  59. <script setup lang="ts">
  60. import { useAuthStore } from '@/store/auth';
  61. import { onLoad } from '@dcloudio/uni-app';
  62. import { back } from '@/common/utils/PageAction';
  63. import { toast } from '@/common/utils/DialogAction';
  64. import { ref } from 'vue';
  65. import { showError } from '@/common/composeabe/ErrorDisplay';
  66. const type = ref('wechat');
  67. const authStore = useAuthStore();
  68. const loginFormModel = ref({
  69. mobile: '',
  70. password: '',
  71. });
  72. const loginFormRules = {
  73. 'mobile': {
  74. type: 'string',
  75. required: true,
  76. message: '请填写手机号',
  77. trigger: ['blur', 'change']
  78. },
  79. 'password': {
  80. type: 'string',
  81. min: 6,
  82. max: 16,
  83. required: true,
  84. message: '请填写密码(6-16位)',
  85. trigger: ['blur', 'change']
  86. },
  87. }
  88. function loginWechat() {
  89. uni.showLoading({ title: '登录中...' });
  90. Promise.all([
  91. uni.login({ provider: 'weixin' }),
  92. uni.getUserProfile({ desc: '用于完善会员资料' }),
  93. ])
  94. .then((res) => {
  95. console.log(res);
  96. //return;
  97. authStore.loginWechart(res[0].code, res[1]).then(() => {
  98. toast('登录成功');
  99. setTimeout(back, 200);
  100. }).catch(showError);
  101. })
  102. .catch(showError)
  103. .finally(() => uni.hideLoading());
  104. }
  105. function loginMobile() {
  106. uni.showLoading({ title: '登录中...' });
  107. authStore.loginMobile(
  108. loginFormModel.value.mobile,
  109. loginFormModel.value.password
  110. ).then(() => {
  111. toast('登录成功');
  112. setTimeout(back, 200);
  113. }).catch(showError)
  114. }
  115. onLoad(() => {
  116. if (authStore.isLogged) {
  117. setTimeout(() => {
  118. back();
  119. }, 200);
  120. } else {
  121. }
  122. })
  123. </script>