login.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <view class="container">
  3. <image src="/static/login-bg.png" class="bg-image" mode="aspectFit" />
  4. <view class="btns">
  5. <!-- <button class="primary-btn" @click="oneClickLogin">手机号一键登录</button> -->
  6. <button class="primary-btn"
  7. open-type="getPhoneNumber"
  8. @getphonenumber="onGetPhoneNumber">手机号一键登录</button>
  9. <button class="plain-btn" plain @click="goBindPhone">使用其他号码登录</button>
  10. </view>
  11. <view class="agreement">
  12. <checkbox class="custom-checkbox" :checked="checked" @click="checked = !checked" />
  13. <text class="agreement-text">
  14. 阅读并同意
  15. <text class="link" @tap="gotourl('/pages/agreement/user')">《用户协议》</text>
  16. <text class="link" @tap="gotourl('/pages/agreement/privacy')">《隐私声明》</text> 若您的手机号未注册,将为您自动注册
  17. </text>
  18. </view>
  19. </view>
  20. </template>
  21. <script setup>
  22. import {
  23. ref
  24. } from 'vue'
  25. import {
  26. onLoad
  27. } from '@dcloudio/uni-app'
  28. import { post, get } from '@/utils/request'
  29. const gotourl = (text) => {
  30. uni.navigateTo({
  31. url: text
  32. });
  33. }
  34. const checked = ref(false)
  35. const onGetPhoneNumber = async (e) => {
  36. if (!checked.value) {
  37. uni.showToast({ title: '请同意协议', icon: 'none' })
  38. return
  39. }
  40. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  41. uni.showToast({ title: '获取手机号失败', icon: 'none' })
  42. return
  43. }
  44. try {
  45. // console.log(e.detail, 'e.detaile.detail');
  46. const { encryptedData, iv, code } = e.detail
  47. const loginRes = await uni.login({ provider: 'weixin' });
  48. const res = await get('/api/waterUser/parsePhone',{
  49. encryptedData,
  50. iv,
  51. code: loginRes.code
  52. })
  53. if (res.code === '200') {
  54. const phone = encodeURIComponent(res.data); // 对手机号进行编码
  55. uni.navigateTo({
  56. url: `/pages/bindPhone/bindPhone?phone=${phone}`
  57. });
  58. } else {
  59. uni.showToast({ title: res.message || '手机号解析失败', icon: 'none' })
  60. }
  61. } catch (err) {
  62. // console.log('请求失败:', err)
  63. uni.showToast({ title: '请求失败,请稍后重试', icon: 'none' })
  64. }
  65. }
  66. const goBindPhone = () => {
  67. if (!checked.value) return uni.showToast({
  68. title: '请同意协议',
  69. icon: 'none'
  70. })
  71. uni.navigateTo({
  72. url: '/pages/bindPhone/bindPhone'
  73. })
  74. }
  75. onLoad(() => {
  76. // console.log(import.meta.env.MODE) // development / production
  77. // console.log(import.meta.env.VITE_API_BASE_URL)
  78. })
  79. </script>
  80. <style scoped>
  81. .container {
  82. padding: 40rpx;
  83. display: flex;
  84. flex-direction: column;
  85. align-items: center;
  86. min-height: 100vh;
  87. box-sizing: border-box;
  88. background-color: #ffffff;
  89. /* justify-content: center; */
  90. }
  91. .bg-image {
  92. width: 100%;
  93. max-width: 600rpx;
  94. margin-top: 120rpx;
  95. }
  96. .btns {
  97. width: 100%;
  98. margin-top: 100rpx;
  99. display: flex;
  100. flex-direction: column;
  101. gap: 30rpx;
  102. }
  103. .primary-btn,
  104. .plain-btn {
  105. width: 100%;
  106. height: 90rpx;
  107. font-size: 32rpx;
  108. border-radius: 16rpx;
  109. }
  110. .primary-btn {
  111. background: linear-gradient(to right, #6da4ff, #3f7ffb);
  112. color: #ffffff;
  113. border: none;
  114. }
  115. .plain-btn {
  116. border: 2rpx solid #2979ff;
  117. color: #2979ff;
  118. background-color: #ffffff;
  119. }
  120. .agreement {
  121. margin-top: 60rpx;
  122. display: flex;
  123. align-items: center;
  124. justify-content: center;
  125. flex-direction: row;
  126. flex-wrap: nowrap;
  127. /* padding: 0 40rpx; */
  128. font-size: 24rpx;
  129. color: #666;
  130. line-height: 1.5;
  131. }
  132. .custom-checkbox {
  133. margin-right: 10rpx;
  134. transform: scale(0.8);
  135. }
  136. .agreement-text {
  137. flex: 1;
  138. text-align: left;
  139. word-break: break-word;
  140. }
  141. .link {
  142. color: #2979ff;
  143. margin: 0 6rpx;
  144. }
  145. </style>