index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <view class="login-page">
  3. <nav-bar title="登录" bgColor="transparent" color="#fff" :showBack="false"></nav-bar>
  4. <!-- 顶部渐变装饰区 -->
  5. <view class="hero-bg">
  6. <view class="deco-circle c1"></view>
  7. <view class="deco-circle c2"></view>
  8. <view class="deco-circle c3"></view>
  9. <!-- 返回按钮 -->
  10. <view class="back-btn" @click="onClickLeft">
  11. <uni-icons type="left" size="22" color="#fff"></uni-icons>
  12. </view>
  13. <!-- Logo 区域 -->
  14. <view class="hero-content">
  15. <view class="logo-wrap">
  16. <uni-icons type="headphones" size="42" color="#fff"></uni-icons>
  17. </view>
  18. <text class="brand-name">好萌友</text>
  19. <text class="brand-desc">专业 · 安心 · 便捷</text>
  20. </view>
  21. </view>
  22. <!-- 表单白色卡片区 -->
  23. <view class="form-card">
  24. <text class="form-title">账号登录</text>
  25. <!-- 账号输入框 -->
  26. <view class="input-group">
  27. <view class="input-icon-wrap">
  28. <uni-icons type="person" size="18" color="#ffc837"></uni-icons>
  29. </view>
  30. <input class="custom-input" v-model="username" placeholder="请输入登录账号"
  31. placeholder-class="input-placeholder" />
  32. </view>
  33. <!-- 密码输入框 -->
  34. <view class="input-group">
  35. <view class="input-icon-wrap">
  36. <uni-icons type="locked" size="18" color="#ffc837"></uni-icons>
  37. </view>
  38. <input class="custom-input" v-model="password" type="password" placeholder="请输入密码"
  39. placeholder-class="input-placeholder" />
  40. </view>
  41. <!-- 提示信息 -->
  42. <view class="tip-row">
  43. <uni-icons type="info" size="13" color="#ffaa00"></uni-icons>
  44. <text>账号由后台创建,不支持自主注册</text>
  45. </view>
  46. <!-- 协议勾选 -->
  47. <view class="agreement-row">
  48. <checkbox-group @change="onCheckChange">
  49. <label class="agree-label">
  50. <checkbox :checked="checked" color="#ffc837" style="transform: scale(0.7);" />
  51. <text class="agree-text">我已阅读并同意</text>
  52. <text class="text-link" @click.stop="showAgreement(2)">《隐私政策》</text>
  53. <text class="agree-text">和</text>
  54. <text class="text-link" @click.stop="showAgreement(4)">《托运协议》</text>
  55. </label>
  56. </checkbox-group>
  57. </view>
  58. <!-- 登录按钮 -->
  59. <button class="login-btn" @click="onSubmit">安全登录</button>
  60. <!-- 注册入口 -->
  61. <view class="register-redirect-row">
  62. <text class="redirect-hint">还没有账号?</text>
  63. <text class="redirect-action" @click="goToRegister">立即注册</text>
  64. </view>
  65. </view>
  66. <!-- 底部装饰 -->
  67. <view class="footer-hint">
  68. <text>安全加密 · 保护您的账号信息</text>
  69. </view>
  70. <!-- 隐私政策/托运协议弹窗 -->
  71. <policy-dialog v-model:visible="dialogVisible" :title="dialogTitle" :content="dialogContent"></policy-dialog>
  72. </view>
  73. </template>
  74. <script setup>
  75. import { ref } from 'vue'
  76. import navBar from '@/components/nav-bar/index.vue'
  77. import policyDialog from '@/components/policy-dialog/index.vue'
  78. import { login } from '@/api/auth'
  79. import { getInfo } from '@/api/system/user'
  80. import { getAgreement } from '@/api/system/agreement'
  81. import { AgreementType } from '@/enums/agreementType'
  82. import { DEFAULT_HEADERS } from '@/utils/config'
  83. const username = ref('')
  84. const password = ref('')
  85. const checked = ref(false)
  86. const dialogVisible = ref(false)
  87. const dialogTitle = ref('')
  88. const dialogContent = ref('')
  89. const onClickLeft = () => uni.reLaunch({ url: '/pages/index/index' })
  90. const goToRegister = () => {
  91. uni.navigateTo({
  92. url: '/pages/login/register'
  93. })
  94. }
  95. const onCheckChange = () => {
  96. checked.value = !checked.value
  97. }
  98. const onSubmit = async () => {
  99. if (!username.value) {
  100. uni.showToast({ title: '请填写账号', icon: 'none' })
  101. return
  102. }
  103. if (!password.value) {
  104. uni.showToast({ title: '请填写密码', icon: 'none' })
  105. return
  106. }
  107. if (!checked.value) {
  108. uni.showToast({ title: '请先阅读并勾选协议', icon: 'none' })
  109. return
  110. }
  111. try {
  112. uni.showLoading({ title: '登录中...' })
  113. const res = await login({
  114. userSource: 0,
  115. username: username.value,
  116. password: password.value,
  117. clientId: DEFAULT_HEADERS.clientid,
  118. grantType: 'password',
  119. source: 1
  120. })
  121. if (res.access_token) {
  122. uni.setStorageSync('token', res.access_token)
  123. // 获取用户信息并存储 tenantId
  124. try {
  125. const userRes = await getInfo()
  126. if (userRes && userRes.user && userRes.user.tenantId) {
  127. uni.setStorageSync('tenantId', userRes.user.tenantId)
  128. }
  129. } catch (e) {
  130. console.error('获取用户信息失败', e)
  131. uni.showToast({ title: typeof e === 'string' ? e : '请求失败', icon: 'none' })
  132. }
  133. uni.showToast({ title: '登录成功', icon: 'success' })
  134. setTimeout(() => {
  135. uni.reLaunch({ url: '/pages/index/index' })
  136. }, 1000)
  137. } else {
  138. uni.showToast({ title: '登录异常:未获取到Token', icon: 'none' })
  139. }
  140. } catch (error) {
  141. console.error('Login error:', error)
  142. uni.showToast({ title: typeof error === 'string' ? error : '登录失败', icon: 'none' })
  143. } finally {
  144. uni.hideLoading()
  145. }
  146. }
  147. const showAgreement = async (agreementId) => {
  148. try {
  149. uni.showLoading({ title: '加载中...' })
  150. const res = await getAgreement(agreementId)
  151. if (res && res.title) {
  152. dialogTitle.value = res.title || '协议详情'
  153. let decodedContent = res.content || '暂无内容'
  154. if (res.content) {
  155. try {
  156. decodedContent = decodeURIComponent(escape(atob(res.content)))
  157. } catch (e) {
  158. decodedContent = res.content
  159. }
  160. }
  161. dialogContent.value = decodedContent
  162. dialogVisible.value = true
  163. } else {
  164. console.warn('接口返回数据格式异常:', res)
  165. uni.showToast({ title: '数据格式异常', icon: 'none' })
  166. }
  167. } catch (error) {
  168. console.error('获取协议失败:', error)
  169. uni.showToast({ title: typeof error === 'string' ? error : '加载失败,请稍后重试', icon: 'none' })
  170. } finally {
  171. uni.hideLoading()
  172. }
  173. }
  174. </script>
  175. <style lang="scss" scoped>
  176. .login-page {
  177. min-height: 100vh;
  178. background: #f2f3f7;
  179. display: flex;
  180. flex-direction: column;
  181. }
  182. :deep(.nav-bar) {
  183. position: absolute !important;
  184. }
  185. .hero-bg {
  186. background: linear-gradient(150deg, #ffd53f 0%, #ff9500 100%);
  187. padding: 0 40rpx 140rpx;
  188. position: relative;
  189. overflow: hidden;
  190. min-height: 560rpx;
  191. display: flex;
  192. flex-direction: column;
  193. justify-content: flex-end;
  194. }
  195. .deco-circle {
  196. position: absolute;
  197. border-radius: 50%;
  198. background: rgba(255, 255, 255, 0.12);
  199. }
  200. .c1 {
  201. width: 400rpx;
  202. height: 400rpx;
  203. top: -160rpx;
  204. right: -120rpx;
  205. }
  206. .c2 {
  207. width: 260rpx;
  208. height: 260rpx;
  209. top: 80rpx;
  210. left: -100rpx;
  211. }
  212. .c3 {
  213. width: 160rpx;
  214. height: 160rpx;
  215. bottom: 80rpx;
  216. right: 80rpx;
  217. }
  218. .back-btn {
  219. position: absolute;
  220. top: calc(var(--status-bar-height, 44px) + 20rpx);
  221. left: 32rpx;
  222. width: 76rpx;
  223. height: 76rpx;
  224. border-radius: 50%;
  225. background: rgba(255, 255, 255, 0.35);
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  230. z-index: 1000;
  231. }
  232. .hero-content {
  233. position: relative;
  234. z-index: 2;
  235. }
  236. .logo-wrap {
  237. width: 140rpx;
  238. height: 140rpx;
  239. background: rgba(255, 255, 255, 0.25);
  240. border-radius: 44rpx;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. margin-bottom: 28rpx;
  245. }
  246. .brand-name {
  247. display: block;
  248. font-size: 52rpx;
  249. font-weight: 800;
  250. color: #fff;
  251. margin-bottom: 8rpx;
  252. }
  253. .brand-desc {
  254. display: block;
  255. font-size: 28rpx;
  256. color: rgba(255, 255, 255, 0.8);
  257. letter-spacing: 4rpx;
  258. }
  259. .form-card {
  260. background: #fff;
  261. border-radius: 56rpx 56rpx 0 0;
  262. margin-top: -60rpx;
  263. padding: 60rpx 48rpx 40rpx;
  264. flex: 1;
  265. position: relative;
  266. z-index: 10;
  267. }
  268. .form-title {
  269. display: block;
  270. font-size: 38rpx;
  271. font-weight: 700;
  272. color: #1a1a1a;
  273. margin-bottom: 48rpx;
  274. }
  275. .input-group {
  276. display: flex;
  277. align-items: center;
  278. background: #f8f8f8;
  279. border-radius: 28rpx;
  280. padding: 8rpx 24rpx;
  281. margin-bottom: 28rpx;
  282. border: 3rpx solid transparent;
  283. }
  284. .input-icon-wrap {
  285. margin-right: 16rpx;
  286. flex-shrink: 0;
  287. }
  288. .custom-input {
  289. flex: 1;
  290. height: 88rpx;
  291. font-size: 30rpx;
  292. color: #222;
  293. background: transparent;
  294. }
  295. .input-placeholder {
  296. color: #c0c0c0;
  297. }
  298. .tip-row {
  299. display: flex;
  300. align-items: center;
  301. gap: 10rpx;
  302. font-size: 24rpx;
  303. color: #aaa;
  304. padding: 8rpx 8rpx 24rpx;
  305. }
  306. .agreement-row {
  307. margin-bottom: 48rpx;
  308. padding: 0 4rpx;
  309. }
  310. .agree-label {
  311. display: flex;
  312. align-items: center;
  313. flex-wrap: wrap;
  314. }
  315. .agree-text {
  316. font-size: 24rpx;
  317. color: #999;
  318. }
  319. .text-link {
  320. font-size: 24rpx;
  321. color: #ff9500;
  322. }
  323. .login-btn {
  324. width: 100%;
  325. height: 104rpx;
  326. font-size: 34rpx;
  327. font-weight: 700;
  328. color: #fff;
  329. background: linear-gradient(90deg, #ffd53f, #ff9500);
  330. border: none;
  331. border-radius: 52rpx;
  332. letter-spacing: 4rpx;
  333. &::after { border: none; }
  334. }
  335. .footer-hint {
  336. text-align: center;
  337. font-size: 22rpx;
  338. color: #bbb;
  339. padding: 32rpx 0 60rpx;
  340. background: #fff;
  341. }
  342. .register-redirect-row {
  343. display: flex;
  344. justify-content: center;
  345. align-items: center;
  346. margin-top: 36rpx;
  347. font-size: 26rpx;
  348. }
  349. .redirect-hint {
  350. color: #999;
  351. }
  352. .redirect-action {
  353. color: #ff9500;
  354. font-weight: bold;
  355. margin-left: 12rpx;
  356. text-decoration: underline;
  357. }
  358. </style>