index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <view class="login-page">
  3. <nav-bar title="登录" bgColor="transparent" color="#fff"></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="20" 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. </view>
  61. <!-- 底部装饰 -->
  62. <view class="footer-hint">
  63. <text>安全加密 · 保护您的账号信息</text>
  64. </view>
  65. <!-- 隐私政策/托运协议弹窗 -->
  66. <policy-dialog v-model:visible="dialogVisible" :title="dialogTitle" :content="dialogContent"></policy-dialog>
  67. </view>
  68. </template>
  69. <script setup>
  70. import { ref } from 'vue'
  71. import navBar from '@/components/nav-bar/index.vue'
  72. import policyDialog from '@/components/policy-dialog/index.vue'
  73. import { login } from '@/api/auth'
  74. import { getInfo } from '@/api/system/user'
  75. import { getAgreement } from '@/api/system/agreement'
  76. import { AgreementType } from '@/enums/agreementType'
  77. import { DEFAULT_HEADERS } from '@/utils/config'
  78. const username = ref('')
  79. const password = ref('')
  80. const checked = ref(false)
  81. const dialogVisible = ref(false)
  82. const dialogTitle = ref('')
  83. const dialogContent = ref('')
  84. const onClickLeft = () => uni.navigateBack()
  85. const onCheckChange = () => {
  86. checked.value = !checked.value
  87. }
  88. const onSubmit = async () => {
  89. if (!username.value) {
  90. uni.showToast({ title: '请填写账号', icon: 'none' })
  91. return
  92. }
  93. if (!password.value) {
  94. uni.showToast({ title: '请填写密码', icon: 'none' })
  95. return
  96. }
  97. if (!checked.value) {
  98. uni.showToast({ title: '请先阅读并勾选协议', icon: 'none' })
  99. return
  100. }
  101. try {
  102. uni.showLoading({ title: '登录中...' })
  103. const res = await login({
  104. userSource: 0,
  105. username: username.value,
  106. password: password.value,
  107. clientId: DEFAULT_HEADERS.clientid,
  108. grantType: 'password',
  109. source: 1
  110. })
  111. if (res.access_token) {
  112. uni.setStorageSync('token', res.access_token)
  113. // 获取用户信息并存储 tenantId
  114. try {
  115. const userRes = await getInfo()
  116. if (userRes && userRes.user && userRes.user.tenantId) {
  117. uni.setStorageSync('tenantId', userRes.user.tenantId)
  118. }
  119. } catch (e) {
  120. console.error('获取用户信息失败', e)
  121. }
  122. uni.showToast({ title: '登录成功', icon: 'success' })
  123. setTimeout(() => {
  124. uni.reLaunch({ url: '/pages/index/index' })
  125. }, 1000)
  126. } else {
  127. uni.showToast({ title: '登录异常:未获取到Token', icon: 'none' })
  128. }
  129. } catch (error) {
  130. console.error('Login error:', error)
  131. // 注意这里的提示也可以通过request.js自带去提示,这里静默或者输出
  132. } finally {
  133. uni.hideLoading()
  134. }
  135. }
  136. const showAgreement = async (agreementId) => {
  137. try {
  138. uni.showLoading({ title: '加载中...' })
  139. const res = await getAgreement(agreementId)
  140. if (res && res.title) {
  141. dialogTitle.value = res.title || '协议详情'
  142. dialogContent.value = res.content || '暂无内容'
  143. dialogVisible.value = true
  144. } else {
  145. console.warn('接口返回数据格式异常:', res)
  146. uni.showToast({ title: '数据格式异常', icon: 'none' })
  147. }
  148. } catch (error) {
  149. console.error('获取协议失败:', error)
  150. uni.showToast({ title: '加载失败,请稍后重试', icon: 'none' })
  151. } finally {
  152. uni.hideLoading()
  153. }
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. .login-page {
  158. min-height: 100vh;
  159. background: #f2f3f7;
  160. display: flex;
  161. flex-direction: column;
  162. }
  163. .hero-bg {
  164. background: linear-gradient(150deg, #ffd53f 0%, #ff9500 100%);
  165. padding: 0 40rpx 140rpx;
  166. position: relative;
  167. overflow: hidden;
  168. min-height: 560rpx;
  169. display: flex;
  170. flex-direction: column;
  171. justify-content: flex-end;
  172. margin-top: calc(-44px - var(--status-bar-height, 44px));
  173. }
  174. .deco-circle {
  175. position: absolute;
  176. border-radius: 50%;
  177. background: rgba(255, 255, 255, 0.12);
  178. }
  179. .c1 {
  180. width: 400rpx;
  181. height: 400rpx;
  182. top: -160rpx;
  183. right: -120rpx;
  184. }
  185. .c2 {
  186. width: 260rpx;
  187. height: 260rpx;
  188. top: 80rpx;
  189. left: -100rpx;
  190. }
  191. .c3 {
  192. width: 160rpx;
  193. height: 160rpx;
  194. bottom: 80rpx;
  195. right: 80rpx;
  196. }
  197. .back-btn {
  198. position: absolute;
  199. top: calc(var(--status-bar-height, 44px) + 20rpx);
  200. left: 32rpx;
  201. width: 72rpx;
  202. height: 72rpx;
  203. border-radius: 50%;
  204. background: rgba(255, 255, 255, 0.25);
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. }
  209. .hero-content {
  210. position: relative;
  211. z-index: 2;
  212. }
  213. .logo-wrap {
  214. width: 140rpx;
  215. height: 140rpx;
  216. background: rgba(255, 255, 255, 0.25);
  217. border-radius: 44rpx;
  218. display: flex;
  219. align-items: center;
  220. justify-content: center;
  221. margin-bottom: 28rpx;
  222. }
  223. .brand-name {
  224. display: block;
  225. font-size: 52rpx;
  226. font-weight: 800;
  227. color: #fff;
  228. margin-bottom: 8rpx;
  229. }
  230. .brand-desc {
  231. display: block;
  232. font-size: 28rpx;
  233. color: rgba(255, 255, 255, 0.8);
  234. letter-spacing: 4rpx;
  235. }
  236. .form-card {
  237. background: #fff;
  238. border-radius: 56rpx 56rpx 0 0;
  239. margin-top: -60rpx;
  240. padding: 60rpx 48rpx 40rpx;
  241. flex: 1;
  242. position: relative;
  243. z-index: 10;
  244. }
  245. .form-title {
  246. display: block;
  247. font-size: 38rpx;
  248. font-weight: 700;
  249. color: #1a1a1a;
  250. margin-bottom: 48rpx;
  251. }
  252. .input-group {
  253. display: flex;
  254. align-items: center;
  255. background: #f8f8f8;
  256. border-radius: 28rpx;
  257. padding: 8rpx 24rpx;
  258. margin-bottom: 28rpx;
  259. border: 3rpx solid transparent;
  260. }
  261. .input-icon-wrap {
  262. margin-right: 16rpx;
  263. flex-shrink: 0;
  264. }
  265. .custom-input {
  266. flex: 1;
  267. height: 88rpx;
  268. font-size: 30rpx;
  269. color: #222;
  270. background: transparent;
  271. }
  272. .input-placeholder {
  273. color: #c0c0c0;
  274. }
  275. .tip-row {
  276. display: flex;
  277. align-items: center;
  278. gap: 10rpx;
  279. font-size: 24rpx;
  280. color: #aaa;
  281. padding: 8rpx 8rpx 24rpx;
  282. }
  283. .agreement-row {
  284. margin-bottom: 48rpx;
  285. padding: 0 4rpx;
  286. }
  287. .agree-label {
  288. display: flex;
  289. align-items: center;
  290. flex-wrap: wrap;
  291. }
  292. .agree-text {
  293. font-size: 24rpx;
  294. color: #999;
  295. }
  296. .text-link {
  297. font-size: 24rpx;
  298. color: #ff9500;
  299. }
  300. .login-btn {
  301. width: 100%;
  302. height: 104rpx;
  303. font-size: 34rpx;
  304. font-weight: 700;
  305. color: #333;
  306. background: linear-gradient(90deg, #ffd53f, #ff9500);
  307. border: none;
  308. border-radius: 52rpx;
  309. letter-spacing: 4rpx;
  310. }
  311. .footer-hint {
  312. text-align: center;
  313. font-size: 22rpx;
  314. color: #bbb;
  315. padding: 32rpx 0 60rpx;
  316. background: #fff;
  317. }
  318. </style>