吉安感知网项目-前端
chenyao
2026-01-08 94bff04da6a07e91e04af114689ce533fa1545c6
feat:更新小程序
4 files modified
132 ■■■■■ changed files
uniapps/work-wx/src/api/user/index.js 18 ●●●●● patch | view | raw | blame | history
uniapps/work-wx/src/pages/login/index.vue 106 ●●●●● patch | view | raw | blame | history
uniapps/work-wx/src/pages/page/index.vue 4 ●●●● patch | view | raw | blame | history
uniapps/work-wx/src/router/index.js 4 ●●●● patch | view | raw | blame | history
uniapps/work-wx/src/api/user/index.js
@@ -70,6 +70,24 @@
  });
};
// 微信登录接口
export const loginByWechat = (code) => {
  return request({
    url: '/blade-auth/oauth/token',
    method: 'post',
    header: {
      'Tenant-Id': '000000',
    },
    params: {
      tenantId: '000000',
      code,
      grant_type: 'wechat',
      scope: 'all',
      type: 'wechat',
    },
  });
};
// 发送通话信息
export const sendVoiceCallApi = (data) => {
  return request({
uniapps/work-wx/src/pages/login/index.vue
@@ -30,7 +30,14 @@
    </div>
    <button class="login-btn" :style="[inputStyle]" @tap="submit">登录</button>
    <image class="lowerRightCorner" :src="droneSvg" />
    <button class="wechat-login-btn" @tap="wechatLogin">微信登录</button>
    <view class="agreement">
      <label>
        <checkbox :checked="agreed" @click="agreed = !agreed" />
        同意《用户协议》与《隐私政策》
      </label>
    </view>
  </view>
</template>
@@ -41,7 +48,7 @@
import { useUserStore } from "@/store/index.js";
import { HOME_PATH, LOGIN_PATH, removeQueryString } from "@/router";
import { onMounted } from "vue";
import { ref } from "vue";
import logoSvg from "@/static/images/logo.svg";
import showPasswordSvg from "@/static/images/show-password.svg";
@@ -50,6 +57,8 @@
const usernameSvg = getAssetsImage("/images/login/username.svg");
const passwordSvg = getAssetsImage("/images/login/password.svg");
// const logoSvg = getAssetsImage("/images/login/logo.svg");
const agreed = ref(true);
const userStore = useUserStore();
const loginForm = ref({
@@ -121,7 +130,7 @@
    );
    userStore.setUserInfo(res.data);
    uni.reLaunch({
      url: "/pages/map/index",
      url: "/pages/page/index",
    });
  } catch (error) {
    const errorMsg =
@@ -135,6 +144,82 @@
    });
  }
}
// 核心登录函数
const wechatLogin = async () => {
  // 1. 协议检查
  if (!agreed.value) {
    uni.showToast({ title: '请先同意协议', icon: 'none' });
    return;
  }
  // loading.value = true;
  try {
    // 2. 获取登录凭证 (code)
    const loginRes = await uni.login({
      provider: 'weixin',
      timeout: 10000
    });
    if (!loginRes.code) {
      throw new Error('获取登录码失败');
    }
    console.log('获取到code:', loginRes.code);
    // 3. 获取用户信息 (需要用户授权弹窗)
    const userProfileRes = await uni.getUserProfile({
      desc: '用于完善您的会员信息', // 声明用途,会展示给用户
      lang: 'zh_CN'
    });
    console.log('用户信息:', userProfileRes.userInfo);
    // 4. 调用后端登录接口
    const serverRes = await uni.request({
      // url: 'https://你的域名.com/api/wx-login', // 替换为你的真实接口
      // method: 'POST',
      // data: {
      //   code: loginRes.code,
      //   rawData: userProfileRes.rawData,
      //   signature: userProfileRes.signature,
      //   encryptedData: userProfileRes.encryptedData,
      //   iv: userProfileRes.iv,
      //   userInfo: userProfileRes.userInfo // 注意:userInfo 仅为昵称头像等非敏感信息
      // },
      // header: { 'Content-Type': 'application/json' }
    });
    // 5. 处理登录结果
    const { data } = serverRes;
    if (data.success && data.token) {
      // 保存登录状态
      uni.setStorageSync('token', data.token);
      uni.setStorageSync('userInfo', data.userInfo || userProfileRes.userInfo);
      uni.showToast({ title: '登录成功', icon: 'success' });
      // 登录成功后的跳转逻辑
      const pages = getCurrentPages();
      if (pages.length > 1) {
        uni.navigateBack();
      } else {
        uni.switchTab({ url: '/pages/page/index' }); // 跳转到首页
      }
    } else {
      throw new Error(data.message || '登录失败');
    }
  } catch (error) {
    console.error('登录全过程失败:', error);
    uni.showToast({
      title: error.errMsg || error.message || '登录失败',
      icon: 'none'
    });
  } finally {
    // loading.value = false;
  }
};
// 从本地存储加载记住的密码
onMounted(() => {
@@ -271,6 +356,21 @@
      @apply border-none;
    }
  }
  .wechat-login-btn {
    z-index: 1;
    @apply flex items-center justify-center py-12rpx px-0 text-30rpx border-none;
    background: #07C160;
    color: white;
    width: 590rpx;
    height: 76rpx;
    border-radius: 40rpx 40rpx 40rpx 40rpx;
    margin-top: 30rpx;
    &::after {
      @apply border-none;
    }
  }
  .alternative {
    @apply flex justify-between mt-30rpx;
uniapps/work-wx/src/pages/page/index.vue
@@ -66,10 +66,6 @@
  if (args.length >= 2) {
    const [item, index] = args;
    console.log('点击了轮播图第', index + 1, '张', item);
    // 轮播图点击跳转,使用switchTab因为目标页面是tabBar页面
    uni.switchTab({
      url: item.link,
    })
  }
  // 处理网格组件点击事件
  else if (args.length === 1 && typeof args[0] === 'object') {
uniapps/work-wx/src/router/index.js
@@ -11,9 +11,9 @@
import pagesJson from "@/pages.json"
// 路径常量
export const HOME_PATH = "/pages/map/index"
export const HOME_PATH = "/pages/page/index"
export const LOGIN_PATH = "/pages/login/index"
export const ERROR404_PATH = "/pages/common/404/index"
export const ERROR404_PATH = "/subPackages/404/index"
/**
 * 解析路由地址