me.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view class="container">
  3. <view :style="{height: statusBarHeight + 'px'}"></view>
  4. <!-- 用户信息展示 -->
  5. <view class="user-section" @click="showLoginPopup = true">
  6. <text v-if="!userInfo.phone">欢迎登录</text>
  7. <text v-else>{{ userInfo.phone }}</text>
  8. </view>
  9. <!-- 登录弹窗 -->
  10. <view v-if="showLoginPopup" class="login-popup" @click="showLoginPopup = false">
  11. <view class="popup-content" @click.stop>
  12. <button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" class="login-btn">
  13. 一键登录
  14. </button>
  15. </view>
  16. </view>
  17. <!-- 底部导航 -->
  18. <Tabbar :current-page="4" />
  19. </view>
  20. </template>
  21. <script>
  22. import Tabbar from "@/components/Tabbar/index.vue";
  23. export default {
  24. components: {
  25. Tabbar
  26. },
  27. data() {
  28. return {
  29. statusBarHeight: getApp().globalData.statusBarHeight,
  30. showLoginPopup: false,
  31. userInfo: {
  32. phone: ''
  33. }
  34. }
  35. },
  36. methods: {
  37. // 获取手机号回调
  38. async onGetPhoneNumber(e) {
  39. console.log(1111, e)
  40. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  41. try {
  42. const {
  43. code,
  44. encryptedData,
  45. iv
  46. } = e.detail
  47. // 发送到后端解密
  48. const res = await uni.request({
  49. url: 'YOUR_BACKEND_API',
  50. method: 'POST',
  51. data: {
  52. code,
  53. encryptedData,
  54. iv
  55. }
  56. })
  57. // 处理登录成功
  58. if (res.data.success) {
  59. this.userInfo.phone = res.data.phone
  60. uni.setStorageSync('userInfo', this.userInfo)
  61. this.showLoginPopup = false
  62. uni.showToast({
  63. title: '登录成功'
  64. })
  65. }
  66. } catch (error) {
  67. uni.showToast({
  68. title: '登录失败',
  69. icon: 'none'
  70. })
  71. }
  72. } else {
  73. uni.showToast({
  74. title: '已取消授权',
  75. icon: 'none'
  76. })
  77. }
  78. }
  79. },
  80. onLoad() {
  81. // 加载本地存储的用户信息
  82. const user = uni.getStorageSync('userInfo')
  83. if (user) this.userInfo = user
  84. }
  85. }
  86. </script>
  87. <style scoped>
  88. .user-section {
  89. padding: 30rpx;
  90. font-size: 32rpx;
  91. color: #333;
  92. }
  93. .login-popup {
  94. position: fixed;
  95. top: 0;
  96. left: 0;
  97. right: 0;
  98. bottom: 0;
  99. background: rgba(0, 0, 0, 0.5);
  100. display: flex;
  101. justify-content: center;
  102. align-items: flex-end;
  103. transition: all 0.3s ease;
  104. z-index: 999;
  105. }
  106. .popup-content {
  107. background: #fff;
  108. width: 100%;
  109. border-radius: 24rpx 24rpx 0 0;
  110. padding: 40rpx;
  111. box-sizing: border-box;
  112. transform: translateY(100%);
  113. transition: transform 0.3s ease;
  114. }
  115. .login-popup .popup-content {
  116. transform: translateY(0);
  117. }
  118. .login-btn {
  119. background: #07c160;
  120. color: #fff;
  121. border-radius: 48rpx;
  122. font-size: 32rpx;
  123. height: 96rpx;
  124. line-height: 96rpx;
  125. margin: 0;
  126. }
  127. .login-btn::after {
  128. border: none;
  129. }
  130. </style>