index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. </view>
  44. </template>
  45. <script setup>
  46. // 扫码登录注册
  47. import { ref, unref } from 'vue'
  48. import { mobile, code } from '@/utils/validate'
  49. import { getSmsCode, getSmsTimer } from '@/utils/code'
  50. import { userStore } from '@/store/user'
  51. import { onLoad } from '@dcloudio/uni-app'
  52. import { useIM } from '@/hooks/useIM'
  53. import { watch } from 'vue'
  54. const useUserStore = userStore()
  55. const smsLoginRef = ref()
  56. const protocol = ref(false)
  57. const state = ref({
  58. isMobileEnd: false, // 手机号输入完毕
  59. codeText: '获取验证码',
  60. sms: {
  61. phone: '',
  62. code: '',
  63. inviteCode: ''
  64. },
  65. smsRules: {
  66. code,
  67. phone: mobile
  68. }
  69. })
  70. const { resetConfig } = useIM()
  71. watch(() => useUserStore?.accountInfo?.userId, (newVal, oldVal) => {
  72. if (useUserStore.refreshToken) {
  73. // 监听登录状态
  74. resetConfig()
  75. }
  76. })
  77. onLoad((options) => {
  78. console.log(options, 'options-my-share=========')
  79. // const testOptions = { scene: "shareId%3D1" }
  80. if (options.scene) {
  81. const scene = decodeURIComponent(options.scene)
  82. const shareUserId = scene.split('=')[1]
  83. state.value.sms.inviteCode = shareUserId
  84. console.log(shareUserId, 'shareUserId')
  85. }
  86. })
  87. // 获取验证码
  88. const handleCode = () => {
  89. if (!state.value.sms.phone) {
  90. uni.showToast({
  91. title: '请输入手机号',
  92. icon: 'none',
  93. duration: 2000
  94. })
  95. return
  96. }
  97. getSmsCode('smsLogin', state.value.sms.phone)
  98. }
  99. // 查看协议详情
  100. const handleToDetail = (type) => {
  101. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  102. uni.navigateTo({
  103. url
  104. })
  105. }
  106. // 登录
  107. const handleLogin = async () => {
  108. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  109. const validate = await unref(smsLoginRef).validate()
  110. if (!validate) return
  111. if (!state.value.sms.inviteCode) {
  112. uni.showToast({
  113. title: '邀请码缺失,请重新扫码',
  114. icon: 'none'
  115. })
  116. return
  117. }
  118. await useUserStore.handleShareUserRegister(state.value.sms)
  119. }
  120. </script>
  121. <style scoped lang="scss">
  122. .login-code {
  123. width: 73px;
  124. min-width: 73px;
  125. color: #00897B;
  126. text-align: center;
  127. font-size: 12px;
  128. cursor: pointer;
  129. border: 1px dashed #00897B;
  130. border-radius: 26px;
  131. padding: 0;
  132. }
  133. .head-title {
  134. font-size: 40rpx;
  135. text-align: center;
  136. color: #00897B;
  137. }
  138. </style>