login.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <template>
  2. <view class="login-container">
  3. <!-- 应用Logo和名称 -->
  4. <view class="app-header">
  5. <view class="app-logo">
  6. <view class="logo-square">
  7. <view class="book-icon">
  8. <view class="book-left"></view>
  9. <view class="book-right"></view>
  10. <view class="book-line"></view>
  11. </view>
  12. </view>
  13. </view>
  14. <text class="app-title">云阅读</text>
  15. </view>
  16. <!-- 输入表单 -->
  17. <view class="form-container">
  18. <view class="input-group">
  19. <input
  20. class="input-field"
  21. type="text"
  22. placeholder="请输入用户名"
  23. v-model="username"
  24. maxlength="20"
  25. />
  26. <view class="input-line"></view>
  27. </view>
  28. <view class="input-group">
  29. <input
  30. class="input-field password-input"
  31. :type="showPassword ? 'text' : 'password'"
  32. placeholder="请输入密码"
  33. v-model="password"
  34. maxlength="20"
  35. />
  36. <text
  37. class="toggle-password-btn"
  38. @click="togglePassword"
  39. >
  40. {{ showPassword ? '隐藏' : '显示' }}
  41. </text>
  42. <view class="input-line"></view>
  43. </view>
  44. <button
  45. class="login-btn"
  46. @click="handleLogin"
  47. :disabled="!canLogin"
  48. >
  49. {{ isLoading ? '登录中...' : '登录' }}
  50. </button>
  51. <!-- 注册按钮 -->
  52. <view class="register-link" @click="goToRegister">
  53. <text class="register-text">还没有账号?立即注册</text>
  54. </view>
  55. </view>
  56. <!-- 第三方登录 -->
  57. <view class="third-party-login">
  58. <view class="third-party-icon" @click="loginWithWeChat">
  59. <view class="wechat-icon">
  60. <view class="wechat-bubble1"></view>
  61. <view class="wechat-bubble2"></view>
  62. </view>
  63. </view>
  64. <view class="third-party-icon" @click="loginWithQQ">
  65. <text class="qq-icon">Q</text>
  66. </view>
  67. <view class="third-party-icon" @click="loginWithWeibo">
  68. <text class="weibo-icon">W</text>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import { login } from '../../utils/api.js'
  75. export default {
  76. data() {
  77. return {
  78. username: '',
  79. password: '',
  80. showPassword: false,
  81. isLoading: false
  82. }
  83. },
  84. onLoad(options) {
  85. // 如果从注册页面传递了用户名,自动填充
  86. if (options.username) {
  87. this.username = decodeURIComponent(options.username)
  88. uni.showToast({
  89. title: '注册成功,请输入密码登录',
  90. icon: 'success',
  91. duration: 2000
  92. })
  93. }
  94. },
  95. computed: {
  96. canLogin() {
  97. return this.username.trim().length > 0 && this.password.length > 0 && !this.isLoading
  98. }
  99. },
  100. methods: {
  101. togglePassword() {
  102. this.showPassword = !this.showPassword
  103. },
  104. handleLogin() {
  105. if (!this.canLogin) {
  106. uni.showToast({
  107. title: '请填写完整信息',
  108. icon: 'none'
  109. })
  110. return
  111. }
  112. // 显示加载中
  113. this.isLoading = true
  114. uni.showLoading({
  115. title: '登录中...',
  116. mask: true
  117. })
  118. // 调用后端登录接口
  119. login(this.username.trim(), this.password)
  120. .then((res) => {
  121. uni.hideLoading()
  122. this.isLoading = false
  123. if (res.code === 200 && res.data) {
  124. // 登录成功,保存用户信息
  125. try {
  126. const userInfo = res.data.user || {}
  127. uni.setStorageSync('userInfo', {
  128. id: userInfo.id,
  129. username: userInfo.username,
  130. nickname: userInfo.nickname,
  131. avatar: userInfo.avatar,
  132. gender: userInfo.gender,
  133. birthday: userInfo.birthday,
  134. bio: userInfo.bio,
  135. phone: userInfo.phone,
  136. email: userInfo.email,
  137. isVip: userInfo.isVip,
  138. token: res.data.token,
  139. loginTime: new Date().getTime()
  140. })
  141. uni.setStorageSync('isLogin', true)
  142. } catch (e) {
  143. console.error('保存用户信息失败', e)
  144. }
  145. uni.showToast({
  146. title: '登录成功',
  147. icon: 'success'
  148. })
  149. setTimeout(() => {
  150. uni.switchTab({
  151. url: '/pages/index/index'
  152. })
  153. }, 1500)
  154. } else {
  155. uni.showToast({
  156. title: res.message || '登录失败',
  157. icon: 'none'
  158. })
  159. }
  160. })
  161. .catch((err) => {
  162. uni.hideLoading()
  163. this.isLoading = false
  164. console.error('登录失败:', err)
  165. uni.showToast({
  166. title: err.message || '登录失败,请检查网络连接',
  167. icon: 'none',
  168. duration: 2000
  169. })
  170. })
  171. },
  172. loginWithWeChat() {
  173. uni.showToast({
  174. title: '微信登录',
  175. icon: 'none'
  176. })
  177. },
  178. loginWithQQ() {
  179. uni.showToast({
  180. title: 'QQ登录',
  181. icon: 'none'
  182. })
  183. },
  184. loginWithWeibo() {
  185. uni.showToast({
  186. title: '微博登录',
  187. icon: 'none'
  188. })
  189. },
  190. goToRegister() {
  191. uni.navigateTo({
  192. url: '/pages/register/register'
  193. })
  194. }
  195. }
  196. }
  197. </script>
  198. <style scoped>
  199. .login-container {
  200. width: 100%;
  201. height: 100vh;
  202. background-color: #FFFFFF;
  203. display: flex;
  204. flex-direction: column;
  205. padding-top: 30px;
  206. box-sizing: border-box;
  207. align-items: center;
  208. padding: 0 60rpx;
  209. box-sizing: border-box;
  210. position: relative;
  211. }
  212. .app-header {
  213. display: flex;
  214. flex-direction: column;
  215. align-items: center;
  216. margin-top: 180rpx;
  217. margin-bottom: 80rpx;
  218. flex-shrink: 0;
  219. }
  220. .app-logo {
  221. margin-bottom: 30rpx;
  222. }
  223. .logo-square {
  224. width: 140rpx;
  225. height: 140rpx;
  226. background-color: #4FC3F7;
  227. border-radius: 20rpx;
  228. display: flex;
  229. align-items: center;
  230. justify-content: center;
  231. }
  232. .book-icon {
  233. position: relative;
  234. width: 90rpx;
  235. height: 70rpx;
  236. }
  237. .book-left,
  238. .book-right {
  239. position: absolute;
  240. width: 45rpx;
  241. height: 70rpx;
  242. background-color: #FFFFFF;
  243. border-radius: 3rpx 0 0 3rpx;
  244. }
  245. .book-right {
  246. right: 0;
  247. border-radius: 0 3rpx 3rpx 0;
  248. }
  249. .book-line {
  250. position: absolute;
  251. left: 45rpx;
  252. top: 8rpx;
  253. width: 2rpx;
  254. height: 54rpx;
  255. background-color: #4FC3F7;
  256. }
  257. .app-title {
  258. font-size: 44rpx;
  259. color: #333333;
  260. font-weight: 600;
  261. }
  262. .form-container {
  263. width: 100%;
  264. flex: 1;
  265. display: flex;
  266. flex-direction: column;
  267. justify-content: flex-start;
  268. }
  269. .input-group {
  270. position: relative;
  271. margin-bottom: 50rpx;
  272. }
  273. .input-field {
  274. width: 100%;
  275. height: 88rpx;
  276. font-size: 30rpx;
  277. padding: 0 20rpx;
  278. color: #333333;
  279. }
  280. .password-input {
  281. padding-right: 80rpx;
  282. }
  283. .toggle-password-btn {
  284. position: absolute;
  285. right: 20rpx;
  286. top: 50%;
  287. transform: translateY(-50%);
  288. font-size: 26rpx;
  289. color: #4FC3F7;
  290. z-index: 10;
  291. padding: 8rpx 12rpx;
  292. }
  293. .input-line {
  294. position: absolute;
  295. bottom: 0;
  296. left: 0;
  297. right: 0;
  298. height: 1rpx;
  299. background-color: #E0E0E0;
  300. }
  301. .login-btn {
  302. width: 100%;
  303. height: 96rpx;
  304. background-color: #E0E0E0;
  305. color: #999999;
  306. font-size: 34rpx;
  307. border-radius: 48rpx;
  308. margin-top: 60rpx;
  309. display: flex;
  310. align-items: center;
  311. justify-content: center;
  312. border: none;
  313. flex-shrink: 0;
  314. }
  315. .login-btn:not([disabled]) {
  316. background-color: #4FC3F7;
  317. color: #FFFFFF;
  318. }
  319. .login-btn[disabled] {
  320. background-color: #E0E0E0;
  321. color: #999999;
  322. }
  323. .register-link {
  324. margin-top: 40rpx;
  325. text-align: center;
  326. }
  327. .register-text {
  328. font-size: 28rpx;
  329. color: #4FC3F7;
  330. }
  331. .third-party-login {
  332. display: flex;
  333. justify-content: center;
  334. align-items: center;
  335. gap: 70rpx;
  336. position: absolute;
  337. bottom: 80rpx;
  338. left: 0;
  339. right: 0;
  340. flex-shrink: 0;
  341. padding-bottom: env(safe-area-inset-bottom);
  342. }
  343. .third-party-icon {
  344. width: 88rpx;
  345. height: 88rpx;
  346. border-radius: 50%;
  347. border: 1rpx solid #E0E0E0;
  348. display: flex;
  349. align-items: center;
  350. justify-content: center;
  351. background-color: #FFFFFF;
  352. }
  353. .wechat-icon {
  354. position: relative;
  355. width: 56rpx;
  356. height: 56rpx;
  357. }
  358. .wechat-bubble1,
  359. .wechat-bubble2 {
  360. position: absolute;
  361. border-radius: 50%;
  362. background-color: #666666;
  363. }
  364. .wechat-bubble1 {
  365. width: 32rpx;
  366. height: 32rpx;
  367. left: 0;
  368. top: 0;
  369. }
  370. .wechat-bubble2 {
  371. width: 26rpx;
  372. height: 26rpx;
  373. right: 0;
  374. bottom: 0;
  375. }
  376. .qq-icon,
  377. .weibo-icon {
  378. font-size: 44rpx;
  379. color: #666666;
  380. font-weight: bold;
  381. }
  382. </style>