index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="ss-p-30 head-box">
  3. <view class="head-title">欢迎来到门墩儿招聘</view>
  4. <uni-segmented-control class="ss-m-t-60" :current="current" :values="items" style-type="text" active-color="#00897B" @clickItem="onClickItem" />
  5. <view class="head-subtitle ss-m-t-10">未注册的手机号,验证后自动注册账号</view>
  6. <view class="ss-m-t-30">
  7. <!-- 短信验证码登录 -->
  8. <uni-forms
  9. v-if="current === 0"
  10. ref="smsLoginRef"
  11. v-model="state.sms"
  12. :rules="state.smsRules"
  13. validateTrigger="bind"
  14. labelWidth="140"
  15. labelAlign="center"
  16. >
  17. <uni-forms-item name="phone" label="手机号">
  18. <uni-easyinput placeholder="请输入手机号" v-model="state.sms.phone" :inputBorder="false" type="number">
  19. <template v-slot:right>
  20. <button class="login-code" :disabled="state.isMobileEnd" :class="{ 'code-btn-end': state.isMobileEnd }" @tap="handleCode">
  21. {{ getSmsTimer('smsLogin') }}
  22. </button>
  23. </template>
  24. </uni-easyinput>
  25. </uni-forms-item>
  26. <uni-forms-item name="code" label="验证码">
  27. <uni-easyinput placeholder="请输入验证码" v-model="state.sms.code" :inputBorder="false" type="number" maxlength="6"></uni-easyinput>
  28. </uni-forms-item>
  29. </uni-forms>
  30. <!-- 账号密码登录 -->
  31. <uni-forms
  32. v-else
  33. ref="accountLoginRef"
  34. v-model="state.account"
  35. :rules="state.rules"
  36. validateTrigger="bind"
  37. labelWidth="140"
  38. labelAlign="center"
  39. >
  40. <uni-forms-item name="phone" label="账号">
  41. <uni-easyinput placeholder="请输入账号" v-model="state.account.phone" :inputBorder="false"></uni-easyinput>
  42. </uni-forms-item>
  43. <uni-forms-item name="password" label="密码">
  44. <uni-easyinput type="password" placeholder="请输入密码" v-model="state.account.password" :inputBorder="false"></uni-easyinput>
  45. </uni-forms-item>
  46. </uni-forms>
  47. <button class="send-button" @tap="handleLogin"> 登录/注册 </button>
  48. </view>
  49. </view>
  50. </template>
  51. <script setup>
  52. import { ref, unref } from 'vue'
  53. import { mobile, password, code } from '@/utils/validate'
  54. import { getSmsCode, getSmsTimer } from '@/utils/code'
  55. import { userStore } from '@/store/user'
  56. const useUserStore = userStore()
  57. const items = ['短信登录', '账号登录']
  58. const current = ref(1)
  59. const accountLoginRef = ref()
  60. const smsLoginRef = ref()
  61. const state = ref({
  62. isMobileEnd: false, // 手机号输入完毕
  63. codeText: '获取验证码',
  64. sms: {
  65. phone: '13229740092',
  66. code: ''
  67. },
  68. account: {
  69. phone: '13229740091',
  70. password: 'Citu123'
  71. },
  72. rules: {
  73. phone: mobile,
  74. password,
  75. },
  76. smsRules: {
  77. code,
  78. phone: mobile
  79. }
  80. })
  81. // 登录成功后的返回页面
  82. const backUrl = getCurrentPages().length ? getCurrentPages()[getCurrentPages().length - 2].route : ''
  83. const onClickItem = (e) => {
  84. current.value = e.currentIndex
  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 handleLogin = async () => {
  100. const validate = await unref(current.value === 0 ? smsLoginRef : accountLoginRef).validate()
  101. if (!validate) return
  102. await useUserStore.handleSmsLogin(current.value === 0 ? state.value.sms : state.value.account, current.value === 0 ? true : false, backUrl || 'pages/index/my')
  103. }
  104. </script>
  105. <style scoped lang="scss">
  106. .login-code {
  107. width: 73px;
  108. min-width: 73px;
  109. color: #00897B;
  110. text-align: center;
  111. font-size: 12px;
  112. cursor: pointer;
  113. border: 1px dashed #00897B;
  114. border-radius: 26px;
  115. padding: 0;
  116. }
  117. .head-title {
  118. font-size: 40rpx;
  119. text-align: center;
  120. color: #00897B;
  121. margin-bottom: 100rpx;
  122. }
  123. </style>