index.vue 7.0 KB

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