index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="ss-p-30 head-box">
  3. <view class="head-title">欢迎来到门墩儿,登录/注册领积分</view>
  4. <view class="head-subtitle ss-m-t-30 justify-center">未注册的手机号,验证后自动注册账号</view>
  5. <view class="ss-m-t-60">
  6. <!-- 短信验证码登录 -->
  7. <uni-forms
  8. ref="smsLoginRef"
  9. v-model="state.sms"
  10. :rules="state.smsRules"
  11. validateTrigger="bind"
  12. labelWidth="140"
  13. labelAlign="center"
  14. >
  15. <uni-forms-item name="phone" label="手机号">
  16. <uni-easyinput placeholder="请输入手机号" v-model="state.sms.phone" :inputBorder="false" type="number">
  17. <template v-slot:right>
  18. <button class="login-code" :disabled="state.isMobileEnd" :class="{ 'code-btn-end': state.isMobileEnd }" @tap="handleCode">
  19. {{ getSmsTimer('smsLogin') }}
  20. </button>
  21. </template>
  22. </uni-easyinput>
  23. </uni-forms-item>
  24. <uni-forms-item name="code" label="验证码">
  25. <uni-easyinput placeholder="请输入验证码" v-model="state.sms.code" :inputBorder="false" type="number" maxlength="6"></uni-easyinput>
  26. </uni-forms-item>
  27. </uni-forms>
  28. <button class="send-button" @tap="handleLogin"> 登录/注册 </button>
  29. <view class="agreement-box ss-flex ss-row-center">
  30. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00897B' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  31. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  32. 我已阅读并遵守
  33. <view class="color-primary" @tap.stop="handleToDetail('user')">
  34. 《用户协议》
  35. </view>
  36. <view class="agreement-text">和</view>
  37. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  38. 《隐私协议》
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. <AdvertisePop></AdvertisePop>
  44. </view>
  45. </template>
  46. <script setup>
  47. // 扫码登录注册
  48. import { ref, unref } from 'vue'
  49. import { mobile, code } from '@/utils/validate'
  50. import { getSmsCode, getSmsTimer } from '@/utils/code'
  51. import { userStore } from '@/store/user'
  52. import { onLoad } from '@dcloudio/uni-app'
  53. import { useIM } from '@/hooks/useIM'
  54. import { watch } from 'vue'
  55. import AdvertisePop from '@/components/Advertisement'
  56. const useUserStore = userStore()
  57. const smsLoginRef = ref()
  58. const protocol = ref(false)
  59. const state = ref({
  60. isMobileEnd: false, // 手机号输入完毕
  61. codeText: '获取验证码',
  62. sms: {
  63. phone: '',
  64. code: '',
  65. inviteCode: ''
  66. },
  67. smsRules: {
  68. code,
  69. phone: mobile
  70. }
  71. })
  72. const { resetConfig } = useIM()
  73. watch(() => useUserStore?.accountInfo?.userId, (newVal, oldVal) => {
  74. if (useUserStore.refreshToken) {
  75. // 监听登录状态
  76. resetConfig()
  77. }
  78. })
  79. onLoad((options) => {
  80. console.log(options, 'options-my-share=========')
  81. // const testOptions = { scene: "shareId%3D1" }
  82. if (options.scene) {
  83. const scene = decodeURIComponent(options.scene)
  84. const shareUserId = scene.split('=')[1]
  85. state.value.sms.inviteCode = shareUserId
  86. console.log(shareUserId, 'shareUserId')
  87. }
  88. })
  89. // 获取验证码
  90. const handleCode = () => {
  91. if (!state.value.sms.phone) {
  92. uni.showToast({
  93. title: '请输入手机号',
  94. icon: 'none',
  95. duration: 2000
  96. })
  97. return
  98. }
  99. getSmsCode('smsLogin', state.value.sms.phone)
  100. }
  101. // 查看协议详情
  102. const handleToDetail = (type) => {
  103. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  104. uni.navigateTo({
  105. url
  106. })
  107. }
  108. // 登录
  109. const handleLogin = async () => {
  110. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  111. const validate = await unref(smsLoginRef).validate()
  112. if (!validate) return
  113. if (!state.value.sms.inviteCode) {
  114. uni.showToast({
  115. title: '邀请码缺失,请重新扫码',
  116. icon: 'none'
  117. })
  118. return
  119. }
  120. await useUserStore.handleShareUserRegister(state.value.sms)
  121. }
  122. </script>
  123. <style scoped lang="scss">
  124. .login-code {
  125. width: 73px;
  126. min-width: 73px;
  127. color: #00897B;
  128. text-align: center;
  129. font-size: 12px;
  130. cursor: pointer;
  131. border: 1px dashed #00897B;
  132. border-radius: 26px;
  133. padding: 0;
  134. }
  135. .head-title {
  136. font-size: 40rpx;
  137. text-align: center;
  138. color: #00897B;
  139. }
  140. </style>