index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <view class="ss-p-50 head-box" style="width: 75vw;">
  3. <template v-if="changeType === 'login'">
  4. <view class="head-title">温馨提示</view>
  5. <view class="ss-m-t-50">
  6. <view>
  7. <!-- <button
  8. class="send-button MiSans-Medium"
  9. @click="handleLogin"
  10. >
  11. 一键授权注册/登录
  12. </button> -->
  13. <!-- <button
  14. v-else
  15. class="send-button MiSans-Medium"
  16. :loading="phoneNumberLoading"
  17. :disabled="phoneNumberLoading"
  18. open-type="getPhoneNumber"
  19. @getphonenumber="getPhoneNumber"
  20. >
  21. 微信授权注册/登录
  22. </button> -->
  23. <view class="text-center ss-p-x-30 font-size-15 color-666">为了给您提供完整服务,需要您授权注册/登录,是否同意?</view>
  24. <view class="ss-flex ss-row-center ss-m-t-50">
  25. <button class="refuse-button" plain @click="handleRefuse">不同意并退出</button>
  26. <button class="agree-button" @click="handleLogin">同意并继续</button>
  27. </view>
  28. </view>
  29. <!-- <view class="ss-flex ss-row-center ss-m-y-50">
  30. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  31. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  32. <span class="MiSans-Normal">我已阅读并遵守</span>
  33. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('user')">
  34. 《用户协议》
  35. </view>
  36. <view class="agreement-text MiSans-Normal">和</view>
  37. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('privacy')">
  38. 《隐私协议》
  39. </view>
  40. </view>
  41. </view> -->
  42. </view>
  43. </template>
  44. </view>
  45. </template>
  46. <script setup>
  47. import { ref } from 'vue'
  48. import { weChatRegister } from '@/api/common'
  49. import { closeAuthModal } from '@/hooks/useModal'
  50. const phoneNumberLoading = ref(false)
  51. // const protocol = ref(true)
  52. const changeType = ref('login')
  53. // 查看协议详情
  54. const handleToDetail = (type) => {
  55. const url = type === 'user' ? '/pagesA/agreement/user' : '/pagesA/agreement/privacy'
  56. uni.navigateTo({
  57. url
  58. })
  59. }
  60. // 不同意并退出
  61. const handleRefuse = () => {
  62. wx.exitMiniProgram({
  63. success: () => {},
  64. fail: () => {
  65. console.log('退出小程序失败')
  66. }
  67. })
  68. }
  69. // 授权
  70. const handleLogin = () => {
  71. // if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  72. phoneNumberLoading.value = true
  73. wx.login({
  74. success: async (res) => {
  75. try {
  76. const { result } = await weChatRegister({ wechat_code: res?.code })
  77. console.log(result, '用户注册返回结果')
  78. if (!result) return uni.showToast({ title: '授权失败,请稍后重试', icon: 'none', duration: 2000 })
  79. uni.showToast({ title: '授权成功', icon: 'success', duration: 2000 })
  80. uni.setStorageSync('wechat_user', result)
  81. uni.$emit && uni.$emit('auth:login', result)
  82. closeAuthModal()
  83. } finally {
  84. phoneNumberLoading.value = false
  85. }
  86. },
  87. fail:(res)=> {
  88. phoneNumberLoading.value = false
  89. console.log("获取登录凭证code失败!", res)
  90. }
  91. })
  92. }
  93. // 微信登录/注册
  94. const getPhoneNumber = async (e) => {
  95. if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
  96. uni.showToast({ title: '微信登录失败', icon: 'none' })
  97. return
  98. }
  99. changeType.value = 'login'
  100. phoneNumberLoading.value = true
  101. wx.login({
  102. success: async (res) => {
  103. try {
  104. const { result } = await weChatRegister({ wechat_code: res?.code })
  105. console.log(result, '用户注册返回结果')
  106. uni.setStorageSync('wechat_user', result)
  107. uni.$emit && uni.$emit('auth:login', result)
  108. closeAuthModal()
  109. } finally {
  110. phoneNumberLoading.value = false
  111. }
  112. },
  113. fail:(res)=> {
  114. phoneNumberLoading.value = false
  115. console.log("获取登录凭证code失败!", res)
  116. }
  117. })
  118. }
  119. </script>
  120. <style scoped lang="scss">
  121. .head-title {
  122. font-size: 35rpx;
  123. text-align: center;
  124. // color: #00B760;
  125. // margin-top: 30rpx;
  126. }
  127. .refuse-button {
  128. width: 126px;
  129. font-size: 16px;
  130. color: #666;
  131. border-radius: 25px;
  132. border: 1px solid #ccc;
  133. // padding: 0 10px;
  134. }
  135. .agree-button {
  136. width: 126px;
  137. font-size: 16px;
  138. color: #fff;
  139. border-radius: 25px;
  140. background-color: #007aff;
  141. }
  142. </style>