四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
9.8 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="login-container">
  3. <!-- 背景图 -->
  4. <!-- <image class="bg-image" src="@/subPages/static/登录_背景图.png" mode="aspectFill"></image> -->
  5. <!-- 主要内容 -->
  6. <view class="content">
  7. <!-- Logo和标题区域 -->
  8. <view class="logo-section">
  9. <image class="logo" :src="configParamContent('login_logo')" mode="aspectFit"></image>
  10. <text class="title-text">{{ configParamContent('app_name') }}</text>
  11. </view>
  12. <!-- 登录按钮区域 -->
  13. <view class="login-section">
  14. <button class="login-btn" @click="handleLogin">
  15. 登录
  16. </button>
  17. <button class="guest-btn" @click="handleGuestLogin">
  18. 取消登录
  19. </button>
  20. <!-- 协议文本 -->
  21. <view class="agreement-text">
  22. <view class="agreement-content">
  23. <view class="checkbox-container" @click="toggleAgreement">
  24. <view class="checkbox" :class="{ 'checked': isAgreed }">
  25. <view class="checkbox-inner" v-if="isAgreed"></view>
  26. </view>
  27. </view>
  28. <text >我已阅读并同意
  29. <text class="link-text" @click="showServiceAgreement">服务协议</text>
  30. <text></text>
  31. <text class="link-text" @click="showPrivacyPolicy">隐私政策</text>
  32. </text>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <!-- 用户协议和隐私政策弹窗 -->
  38. <uv-modal ref="serviceModal" title="《服务协议与隐私条款》" confirm-text="我知道了" confirm-color="#06DADC" @confirm="isAgreed = true">
  39. <view class="privacy-content">
  40. <scroll-view scroll-y style="height: 600rpx;">
  41. <!-- 如果是富文本 -->
  42. <uv-parse :content="configParamContent('privacy_policy')"></uv-parse>
  43. </scroll-view>
  44. </view>
  45. </uv-modal>
  46. <!-- 用户协议和隐私政策弹窗 -->
  47. <uv-modal ref="guideModal" title="《个人信息保护指引》" confirm-text="我知道了" confirm-color="#06DADC" @confirm="isAgreed = true">
  48. <view class="privacy-content">
  49. <scroll-view scroll-y style="height: 600rpx;">
  50. <!-- 如果是富文本 -->
  51. <uv-parse :content="configParamContent('user_agreement')"></uv-parse>
  52. </scroll-view>
  53. </view>
  54. </uv-modal>
  55. </view>
  56. </template>
  57. <script>
  58. import uvParse from '../../uni_modules/uv-parse/components/uv-parse/uv-parse.vue';
  59. export default {
  60. components: { uvParse },
  61. name: 'Login',
  62. data() {
  63. return {
  64. isAgreed: false
  65. }
  66. },
  67. mounted() {
  68. // #ifdef H5
  69. // H5环境下检查URL参数,处理微信授权回调
  70. this.checkWechatAuthCallback();
  71. // #endif
  72. },
  73. methods: {
  74. // 微信登录统一入口
  75. handleLogin() {
  76. if (!this.isAgreed) {
  77. this.$refs.serviceModal.open();
  78. this.$refs.guideModal.open();
  79. return
  80. }
  81. // #ifdef MP-WEIXIN
  82. // 小程序环境
  83. this.miniProgramLogin();
  84. // #endif
  85. // #ifdef H5
  86. // H5环境 - 微信公众号授权
  87. this.wechatOfficialLogin();
  88. // #endif
  89. },
  90. // 小程序登录
  91. miniProgramLogin() {
  92. uni.login({
  93. provider: 'weixin',
  94. success: async (res) => {
  95. console.log('小程序登录成功', res);
  96. await this.processLogin(res.code);
  97. },
  98. fail: (err) => {
  99. console.log('小程序登录失败', err);
  100. uni.showToast({
  101. title: '登录失败',
  102. icon: 'none'
  103. });
  104. }
  105. })
  106. },
  107. // 微信公众号授权登录
  108. wechatOfficialLogin() {
  109. console.log('开始微信公众号授权登录');
  110. // 构建微信授权URL
  111. const appId = this.configParamContent('official_appid') || 'your_wechat_appid'; // 从配置中获取AppID
  112. const redirectUri = encodeURIComponent(window.location.origin + window.location.pathname + '#/subPages/login/login');
  113. console.log('redirectUri:', window.location.origin + window.location.pathname + '#/subPages/login/login');
  114. const state = Date.now().toString();
  115. // 保存当前邀请人信息
  116. if (uni.getStorageSync('inviter')) {
  117. sessionStorage.setItem('temp_inviter', uni.getStorageSync('inviter'));
  118. }
  119. const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
  120. console.log('跳转到微信授权页面:', authUrl);
  121. window.location.href = authUrl;
  122. },
  123. // 检查微信授权回调
  124. checkWechatAuthCallback() {
  125. const code = this.getUrlParameter('code');
  126. const state = this.getUrlParameter('state');
  127. if (code && state) {
  128. console.log('检测到微信授权回调, code:', code, 'state:', state);
  129. // 恢复邀请人信息
  130. const tempInviter = sessionStorage.getItem('temp_inviter');
  131. if (tempInviter) {
  132. uni.setStorageSync('inviter', tempInviter);
  133. sessionStorage.removeItem('temp_inviter');
  134. }
  135. // 处理登录
  136. this.processLogin(code, 'official');
  137. }
  138. },
  139. // 统一登录处理
  140. async processLogin(code, type = 'miniprogram') {
  141. try {
  142. const loginParams = {
  143. code: code
  144. };
  145. // 添加邀请人信息
  146. if (uni.getStorageSync('inviter')) {
  147. loginParams.inviter = uni.getStorageSync('inviter');
  148. }
  149. // 添加登录类型参数
  150. if (type === 'official') {
  151. loginParams.type = 'official';
  152. }
  153. console.log('登录参数:', loginParams);
  154. const { result: loginRes } = await this.$api.login.login(loginParams);
  155. uni.setStorageSync('token', loginRes.token);
  156. const userInfo = loginRes.userInfo;
  157. // 存储用户信息到store
  158. this.$store.dispatch('updateUserInfo', userInfo);
  159. if (!userInfo.avatar || !userInfo.name || !userInfo.phone) {
  160. uni.navigateTo({
  161. url: '/subPages/login/userInfo'
  162. });
  163. return;
  164. } else {
  165. uni.showToast({
  166. title: '登录成功',
  167. icon: 'success'
  168. });
  169. uni.switchTab({
  170. url: '/pages/index/home'
  171. });
  172. }
  173. } catch (error) {
  174. console.error('登录失败:', error);
  175. uni.showToast({
  176. title: '登录失败,请重试',
  177. icon: 'none'
  178. });
  179. // #ifdef H5
  180. // H5环境下如果登录失败,清除URL参数并重新加载页面
  181. if (this.getUrlParameter('code')) {
  182. const cleanUrl = window.location.origin + window.location.pathname + window.location.hash.split('?')[0];
  183. window.history.replaceState({}, document.title, cleanUrl);
  184. }
  185. // #endif
  186. }
  187. },
  188. // 获取URL参数
  189. getUrlParameter(name) {
  190. // #ifdef H5
  191. const url = window.location.href;
  192. try {
  193. const urlParams = new URLSearchParams(window.location.search);
  194. return urlParams.get(name) || '';
  195. } catch (e) {
  196. // 兼容性处理
  197. const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
  198. const results = regex.exec(url);
  199. if (!results) return '';
  200. if (!results[2]) return '';
  201. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  202. }
  203. // #endif
  204. // #ifndef H5
  205. return '';
  206. // #endif
  207. },
  208. // 游客登录
  209. handleGuestLogin() {
  210. console.log('暂不登录');
  211. // 跳转到主页
  212. uni.switchTab({
  213. url: '/pages/index/home'
  214. });
  215. },
  216. // 显示服务协议
  217. showServiceAgreement() {
  218. this.$refs.serviceModal.open();
  219. console.log('查看服务协议');
  220. // 这里可以跳转到协议页面或显示弹窗
  221. },
  222. // 显示隐私政策
  223. showPrivacyPolicy() {
  224. this.$refs.guideModal.open();
  225. console.log('查看隐私条款');
  226. // 这里可以跳转到隐私政策页面或显示弹窗
  227. },
  228. // 切换协议同意状态
  229. toggleAgreement() {
  230. this.isAgreed = !this.isAgreed;
  231. }
  232. }
  233. }
  234. </script>
  235. <style lang="scss" scoped>
  236. .login-container {
  237. position: relative;
  238. width: 100vw;
  239. height: 100vh;
  240. overflow: hidden;
  241. background: #E8FBFB;
  242. .bg-image {
  243. position: absolute;
  244. top: 0;
  245. left: 0;
  246. width: 100%;
  247. height: 100%;
  248. z-index: 1;
  249. }
  250. .content {
  251. position: relative;
  252. z-index: 2;
  253. height: 100%;
  254. display: flex;
  255. flex-direction: column;
  256. align-items: center;
  257. justify-content: center;
  258. padding: 0 60rpx;
  259. .logo-section {
  260. display: flex;
  261. flex-direction: column;
  262. align-items: center;
  263. margin-bottom: 120rpx;
  264. .logo {
  265. width: 120rpx;
  266. height: 120rpx;
  267. margin-bottom: 40rpx;
  268. }
  269. .title-text {
  270. font-size: 36rpx;
  271. font-weight: 600;
  272. color: $primary-text-color;
  273. text-align: center;
  274. }
  275. }
  276. .login-section {
  277. width: 100%;
  278. display: flex;
  279. flex-direction: column;
  280. align-items: center;
  281. .login-btn {
  282. width: 630rpx;
  283. height: 88rpx;
  284. margin-bottom: 30rpx;
  285. background-color: $primary-color;
  286. border: none;
  287. border-radius: 44rpx;
  288. color: white;
  289. font-size: 32rpx;
  290. font-weight: 500;
  291. display: flex;
  292. align-items: center;
  293. justify-content: center;
  294. }
  295. .guest-btn {
  296. width: 630rpx;
  297. height: 88rpx;
  298. margin-bottom: 60rpx;
  299. border: 2rpx solid $primary-color;
  300. border-radius: 44rpx;
  301. color: $primary-color;
  302. font-size: 32rpx;
  303. font-weight: 400;
  304. background-color: transparent;
  305. display: flex;
  306. align-items: center;
  307. justify-content: center;
  308. }
  309. .agreement-text {
  310. display: flex;
  311. align-items: center;
  312. justify-content: center;
  313. font-size: 24rpx;
  314. color: $secondary-text-color;
  315. line-height: 1.5;
  316. .checkbox-container {
  317. margin-right: 12rpx;
  318. cursor: pointer;
  319. .checkbox {
  320. width: 29rpx;
  321. height: 29rpx;
  322. border: 1rpx solid $secondary-text-color;
  323. border-radius: 50%;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. transition: all 0.3s ease;
  328. &.checked {
  329. border-color: $primary-color;
  330. background-color: $primary-color;
  331. }
  332. .checkbox-inner {
  333. width: 16rpx;
  334. height: 16rpx;
  335. background-color: white;
  336. border-radius: 50%;
  337. }
  338. }
  339. }
  340. .agreement-content {
  341. flex: 1;
  342. text-align: left;
  343. display: flex;
  344. .link-text {
  345. color: $primary-color;
  346. // text-decoration: underline;
  347. }
  348. }
  349. }
  350. }
  351. }
  352. .privacy-content{
  353. max-height: 600rpx;
  354. }
  355. }
  356. </style>