index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. <!-- <view>
  29. <button v-if="!protocol" class="wxLogon" type="text" :plain="true" @click="showProtocolToast">微信一键登录</button>
  30. <button v-else class="wxLogon" type="text" :plain="true" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信一键登录</button>
  31. </view> -->
  32. <button class="send-button" @tap="handleLogin"> 登录/注册 </button>
  33. <view class="agreement-box ss-flex ss-row-center">
  34. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00897B' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  35. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  36. 我已阅读并遵守
  37. <view class="color-primary" @tap.stop="handleToDetail('user')">
  38. 《用户协议》
  39. </view>
  40. <view class="agreement-text">和</view>
  41. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  42. 《隐私协议》
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <AdvertisePop></AdvertisePop>
  48. </view>
  49. </template>
  50. <script setup>
  51. // 扫码登录注册
  52. import { ref, unref } from 'vue'
  53. import { mobile, code } from '@/utils/validate'
  54. import { getSmsCode, getSmsTimer } from '@/utils/code'
  55. import { userStore } from '@/store/user'
  56. import { onLoad } from '@dcloudio/uni-app'
  57. import { useIM } from '@/hooks/useIM'
  58. import { watch } from 'vue'
  59. import AdvertisePop from '@/components/Advertisement'
  60. const useUserStore = userStore()
  61. const smsLoginRef = ref()
  62. const protocol = ref(false)
  63. const state = ref({
  64. isMobileEnd: false, // 手机号输入完毕
  65. codeText: '获取验证码',
  66. sms: {
  67. phone: '',
  68. code: '',
  69. inviteCode: ''
  70. },
  71. smsRules: {
  72. code,
  73. phone: mobile
  74. }
  75. })
  76. const { resetConfig } = useIM()
  77. watch(() => useUserStore?.accountInfo?.userId, (newVal, oldVal) => {
  78. if (useUserStore.refreshToken) {
  79. // 监听登录状态
  80. resetConfig()
  81. }
  82. })
  83. onLoad((options) => {
  84. console.log(options, 'options-my-share=========')
  85. // const testOptions = { scene: "shareId%3D1" }
  86. if (options.scene) {
  87. const scene = decodeURIComponent(options.scene)
  88. const shareUserId = scene.split('=')[1]
  89. state.value.sms.inviteCode = shareUserId
  90. console.log(shareUserId, 'shareUserId')
  91. }
  92. })
  93. // 获取验证码
  94. const handleCode = () => {
  95. if (!state.value.sms.phone) {
  96. uni.showToast({
  97. title: '请输入手机号',
  98. icon: 'none',
  99. duration: 2000
  100. })
  101. return
  102. }
  103. getSmsCode('smsLogin', state.value.sms.phone)
  104. }
  105. // 查看协议详情
  106. const handleToDetail = (type) => {
  107. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  108. uni.navigateTo({
  109. url
  110. })
  111. }
  112. // 登录
  113. const handleLogin = async () => {
  114. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  115. const validate = await unref(smsLoginRef).validate()
  116. if (!validate) return
  117. if (!state.value.sms.inviteCode) {
  118. uni.showToast({
  119. title: '邀请码缺失,请重新扫码',
  120. icon: 'none'
  121. })
  122. return
  123. }
  124. await useUserStore.handleShareUserRegister(state.value.sms)
  125. }
  126. // const showProtocolToast = () => {
  127. // uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  128. // }
  129. // 微信登录
  130. // const getPhoneNumber = async (e) => {
  131. // if (e?.detail?.errMsg !== 'getPhoneNumber:ok') {
  132. // uni.showToast({ title: '微信登录失败', icon: 'none' })
  133. // return
  134. // }
  135. // changeType.value = 'login'
  136. // wx.login({
  137. // success: async (result) => {
  138. // const wxLoginCode = result?.code || ''
  139. // const query = {
  140. // loginCode: wxLoginCode,
  141. // phoneCode: e.detail.code,
  142. // state: e.detail.encryptedData,
  143. // }
  144. // await useUserStore.handleSmsLogin(query, 2)
  145. // },
  146. // fail:(res)=> { console.log("获取登录凭证code失败!", res) }
  147. // })
  148. // }
  149. </script>
  150. <style scoped lang="scss">
  151. .login-code {
  152. width: 73px;
  153. min-width: 73px;
  154. color: #00897B;
  155. text-align: center;
  156. font-size: 12px;
  157. cursor: pointer;
  158. border: 1px dashed #00897B;
  159. border-radius: 26px;
  160. padding: 0;
  161. }
  162. .head-title {
  163. font-size: 40rpx;
  164. text-align: center;
  165. color: #00897B;
  166. }
  167. .wxLogon {
  168. text-align: center;
  169. font-size: .85em;
  170. color: #00897B;
  171. border: none;
  172. margin: 0;
  173. padding: 0;
  174. // margin: 40rpx;
  175. margin-top: -20px;
  176. margin-bottom: -15px;
  177. }
  178. </style>