login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. let protocolDialogResolver = null
  16. onShow(() => {
  17. userStore.checkLoginStatus()
  18. if (userStore.isLoggedIn) {
  19. userStore.navigateToHome()
  20. }
  21. })
  22. const loginPageStyle = computed(() => ({ '--theme-color': '#0a9f6d' }))
  23. const handleSubmit = async () => {
  24. if (!agreeProtocol.value) {
  25. const shouldContinue = await new Promise((resolve) => {
  26. protocolDialogResolver = resolve
  27. protocolDialogVisible.value = true
  28. })
  29. if (!shouldContinue) return
  30. agreeProtocol.value = true
  31. }
  32. if (!form.value.username) {
  33. uni.showToast({ title: '请输入用户名', icon: 'none' })
  34. return
  35. }
  36. if (!form.value.password) {
  37. uni.showToast({ title: '请输入密码', icon: 'none' })
  38. return
  39. }
  40. isLoading.value = true
  41. try {
  42. const success = await userStore.login({
  43. username: form.value.username,
  44. password: form.value.password
  45. })
  46. if (success) {
  47. userStore.navigateToHome()
  48. }
  49. } finally {
  50. isLoading.value = false
  51. }
  52. }
  53. const goUserAgreement = () => {
  54. uni.navigateTo({ url: '/pages/login/user-agreement' })
  55. }
  56. const goPrivacyPolicy = () => {
  57. uni.navigateTo({ url: '/pages/login/privacy-policy' })
  58. }
  59. const toggleProtocolAgreement = () => {
  60. agreeProtocol.value = !agreeProtocol.value
  61. }
  62. const resolveProtocolDialog = (confirmed) => {
  63. protocolDialogVisible.value = false
  64. if (protocolDialogResolver) {
  65. protocolDialogResolver(confirmed)
  66. protocolDialogResolver = null
  67. }
  68. }
  69. </script>
  70. <template>
  71. <view class="login-page" :style="loginPageStyle">
  72. <!-- 背景装饰 -->
  73. <view class="login-bg">
  74. <view class="bg-circle circle-1"></view>
  75. <view class="bg-circle circle-2"></view>
  76. <view class="bg-circle circle-3"></view>
  77. </view>
  78. <view class="login-content">
  79. <view class="logo-section">
  80. <image class="logo-image" :src="DEFAULT_PLACEHOLDER_IMAGE_URL" mode="aspectFit" />
  81. <text class="app-name">山峡化工</text>
  82. <text class="app-slogan">一体化业务移动端</text>
  83. </view>
  84. <view class="login-form">
  85. <view class="form-item">
  86. <text class="form-label">用户名</text>
  87. <view class="form-input-box">
  88. <uni-icons class="form-input-ico" type="person" size="18" color="#c0c4cc" />
  89. <input
  90. v-model="form.username"
  91. class="form-input"
  92. type="text"
  93. placeholder="请输入用户名"
  94. placeholder-class="input-placeholder"
  95. />
  96. </view>
  97. </view>
  98. <view class="form-item">
  99. <text class="form-label">密码</text>
  100. <view class="form-input-box">
  101. <uni-icons class="form-input-ico" type="locked" size="18" color="#c0c4cc" />
  102. <input
  103. v-model="form.password"
  104. class="form-input"
  105. type="password"
  106. placeholder="请输入密码"
  107. placeholder-class="input-placeholder"
  108. />
  109. </view>
  110. </view>
  111. <view class="form-actions">
  112. <button class="btn-login" :disabled="loginDisabled" @click="handleSubmit">
  113. <text v-if="isLoading" class="btn-login-text">登录中...</text>
  114. <text v-else class="btn-login-text">账号密码登录</text>
  115. </button>
  116. </view>
  117. </view>
  118. <view class="login-footer">
  119. <view class="footer-line">
  120. <view class="agree-check" @tap="toggleProtocolAgreement">
  121. <view class="agree-check__icon" :class="{ 'agree-check__icon--checked': agreeProtocol }">
  122. <text class="agree-check__tick">✓</text>
  123. </view>
  124. </view>
  125. <text class="footer-text">登录即表示同意</text>
  126. <text class="link" @tap="goUserAgreement">《用户服务协议》</text>
  127. <text class="footer-text">和</text>
  128. <text class="link" @tap="goPrivacyPolicy">《隐私政策》</text>
  129. </view>
  130. </view>
  131. </view>
  132. <view v-if="protocolDialogVisible" class="protocol-dialog-mask" @tap="resolveProtocolDialog(false)">
  133. <view class="protocol-dialog" @tap.stop>
  134. <text class="protocol-dialog__title">协议确认</text>
  135. <text class="protocol-dialog__desc">登录前请先同意《用户服务协议》和《隐私政策》</text>
  136. <view class="protocol-dialog__actions">
  137. <view class="protocol-dialog__btn protocol-dialog__btn--cancel" @tap="resolveProtocolDialog(false)">否</view>
  138. <view class="protocol-dialog__btn protocol-dialog__btn--confirm" @tap="resolveProtocolDialog(true)">是,继续登录</view>
  139. </view>
  140. </view>
  141. </view>
  142. </view>
  143. </template>
  144. <style lang="scss" scoped>
  145. .login-page {
  146. min-height: 100vh;
  147. background: linear-gradient(180deg, #f8fff8 0%, #ffffff 100%);
  148. position: relative;
  149. overflow: hidden;
  150. /* 与 placeholder 同色,供图标 mask 与 :deep(.input-placeholder) 共用 */
  151. --login-input-placeholder: #c0c4cc;
  152. }
  153. .login-bg {
  154. position: absolute;
  155. top: 0;
  156. left: 0;
  157. right: 0;
  158. bottom: 0;
  159. pointer-events: none;
  160. }
  161. .login-bg .bg-circle {
  162. position: absolute;
  163. border-radius: 50%;
  164. background: var(--theme-color);
  165. opacity: 0.05;
  166. }
  167. .login-bg .circle-1 {
  168. width: 400rpx;
  169. height: 400rpx;
  170. top: -100rpx;
  171. right: -100rpx;
  172. }
  173. .login-bg .circle-2 {
  174. width: 300rpx;
  175. height: 300rpx;
  176. top: 300rpx;
  177. left: -150rpx;
  178. }
  179. .login-bg .circle-3 {
  180. width: 200rpx;
  181. height: 200rpx;
  182. bottom: 100rpx;
  183. right: -50rpx;
  184. }
  185. .login-content {
  186. position: relative;
  187. z-index: 1;
  188. width: 100%;
  189. max-width: 680rpx;
  190. margin: 0 auto;
  191. box-sizing: border-box;
  192. padding: calc(120rpx + var(--sq-safe-top, env(safe-area-inset-top, 0px))) 40rpx 60rpx;
  193. }
  194. .logo-section {
  195. display: flex;
  196. flex-direction: column;
  197. align-items: center;
  198. margin-bottom: 80rpx;
  199. }
  200. .logo-image {
  201. width: 160rpx;
  202. height: 160rpx;
  203. border-radius: 24rpx;
  204. }
  205. .app-name {
  206. font-size: 48rpx;
  207. font-weight: 700;
  208. color: #303133;
  209. margin-top: 30rpx;
  210. }
  211. .app-slogan {
  212. font-size: 28rpx;
  213. color: #909399;
  214. margin-top: 12rpx;
  215. }
  216. .login-form {
  217. background: #fff;
  218. border-radius: 24rpx;
  219. padding: 50rpx 40rpx;
  220. box-shadow: 0 10rpx 40rpx rgba(0, 0, 0, 0.08);
  221. }
  222. .form-item {
  223. margin-bottom: 36rpx;
  224. }
  225. .form-item:last-of-type {
  226. margin-bottom: 50rpx;
  227. }
  228. .form-label {
  229. display: block;
  230. font-size: 28rpx;
  231. color: #303133;
  232. margin-bottom: 16rpx;
  233. font-weight: 500;
  234. }
  235. .form-input-box {
  236. display: flex;
  237. align-items: center;
  238. height: 88rpx;
  239. background: #f5f7fa;
  240. border-radius: 16rpx;
  241. padding: 0 24rpx;
  242. box-sizing: border-box;
  243. }
  244. .form-input-ico {
  245. margin-right: 10rpx;
  246. flex-shrink: 0;
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. }
  251. .form-input {
  252. flex: 1;
  253. min-width: 0;
  254. height: 100%;
  255. padding: 0;
  256. border: none;
  257. background: transparent;
  258. font-size: 28rpx;
  259. color: #303133;
  260. line-height: 1.25;
  261. }
  262. .form-actions {
  263. width: 100%;
  264. }
  265. .btn-login {
  266. width: 100%;
  267. height: 88rpx;
  268. border-radius: 44rpx;
  269. font-size: 30rpx;
  270. color: #fff;
  271. background: linear-gradient(135deg, var(--theme-color) 0%, #07c160 100%);
  272. border: none;
  273. display: flex;
  274. align-items: center;
  275. justify-content: center;
  276. padding: 0;
  277. line-height: 1.2;
  278. }
  279. .btn-login::after {
  280. border: none;
  281. }
  282. .btn-login.button-hover,
  283. .btn-login:active {
  284. background: linear-gradient(135deg, var(--theme-color) 0%, #07c160 100%);
  285. color: #fff;
  286. opacity: 1;
  287. }
  288. .btn-login-text {
  289. line-height: 1.2;
  290. font-size: 30rpx;
  291. }
  292. .login-footer {
  293. margin-top: 60rpx;
  294. padding: 0 20rpx;
  295. }
  296. .footer-line {
  297. display: flex;
  298. flex-wrap: wrap;
  299. justify-content: center;
  300. align-items: center;
  301. column-gap: 4rpx;
  302. row-gap: 8rpx;
  303. }
  304. .agree-check {
  305. display: inline-flex;
  306. align-items: center;
  307. justify-content: center;
  308. padding: 8rpx;
  309. margin-right: 4rpx;
  310. flex-shrink: 0;
  311. }
  312. .agree-check__icon {
  313. width: 30rpx;
  314. height: 30rpx;
  315. border-radius: 50%;
  316. border: 2rpx solid #c0c4cc;
  317. box-sizing: border-box;
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. background: #fff;
  322. }
  323. .agree-check__tick {
  324. font-size: 18rpx;
  325. font-weight: 700;
  326. line-height: 1;
  327. color: transparent;
  328. }
  329. .agree-check__icon--checked {
  330. border-color: var(--theme-color);
  331. background: var(--theme-color);
  332. }
  333. .agree-check__icon--checked .agree-check__tick {
  334. color: #fff;
  335. }
  336. .footer-text,
  337. .link {
  338. font-size: 22rpx;
  339. line-height: 1.6;
  340. }
  341. .footer-text {
  342. color: #909399;
  343. }
  344. .link {
  345. color: var(--theme-color);
  346. }
  347. .protocol-dialog-mask {
  348. position: fixed;
  349. inset: 0;
  350. background: rgba(0, 0, 0, 0.42);
  351. z-index: 99;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. padding: 40rpx;
  356. box-sizing: border-box;
  357. }
  358. .protocol-dialog {
  359. width: 100%;
  360. max-width: 620rpx;
  361. background: #fff;
  362. border-radius: 24rpx;
  363. padding: 36rpx 30rpx 28rpx;
  364. box-sizing: border-box;
  365. box-shadow: 0 14rpx 48rpx rgba(0, 0, 0, 0.16);
  366. }
  367. .protocol-dialog__title {
  368. display: block;
  369. text-align: center;
  370. font-size: 34rpx;
  371. font-weight: 700;
  372. color: #303133;
  373. }
  374. .protocol-dialog__desc {
  375. display: block;
  376. margin-top: 16rpx;
  377. text-align: center;
  378. font-size: 26rpx;
  379. line-height: 1.6;
  380. color: #606266;
  381. }
  382. .protocol-dialog__actions {
  383. margin-top: 30rpx;
  384. display: flex;
  385. gap: 16rpx;
  386. }
  387. .protocol-dialog__btn {
  388. flex: 1;
  389. min-height: 78rpx;
  390. border-radius: 999rpx;
  391. display: flex;
  392. align-items: center;
  393. justify-content: center;
  394. font-size: 28rpx;
  395. font-weight: 600;
  396. }
  397. .protocol-dialog__btn--cancel {
  398. background: #f4f7f6;
  399. color: #5b6b64;
  400. border: 1px solid #ddebe5;
  401. }
  402. .protocol-dialog__btn--confirm {
  403. background: linear-gradient(135deg, var(--theme-color) 0%, #07c160 100%);
  404. color: #fff;
  405. }
  406. </style>
  407. <!-- placeholder-class 在部分真机需非 scoped 才稳定生效;颜色与 --login-input-placeholder 一致 -->
  408. <style lang="scss">
  409. .input-placeholder {
  410. color: #c0c4cc;
  411. font-size: 28rpx;
  412. }
  413. </style>