login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <script setup>
  2. import { computed, ref } from 'vue'
  3. import { onShow } from '@dcloudio/uni-app'
  4. import { useUserStore } from '@/stores/user'
  5. import { DEFAULT_PLACEHOLDER_IMAGE_URL } from '@/utils/staticAsset'
  6. const userStore = useUserStore()
  7. const form = ref({
  8. username: '',
  9. password: ''
  10. })
  11. const isLoading = ref(false)
  12. const agreeProtocol = ref(false)
  13. const loginDisabled = computed(() => isLoading.value)
  14. const protocolDialogVisible = ref(false)
  15. const protocolDialogForWechat = ref(false)
  16. let protocolDialogResolver = null
  17. onShow(() => {
  18. userStore.checkLoginStatus()
  19. if (userStore.isLoggedIn) {
  20. userStore.navigateToHome()
  21. }
  22. })
  23. const loginPageStyle = computed(() => ({ '--theme-color': '#0a9f6d' }))
  24. const handleSubmit = async () => {
  25. if (!agreeProtocol.value) {
  26. protocolDialogForWechat.value = false
  27. const shouldContinue = await new Promise((resolve) => {
  28. protocolDialogResolver = resolve
  29. protocolDialogVisible.value = true
  30. })
  31. if (!shouldContinue) return
  32. agreeProtocol.value = true
  33. }
  34. if (!form.value.username) {
  35. uni.showToast({ title: '请输入用户名', icon: 'none' })
  36. return
  37. }
  38. if (!form.value.password) {
  39. uni.showToast({ title: '请输入密码', icon: 'none' })
  40. return
  41. }
  42. isLoading.value = true
  43. try {
  44. const success = await userStore.login({
  45. username: form.value.username,
  46. password: form.value.password
  47. })
  48. if (success) {
  49. userStore.navigateToHome()
  50. }
  51. } finally {
  52. isLoading.value = false
  53. }
  54. }
  55. const goUserAgreement = () => {
  56. uni.navigateTo({ url: '/pages/login/user-agreement' })
  57. }
  58. const goPrivacyPolicy = () => {
  59. uni.navigateTo({ url: '/pages/login/privacy-policy' })
  60. }
  61. const toggleProtocolAgreement = () => {
  62. agreeProtocol.value = !agreeProtocol.value
  63. }
  64. const resolveProtocolDialog = (confirmed) => {
  65. protocolDialogVisible.value = false
  66. protocolDialogForWechat.value = false
  67. if (protocolDialogResolver) {
  68. protocolDialogResolver(confirmed)
  69. protocolDialogResolver = null
  70. }
  71. }
  72. const onWechatGate = () => {
  73. if (isLoading.value) return
  74. protocolDialogForWechat.value = true
  75. protocolDialogVisible.value = true
  76. }
  77. const onAgreeGetPhoneNumber = (e) => {
  78. agreeProtocol.value = true
  79. protocolDialogVisible.value = false
  80. protocolDialogForWechat.value = false
  81. onGetPhoneNumber(e)
  82. }
  83. const onGetPhoneNumber = async (e) => {
  84. if (isLoading.value) return
  85. if (!agreeProtocol.value) {
  86. onWechatGate()
  87. return
  88. }
  89. const detail = (e && e.detail) || {}
  90. const errMsg = detail.errMsg || ''
  91. if (errMsg && errMsg.indexOf('ok') === -1) {
  92. uni.showToast({ title: '需要授权手机号,或请使用账号密码登录', icon: 'none' })
  93. return
  94. }
  95. const phoneCode = detail.code
  96. if (!phoneCode) {
  97. uni.showToast({ title: '需要授权手机号,或请使用账号密码登录', icon: 'none' })
  98. return
  99. }
  100. isLoading.value = true
  101. try {
  102. const loginRes = await new Promise((resolve, reject) => {
  103. uni.login({ provider: 'weixin', success: resolve, fail: reject })
  104. })
  105. const ok = await userStore.wechatLogin({
  106. loginCode: loginRes.code,
  107. phoneCode
  108. })
  109. if (ok) {
  110. userStore.navigateToHome()
  111. }
  112. } catch (err) {
  113. uni.showToast({ title: (err && err.message) || '登录失败', icon: 'none' })
  114. } finally {
  115. isLoading.value = false
  116. }
  117. }
  118. </script>
  119. <template>
  120. <view class="login-page" :style="loginPageStyle">
  121. <!-- 背景装饰 -->
  122. <view class="login-bg">
  123. <view class="bg-circle circle-1"></view>
  124. <view class="bg-circle circle-2"></view>
  125. <view class="bg-circle circle-3"></view>
  126. </view>
  127. <view class="login-content">
  128. <view class="logo-section">
  129. <image class="logo-image" :src="DEFAULT_PLACEHOLDER_IMAGE_URL" mode="widthFix" />
  130. <text class="app-slogan">一体化业务移动端</text>
  131. </view>
  132. <view class="login-form">
  133. <view class="form-item">
  134. <text class="form-label">用户名</text>
  135. <view class="form-input-box">
  136. <uni-icons class="form-input-ico" type="person" size="18" color="#c0c4cc" />
  137. <input
  138. v-model="form.username"
  139. class="form-input"
  140. type="text"
  141. placeholder="请输入用户名"
  142. placeholder-class="input-placeholder"
  143. />
  144. </view>
  145. </view>
  146. <view class="form-item">
  147. <text class="form-label">密码</text>
  148. <view class="form-input-box">
  149. <uni-icons class="form-input-ico" type="locked" size="18" color="#c0c4cc" />
  150. <input
  151. v-model="form.password"
  152. class="form-input"
  153. type="password"
  154. placeholder="请输入密码"
  155. placeholder-class="input-placeholder"
  156. />
  157. </view>
  158. </view>
  159. <view class="form-actions">
  160. <button class="btn-login" :disabled="loginDisabled" @click="handleSubmit">
  161. <text v-if="isLoading" class="btn-login-text">登录中...</text>
  162. <text v-else class="btn-login-text">账号密码登录</text>
  163. </button>
  164. </view>
  165. </view>
  166. <view class="login-footer">
  167. <view class="footer-line">
  168. <view class="agree-check" @tap="toggleProtocolAgreement">
  169. <view class="agree-check__icon" :class="{ 'agree-check__icon--checked': agreeProtocol }">
  170. <text class="agree-check__tick">✓</text>
  171. </view>
  172. </view>
  173. <text class="footer-text">登录即表示同意</text>
  174. <text class="link" @tap="goUserAgreement">《用户服务协议》</text>
  175. <text class="footer-text">和</text>
  176. <text class="link" @tap="goPrivacyPolicy">《隐私政策》</text>
  177. </view>
  178. </view>
  179. <view class="login-divider">或</view>
  180. <button
  181. v-if="agreeProtocol"
  182. class="login-wechat"
  183. :disabled="isLoading"
  184. open-type="getPhoneNumber"
  185. @getphonenumber="onGetPhoneNumber"
  186. >
  187. <text class="login-wechat__label">手机号快捷登录</text>
  188. </button>
  189. <view
  190. v-else
  191. class="login-wechat"
  192. :class="{ 'is-disabled': isLoading }"
  193. @tap="onWechatGate"
  194. >
  195. <text class="login-wechat__label">手机号快捷登录</text>
  196. </view>
  197. </view>
  198. <view v-if="protocolDialogVisible" class="protocol-dialog-mask" @tap="resolveProtocolDialog(false)">
  199. <view class="protocol-dialog" @tap.stop>
  200. <text class="protocol-dialog__title">协议确认</text>
  201. <text class="protocol-dialog__desc">登录前请先同意《用户服务协议》和《隐私政策》</text>
  202. <view class="protocol-dialog__actions">
  203. <view class="protocol-dialog__btn protocol-dialog__btn--cancel" @tap="resolveProtocolDialog(false)">否</view>
  204. <button
  205. v-if="protocolDialogForWechat"
  206. class="protocol-dialog__btn protocol-dialog__btn--confirm"
  207. open-type="getPhoneNumber"
  208. @getphonenumber="onAgreeGetPhoneNumber"
  209. >
  210. 同意并登录
  211. </button>
  212. <view
  213. v-else
  214. class="protocol-dialog__btn protocol-dialog__btn--confirm"
  215. @tap="resolveProtocolDialog(true)"
  216. >
  217. 是,继续登录
  218. </view>
  219. </view>
  220. </view>
  221. </view>
  222. </view>
  223. </template>
  224. <style lang="scss" scoped>
  225. .login-page {
  226. min-height: 100vh;
  227. min-height: 100dvh;
  228. background: linear-gradient(180deg, #f8fff8 0%, #ffffff 100%);
  229. position: relative;
  230. overflow: hidden;
  231. display: flex;
  232. flex-direction: column;
  233. align-items: center;
  234. justify-content: center;
  235. box-sizing: border-box;
  236. padding: calc(32rpx + var(--sq-safe-top, env(safe-area-inset-top, 0px))) 32rpx
  237. calc(32rpx + env(safe-area-inset-bottom, 0px));
  238. /* 与 placeholder 同色,供图标 mask 与 :deep(.input-placeholder) 共用 */
  239. --login-input-placeholder: #c0c4cc;
  240. }
  241. .login-bg {
  242. position: absolute;
  243. top: 0;
  244. left: 0;
  245. right: 0;
  246. bottom: 0;
  247. pointer-events: none;
  248. }
  249. .login-bg .bg-circle {
  250. position: absolute;
  251. border-radius: 50%;
  252. background: var(--theme-color);
  253. opacity: 0.05;
  254. }
  255. .login-bg .circle-1 {
  256. width: 400rpx;
  257. height: 400rpx;
  258. top: -100rpx;
  259. right: -100rpx;
  260. }
  261. .login-bg .circle-2 {
  262. width: 300rpx;
  263. height: 300rpx;
  264. top: 300rpx;
  265. left: -150rpx;
  266. }
  267. .login-bg .circle-3 {
  268. width: 200rpx;
  269. height: 200rpx;
  270. bottom: 100rpx;
  271. right: -50rpx;
  272. }
  273. .login-content {
  274. position: relative;
  275. z-index: 1;
  276. width: 100%;
  277. max-width: 680rpx;
  278. margin: 0 auto;
  279. box-sizing: border-box;
  280. padding: 0 8rpx;
  281. flex-shrink: 0;
  282. }
  283. /* 对齐 bxapp 登录页 brand-logo:横向自适应宽图,非圆角方标 */
  284. .logo-section {
  285. display: flex;
  286. flex-direction: column;
  287. align-items: center;
  288. text-align: center;
  289. margin-bottom: 44rpx;
  290. }
  291. .logo-image {
  292. display: block;
  293. width: 360rpx;
  294. max-width: 48vw;
  295. height: auto;
  296. margin: 0 auto 24rpx;
  297. }
  298. .app-slogan {
  299. font-size: 44rpx;
  300. font-weight: 700;
  301. letter-spacing: 0.02em;
  302. color: #1e293b;
  303. margin-top: 4rpx;
  304. }
  305. .login-form {
  306. background: #fff;
  307. border-radius: 24rpx;
  308. padding: 50rpx 40rpx;
  309. box-shadow: 0 10rpx 40rpx rgba(0, 0, 0, 0.08);
  310. }
  311. .form-item {
  312. margin-bottom: 36rpx;
  313. }
  314. .form-item:last-of-type {
  315. margin-bottom: 50rpx;
  316. }
  317. .form-label {
  318. display: block;
  319. font-size: 28rpx;
  320. color: #303133;
  321. margin-bottom: 16rpx;
  322. font-weight: 500;
  323. }
  324. .form-input-box {
  325. display: flex;
  326. align-items: center;
  327. height: 88rpx;
  328. background: #f5f7fa;
  329. border-radius: 16rpx;
  330. padding: 0 24rpx;
  331. box-sizing: border-box;
  332. }
  333. .form-input-ico {
  334. margin-right: 10rpx;
  335. flex-shrink: 0;
  336. display: flex;
  337. align-items: center;
  338. justify-content: center;
  339. }
  340. .form-input {
  341. flex: 1;
  342. min-width: 0;
  343. height: 100%;
  344. padding: 0;
  345. border: none;
  346. background: transparent;
  347. font-size: 28rpx;
  348. color: #303133;
  349. line-height: 1.25;
  350. }
  351. .form-actions {
  352. width: 100%;
  353. }
  354. .btn-login {
  355. width: 100%;
  356. height: 88rpx;
  357. border-radius: 44rpx;
  358. font-size: 30rpx;
  359. color: #fff;
  360. background: linear-gradient(135deg, var(--theme-color) 0%, #07c160 100%);
  361. border: none;
  362. display: flex;
  363. align-items: center;
  364. justify-content: center;
  365. padding: 0;
  366. line-height: 1.2;
  367. }
  368. .btn-login::after {
  369. border: none;
  370. }
  371. .btn-login.button-hover,
  372. .btn-login:active {
  373. background: linear-gradient(135deg, var(--theme-color) 0%, #07c160 100%);
  374. color: #fff;
  375. opacity: 1;
  376. }
  377. .btn-login-text {
  378. line-height: 1.2;
  379. font-size: 30rpx;
  380. }
  381. .login-footer {
  382. margin-top: 60rpx;
  383. padding: 0 20rpx;
  384. }
  385. .footer-line {
  386. display: flex;
  387. flex-wrap: wrap;
  388. justify-content: center;
  389. align-items: center;
  390. column-gap: 4rpx;
  391. row-gap: 8rpx;
  392. }
  393. .agree-check {
  394. display: inline-flex;
  395. align-items: center;
  396. justify-content: center;
  397. padding: 8rpx;
  398. margin-right: 4rpx;
  399. flex-shrink: 0;
  400. }
  401. .agree-check__icon {
  402. width: 30rpx;
  403. height: 30rpx;
  404. border-radius: 50%;
  405. border: 2rpx solid #c0c4cc;
  406. box-sizing: border-box;
  407. display: flex;
  408. align-items: center;
  409. justify-content: center;
  410. background: #fff;
  411. }
  412. .agree-check__tick {
  413. font-size: 18rpx;
  414. font-weight: 700;
  415. line-height: 1;
  416. color: transparent;
  417. }
  418. .agree-check__icon--checked {
  419. border-color: var(--theme-color);
  420. background: var(--theme-color);
  421. }
  422. .agree-check__icon--checked .agree-check__tick {
  423. color: #fff;
  424. }
  425. .footer-text,
  426. .link {
  427. font-size: 22rpx;
  428. line-height: 1.6;
  429. }
  430. .footer-text {
  431. color: #909399;
  432. }
  433. .link {
  434. color: var(--theme-color);
  435. }
  436. .protocol-dialog-mask {
  437. position: fixed;
  438. inset: 0;
  439. background: rgba(0, 0, 0, 0.42);
  440. z-index: 99;
  441. display: flex;
  442. align-items: center;
  443. justify-content: center;
  444. padding: 40rpx;
  445. box-sizing: border-box;
  446. }
  447. .protocol-dialog {
  448. width: 100%;
  449. max-width: 620rpx;
  450. background: #fff;
  451. border-radius: 24rpx;
  452. padding: 36rpx 30rpx 28rpx;
  453. box-sizing: border-box;
  454. box-shadow: 0 14rpx 48rpx rgba(0, 0, 0, 0.16);
  455. }
  456. .protocol-dialog__title {
  457. display: block;
  458. text-align: center;
  459. font-size: 34rpx;
  460. font-weight: 700;
  461. color: #303133;
  462. }
  463. .protocol-dialog__desc {
  464. display: block;
  465. margin-top: 16rpx;
  466. text-align: center;
  467. font-size: 26rpx;
  468. line-height: 1.6;
  469. color: #606266;
  470. }
  471. .protocol-dialog__actions {
  472. margin-top: 30rpx;
  473. display: flex;
  474. gap: 16rpx;
  475. }
  476. .protocol-dialog__btn {
  477. flex: 1;
  478. min-height: 78rpx;
  479. border-radius: 999rpx;
  480. display: flex;
  481. align-items: center;
  482. justify-content: center;
  483. font-size: 28rpx;
  484. font-weight: 600;
  485. }
  486. .protocol-dialog__btn--cancel {
  487. background: #f4f7f6;
  488. color: #5b6b64;
  489. border: 1px solid #ddebe5;
  490. }
  491. .protocol-dialog__btn--confirm {
  492. background: linear-gradient(135deg, var(--theme-color) 0%, #07c160 100%);
  493. color: #fff;
  494. border: none;
  495. margin: 0;
  496. padding: 0;
  497. line-height: normal;
  498. }
  499. button.protocol-dialog__btn--confirm::after {
  500. border: none;
  501. }
  502. .login-divider {
  503. display: flex;
  504. align-items: center;
  505. gap: 24rpx;
  506. margin: 44rpx 0 28rpx;
  507. width: 100%;
  508. font-size: 24rpx;
  509. color: #94a3b8;
  510. }
  511. .login-divider::before,
  512. .login-divider::after {
  513. content: '';
  514. flex: 1;
  515. height: 1px;
  516. background: #e2e8f0;
  517. }
  518. .login-wechat {
  519. height: 88rpx;
  520. border-radius: 24rpx;
  521. border: 1px solid #e2e8f0;
  522. background: #fff;
  523. display: flex;
  524. align-items: center;
  525. justify-content: center;
  526. width: 100%;
  527. margin: 0;
  528. padding: 0;
  529. line-height: normal;
  530. box-sizing: border-box;
  531. font-size: 28rpx;
  532. font-weight: 500;
  533. color: #1e293b;
  534. }
  535. button.login-wechat::after {
  536. border: none;
  537. }
  538. .login-wechat__label {
  539. font-size: 28rpx;
  540. font-weight: 500;
  541. color: #1e293b;
  542. }
  543. </style>
  544. <!-- placeholder-class 在部分真机需非 scoped 才稳定生效;颜色与 --login-input-placeholder 一致 -->
  545. <style lang="scss">
  546. .input-placeholder {
  547. color: #c0c4cc;
  548. font-size: 28rpx;
  549. }
  550. </style>