other-phone.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div>
  3. <CXBBanner/>
  4. <nut-input
  5. v-model="state.phone"
  6. placeholder="请输入手机号"
  7. >
  8. <template #right>
  9. <nut-button type='primary' size="small" @click="handleGetCode" :loading="state.isAuthVerify">
  10. {{ state.buttonText }}
  11. </nut-button>
  12. </template>
  13. </nut-input>
  14. <nut-input
  15. v-model="state.code"
  16. placeholder="请输入验证码"
  17. clearable
  18. >
  19. </nut-input>
  20. <div class="flex w-full">
  21. <div class="p-3 w-full">
  22. <nut-button type="primary" @click="handleLogin" block :disabled="!isFormVerify">登录</nut-button>
  23. <!-- <div class="text-xs text-gray flex flex-col justify-center items-center p-3">-->
  24. <!-- <div>-->
  25. <!-- 我已阅读并同意<span class="text-red text-xs" @click="handleViewXieyi(2)">《注册协议》</span>和<span-->
  26. <!-- class="text-red text-xs" @click="handleViewXieyi(1)">《隐私协议》</span>-->
  27. <!-- </div>-->
  28. <!-- </div>-->
  29. </div>
  30. </div>
  31. </div>
  32. </template>
  33. <script setup lang="ts">
  34. import CXBBasePage from "~/components/CXBBasePage/CXBBasePage.vue";
  35. import CXBBanner from "~/components/CXBBanner/CXBBanner.vue";
  36. import {authSmsLogin, authVerify} from "~/apis/auth.api";
  37. import Cache from "~/utils/cache";
  38. const userStore = useUserStore()
  39. const state = reactive({
  40. phone: '',
  41. code: '',
  42. isAuthVerify: false,
  43. buttonText: '获取验证码',
  44. })
  45. function sendCode() {
  46. if (state.isAuthVerify) return
  47. // if (this.disabled) return;
  48. // this.disabled = true;
  49. state.isAuthVerify = true
  50. let n = 60;
  51. state.buttonText = "剩余 " + n + "s";
  52. const run = setInterval(() => {
  53. n = n - 1;
  54. if (n < 0) {
  55. clearInterval(run);
  56. n = 0
  57. }
  58. state.buttonText = "剩余 " + n + "s";
  59. if (state.buttonText === "剩余 " + 0 + "s") {
  60. state.isAuthVerify = false;
  61. state.buttonText = "重新获取";
  62. }
  63. }, 1000);
  64. }
  65. const isFormVerify = computed(() => {
  66. if (state.phone !== '' && state.code !== '') {
  67. return true
  68. }
  69. })
  70. function handleGetCode() {
  71. if (!state.phone) {
  72. return uni.showToast({
  73. title: '请填写手机号码',
  74. icon: 'none'
  75. })
  76. }
  77. if (!/^1(3|4|5|7|8|9|6)\d{9}$/i.test(state.phone)) {
  78. return uni.showToast({
  79. title: '请输入正确的手机号码',
  80. icon: 'none'
  81. })
  82. }
  83. state.isAuthVerify = true
  84. authVerify(state.phone).then(res => {
  85. state.isAuthVerify = false
  86. sendCode()
  87. }).catch(res => {
  88. })
  89. }
  90. function handleViewXieyi(type: number) {
  91. uni.navigateTo({
  92. url: `/pages/user/login/user-agreement?type=${type}`
  93. })
  94. }
  95. function handleLogin() {
  96. const shareId = Cache.get('SHARE_ID')
  97. let params = {
  98. phone: state.phone,
  99. sms_code: state.code,
  100. // share_id:
  101. }
  102. if (shareId && shareId !== '') {
  103. params.share_id = shareId
  104. }
  105. authSmsLogin({
  106. ...params
  107. }).then(authRes => {
  108. console.log(authRes)
  109. userStore.login(authRes.token, authRes.exp)
  110. userStore.setUID(authRes.user.uid)
  111. userStore.setUserInfo(authRes.user)
  112. uni.showToast({
  113. title: '登录成功',
  114. icon: 'none'
  115. })
  116. Cache.clear('SHARE_ID')
  117. state.isAuthVerify = false
  118. let redirectPageObj: {page: string, type: 'page' | 'tab'}
  119. if (Cache.get('SHARE_REDIRECT_PAGE')) {
  120. redirectPageObj = JSON.parse(Cache.get('SHARE_REDIRECT_PAGE'))
  121. }
  122. setTimeout(() => {
  123. if (redirectPageObj) {
  124. if (redirectPageObj.type === 'page') {
  125. return uni.redirectTo({
  126. url: redirectPageObj.page
  127. })
  128. }
  129. if (redirectPageObj.type === 'tab') {
  130. return uni.switchTab({
  131. url: redirectPageObj.page
  132. })
  133. }
  134. } else {
  135. uni.switchTab({
  136. url: '/pages/home/index'
  137. })
  138. }
  139. }, 200)
  140. }).catch((errorMsg) => {
  141. console.log(errorMsg)
  142. uni.showToast({
  143. title: errorMsg,
  144. icon: 'none'
  145. })
  146. })
  147. }
  148. </script>
  149. <route lang="yaml">
  150. style:
  151. navigationBarTitleText: 手机登录
  152. </route>
  153. <style>
  154. page {
  155. background: #f8f8f8;
  156. }
  157. </style>