阶段四:UI优化与项目发布

1. 教学目标

2. 优化与发布流程图 (Mermaid)

graph TD subgraph "UI 优化" A[用户点击生活指数] --> B[LifeIndex.vue 发出 show-detail 事件]; B --> C[index.vue 监听到事件
更新 visible 和 currentIndex 状态]; C --> D[IndexDetailPopup.vue
根据 props 弹出显示详情]; end subgraph "UX 优化" E[用户在首页下拉] --> F[scroll-view 触发 refresherrefresh 事件]; F --> G[index.vue 调用 onRefresh 方法]; G --> H[设置 refreshing=true, 重新请求数据]; H --> I[数据返回后设置 refreshing=false
刷新动画收起]; end subgraph "打包发布" J[配置 manifest.json
填入AppID] --> K[执行打包命令
npm run build:mp-weixin]; K --> L[在微信开发者工具中
导入 dist/build/mp-weixin 目录]; L --> M[预览、测试后点击上传]; end

3. 核心步骤详解

Component Icon 步骤一:实现生活指数详情弹窗

当用户点击首页的某个生活指数时,我们弹出一个浮层来展示更详细的说明。

1. 创建详情弹窗组件 IndexDetailPopup.vue

点击展开/折叠 IndexDetailPopup.vue 源代码
<template>
  <uni-popup ref="popup" type="center" :is-mask-click="false">
    <view class="popup-content">
      <text class="popup-title">{{ indexData.name }}</text>
      <view class="popup-body">
        <uni-icons :type="indexData.icon" size="64" :color="indexData.color"></uni-icons>
        <text class="popup-value">{{ indexData.value }}</text>
        <text class="popup-desc">{{ indexData.desc }}</text>
      </view>
      <button @click="onClose">关闭</button>
    </view>
  </uni-popup>
</template>

<script setup>
import { defineProps, defineEmits, ref, watch } from 'vue';

const props = defineProps({
  visible: { type: Boolean, default: false },
  indexData: { type: Object, default: () => ({}) }
});

const emit = defineEmits(['close']);
const popup = ref(null);

// 监听父组件传来的 visible 状态,控制弹窗的打开和关闭
watch(() => props.visible, (newVal) => {
  if (!popup.value) return;
  if (newVal) {
    popup.value.open();
  } else {
    popup.value.close();
  }
});

// 当用户点击关闭按钮时,通知父组件
const onClose = () => {
  emit('close');
};
</script>

2. 在 index.vue 中使用弹窗

点击展开/折叠 index.vue 与弹窗交互的相关代码
<template>
    <!-- ...其他组件... -->
    
    <!-- 生活指数,监听子组件的 show-detail 事件 -->
    <LifeIndex :index-data="lifeIndices" @show-detail="showIndexDetail"></LifeIndex>
      
    <!-- 指数详情弹窗,传递 visible 状态并监听 close 事件 -->
    <IndexDetailPopup :visible="detailVisible" :index-data="currentIndex" @close="closePopup"></IndexDetailPopup>
</template>

<script setup>
import { ref } from 'vue';
// ... 其他导入 ...

// 控制弹窗显隐的状态
const detailVisible = ref(false);
// 存储当前要展示的指数详情
const currentIndex = ref({});

// 显示指数详情(由 LifeIndex 组件触发)
const showIndexDetail = (index) => {
  currentIndex.value = index;
  detailVisible.value = true;
};

// 关闭弹窗(由 IndexDetailPopup 组件触发)
const closePopup = () => {
  detailVisible.value = false;
};
</script>

Deploy Icon 步骤二:实现下拉刷新功能

本项目未使用页面级的下拉刷新,而是在 index.vue 内部使用了 <scroll-view> 组件提供的下拉刷新,这种方式更灵活。

点击展开/折叠 index.vue 下拉刷新相关代码
<template>
  <view class="weather-container">
    <scroll-view 
      scroll-y 
      refresher-enabled="true" 
      @refresherrefresh="onRefresh"
      :refresher-triggered="refreshing"
      style="height: 100vh;"
    >
      <!-- 所有页面内容... -->
    </scroll-view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
// ...

// 控制刷新动画的状态
const refreshing = ref(false);

const initData = async () => { /* ... */ };

// 下拉刷新事件处理
const onRefresh = async () => {
  // 1. 显示刷新动画
  refreshing.value = true;
  
  // 2. 重新获取所有数据
  await initData();
  
  // 3. 数据获取完毕,隐藏刷新动画
  refreshing.value = false;
};
</script>

Code Icon 步骤三:自定义SVG图标组件 (SvgIcon.vue) (可选)

点击展开/折叠 SvgIcon.vue 源代码
<template>
  <image 
    class="weather-icon"
    :src="`/src/static/SvgIcon/${name}.svg`"
    mode="widthFix"
    :style="{ width: size + 'px', height: size + 'px', color: color }"
  />
</template>

<script setup>
import { defineProps } from 'vue'

defineProps({
  name: { type: String, required: true },
  size: { type: [Number, String], default: 24 },
  color: { type: String, default: '' }
})
</script>

Deploy Icon 步骤四:项目打包与发布

这是将我们的劳动成果交付给用户的最后一步。

  1. 配置 manifest.json:
    • 打开项目根目录的 manifest.json 文件。
    • 在左侧菜单中选择 “微信小程序配置”。
    • 将从微信公众平台申请到的 微信小程序AppID 填入对应的输入框中。这是小程序身份的唯一标识,必须填写
    • (可选)在此处还可以配置小程序名称、图标等。
  2. 执行打包命令:
    • 打开 package.json 文件,可以看到 scripts 中定义了各种平台的打包命令。
    • 我们需要的是 build:mp-weixin
    • 打开终端(HBuilderX内置终端或系统终端),在项目根目录下执行:
    npm run build:mp-weixin
    
    • 等待命令执行完毕。成功后,会在项目根目录生成一个 dist 文件夹,其中 dist/build/mp-weixin 就是我们打包好的小程序代码。
  3. 在微信开发者工具中发布:
    • 打开你的 微信开发者工具
    • 点击 “导入”,项目目录选择上一步生成的 dist/build/mp-weixin 文件夹。
    • AppID 会自动填入,确认无误后点击导入。
    • 在开发者工具中,可以进行最后的预览和调试。
    • 确认无误后,点击右上角的 “上传” 按钮,填写版本号和项目备注。
    • 上传成功后,登录 微信公众平台,在 “版本管理” 中可以看到你刚上传的版本。你可以将其设为“体验版”供内部测试,或直接“提交审核”。审核通过后,即可发布上线。

4. 课程总结与拓展任务

本次课程我们从零开始,完整地经历了一个真实项目的全部生命周期。我们不仅学习了 uni-appVue3 的具体技术,更重要的是,我们建立了一套结构化、组件化、服务化的工程思想。这套思想可以被应用到未来的任何项目中。

拓展任务:


教师提示: 最后一阶段,重点在于培养学生的“产品完结”意识。一个项目不仅要功能跑通,还要好用、好看、稳定。让学生理解,代码的终点是服务于最终用户。发布流程是理论知识,能让学生对项目开发的“最后一公里”有一个完整的认知。