page3.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="content">
  3. <!-- 背景图片 -->
  4. <image class="bg-image" src="/static/login-bg.png" mode="aspectFill"></image>
  5. <!-- 顶部标题区域 -->
  6. <view class="header-section">
  7. <image class="app-logo" src="/static/logo.png" mode="widthFix"></image>
  8. <view class="app-slogan">真诚相遇 · 携手一生</view>
  9. </view>
  10. <!-- 登录框 -->
  11. <view class="login-box">
  12. <button class="wechat-login-btn" @click="login_zheshow">
  13. <image class="wechat-icon" src="/static/wechat-icon.png" mode="aspectFit"></image>
  14. <text>微信一键登录</text>
  15. </button>
  16. </view>
  17. <!-- 底部提示 -->
  18. <view class="footer-tip">
  19. <text class="tip-icon">💗</text>
  20. <text class="tip-text">遇见对的人,不再孤单</text>
  21. </view>
  22. <butlogin :zheshow='zheshow'/>
  23. </view>
  24. </template>
  25. <script>
  26. import butlogin from '@/components/butlogin'
  27. import api from '@/utils/api.js'
  28. import userAuth from '@/utils/userAuth.js'
  29. import timManager from '@/utils/tim-manager.js'
  30. import timPresenceManager from '@/utils/tim-presence-manager.js'
  31. export default {
  32. components: {
  33. butlogin
  34. },
  35. data() {
  36. return {
  37. zheshow: false, // 控制弹窗显示隐藏
  38. redirectUrl: '' // 登录成功后跳转地址(可选)
  39. }
  40. },
  41. onLoad(options) {
  42. // 处理跳转参数
  43. this.redirectUrl = options && options.redirect ? decodeURIComponent(options.redirect) : ''
  44. },
  45. methods: {
  46. login_zheshow() {
  47. this.zheshow = !this.zheshow
  48. },
  49. loset(Logon_Credentials) {
  50. console.log('=== 开始微信登录流程 ===')
  51. console.log('用户输入信息:', Logon_Credentials)
  52. // 获取微信登录code
  53. uni.login({
  54. provider: 'weixin',
  55. success: async (loginRes) => {
  56. const loginParams = {
  57. code: loginRes.code.trim(), // 必需:微信登录code
  58. nickname: Logon_Credentials.nickname?.trim() || '', // 可选:用户昵称
  59. avatarUrl: Logon_Credentials.active?.trim() || '', // 可选:用户头像
  60. phoneCode: Logon_Credentials.code?.trim() || '' // 可选:手机号授权code
  61. };
  62. uni.showLoading({ title: '登录中...' })
  63. try {
  64. // ✅ 获取手机号code
  65. const phoneCode = (Logon_Credentials && Logon_Credentials.code && String(Logon_Credentials.code).trim())
  66. ? String(Logon_Credentials.code).trim()
  67. : null;
  68. console.log('微信登录code:', loginRes.code)
  69. console.log('用户昵称:', Logon_Credentials.nickname)
  70. console.log('用户头像:', Logon_Credentials.active ? '已提供' : '未提供')
  71. console.log('手机号code:', phoneCode || '未提供')
  72. // ✅ 一次性调用微信登录接口,传入所有信息
  73. const wechatLoginResult = await api.auth.wechatLogin({
  74. code: loginRes.code,
  75. nickname: Logon_Credentials.nickname || '微信用户',
  76. avatarUrl: Logon_Credentials.active || null,
  77. phoneCode: phoneCode
  78. });
  79. const token = wechatLoginResult?.token || wechatLoginResult?.data?.token
  80. const user = wechatLoginResult?.user || wechatLoginResult?.data?.user
  81. if (!token || !user) {
  82. throw new Error('微信登录返回数据异常')
  83. }
  84. console.log('✅ 微信登录成功,用户ID:', user.userId)
  85. uni.setStorageSync("userId", user.userId);
  86. console.log('📱 使用phoneCode:', phoneCode)
  87. console.log('✅ 微信登录成功')
  88. console.log('用户ID:', user.userId)
  89. console.log('昵称:', user.nickname)
  90. console.log('头像:', user.avatarUrl ? '已设置' : '未设置')
  91. console.log('手机号:', user.phone || '未设置')
  92. // ✅ 保存登录信息(后端已返回完整信息,直接保存)
  93. userAuth.saveLoginInfo(token, user)
  94. uni.hideLoading()
  95. uni.showToast({ title: '登录成功', icon: 'success' })
  96. // ✅ 跳转首页
  97. setTimeout(() => {
  98. const target = this.redirectUrl || '/pages/index/index'
  99. uni.reLaunch({ url: target })
  100. }, 500)
  101. } catch (error) {
  102. console.error('❌ 微信登录失败:', error)
  103. uni.hideLoading()
  104. uni.showToast({
  105. title: error?.message || '登录失败,请重试',
  106. icon: 'none',
  107. duration: 2000
  108. })
  109. }
  110. },
  111. fail: (err) => {
  112. console.error('获取微信code失败:', err)
  113. uni.showToast({ title: '微信登录失败', icon: 'none' })
  114. }
  115. })
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss" scoped>
  121. .content {
  122. padding: 40rpx;
  123. min-height: 100vh;
  124. position: relative;
  125. overflow: hidden;
  126. .bg-image {
  127. position: fixed;
  128. top: 0;
  129. left: 0;
  130. width: 100%;
  131. height: 100%;
  132. z-index: 0;
  133. pointer-events: none;
  134. }
  135. &::before {
  136. content: '❤️';
  137. position: absolute;
  138. top: 100rpx;
  139. right: 60rpx;
  140. font-size: 60rpx;
  141. opacity: 0.2;
  142. animation: heartbeat 2s infinite;
  143. }
  144. &::after {
  145. content: '💕';
  146. position: absolute;
  147. bottom: 200rpx;
  148. left: 40rpx;
  149. font-size: 50rpx;
  150. opacity: 0.2;
  151. animation: heartbeat 2s infinite 0.5s;
  152. }
  153. }
  154. @keyframes heartbeat {
  155. 0%, 100% {
  156. transform: scale(1);
  157. }
  158. 50% {
  159. transform: scale(1.1);
  160. }
  161. }
  162. .header-section {
  163. text-align: center;
  164. margin-bottom: 60rpx;
  165. position: relative;
  166. z-index: 1;
  167. .app-logo {
  168. font-size: 120rpx;
  169. margin-bottom: 20rpx;
  170. }
  171. .app-slogan {
  172. font-size: 26rpx;
  173. color: #333333;
  174. opacity: 0.8;
  175. }
  176. }
  177. .login-box {
  178. background: rgba(211, 127, 140, 0);
  179. border-radius: 30rpx;
  180. padding: 80rpx 40rpx;
  181. margin-bottom: 30rpx;
  182. box-shadow: 0 8rpx 24rpx rgba(255, 107, 157, 0.15);
  183. border: 2rpx solid rgba(255, 107, 157, 0.1);
  184. display: flex;
  185. justify-content: center;
  186. align-items: center;
  187. position: relative;
  188. z-index: 1;
  189. .wechat-login-btn {
  190. width: 100%;
  191. height: 96rpx;
  192. background: linear-gradient(90deg, #9d40e9 0%, #5a35f7 100%),
  193. radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.15), transparent 50%);
  194. background-blend-mode: overlay;
  195. color: #ffffff;
  196. border-radius: 60rpx;
  197. font-size: 32rpx;
  198. font-weight: bold;
  199. display: flex;
  200. align-items: center;
  201. justify-content: center;
  202. border: none;
  203. box-shadow: 0 6rpx 16rpx rgba(90, 53, 247, 0.4),
  204. 0 3rpx 8rpx rgba(90, 53, 247, 0.2),
  205. inset 0 2rpx 3rpx rgba(255, 255, 255, 0.25),
  206. inset 0 -2rpx 3rpx rgba(0, 0, 0, 0.15);
  207. transition: all 0.2s ease;
  208. }
  209. .wechat-login-btn:active {
  210. transform: translateY(2rpx);
  211. box-shadow: 0 3rpx 8rpx rgba(90, 53, 247, 0.3),
  212. 0 1rpx 4rpx rgba(90, 53, 247, 0.2),
  213. inset 0 1rpx 2rpx rgba(255, 255, 255, 0.15),
  214. inset 0 -2rpx 3rpx rgba(0, 0, 0, 0.2);
  215. }
  216. .wechat-icon {
  217. width: 45rpx;
  218. height: 45rpx;
  219. margin-right: 15rpx;
  220. filter: drop-shadow(0 1rpx 2rpx rgba(0, 0, 0, 0.1));
  221. }
  222. }
  223. .footer-tip {
  224. text-align: center;
  225. margin-top: 60rpx;
  226. padding: 30rpx;
  227. position: relative;
  228. z-index: 1;
  229. .tip-icon {
  230. font-size: 50rpx;
  231. display: block;
  232. margin-bottom: 15rpx;
  233. animation: heartbeat 2s infinite;
  234. }
  235. .tip-text {
  236. font-size: 26rpx;
  237. color: #333333;
  238. font-weight: 500;
  239. letter-spacing: 2rpx;
  240. }
  241. }
  242. button {
  243. &::after {
  244. border: none;
  245. }
  246. }
  247. </style>