index.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ? '#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. 我已阅读并遵守
  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. if (options.scene) {
  80. const scene = decodeURIComponent(options.scene)
  81. const shareUserId = scene.split('=')[1]
  82. state.value.sms.inviteCode = shareUserId
  83. console.log(shareUserId, 'shareUserId')
  84. }
  85. })
  86. // 获取验证码
  87. const handleCode = () => {
  88. if (!state.value.sms.phone) {
  89. uni.showToast({
  90. title: '请输入手机号',
  91. icon: 'none',
  92. duration: 2000
  93. })
  94. return
  95. }
  96. getSmsCode('smsLogin', state.value.sms.phone)
  97. }
  98. // 查看协议详情
  99. const handleToDetail = (type) => {
  100. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  101. uni.navigateTo({
  102. url
  103. })
  104. }
  105. // 登录
  106. const handleLogin = async () => {
  107. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  108. const validate = await unref(smsLoginRef).validate()
  109. if (!validate) return
  110. if (!state.value.sms.inviteCode) {
  111. uni.showToast({
  112. title: '邀请码缺失,请重新扫码',
  113. icon: 'none'
  114. })
  115. return
  116. }
  117. const result = await useUserStore.handleShareUserRegister(state.value.sms)
  118. if (!result || !Object.keys(result).length) return
  119. uni.switchTab({
  120. url: '/pages/index/my'
  121. })
  122. }
  123. </script>
  124. <style scoped lang="scss">
  125. .login-code {
  126. width: 73px;
  127. min-width: 73px;
  128. color: #00B760;
  129. text-align: center;
  130. font-size: 12px;
  131. cursor: pointer;
  132. border: 1px dashed #00B760;
  133. border-radius: 26px;
  134. padding: 0;
  135. }
  136. .head-title {
  137. font-size: 40rpx;
  138. text-align: center;
  139. color: #00B760;
  140. }
  141. .wxLogon {
  142. text-align: center;
  143. font-size: .85em;
  144. color: #00B760;
  145. border: none;
  146. margin: 0;
  147. padding: 0;
  148. // margin: 40rpx;
  149. margin-top: -20px;
  150. margin-bottom: -15px;
  151. }
  152. </style>