page3.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="content">
  3. <!-- 背景图片 -->
  4. <image class="bg-image" src="http://115.190.125.125:9000/static-images/login-bg.png" mode="aspectFill"></image>
  5. <!-- 顶部标题区域 -->
  6. <view class="header-section">
  7. <!-- <view class="app-logo">🪶🪶</view> -->
  8. <image class="app-logo" src="http://115.190.125.125:9000/static-images/logo.png" mode="widthFix"></image>
  9. <!-- <view class="app-name">青鸾之恋</view> -->
  10. <view class="app-slogan">真诚相遇 · 携手一生</view>
  11. </view>
  12. <!-- 登录框 -->
  13. <view class="login-box">
  14. <!-- 微信一键登录 -->
  15. <button class="wechat-login-btn" @click="login_zheshow">
  16. <image class="wechat-icon" src="http://115.190.125.125:9000/static-images/wechat-icon.png" mode="aspectFit"></image>
  17. <text>微信一键登录</text>
  18. </button>
  19. </view>
  20. <!-- 底部提示 -->
  21. <view class="footer-tip">
  22. <text class="tip-icon">💗</text>
  23. <text class="tip-text">遇见对的人,不再孤单</text>
  24. </view>
  25. <btnlogin :zheshow='zheshow'/>
  26. </view>
  27. </template>
  28. <script>
  29. import btnlogin from '@/components/butlogin';
  30. import api from '@/utils/api.js'
  31. import userAuth from '@/utils/userAuth.js'
  32. export default {
  33. components: {
  34. btnlogin
  35. },
  36. data() {
  37. return {
  38. zheshow: false, // 控制弹窗显示隐藏
  39. redirectUrl: '' // 登录成功后跳转地址(可选)
  40. }
  41. },
  42. onLoad(options) {
  43. // 处理跳转参数
  44. this.redirectUrl = options && options.redirect ? decodeURIComponent(options.redirect) : ''
  45. },
  46. methods: {
  47. login_zheshow() {
  48. this.zheshow = !this.zheshow
  49. },
  50. loset(Logon_Credentials) {
  51. console.log('=== 开始微信登录流程 ===')
  52. console.log('用户登录信息:', Logon_Credentials)
  53. // 获取微信登录code
  54. uni.login({
  55. provider: 'weixin',
  56. success: async (loginRes) => {
  57. // ✅ 获取手机号code
  58. const phoneCode = (Logon_Credentials && Logon_Credentials.code && String(Logon_Credentials.code).trim())
  59. ? String(Logon_Credentials.code).trim()
  60. : null;
  61. console.log('微信登录code:', loginRes.code)
  62. console.log('手机号code:', phoneCode || '未提供')
  63. // ✅ 一次性调用微信登录接口,无需用户提供头像和昵称
  64. const loginParams = {
  65. code: loginRes.code,
  66. phoneCode: phoneCode
  67. };
  68. uni.showLoading({title: '登录中...'})
  69. try {
  70. const wechatLoginResult = await api.auth.wechatLogin(loginParams);
  71. const token = wechatLoginResult?.token || wechatLoginResult?.data?.token
  72. const user = wechatLoginResult?.user || wechatLoginResult?.data?.user
  73. if (!token || !user) {
  74. throw new Error('微信登录返回数据异常')
  75. }
  76. console.log('✅ 微信登录成功,用户ID:', user.userId)
  77. console.log('✅ 微信登录成功')
  78. console.log('用户ID:', user.userId)
  79. console.log('昵称:', user.nickname)
  80. console.log('头像:', user.avatarUrl ? '已设置' : '未设置')
  81. console.log('手机号:', user.phone || '未设置')
  82. uni.setStorageSync("userId", user.userId);
  83. // ✅ 保存登录信息(后端已返回完整信息,直接保存)
  84. userAuth.saveLoginInfo(token, user)
  85. uni.hideLoading()
  86. uni.showToast({title: '登录成功', icon: 'success'})
  87. // ✅ 初始化 TIM 和 WebSocket
  88. console.log('🚀 登录成功,开始初始化 TIM 和 WebSocket...')
  89. // 延迟一下,确保全局方法已挂载
  90. await new Promise(resolve => setTimeout(resolve, 200))
  91. try {
  92. const app = getApp()
  93. if (app && app.globalData && app.globalData.initGlobalTIM) {
  94. await app.globalData.initGlobalTIM()
  95. console.log('✅ TIM 和 WebSocket 初始化完成')
  96. } else {
  97. console.warn('⚠️ 全局 initGlobalTIM 方法不存在,尝试直接导入')
  98. // 如果全局方法不存在,直接导入 tim-manager
  99. const timManager = require('@/utils/tim-manager.js').default
  100. const timPresenceManager = require('@/utils/tim-presence-manager.js').default
  101. // 初始化 TIM
  102. if (!timManager.isLogin) {
  103. const SDKAppID = 1600109674
  104. if (!timManager.tim) {
  105. timManager.init(SDKAppID)
  106. console.log('✅ TIM SDK 初始化完成')
  107. }
  108. const res = await uni.request({
  109. url: `http://localhost:8083/api/im/getUserSig?userId=${user.userId}`,
  110. method: 'GET'
  111. })
  112. if (res[1].data.code === 200) {
  113. const userSig = res[1].data.data.userSig
  114. await timManager.login(String(user.userId), userSig)
  115. console.log('✅ TIM 登录成功')
  116. // 初始化 WebSocket
  117. await timPresenceManager.init(String(user.userId))
  118. console.log('✅ WebSocket 初始化完成')
  119. }
  120. }
  121. }
  122. } catch (error) {
  123. console.error('❌ 初始化 TIM 失败:', error)
  124. }
  125. // ✅ 跳转首页
  126. setTimeout(() => {
  127. const target = this.redirectUrl || '/pages/index/index'
  128. uni.reLaunch({url: target})
  129. }, 500)
  130. } catch (error) {
  131. console.error('❌ 微信登录失败:', error)
  132. uni.hideLoading()
  133. uni.showToast({
  134. title: error?.message || '登录失败,请重试',
  135. icon: 'none',
  136. duration: 2000
  137. })
  138. }
  139. },
  140. fail: (err) => {
  141. console.error('获取微信code失败:', err)
  142. uni.showToast({title: '微信登录失败', icon: 'none'})
  143. }
  144. })
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .content {
  151. padding: 40rpx;
  152. min-height: 100vh;
  153. position: relative;
  154. overflow: hidden;
  155. .bg-image {
  156. position: fixed;
  157. top: 0;
  158. left: 0;
  159. width: 100%;
  160. height: 100%;
  161. z-index: 0;
  162. pointer-events: none;
  163. }
  164. // 添加装饰性元素
  165. &::before {
  166. content: '❤️';
  167. position: absolute;
  168. top: 100rpx;
  169. right: 60rpx;
  170. font-size: 60rpx;
  171. opacity: 0.2;
  172. animation: heartbeat 2s infinite;
  173. }
  174. &::after {
  175. content: '💕';
  176. position: absolute;
  177. bottom: 200rpx;
  178. left: 40rpx;
  179. font-size: 50rpx;
  180. opacity: 0.2;
  181. animation: heartbeat 2s infinite 0.5s;
  182. }
  183. }
  184. @keyframes heartbeat {
  185. 0%, 100% {
  186. transform: scale(1);
  187. }
  188. 50% {
  189. transform: scale(1.1);
  190. }
  191. }
  192. .header-section {
  193. text-align: center;
  194. margin-bottom: 60rpx;
  195. position: relative;
  196. z-index: 1;
  197. .app-logo {
  198. font-size: 120rpx;
  199. margin-bottom: 20rpx;
  200. // animation: heartbeat 1s infinite;
  201. }
  202. .app-name {
  203. font-size: 48rpx;
  204. font-weight: bold;
  205. background: linear-gradient(135deg, #ff6b9d 0%, #ff8fab 100%);
  206. -webkit-background-clip: text;
  207. -webkit-text-fill-color: transparent;
  208. margin-bottom: 15rpx;
  209. }
  210. .app-slogan {
  211. font-size: 26rpx;
  212. color: #333333;
  213. opacity: 0.8;
  214. }
  215. }
  216. .login-box {
  217. background: rgba(211, 127, 140, 0);
  218. border-radius: 30rpx;
  219. padding: 80rpx 40rpx;
  220. margin-bottom: 30rpx;
  221. box-shadow: 0 8rpx 24rpx rgba(255, 107, 157, 0.15);
  222. border: 2rpx solid rgba(255, 107, 157, 0.1);
  223. display: flex;
  224. justify-content: center;
  225. align-items: center;
  226. position: relative;
  227. z-index: 1;
  228. // .wechat-login-btn {
  229. // width: 100%;
  230. // height: 90rpx;
  231. // background: #FFFFFF;
  232. // color: #ff6b9d;
  233. // border-radius: 50rpx;
  234. // font-size: 30rpx;
  235. // font-weight: bold;
  236. // display: flex;
  237. // align-items: center;
  238. // justify-content: center;
  239. // border: 2rpx solid #ff6b9d;
  240. // box-shadow: 0 8rpx 20rpx rgba(255, 107, 157, 0.2);
  241. // .wechat-icon {
  242. // width: 40rpx;
  243. // height: 40rpx;
  244. // margin-right: 10rpx;
  245. // }
  246. // &:active {
  247. // opacity: 0.9;
  248. // transform: translateY(2rpx);
  249. // }
  250. // }
  251. .wechat-login-btn {
  252. width: 100%;
  253. height: 96rpx;
  254. // 渐变背景+光泽叠加
  255. background: linear-gradient(90deg, #9d40e9 0%, #5a35f7 100%),
  256. radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.15), transparent 50%);
  257. background-blend-mode: overlay;
  258. color: #ffffff;
  259. border-radius: 60rpx;
  260. font-size: 32rpx;
  261. font-weight: bold;
  262. display: flex;
  263. align-items: center;
  264. justify-content: center;
  265. border: none;
  266. // 多层光影:外层投影+内高光+内阴影
  267. box-shadow: 0 6rpx 16rpx rgba(90, 53, 247, 0.4),
  268. 0 3rpx 8rpx rgba(90, 53, 247, 0.2),
  269. inset 0 2rpx 3rpx rgba(255, 255, 255, 0.25),
  270. inset 0 -2rpx 3rpx rgba(0, 0, 0, 0.15);
  271. transition: all 0.2s ease; // 动画过渡,更自然
  272. }
  273. .wechat-login-btn:active {
  274. transform: translateY(2rpx);
  275. box-shadow: 0 3rpx 8rpx rgba(90, 53, 247, 0.3),
  276. 0 1rpx 4rpx rgba(90, 53, 247, 0.2),
  277. inset 0 1rpx 2rpx rgba(255, 255, 255, 0.15),
  278. inset 0 -2rpx 3rpx rgba(0, 0, 0, 0.2);
  279. }
  280. .wechat-icon {
  281. width: 45rpx;
  282. height: 45rpx;
  283. margin-right: 15rpx;
  284. filter: drop-shadow(0 1rpx 2rpx rgba(0, 0, 0, 0.1)); // 给图标加轻微阴影,更立体
  285. }
  286. }
  287. .footer-tip {
  288. text-align: center;
  289. margin-top: 60rpx;
  290. padding: 30rpx;
  291. position: relative;
  292. z-index: 1;
  293. .tip-icon {
  294. font-size: 50rpx;
  295. display: block;
  296. margin-bottom: 15rpx;
  297. animation: heartbeat 2s infinite;
  298. }
  299. .tip-text {
  300. font-size: 26rpx;
  301. color: #333333;
  302. font-weight: 500;
  303. letter-spacing: 2rpx;
  304. }
  305. }
  306. button {
  307. &::after {
  308. border: none;
  309. }
  310. }
  311. </style>