index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view class="login-container">
  3. <view class="login-box">
  4. <!-- Logo区域 -->
  5. <view class="logo-area">
  6. <image class="logo" src="/static/login.png" mode="aspectFit"></image>
  7. <text class="title">小鹅通</text>
  8. <text class="slogan">让知识分享更简单</text>
  9. </view>
  10. <!-- 登录方式区域 -->
  11. <view class="login-methods">
  12. <!-- 微信登录按钮 -->
  13. <button class="login-btn wechat" @click="handleWechatLogin">
  14. <text class="iconfont icon-wechat"></text>
  15. 微信一键登录
  16. </button>
  17. <!-- 其他登录方式 -->
  18. <view class="other-login">
  19. <text class="divider">其他登录方式</text>
  20. <view class="login-options">
  21. <view class="option" @click="navigateToVerifyCodeLogin">
  22. <text class="iconfont icon-verify"></text>
  23. <text>手机号码登录</text>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. <!-- 协议区域 -->
  29. <view class="agreement">
  30. <checkbox-group @change="handleAgreementChange">
  31. <label class="agreement-label">
  32. <checkbox :checked="isAgreed" color="#007AFF" />
  33. <text class="agreement-text">
  34. 我已阅读并同意
  35. <text class="link" @click="openServiceAgreement">《服务协议》</text>
  36. <text class="link" @click="openPrivacyPolicy">《隐私政策》</text>
  37. </text>
  38. </label>
  39. </checkbox-group>
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. export default {
  46. data() {
  47. return {
  48. isAgreed: false,
  49. showServiceAgreement: false,
  50. showPrivacyPolicy: false
  51. };
  52. },
  53. methods: {
  54. // 处理协议勾选
  55. handleAgreementChange(e) {
  56. this.isAgreed = e.detail.value.length > 0;
  57. },
  58. // 微信登录
  59. handleWechatLogin() {
  60. if (!this.isAgreed) {
  61. uni.showToast({ title: '请先同意用户协议和隐私政策', icon: 'none' });
  62. return;
  63. }
  64. // 直接在按钮点击回调中调用 wx.getUserProfile(用户主动触发)
  65. wx.getUserProfile({
  66. desc: '用于登录和完善个人信息', // 清晰的授权用途说明
  67. success: async (userRes) => {
  68. const { encryptedData, iv } = userRes;
  69. console.log(userRes)
  70. // 获取登录凭证(必须在用户授权后调用,保持上下文)
  71. const loginResult = await new Promise((resolve, reject) => {
  72. wx.login({ success: resolve, fail: reject });
  73. });
  74. if (!loginResult.code) {
  75. uni.showToast({ title: '登录凭证获取失败', icon: 'none' });
  76. return;
  77. }
  78. const code = loginResult.code;
  79. console.log(code)
  80. const avatar_url=userRes.userInfo.avatarUrl;
  81. const nickname=userRes.userInfo.nickName;
  82. // 调用后端接口
  83. try {
  84. const loginRes = await uni.request({
  85. url: 'http://localhost:9527/api/wx-login',
  86. method: 'POST',
  87. data: { code, encryptedData, iv ,avatar_url ,nickname},
  88. header: { 'content-type': 'application/json' }
  89. });
  90. console.log(loginRes)
  91. if (loginRes.statusCode !== 200 || loginRes.data.errcode) {
  92. throw new Error(loginRes.data.errmsg || '登录失败');
  93. }
  94. // 保存用户信息和 token
  95. uni.setStorageSync('uid', loginRes.data.data.id);
  96. uni.setStorageSync('token', loginRes.data.data.token);
  97. uni.setStorageSync('userInfo', loginRes.data.data.userInfo);
  98. // 跳转到 tabBar 页面(确保目标页面在 pages.json 中配置了 tabBar)
  99. uni.switchTab({ url: '/pages/discover/index' });
  100. } catch (error) {
  101. console.error('登录接口调用失败:', error);
  102. uni.showToast({ title: '登录失败,请重试', icon: 'none' });
  103. }
  104. },
  105. fail: (err) => {
  106. console.error('用户授权失败:', err);
  107. if (err.errCode === 10004) { // 用户点击授权弹窗的“拒绝”按钮
  108. uni.showToast({ title: '请授权后再登录', icon: 'none' });
  109. }
  110. }
  111. });
  112. },
  113. // 跳转到验证码登录页面
  114. navigateToVerifyCodeLogin() {
  115. uni.navigateTo({
  116. url: '/pages/login/verify-code'
  117. });
  118. },
  119. // 打开服务协议
  120. openServiceAgreement() {
  121. uni.navigateTo({
  122. url: '/pages/agreement/service'
  123. });
  124. },
  125. // 打开隐私政策
  126. openPrivacyPolicy() {
  127. uni.navigateTo({
  128. url: '/pages/agreement/privacy'
  129. });
  130. }
  131. }
  132. };
  133. </script>
  134. <style lang="scss">
  135. .login-container {
  136. min-height: 100vh;
  137. background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
  138. display: flex;
  139. align-items: center;
  140. justify-content: center;
  141. padding: 40rpx;
  142. }
  143. .login-box {
  144. width: 100%;
  145. max-width: 750rpx;
  146. background: rgba(255, 255, 255, 0.98);
  147. border-radius: 40rpx;
  148. padding: 80rpx 60rpx;
  149. box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.08);
  150. backdrop-filter: blur(10px);
  151. transform: translateY(0);
  152. transition: transform 0.3s ease;
  153. margin: 0 auto;
  154. &:hover {
  155. transform: translateY(-5rpx);
  156. }
  157. }
  158. .logo-area {
  159. text-align: center;
  160. margin-bottom: 60rpx;
  161. .logo {
  162. width: 240rpx;
  163. height: 240rpx;
  164. margin-bottom: 30rpx;
  165. filter: drop-shadow(0 4rpx 8rpx rgba(0, 0, 0, 0.1));
  166. }
  167. .title {
  168. font-size: 48rpx;
  169. font-weight: 800;
  170. background: linear-gradient(45deg, #2c3e50, #3498db);
  171. -webkit-background-clip: text;
  172. color: transparent;
  173. letter-spacing: 4rpx;
  174. margin-bottom: 20rpx;
  175. display: block;
  176. }
  177. .slogan {
  178. font-size: 32rpx;
  179. color: #666;
  180. font-weight: 500;
  181. letter-spacing: 2rpx;
  182. font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  183. background: linear-gradient(45deg, #34495e, #7f8c8d);
  184. -webkit-background-clip: text;
  185. color: transparent;
  186. }
  187. }
  188. .login-methods {
  189. margin-top: 80rpx;
  190. .login-btn {
  191. width: 100%;
  192. height: 100rpx;
  193. border-radius: 50rpx;
  194. display: flex;
  195. align-items: center;
  196. justify-content: center;
  197. margin-bottom: 40rpx;
  198. font-size: 36rpx;
  199. font-weight: 600;
  200. border: none;
  201. transition: all 0.3s ease;
  202. box-shadow: 0 8rpx 20rpx rgba(0, 0, 0, 0.08);
  203. .iconfont {
  204. margin-right: 16rpx;
  205. font-size: 44rpx;
  206. }
  207. &.primary {
  208. background: linear-gradient(45deg, #2c3e50, #3498db);
  209. color: #ffffff;
  210. &:active {
  211. transform: scale(0.98);
  212. box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.08);
  213. }
  214. }
  215. &.wechat {
  216. background: linear-gradient(45deg, #07C160, #0a9d4e);
  217. color: #ffffff;
  218. &:active {
  219. transform: scale(0.98);
  220. box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.08);
  221. }
  222. }
  223. }
  224. }
  225. .other-login {
  226. margin-top: 60rpx;
  227. .divider {
  228. display: block;
  229. text-align: center;
  230. color: #666666;
  231. font-size: 30rpx;
  232. margin-bottom: 50rpx;
  233. position: relative;
  234. font-weight: 500;
  235. &::before,
  236. &::after {
  237. content: '';
  238. position: absolute;
  239. top: 50%;
  240. width: 140rpx;
  241. height: 1px;
  242. background: linear-gradient(90deg, transparent, #666666, transparent);
  243. }
  244. &::before {
  245. left: 50%;
  246. margin-left: -200rpx;
  247. }
  248. &::after {
  249. right: 50%;
  250. margin-right: -200rpx;
  251. }
  252. }
  253. .login-options {
  254. display: flex;
  255. justify-content: center;
  256. gap: 100rpx;
  257. .option {
  258. display: flex;
  259. flex-direction: column;
  260. align-items: center;
  261. color: #444444;
  262. font-size: 30rpx;
  263. transition: all 0.3s ease;
  264. padding: 24rpx;
  265. border-radius: 24rpx;
  266. &:active {
  267. background: rgba(0, 0, 0, 0.05);
  268. }
  269. .iconfont {
  270. font-size: 64rpx;
  271. margin-bottom: 16rpx;
  272. color: #2c3e50;
  273. transition: transform 0.3s ease;
  274. }
  275. &:hover .iconfont {
  276. transform: scale(1.1);
  277. }
  278. }
  279. }
  280. }
  281. .agreement {
  282. margin-top: 80rpx;
  283. text-align: center;
  284. .agreement-label {
  285. display: inline-flex;
  286. align-items: center;
  287. font-size: 26rpx;
  288. color: #666666;
  289. }
  290. .agreement-text {
  291. margin-left: 10rpx;
  292. }
  293. .link {
  294. color: #3498db;
  295. text-decoration: none;
  296. font-weight: 500;
  297. position: relative;
  298. &::after {
  299. content: '';
  300. position: absolute;
  301. bottom: -2rpx;
  302. left: 0;
  303. width: 100%;
  304. height: 1px;
  305. background: #3498db;
  306. transform: scaleX(0);
  307. transition: transform 0.3s ease;
  308. }
  309. &:active::after {
  310. transform: scaleX(1);
  311. }
  312. }
  313. }
  314. </style>