index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view class="ss-p-30 head-box">
  3. <template v-if="changeType === 'login'">
  4. <view class="head-title">欢迎来到门墩儿招聘</view>
  5. <uni-segmented-control
  6. class="ss-m-t-30"
  7. :current="current"
  8. :values="items"
  9. style-type="text"
  10. active-color="#00897B"
  11. @clickItem="onClickItem"
  12. />
  13. <!-- <view class="head-subtitle ss-m-t-10">未注册的手机号,验证后自动注册账号</view> -->
  14. <view class="ss-m-t-30">
  15. <!-- 短信验证码登录 -->
  16. <uni-forms
  17. v-if="current === 0"
  18. ref="smsLoginRef"
  19. v-model="state.sms"
  20. :rules="state.smsRules"
  21. validateTrigger="bind"
  22. labelWidth="140"
  23. labelAlign="center"
  24. >
  25. <uni-forms-item name="phone" label="手机号">
  26. <uni-easyinput placeholder="请输入手机号" v-model="state.sms.phone" :inputBorder="false" type="number">
  27. <template v-slot:right>
  28. <button class="login-code" :disabled="state.isMobileEnd" :class="{ 'code-btn-end': state.isMobileEnd }" @tap="handleCode">
  29. {{ getSmsTimer('smsLogin') }}
  30. </button>
  31. </template>
  32. </uni-easyinput>
  33. </uni-forms-item>
  34. <uni-forms-item name="code" label="验证码">
  35. <uni-easyinput placeholder="请输入验证码" v-model="state.sms.code" :inputBorder="false" type="number" maxlength="6"></uni-easyinput>
  36. </uni-forms-item>
  37. </uni-forms>
  38. <!-- 账号密码登录 -->
  39. <uni-forms
  40. v-else
  41. ref="accountLoginRef"
  42. v-model="state.account"
  43. :rules="state.rules"
  44. validateTrigger="bind"
  45. labelWidth="140"
  46. labelAlign="center"
  47. >
  48. <uni-forms-item name="phone" label="账号">
  49. <uni-easyinput placeholder="请输入账号" v-model="state.account.phone" :inputBorder="false"></uni-easyinput>
  50. </uni-forms-item>
  51. <uni-forms-item name="password" label="密码">
  52. <uni-easyinput type="password" placeholder="请输入密码" v-model="state.account.password" :inputBorder="false"></uni-easyinput>
  53. </uni-forms-item>
  54. </uni-forms>
  55. <view class="register" @tap="handleChangeRegister">还没有登录账户?去注册</view>
  56. <button class="send-button" @tap="handleLogin"> 登 录 </button>
  57. <view class="agreement-box ss-flex ss-row-center">
  58. <uni-icons size="20" :type="protocol ? 'checkbox-filled' : 'circle'" :color="protocol ? '#00897B' : '#ccc'" @tap="protocol = !protocol"></uni-icons>
  59. <view class="color-999 ss-flex ss-col-center ss-m-l-8 font-size-13">
  60. 我已阅读并遵守
  61. <view class="color-primary" @tap.stop="handleToDetail('user')">
  62. 《用户协议》
  63. </view>
  64. <view class="agreement-text">和</view>
  65. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  66. 《隐私协议》
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <template v-if="changeType === 'register'">
  73. <view class="head-title pb-20">手机号注册</view>
  74. <uni-forms
  75. ref="registerForm"
  76. v-model="state.register"
  77. :rules="state.smsRules"
  78. validateTrigger="bind"
  79. labelWidth="140"
  80. labelAlign="center"
  81. >
  82. <uni-forms-item name="phone" label="手机号">
  83. <uni-easyinput placeholder="请输入手机号" v-model="state.register.phone" :inputBorder="false" type="number">
  84. <template v-slot:right>
  85. <button
  86. class="login-code"
  87. :disabled="state.isMobileEnd"
  88. :class="{ 'code-btn-end': state.isMobileEnd }"
  89. @tap="handleRegisterCode"
  90. >
  91. {{ getSmsTimer('smsRegister') }}
  92. </button>
  93. </template>
  94. </uni-easyinput>
  95. </uni-forms-item>
  96. <uni-forms-item name="code" label="验证码">
  97. <uni-easyinput
  98. placeholder="请输入验证码"
  99. v-model="state.register.code" :inputBorder="false" type="number" maxlength="6"></uni-easyinput>
  100. </uni-forms-item>
  101. </uni-forms>
  102. <view class="register login" @tap="handleChangeLogin">已有账户?去登陆</view>
  103. <view>
  104. <button class="send-button" @tap="handleRegister"> 注 册 </button>
  105. </view>
  106. <view class="color-999 ss-flex ss-col-center ss-row-center ss-m-l-8 font-size-13">
  107. 点击注册即代表您同意
  108. <view class="color-primary" @tap.stop="handleToDetail('user')">
  109. 《用户协议》
  110. </view>
  111. <view class="agreement-text">和</view>
  112. <view class="color-primary" @tap.stop="handleToDetail('privacy')">
  113. 《隐私协议》
  114. </view>
  115. </view>
  116. </template>
  117. </view>
  118. </template>
  119. <script setup>
  120. import { ref, unref } from 'vue'
  121. import { mobile, password, code } from '@/utils/validate'
  122. import { getSmsCode, getSmsTimer } from '@/utils/code'
  123. import { userStore } from '@/store/user'
  124. const useUserStore = userStore()
  125. const items = ['短信登录', '账号登录']
  126. const current = ref(0)
  127. const accountLoginRef = ref()
  128. const smsLoginRef = ref()
  129. const registerForm = ref()
  130. const protocol = ref(false)
  131. const state = ref({
  132. isMobileEnd: false, // 手机号输入完毕
  133. codeText: '获取验证码',
  134. sms: {
  135. phone: '',
  136. code: ''
  137. },
  138. register: {
  139. phone: '',
  140. code: ''
  141. },
  142. account: {
  143. phone: '',
  144. password: ''
  145. },
  146. rules: {
  147. phone: mobile,
  148. password,
  149. },
  150. smsRules: {
  151. code,
  152. phone: mobile
  153. }
  154. })
  155. const changeType = ref('login')
  156. // 登录成功后的返回页面
  157. // const backUrl = getCurrentPages().length ? getCurrentPages()[getCurrentPages().length - 2].route : ''
  158. const onClickItem = (e) => {
  159. current.value = e.currentIndex
  160. }
  161. // 获取验证码
  162. const handleCode = () => {
  163. if (!state.value.sms.phone) {
  164. uni.showToast({
  165. title: '请输入手机号',
  166. icon: 'none',
  167. duration: 2000
  168. })
  169. return
  170. }
  171. getSmsCode('smsLogin', state.value.sms.phone)
  172. }
  173. // 获取验证码
  174. const handleRegisterCode = () => {
  175. if (!state.value.register.phone) {
  176. uni.showToast({
  177. title: '请输入手机号',
  178. icon: 'none',
  179. duration: 2000
  180. })
  181. return
  182. }
  183. getSmsCode('smsRegister', state.value.register.phone)
  184. }
  185. // 查看协议详情
  186. const handleToDetail = (type) => {
  187. const url = type === 'user' ? '/pagesB/agreement/user' : '/pagesB/agreement/privacy'
  188. uni.navigateTo({
  189. url
  190. })
  191. }
  192. // 注册
  193. function handleChangeRegister () {
  194. changeType.value = 'register'
  195. }
  196. // 登录
  197. function handleChangeLogin () {
  198. changeType.value = 'login'
  199. }
  200. async function handleRegister () {
  201. const validate = await unref(registerForm).validate()
  202. if (!validate) return
  203. try {
  204. await useUserStore.handleRegister(state.value.register)
  205. changeType.value = 'login'
  206. } finally {
  207. }
  208. }
  209. // 登录
  210. const handleLogin = async () => {
  211. if (!protocol.value) return uni.showToast({ title: '请先阅读并同意用户协议和隐私政策', icon: 'none' })
  212. const validate = await unref(current.value === 0 ? smsLoginRef : accountLoginRef).validate()
  213. if (!validate) return
  214. const query = current.value === 0 ? state.value.sms : state.value.account
  215. Object.assign(query, {
  216. account: query.phone
  217. })
  218. await useUserStore.handleSmsLogin(query, current.value === 0 ? true : false)
  219. }
  220. </script>
  221. <style scoped lang="scss">
  222. .login-code {
  223. width: 73px;
  224. min-width: 73px;
  225. color: #00897B;
  226. text-align: center;
  227. font-size: 12px;
  228. cursor: pointer;
  229. border: 1px dashed #00897B;
  230. border-radius: 26px;
  231. padding: 0;
  232. }
  233. .head-title {
  234. font-size: 40rpx;
  235. text-align: center;
  236. color: #00897B;
  237. margin-top: 30rpx;
  238. }
  239. .register {
  240. widows: 100%;
  241. font-size: .85em;
  242. color: red;
  243. text-align: right;
  244. &.login {
  245. color: #00897B;
  246. }
  247. }
  248. .pb-20 {
  249. padding-bottom: 40rpx;
  250. }
  251. </style>