index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="ss-p-30 head-box">
  3. <template v-if="changeType === 'login'">
  4. <view class="head-title ss-m-b-30 MiSans-Semibold">欢迎来到辞图科技,请先登录</view>
  5. <view class="ss-m-t-100">
  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>
  24. <view class="ss-flex ss-row-center ss-m-y-50">
  25. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00B760' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  26. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  27. <span class="MiSans-Normal">我已阅读并遵守</span>
  28. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('user')">
  29. 《用户协议》
  30. </view>
  31. <view class="agreement-text MiSans-Normal">和</view>
  32. <view class="color-primary MiSans-Medium" @tap.stop="handleToDetail('privacy')">
  33. 《隐私协议》
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. </view>
  40. </template>
  41. <script setup>
  42. import { ref } from 'vue'
  43. import { weChatRegister } from '@/api/common'
  44. import { closeAuthModal } from '@/hooks/useModal'
  45. const phoneNumberLoading = ref(false)
  46. const protocol = ref(true)
  47. const changeType = ref('login')
  48. // 查看协议详情
  49. const handleToDetail = (type) => {
  50. const url = type === 'user' ? '/pagesA/agreement/user' : '/pagesA/agreement/privacy'
  51. uni.navigateTo({
  52. url
  53. })
  54. }
  55. // 授权
  56. const handleLogin = () => {
  57. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  58. phoneNumberLoading.value = true
  59. wx.login({
  60. success: async (res) => {
  61. try {
  62. const { result } = await weChatRegister({ wechat_code: res?.code })
  63. console.log(result, '用户注册返回结果')
  64. uni.setStorageSync('wechat_user', result)
  65. uni.$emit && uni.$emit('auth:login', result)
  66. closeAuthModal()
  67. } finally {
  68. phoneNumberLoading.value = false
  69. }
  70. },
  71. fail:(res)=> {
  72. phoneNumberLoading.value = false
  73. console.log("获取登录凭证code失败!", res)
  74. }
  75. })
  76. }
  77. // 微信登录/注册
  78. const getPhoneNumber = async (e) => {
  79. if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
  80. uni.showToast({ title: '微信登录失败', icon: 'none' })
  81. return
  82. }
  83. changeType.value = 'login'
  84. phoneNumberLoading.value = true
  85. wx.login({
  86. success: async (res) => {
  87. try {
  88. const { result } = await weChatRegister({ wechat_code: res?.code })
  89. console.log(result, '用户注册返回结果')
  90. uni.setStorageSync('wechat_user', result)
  91. uni.$emit && uni.$emit('auth:login', result)
  92. closeAuthModal()
  93. } finally {
  94. phoneNumberLoading.value = false
  95. }
  96. },
  97. fail:(res)=> {
  98. phoneNumberLoading.value = false
  99. console.log("获取登录凭证code失败!", res)
  100. }
  101. })
  102. }
  103. </script>
  104. <style scoped lang="scss">
  105. .head-title {
  106. font-size: 40rpx;
  107. text-align: center;
  108. color: #00B760;
  109. margin-top: 30rpx;
  110. }
  111. </style>