page3.vue 9.0 KB

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