routine_phone.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view v-if="isPhoneBox">
  3. <view class="mobile-bg" @click="close"></view>
  4. <view class="mobile-mask animated" :class="{ slideInUp: isUp }">
  5. <view class="info-box">
  6. <image :src="logoUrl"></image>
  7. <view class="title">获取授权</view>
  8. <view class="txt">获取微信的手机号授权</view>
  9. </view>
  10. <button class="sub_btn" open-type="getPhoneNumber" @getphonenumber="getphonenumber">获取微信手机号</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script setup>
  15. import { ref } from 'vue'
  16. import Routine from '@/libs/routine'
  17. import { getUserInfo } from "@/api/user"
  18. import { getUserPhone } from '@/api/public'
  19. import { useAppStore } from '@/stores/app'
  20. import { useToast } from "@/hooks/useToast"
  21. const emit = defineEmits(['close'])
  22. const props = defineProps({
  23. isPhoneBox: { type: Boolean, default: false },
  24. logoUrl: { type: String, default: '' },
  25. authKey: { type: String, default: '' }
  26. })
  27. const isStatus = ref(false)
  28. const appStore = useAppStore()
  29. const { Toast } = useToast()
  30. // #ifdef MP
  31. // 小程序获取手机号码
  32. function getphonenumber(e) {
  33. uni.showLoading({ title: '加载中' })
  34. Routine.getCode()
  35. .then(code => {
  36. getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code)
  37. })
  38. .catch(() => {
  39. uni.hideLoading()
  40. })
  41. }
  42. // 小程序获取手机号码回调
  43. function getUserPhoneNumber(encryptedData, iv, code) {
  44. getUserPhone({
  45. encryptedData,
  46. iv,
  47. code,
  48. key: props.authKey,
  49. type: 'routine'
  50. })
  51. .then(res => {
  52. appStore.LOGIN({ token: res.data.token })
  53. appStore.SETUID(res.data.uid)
  54. getUserInfoFn()
  55. })
  56. .catch(res => {
  57. uni.hideLoading()
  58. Toast({title: res})
  59. })
  60. }
  61. // 获取个人用户信息
  62. function getUserInfoFn() {
  63. getUserInfo().then(res => {
  64. uni.hideLoading()
  65. appStore.UPDATE_USERINFO(res.data)
  66. isStatus.value = true
  67. close()
  68. })
  69. }
  70. // #endif
  71. function close() {
  72. emit('close', { isStatus: isStatus.value })
  73. }
  74. </script>
  75. <style lang="scss">
  76. .mobile-bg {
  77. position: fixed;
  78. left: 0;
  79. top: 0;
  80. width: 100%;
  81. height: 100%;
  82. background: rgba(0, 0, 0, 0.5);
  83. }
  84. .mobile-mask {
  85. z-index: 20;
  86. position: fixed;
  87. left: 0;
  88. bottom: 0;
  89. width: 100%;
  90. padding: 67rpx 30rpx;
  91. background: #fff;
  92. .info-box {
  93. display: flex;
  94. flex-direction: column;
  95. align-items: center;
  96. justify-content: center;
  97. image {
  98. width: 150rpx;
  99. height: 150rpx;
  100. border-radius: 10rpx;
  101. }
  102. .title {
  103. margin-top: 30rpx;
  104. margin-bottom: 20rpx;
  105. font-size: 36rpx;
  106. }
  107. .txt {
  108. font-size: 30rpx;
  109. color: #868686;
  110. }
  111. }
  112. .sub_btn {
  113. width: 690rpx;
  114. height: 86rpx;
  115. line-height: 86rpx;
  116. margin-top: 60rpx;
  117. background: $theme-color;
  118. border-radius: 43rpx;
  119. color: #fff;
  120. font-size: 28rpx;
  121. text-align: center;
  122. }
  123. }
  124. .animated {
  125. animation-duration: .4s
  126. }
  127. </style>