index.vue 8.2 KB

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