login.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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="baseLogo" 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="baseLogo" 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 baseLogo from 'https://mn.wenlvti.net/app_static/minnan/logo.png';
  62. import { onLoad } from '@dcloudio/uni-app';
  63. import { back } from '@/common/utils/PageAction';
  64. import { toast } from '@/common/utils/DialogAction';
  65. import { ref } from 'vue';
  66. import { showError } from '@/common/composeabe/ErrorDisplay';
  67. const type = ref('wechat');
  68. const authStore = useAuthStore();
  69. const loginFormModel = ref({
  70. mobile: '',
  71. password: '',
  72. });
  73. const loginFormRules = {
  74. 'mobile': {
  75. type: 'string',
  76. required: true,
  77. message: '请填写手机号',
  78. trigger: ['blur', 'change']
  79. },
  80. 'password': {
  81. type: 'string',
  82. min: 6,
  83. max: 16,
  84. required: true,
  85. message: '请填写密码(6-16位)',
  86. trigger: ['blur', 'change']
  87. },
  88. }
  89. function loginWechat() {
  90. uni.showLoading({ title: '登录中...' });
  91. Promise.all([
  92. uni.login({ provider: 'weixin' }),
  93. uni.getUserProfile({ desc: '用于完善会员资料' }),
  94. ])
  95. .then((res) => {
  96. console.log(res);
  97. //return;
  98. authStore.loginWechart(res[0].code, res[1]).then(() => {
  99. toast('登录成功');
  100. setTimeout(back, 200);
  101. }).catch(showError);
  102. })
  103. .catch(showError)
  104. .finally(() => uni.hideLoading());
  105. }
  106. function loginMobile() {
  107. uni.showLoading({ title: '登录中...' });
  108. authStore.loginMobile(
  109. loginFormModel.value.mobile,
  110. loginFormModel.value.password
  111. ).then(() => {
  112. toast('登录成功');
  113. setTimeout(back, 200);
  114. }).catch(showError)
  115. }
  116. onLoad(() => {
  117. if (authStore.isLogged) {
  118. setTimeout(() => {
  119. back();
  120. }, 200);
  121. } else {
  122. }
  123. })
  124. </script>