page3.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <template>
  2. <view class="content">
  3. <!-- 顶部标题区域 -->
  4. <view class="header-section">
  5. <view class="app-logo">🪶🪶</view>
  6. <view class="app-name">两只羽毛</view>
  7. <view class="app-slogan">真诚相遇 · 携手一生</view>
  8. </view>
  9. <!-- 登录框 -->
  10. <!-- <view class="login-box"> -->
  11. <!-- 切换标签 -->
  12. <!-- <view class="login-tabs"> -->
  13. <!-- <view class="tab-item" :class="{active: loginMode === 'phone'}" @click="loginMode = 'phone'">
  14. <text class="tab-icon">📱</text>
  15. <text>验证码登录</text>
  16. </view>
  17. <view class="tab-item" :class="{active: loginMode === 'password'}" @click="loginMode = 'password'">
  18. <text class="tab-icon">🔐</text>
  19. <text>密码登录</text>
  20. </view> -->
  21. <!-- </view> -->
  22. <!-- 手机号验证码登录 -->
  23. <view v-if="loginMode === 'phone'">
  24. <!-- <view class="login-title">
  25. <text class="title-icon">💌</text>
  26. <text>快速登录</text>
  27. </view>
  28. <view class="form-group">
  29. <input class="input" type="number" maxlength="11" v-model="phoneForm.phone" placeholder="请输入手机号" />
  30. </view>
  31. <view class="form-group code-group">
  32. <input class="input code-input" type="number" maxlength="6" v-model="phoneForm.code" placeholder="请输入验证码" />
  33. <view class="code-btn" :class="{disabled: countdown > 0}" @click="sendCode">
  34. {{ countdown > 0 ? `${countdown}秒` : '发送验证码' }}
  35. </view>
  36. </view>
  37. <button class="login-btn" @click="handlePhoneLogin">登录</button> -->
  38. <!-- 微信一键登录 -->
  39. <view class="divider">
  40. <view class="line"></view>
  41. <text class="text">其他方式登录</text>
  42. <view class="line"></view>
  43. </view>
  44. <button class="wechat-login-btn" @click="login_zheshow">
  45. <text class="wechat-icon">💬</text>
  46. <text>微信一键登录</text>
  47. </button>
  48. </view>
  49. <!-- 账号密码登录 -->
  50. <view v-if="loginMode === 'password'">
  51. <!-- <view class="login-title">
  52. <text class="title-icon">💝</text>
  53. <text>账号登录</text>
  54. </view>
  55. <view class="form-group">
  56. <input class="input" v-model="passwordForm.username" placeholder="请输入账号或手机号" />
  57. </view>
  58. <view class="form-group password-group">
  59. <input class="input" :type="showPassword ? 'text' : 'password'" v-model="passwordForm.password" placeholder="请输入密码" />
  60. <view class="eye-icon" @click="showPassword = !showPassword">
  61. {{ showPassword ? '👁️' : '👁️‍🗨️' }}
  62. </view>
  63. </view>
  64. <view class="remember-row">
  65. <view class="remember-check" @click="rememberMe = !rememberMe">
  66. <image :src="rememberMe ? '/static/4checked.png' : '/static/4unchecked.png'" class="checkbox-icon"></image>
  67. <text>记住密码</text>
  68. </view>
  69. </view>
  70. <button class="login-btn" @click="handlePasswordLogin">登录</button> -->
  71. <!-- 微信一键登录 -->
  72. <!-- <view class="divider">
  73. <view class="line"></view>
  74. <text class="text">其他方式登录</text>
  75. <view class="line"></view>
  76. </view> -->
  77. <button class="wechat-login-btn" @click="login_zheshow">
  78. <text class="wechat-icon">💬</text>
  79. <text>微信一键登录</text>
  80. </button>
  81. <!-- </view> -->
  82. </view>
  83. <!-- 底部提示 -->
  84. <view class="footer-tip">
  85. <text class="tip-icon">💗</text>
  86. <text class="tip-text">遇见对的人,不再孤单</text>
  87. </view>
  88. <btnlogin :zheshow='zheshow' />
  89. </view>
  90. </template>
  91. <script>
  92. import btnlogin from '@/components/butlogin';
  93. import api from '@/utils/api.js'
  94. import userAuth from '@/utils/userAuth.js'
  95. export default {
  96. components:{
  97. btnlogin
  98. },
  99. data() {
  100. return {
  101. zheshow: false, // 控制弹窗显示隐藏
  102. loginMode: 'phone', // 登录模式:phone-验证码登录,password-密码登录
  103. redirectUrl: '', // 登录成功后跳转地址(可选)
  104. // 手机号验证码登录
  105. phoneForm: {
  106. phone: '',
  107. code: ''
  108. },
  109. countdown: 0, // 倒计时
  110. countdownTimer: null,
  111. // 账号密码登录
  112. passwordForm: {
  113. username: '',
  114. password: ''
  115. },
  116. showPassword: false, // 是否显示密码
  117. rememberMe: false // 记住密码
  118. }
  119. },
  120. onLoad(options) {
  121. // 加载记住的密码
  122. this.loadRememberedPassword();
  123. // 处理跳转参数
  124. this.redirectUrl = options && options.redirect ? decodeURIComponent(options.redirect) : ''
  125. },
  126. onUnload() {
  127. // 清除倒计时
  128. if (this.countdownTimer) {
  129. clearInterval(this.countdownTimer);
  130. }
  131. },
  132. methods: {
  133. login_zheshow(){
  134. this.zheshow = !this.zheshow
  135. },
  136. loset(Logon_Credentials){
  137. console.log(Logon_Credentials,'登录信息')
  138. // 获取微信登录code
  139. uni.login({
  140. provider: 'weixin',
  141. success: async (loginRes) => {
  142. uni.showLoading({ title: '登录中...' })
  143. try {
  144. // 1. 微信授权登录
  145. const wechatLoginResult = await api.auth.wechatLogin(loginRes.code)
  146. const token = wechatLoginResult?.token || wechatLoginResult?.data?.token
  147. const user = wechatLoginResult?.user || wechatLoginResult?.data?.user
  148. if (!token || !user) {
  149. throw new Error('微信登录返回数据异常')
  150. }
  151. console.log('✅ 微信登录成功,用户ID:', user.userId)
  152. // 2. 获取手机号
  153. const phoneCode = (Logon_Credentials && Logon_Credentials.code && String(Logon_Credentials.code).trim()) ? String(Logon_Credentials.code).trim() : 'mock_dev_code'
  154. console.log('📱 使用phoneCode:', phoneCode)
  155. try {
  156. const phoneResult = await api.auth.wechatPhone(phoneCode)
  157. const phone = phoneResult?.phone || phoneResult?.data?.phone
  158. if (phone) {
  159. console.log('✅ 成功获取手机号')
  160. user.phone = phone
  161. } else {
  162. console.warn('⚠️ 手机号为空,使用默认值(开发环境)')
  163. // 开发环境:如果后端返回了mock手机号,也可能在user对象中
  164. user.phone = user.phone || '13800138000'
  165. }
  166. } catch (phoneError) {
  167. console.error('❌ 获取手机号接口失败:', phoneError)
  168. // 开发环境容错:继续登录,使用默认手机号
  169. user.phone = user.phone || '13800138000'
  170. console.warn('⚠️ 使用默认手机号继续登录(开发环境)')
  171. }
  172. // 3. 更新用户信息(昵称和头像)
  173. user.nickname = Logon_Credentials.nickname || user.nickname
  174. user.avatar = Logon_Credentials.active || user.avatar
  175. console.log('💾 保存用户信息:', { userId: user.userId, phone: user.phone, nickname: user.nickname })
  176. // 4. 保存登录信息
  177. userAuth.saveLoginInfo(token, user)
  178. uni.hideLoading()
  179. uni.showToast({ title: '登录成功', icon: 'success' })
  180. // 5. 跳转首页
  181. setTimeout(() => {
  182. const target = this.redirectUrl || '/pages/index/index'
  183. uni.reLaunch({ url: target })
  184. }, 500)
  185. } catch (error) {
  186. console.error('❌ 微信登录失败:', error)
  187. uni.hideLoading()
  188. uni.showToast({
  189. title: error?.message || '登录失败,请重试',
  190. icon: 'none',
  191. duration: 2000
  192. })
  193. }
  194. },
  195. fail: (err) => {
  196. console.error('获取微信code失败:', err)
  197. uni.showToast({ title: '微信登录失败', icon: 'none' })
  198. }
  199. })
  200. },
  201. // 发送验证码
  202. async sendCode() {
  203. if (this.countdown > 0) return;
  204. // 验证手机号
  205. if (!this.phoneForm.phone) {
  206. uni.showToast({ title: '请输入手机号', icon: 'none' });
  207. return;
  208. }
  209. if (!/^1[3-9]\d{9}$/.test(this.phoneForm.phone)) {
  210. uni.showToast({ title: '请输入正确的手机号', icon: 'none' });
  211. return;
  212. }
  213. try {
  214. uni.showLoading({ title: '发送中...' })
  215. await api.auth.sendCode(this.phoneForm.phone)
  216. uni.showToast({ title: '验证码已发送', icon: 'success' });
  217. // 开始倒计时
  218. this.countdown = 60;
  219. this.countdownTimer = setInterval(() => {
  220. this.countdown--;
  221. if (this.countdown <= 0) {
  222. clearInterval(this.countdownTimer);
  223. }
  224. }, 1000);
  225. } catch (err) {
  226. console.error('发送验证码失败:', err)
  227. uni.showToast({ title: err?.message || '发送失败', icon: 'none' })
  228. } finally {
  229. uni.hideLoading()
  230. }
  231. },
  232. // 手机号验证码登录
  233. async handlePhoneLogin() {
  234. if (!this.phoneForm.phone) {
  235. uni.showToast({ title: '请输入手机号', icon: 'none' });
  236. return;
  237. }
  238. if (!this.phoneForm.code) {
  239. uni.showToast({ title: '请输入验证码', icon: 'none' });
  240. return;
  241. }
  242. uni.showLoading({ title: '登录中...' });
  243. try {
  244. const result = await api.auth.smsLogin(this.phoneForm.phone, this.phoneForm.code)
  245. const token = result?.token || result?.data?.token
  246. const user = result?.user || result?.data?.user
  247. if (!token || !user) {
  248. throw new Error('登录返回数据异常')
  249. }
  250. // 保存登录信息
  251. userAuth.saveLoginInfo(token, user)
  252. uni.showToast({ title: '登录成功', icon: 'success' })
  253. // 跳转首页
  254. const target = this.redirectUrl || '/pages/index/index'
  255. uni.reLaunch({ url: target })
  256. // 清空表单
  257. this.phoneForm = { phone: '', code: '' }
  258. } catch (error) {
  259. console.error('验证码登录失败:', error)
  260. uni.showToast({ title: error?.message || '登录失败', icon: 'none' })
  261. } finally {
  262. uni.hideLoading()
  263. }
  264. },
  265. // 账号密码登录
  266. async handlePasswordLogin() {
  267. if (!this.passwordForm.username) {
  268. uni.showToast({ title: '请输入账号', icon: 'none' });
  269. return;
  270. }
  271. if (!this.passwordForm.password) {
  272. uni.showToast({ title: '请输入密码', icon: 'none' });
  273. return;
  274. }
  275. // 调用后端API进行登录
  276. uni.showLoading({ title: '登录中...' });
  277. try {
  278. const result = await api.auth.loginByPassword(this.passwordForm.username, this.passwordForm.password)
  279. // 兼容不同返回结构
  280. const token = result?.token || result?.data?.token
  281. const user = result?.user || result?.data?.user
  282. if (!token || !user) {
  283. throw new Error('登录返回数据异常')
  284. }
  285. // 使用工具类保存登录信息 - 确保userId正确处理
  286. console.log('登录成功,用户信息:', user)
  287. console.log('用户ID类型和值:', typeof user.userId, user.userId)
  288. // 确保userId是数字类型
  289. if (user.userId) {
  290. user.userId = parseInt(user.userId)
  291. console.log('处理后的用户ID:', user.userId)
  292. }
  293. userAuth.saveLoginInfo(token, user)
  294. // 是否记住密码
  295. if (this.rememberMe) {
  296. uni.setStorageSync('rememberedAccount', {
  297. username: this.passwordForm.username,
  298. password: this.passwordForm.password
  299. })
  300. } else {
  301. uni.removeStorageSync('rememberedAccount')
  302. }
  303. uni.showToast({ title: '登录成功', icon: 'success' })
  304. // 跳转页面(统一使用 reLaunch 进入首页或指定页面)
  305. const target = this.redirectUrl || '/pages/index/index'
  306. uni.reLaunch({ url: target })
  307. // 清空表单
  308. this.passwordForm = { username: '', password: '' }
  309. } catch (error) {
  310. console.error('登录失败:', error)
  311. uni.showToast({ title: error?.message || '登录失败', icon: 'none' })
  312. } finally {
  313. uni.hideLoading()
  314. }
  315. },
  316. // 加载记住的密码
  317. loadRememberedPassword() {
  318. const remembered = uni.getStorageSync('rememberedAccount');
  319. if (remembered) {
  320. this.passwordForm.username = remembered.username;
  321. this.passwordForm.password = remembered.password;
  322. this.rememberMe = true;
  323. }
  324. }
  325. }
  326. }
  327. </script>
  328. <style lang="scss" scoped>
  329. .content {
  330. padding: 40rpx;
  331. background: linear-gradient(135deg, #ffeef8 0%, #fff5f7 50%, #ffe9f0 100%);
  332. min-height: 100vh;
  333. position: relative;
  334. // 添加装饰性元素
  335. &::before {
  336. content: '❤️';
  337. position: absolute;
  338. top: 100rpx;
  339. right: 60rpx;
  340. font-size: 60rpx;
  341. opacity: 0.2;
  342. animation: heartbeat 2s infinite;
  343. }
  344. &::after {
  345. content: '💕';
  346. position: absolute;
  347. bottom: 200rpx;
  348. left: 40rpx;
  349. font-size: 50rpx;
  350. opacity: 0.2;
  351. animation: heartbeat 2s infinite 0.5s;
  352. }
  353. }
  354. @keyframes heartbeat {
  355. 0%, 100% {
  356. transform: scale(1);
  357. }
  358. 50% {
  359. transform: scale(1.1);
  360. }
  361. }
  362. .header-section {
  363. text-align: center;
  364. margin-bottom: 60rpx;
  365. .app-logo {
  366. font-size: 120rpx;
  367. margin-bottom: 20rpx;
  368. animation: heartbeat 2s infinite;
  369. }
  370. .app-name {
  371. font-size: 48rpx;
  372. font-weight: bold;
  373. background: linear-gradient(135deg, #ff6b9d 0%, #ff8fab 100%);
  374. -webkit-background-clip: text;
  375. -webkit-text-fill-color: transparent;
  376. margin-bottom: 15rpx;
  377. }
  378. .app-slogan {
  379. font-size: 26rpx;
  380. color: #ff6b9d;
  381. opacity: 0.8;
  382. }
  383. }
  384. .login-box {
  385. background: #FFFFFF;
  386. border-radius: 30rpx;
  387. padding: 50rpx 40rpx;
  388. margin-bottom: 30rpx;
  389. box-shadow: 0 8rpx 24rpx rgba(255, 107, 157, 0.15);
  390. border: 2rpx solid rgba(255, 107, 157, 0.1);
  391. .login-tabs {
  392. display: flex;
  393. background: linear-gradient(135deg, #fff0f6 0%, #ffe9f0 100%);
  394. border-radius: 50rpx;
  395. padding: 6rpx;
  396. margin-bottom: 40rpx;
  397. .tab-item {
  398. flex: 1;
  399. height: 70rpx;
  400. display: flex;
  401. align-items: center;
  402. justify-content: center;
  403. font-size: 26rpx;
  404. color: #ff6b9d;
  405. border-radius: 50rpx;
  406. transition: all 0.3s;
  407. .tab-icon {
  408. font-size: 32rpx;
  409. margin-right: 8rpx;
  410. }
  411. &.active {
  412. background: linear-gradient(135deg, #ff6b9d 0%, #ff8fab 100%);
  413. color: #FFFFFF;
  414. font-weight: bold;
  415. box-shadow: 0 4rpx 12rpx rgba(255, 107, 157, 0.3);
  416. }
  417. }
  418. }
  419. .login-title {
  420. font-size: 34rpx;
  421. font-weight: bold;
  422. color: #ff6b9d;
  423. margin-bottom: 40rpx;
  424. text-align: center;
  425. display: flex;
  426. align-items: center;
  427. justify-content: center;
  428. .title-icon {
  429. font-size: 40rpx;
  430. margin-right: 10rpx;
  431. }
  432. }
  433. .form-group {
  434. margin-bottom: 25rpx;
  435. position: relative;
  436. .input {
  437. width: 100%;
  438. height: 90rpx;
  439. background: #fff5f9;
  440. border-radius: 50rpx;
  441. padding: 0 30rpx;
  442. font-size: 28rpx;
  443. border: 2rpx solid #ffe0ed;
  444. box-sizing: border-box;
  445. transition: all 0.3s;
  446. &:focus {
  447. background: #FFFFFF;
  448. border-color: #ff8fab;
  449. }
  450. }
  451. &.code-group {
  452. display: flex;
  453. align-items: center;
  454. .code-input {
  455. flex: 1;
  456. margin-right: 20rpx;
  457. }
  458. .code-btn {
  459. width: 180rpx;
  460. height: 80rpx;
  461. background: linear-gradient(135deg, #ff6b9d 0%, #ff8fab 100%);
  462. color: #FFFFFF;
  463. border-radius: 50rpx;
  464. display: flex;
  465. align-items: center;
  466. justify-content: center;
  467. font-size: 24rpx;
  468. box-shadow: 0 4rpx 12rpx rgba(255, 107, 157, 0.3);
  469. &.disabled {
  470. background: #e8e8e8;
  471. color: #999999;
  472. box-shadow: none;
  473. }
  474. }
  475. }
  476. &.password-group {
  477. display: flex;
  478. align-items: center;
  479. .input {
  480. flex: 1;
  481. padding-right: 80rpx;
  482. }
  483. .eye-icon {
  484. position: absolute;
  485. right: 20rpx;
  486. font-size: 40rpx;
  487. padding: 10rpx;
  488. }
  489. }
  490. }
  491. .remember-row {
  492. margin-bottom: 25rpx;
  493. .remember-check {
  494. display: flex;
  495. align-items: center;
  496. font-size: 26rpx;
  497. color: #666666;
  498. .checkbox-icon {
  499. width: 32rpx;
  500. height: 32rpx;
  501. margin-right: 10rpx;
  502. }
  503. }
  504. }
  505. .login-btn {
  506. width: 100%;
  507. height: 90rpx;
  508. background: linear-gradient(135deg, #ff6b9d 0%, #ff8fab 100%);
  509. color: #FFFFFF;
  510. border-radius: 50rpx;
  511. font-size: 32rpx;
  512. font-weight: bold;
  513. border: none;
  514. box-shadow: 0 8rpx 20rpx rgba(255, 107, 157, 0.4);
  515. margin-bottom: 30rpx;
  516. &:active {
  517. opacity: 0.9;
  518. transform: translateY(2rpx);
  519. }
  520. }
  521. .divider {
  522. display: flex;
  523. align-items: center;
  524. margin-bottom: 30rpx;
  525. .line {
  526. flex: 1;
  527. height: 1rpx;
  528. background: linear-gradient(to right, transparent, #ffcce0, transparent);
  529. }
  530. .text {
  531. margin: 0 20rpx;
  532. font-size: 24rpx;
  533. color: #ff6b9d;
  534. }
  535. }
  536. .wechat-login-btn {
  537. width: 100%;
  538. height: 90rpx;
  539. background: linear-gradient(135deg, #ff6b9d 0%, #ff8fab 100%);
  540. color: #FFFFFF;
  541. border-radius: 50rpx;
  542. font-size: 30rpx;
  543. font-weight: bold;
  544. display: flex;
  545. align-items: center;
  546. justify-content: center;
  547. border: none;
  548. box-shadow: 0 8rpx 20rpx rgba(255, 107, 157, 0.4);
  549. .wechat-icon {
  550. font-size: 40rpx;
  551. margin-right: 10rpx;
  552. }
  553. &:active {
  554. opacity: 0.9;
  555. transform: translateY(2rpx);
  556. }
  557. }
  558. }
  559. .footer-tip {
  560. text-align: center;
  561. margin-top: 60rpx;
  562. padding: 30rpx;
  563. .tip-icon {
  564. font-size: 50rpx;
  565. display: block;
  566. margin-bottom: 15rpx;
  567. animation: heartbeat 2s infinite;
  568. }
  569. .tip-text {
  570. font-size: 26rpx;
  571. color: #ff6b9d;
  572. font-weight: 500;
  573. letter-spacing: 2rpx;
  574. }
  575. }
  576. button {
  577. &::after {
  578. border: none;
  579. }
  580. }
  581. </style>