文件
fenleixinxi/uniapp/pages/post/create.vue
linda 376a7ed793 feat(uniapp): 小程序端(用户端 uni-app)源码入库到 uniapp/ 目录
需求:小程序端代码要同时同步到服务器与 git 远程仓库,此前前端仅存在于本地目录(无 remote,从未推送),
服务器与 gitcode 上只有后端。现并入本仓库,复用现有「push 即部署」链路,一次推送双端落地。

- 新增 uniapp/:小程序端源码全量快照(150 文件 / 1.2M)
  pages(13 页面) · components(fc-navbar/fc-tabbar/fc-post-card/fc-empty) ·
  common(api·utils·mock·assets·store·config) · static(74 图标 + 3 轮播插画 + LOGO) ·
  docs(01-04 交付文档) · tests(零依赖全链路用例) · tools(图标/主题/轮播配图生成脚本)
- 明确排除:AGENTS.md(含后台密码、服务器 root 密码、小程序 AppSecret,切勿入库)、
  unpackage/(构建产物,由 uniapp/.gitignore 忽略)、node_modules、.DS_Store、*.log
- .gitignore 新增 /uniapp/AGENTS.md 兜底(与根 /AGENTS.md、/fluniapp/ 同一约定)
- README.md 增补「本项目结构 + 部署链路」章节,说明 uniapp/ 为源码快照、
  小程序真正上线仍需微信开发者工具 cli upload

说明:本次为源码快照,未携带前端本地仓库历史——其历史提交中含 AGENTS.md 凭据,
若做 subtree 合并会把密码永久写入本仓库历史,故采用快照方式。
2026-10-17 14:19:03 +08:00

656 行
18 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="create fc-page">
<!-- 分类 -->
<view class="fc-card create__section">
<view class="create__label">选择分类<text class="create__required">*</text></view>
<view class="create__cats">
<view v-for="c in categories" :key="c.id" class="create__cat"
:class="{ 'create__cat--active': form.categoryId === c.id }" @click="form.categoryId = c.id">
<image class="create__cat-icon" :src="catIcon(c)" mode="aspectFit" />
<text class="create__cat-name">{{ c.name }}</text>
</view>
</view>
</view>
<!-- 表单 -->
<view class="fc-card create__section">
<view class="create__label">标题<text class="create__required">*</text></view>
<input v-model="form.title" class="create__input" placeholder="一句话说明你的信息(如:急招快递分拣员日结)"
maxlength="40" placeholder-class="create__ph" @input="autoSave" />
<view class="create__label create__label--mt">详细内容<text class="create__required">*</text></view>
<textarea v-model="form.content" class="create__textarea" placeholder="补充价格、时间、要求等细节,描述越详细越容易成交"
maxlength="500" placeholder-class="create__ph" @input="autoSave" />
<text class="create__count">{{ (form.content || '').length }}/500</text>
<view class="create__label create__label--mt">图片 / 视频</view>
<view class="create__media">
<view v-for="(m, i) in form.media" :key="i" class="create__media-item fc-thumb">
<image class="fc-icon fc-icon--md" :src="iconUrl('image')" mode="aspectFit" />
</view>
<view v-if="form.media.length < 6" class="create__upload" @click="chooseMedia">
<image class="fc-icon fc-icon--md" :src="iconUrl('plus-ink')" mode="aspectFit" />
<text class="create__upload-txt">上传</text>
</view>
</view>
</view>
<!-- 联系方式 -->
<view class="fc-card create__section">
<view class="create__row">
<text class="create__row-label">联系人</text>
<input v-model="form.contact" class="create__input create__input--row" placeholder="请输入称呼"
placeholder-class="create__ph" />
</view>
<view class="create__row create__row--line">
<text class="create__row-label">联系电话</text>
<input v-model="form.phone" class="create__input create__input--row" type="number" maxlength="11"
placeholder="请输入手机号" placeholder-class="create__ph" />
</view>
<view class="create__row create__row--line" @click="chooseLocation">
<text class="create__row-label">所在区域</text>
<view class="create__location">
<image class="fc-icon fc-icon--sm" :src="iconUrl('location')" mode="aspectFit" />
<text class="create__location-text fc-ellipsis">{{ form.address || '点击定位/选择区域' }}</text>
<text class="create__location-arrow">›</text>
</view>
</view>
</view>
<!-- 发布方式:由后台「发布与费用配置」决定(免费发布 / 付费发布 + 套餐额度) -->
<view class="fc-card create__section">
<view class="create__label-row">
<text class="create__label">发布方式</text>
<text v-if="publish.mode === 'paid'" class="create__quota">
套餐额度:剩余 {{ publish.quotaLeft }} 条
</text>
</view>
<view v-if="publish.mode === 'free'" class="create__free-tip">
<text>当前为免费发布,所有用户均可免费发布信息</text>
</view>
<view v-else class="create__plans">
<view v-for="p in plans" :key="p.key" class="create__plan"
:class="{ 'create__plan--active': form.plan === p.key }" @click="form.plan = p.key">
<text class="create__plan-name">{{ p.name }}</text>
<text class="create__plan-price">{{ p.price }}</text>
<text class="create__plan-desc">{{ p.desc }}</text>
<text v-if="p.tag" class="create__plan-tag">{{ p.tag }}</text>
</view>
</view>
<view v-if="needPay" class="create__pay">
<text class="create__pay-label">支付方式</text>
<view class="create__pay-types">
<view class="create__pay-type" :class="{ 'create__pay-type--active': form.payType === 'wxpay' }"
@click="form.payType = 'wxpay'">
<image class="fc-icon fc-icon--sm" :src="iconUrl('paywechat')" mode="aspectFit" />
<text class="create__pay-text">微信支付</text>
</view>
<view class="create__pay-type" :class="{ 'create__pay-type--active': form.payType === 'balance' }"
@click="form.payType = 'balance'">
<image class="fc-icon fc-icon--sm" :src="iconUrl('wallet')" mode="aspectFit" />
<text class="create__pay-text">余额支付(¥{{ balance }})</text>
</view>
</view>
</view>
</view>
<!-- 草稿箱 -->
<view v-if="drafts.length" class="fc-card create__section">
<view class="create__draft-head">
<text class="create__label">草稿箱</text>
<text class="create__draft-tip">自动保存,可再次编辑发布</text>
</view>
<view v-for="d in drafts" :key="d.draftId" class="create__draft" @click="restoreDraft(d)">
<text class="create__draft-title fc-ellipsis">{{ d.title }}</text>
<text class="create__draft-arrow">编辑 ›</text>
</view>
</view>
<view class="create__bar">
<view class="create__bar-ghost" @click="saveDraft">
<text>存草稿</text>
</view>
<view class="create__bar-main" :class="{ 'create__bar-main--disabled': submitting }" @click="submit">
<text>{{ submitting ? '提交中…' : (needPay ? '支付并发布' : '免费发布') }}</text>
</view>
</view>
<view class="fc-safe-bottom"></view>
</view>
</template>
<script>
import homeApi from '@/common/api/home.js'
import postApi from '@/common/api/post.js'
import memberApi from '@/common/api/member.js'
import { useUserStore } from '@/common/store/index.js'
import { iconUrl, categoryIconUrl, loadUiConfig } from '@/common/store/ui.js'
import { pay } from '@/common/utils/pay.js'
import { yuan2fen } from '@/common/utils/format.js'
import config from '@/common/config/index.js'
export default {
data() {
return {
categories: [],
drafts: [],
submitting: false,
draftTimer: null,
form: {
categoryId: 0,
title: '',
content: '',
media: [],
contact: '',
phone: '',
address: '',
plan: 'free',
payType: config.defaultPayType,
},
// 发布配置(后台「发布与费用配置」下发:发布模式 / 价格 / 我的剩余额度)
publish: {
mode: 'paid',
singlePrice: 5,
monthPrice: 19.9,
monthQuota: 30,
yearPrice: 168,
yearQuota: 500,
quotaLeft: 0,
freePerDay: 0,
freeUsedToday: 0,
freeLeftToday: 0,
isMember: false,
},
}
},
computed: {
balance() {
try {
return useUserStore().state.userInfo?.balance ?? 0
} catch (e) {
return 0
}
},
/** 发布方式选项:由后台配置生成(免费模式不展示;付费模式按剩余额度插入「套餐额度」) */
plans() {
const p = this.publish
if (p.mode === 'free') return []
const list = []
if (p.quotaLeft > 0) {
list.push({ key: 'quota', name: '套餐额度', price: '免费', desc: `剩余 ${p.quotaLeft} 条`, tag: '可用' })
} else if (p.freeLeftToday > 0) {
list.push({ key: 'quota', name: '今日免费', price: '免费', desc: `今日剩余 ${p.freeLeftToday} 条`, tag: '可用' })
}
list.push({ key: 'single', name: '会员单条', price: `¥${this.money(p.singlePrice)}`, desc: '发布 1 条' })
list.push({ key: 'month', name: '按月发布', price: `¥${this.money(p.monthPrice)}`, desc: `含 ${p.monthQuota} 条`, tag: '推荐' })
list.push({ key: 'year', name: '按年发布', price: `¥${this.money(p.yearPrice)}`, desc: `含 ${p.yearQuota} 条`, tag: '更划算' })
return list
},
needPay() {
return this.publish.mode === 'paid' && this.form.plan !== 'quota'
},
},
onLoad(query) {
if (query && query.categoryId) this.form.categoryId = Number(query.categoryId)
loadUiConfig()
this.init()
},
methods: {
iconUrl,
// 分类图标:优先用后端下发的 iconKey(支持后台自定义覆盖),
// 否则按分类 ID 映射内置配图(与 post/list.vue 保持同一套逻辑)
catIcon(c) {
return (c.iconKey && iconUrl(c.iconKey)) || categoryIconUrl(c.id)
},
async init() {
try {
const [cats, drafts] = await Promise.all([homeApi.categories(), postApi.drafts()])
this.categories = cats
this.drafts = drafts
if (!this.form.categoryId && cats.length) this.form.categoryId = cats[0].id
} catch (e) {
uni.showToast({ title: '加载失败', icon: 'none' })
}
this.loadPublish()
},
/** 读取后台发布的「发布模式 / 价格 / 我的剩余额度」 */
async loadPublish() {
try {
const p = await postApi.publishConfig()
this.publish = { ...this.publish, ...p }
// 默认选中:免费模式→免费;付费模式有额度/每日免费次数→用免费额度,否则按单条
if (p.mode === 'free') {
this.form.plan = 'free'
} else {
const freeAvailable = p.quotaLeft > 0 || p.freeLeftToday > 0
this.form.plan = freeAvailable ? 'quota' : 'single'
}
} catch (e) {
// 配置读取失败时保持付费模式默认值,由后端兜底校验(不会漏收费)
}
},
money(v) {
const n = Number(v || 0)
return Number.isInteger(n) ? String(n) : n.toFixed(2)
},
chooseMedia() {
uni.chooseImage({
count: 6 - this.form.media.length,
success: (res) => {
this.form.media = this.form.media.concat(res.tempFilePaths || [])
},
fail: () => {
this.form.media.push('local://demo.jpg')
},
})
},
chooseLocation() {
uni.chooseLocation({
success: (res) => {
this.form.address = res.address || res.name
this.autoSave()
},
fail: () => {
uni.showActionSheet({
itemList: ['云岩区', '南明区', '观山湖区', '花溪区'],
success: (r) => {
this.form.address = ['云岩区', '南明区', '观山湖区', '花溪区'][r.tapIndex]
this.autoSave()
},
fail: () => {},
})
},
})
},
autoSave() {
if (this.draftTimer) clearTimeout(this.draftTimer)
// 草稿箱:编辑中自动保存(防抖 800ms)
this.draftTimer = setTimeout(() => {
if (this.form.title || this.form.content) this.saveDraft(true)
}, 800)
},
async saveDraft(silent = false) {
if (!this.form.title && !this.form.content) {
if (!silent) uni.showToast({ title: '内容为空,无法保存', icon: 'none' })
return
}
try {
await postApi.saveDraft({ ...this.form })
if (!silent) {
uni.showToast({ title: '已存入草稿箱', icon: 'none' })
this.drafts = await postApi.drafts()
}
} catch (e) {
if (!silent) uni.showToast({ title: e.message, icon: 'none' })
}
},
restoreDraft(d) {
this.form = { ...this.form, ...d }
uni.showToast({ title: '草稿已恢复', icon: 'none' })
},
validate() {
if (!this.form.categoryId) return '请选择分类'
if (!this.form.title) return '请填写标题'
if (!this.form.content) return '请填写详细内容'
if (this.form.phone && !/^1\d{10}$/.test(this.form.phone)) return '手机号格式不正确'
return ''
},
async submit() {
if (this.submitting) return
const err = this.validate()
if (err) {
uni.showToast({ title: err, icon: 'none' })
return
}
this.submitting = true
try {
const plan = this.publish.mode === 'free' ? 'free' : this.form.plan
// 月卡/年卡:先开通会员套餐(含发布条数,复用会员开通接口),额度到账后再按「额度内免费」发布
if (plan === 'month' || plan === 'year') {
await memberApi.open({
planId: plan === 'month' ? 'M1' : 'M12',
payType: this.form.payType,
})
await useUserStore().fetchProfile()
}
// 传给后端只两种:free(免费/额度内/刚开通套餐)与 single(单条付费);金额由服务端算
const apiPlan = (plan === 'free' || plan === 'quota' || plan === 'month' || plan === 'year') ? 'free' : plan
const res = await postApi.create({
...this.form,
plan: apiPlan,
payType: this.form.payType,
})
if (res.order) {
await pay({ ...res.order, amount: yuan2fen(res.order.amount) })
}
if (typeof res.quotaLeft === 'number') this.publish.quotaLeft = res.quotaLeft
uni.showToast({ title: '发布成功,等待审核', icon: 'none' })
// 发布后直接进「我的发布」,可立刻看到审核状态(原先跳首页看不到自己的发布)
setTimeout(() => uni.reLaunch({ url: '/pages/post/mine' }), 800)
} catch (e) {
uni.showToast({ title: e.message || '发布失败', icon: 'none' })
} finally {
this.submitting = false
}
},
},
}
</script>
<style lang="scss" scoped>
.create {
padding-bottom: 200rpx;
}
.create__section {
margin-top: 24rpx;
}
.create__label {
font-size: $fc-font-lg;
font-weight: 700;
color: $fc-text;
&--mt {
margin-top: 32rpx;
}
}
.create__label-row {
display: flex;
align-items: center;
justify-content: space-between;
}
.create__quota {
font-size: $fc-font-sm;
color: $fc-primary-text;
font-weight: 600;
}
.create__free-tip {
margin-top: 18rpx;
padding: 20rpx 24rpx;
border-radius: $fc-radius;
background: $fc-primary-lighter;
font-size: $fc-font-sm;
color: $fc-primary-text;
line-height: 1.5;
}
.create__required {
color: $fc-danger;
margin-left: 6rpx;
}
.create__cats {
display: flex;
flex-wrap: wrap;
margin-top: 20rpx;
}
.create__cat {
width: 23%;
margin: 0 1% 18rpx;
padding: 18rpx 0;
border-radius: $fc-radius;
background: $fc-bg;
border: 2rpx solid transparent;
display: flex;
flex-direction: column;
align-items: center;
&--active {
background: $fc-primary-lighter;
border-color: $fc-primary-text;
}
}
.create__cat-icon {
width: 64rpx;
height: 64rpx;
margin-bottom: 6rpx;
}
.create__cat-name {
margin-top: 8rpx;
font-size: $fc-font-xs;
color: $fc-text-regular;
}
.create__input {
margin-top: 16rpx;
height: 84rpx;
background: $fc-bg;
border-radius: $fc-radius;
padding: 0 24rpx;
font-size: $fc-font;
&--row {
margin-top: 0;
background: transparent;
flex: 1;
text-align: right;
}
}
.create__ph {
color: $fc-text-placeholder;
font-size: $fc-font-sm;
}
.create__textarea {
margin-top: 16rpx;
width: 100%;
height: 240rpx;
background: $fc-bg;
border-radius: $fc-radius;
padding: 22rpx 24rpx;
font-size: $fc-font;
}
.create__count {
display: block;
text-align: right;
margin-top: 8rpx;
font-size: $fc-font-xs;
color: $fc-text-placeholder;
}
.create__media {
display: flex;
flex-wrap: wrap;
margin-top: 16rpx;
}
.create__media-item {
width: 150rpx;
height: 150rpx;
margin: 0 16rpx 16rpx 0;
display: flex;
align-items: center;
justify-content: center;
}
.create__upload {
width: 150rpx;
height: 150rpx;
border-radius: 20rpx;
border: 2rpx dashed $fc-primary-text;
background: $fc-bg;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.create__upload-txt {
margin-top: 6rpx;
font-size: $fc-font-xs;
color: $fc-primary-text;
}
.create__row {
display: flex;
align-items: center;
height: 92rpx;
&--line {
border-top: 2rpx solid $fc-border;
}
}
.create__row-label {
width: 170rpx;
font-size: $fc-font;
color: $fc-text-regular;
}
.create__location {
flex: 1;
display: flex;
align-items: center;
justify-content: flex-end;
}
.create__location-text {
font-size: $fc-font;
color: $fc-text;
max-width: 380rpx;
margin-left: 8rpx;
}
.create__location-arrow {
margin-left: 8rpx;
color: $fc-text-placeholder;
}
.create__plans {
display: flex;
margin-top: 20rpx;
}
.create__plan {
flex: 1;
position: relative;
border: 2rpx solid $fc-border;
border-radius: $fc-radius-lg;
padding: 24rpx 16rpx;
background: $fc-bg;
display: flex;
flex-direction: column;
align-items: center;
&:not(:last-child) {
margin-right: 16rpx;
}
&--active {
border-color: $fc-primary-text;
background: $fc-primary-lighter;
}
}
.create__plan-name {
font-size: $fc-font-sm;
color: $fc-text-regular;
}
.create__plan-price {
margin-top: 8rpx;
font-size: 34rpx;
font-weight: 700;
color: $fc-price;
}
.create__plan-desc {
margin-top: 4rpx;
font-size: 20rpx;
color: $fc-text-secondary;
}
.create__plan-tag {
position: absolute;
top: -12rpx;
right: 12rpx;
padding: 2rpx 12rpx;
border-radius: $fc-radius-pill;
background: $fc-gradient-warm;
color: #ffffff;
font-size: 18rpx;
}
.create__pay {
margin-top: 28rpx;
}
.create__pay-label {
font-size: $fc-font-sm;
color: $fc-text-secondary;
}
.create__pay-types {
display: flex;
margin-top: 14rpx;
}
.create__pay-type {
flex: 1;
height: 80rpx;
border-radius: $fc-radius;
border: 2rpx solid $fc-border;
background: $fc-bg;
display: flex;
align-items: center;
justify-content: center;
font-size: $fc-font-sm;
color: $fc-text-regular;
&:first-child {
margin-right: 16rpx;
}
&--active {
border-color: $fc-primary-text;
background: $fc-primary-lighter;
color: $fc-primary-text;
font-weight: 600;
}
}
.create__pay-text {
margin-left: 10rpx;
}
.create__draft-head {
display: flex;
align-items: baseline;
justify-content: space-between;
}
.create__draft-tip {
font-size: $fc-font-xs;
color: $fc-text-placeholder;
}
.create__draft {
margin-top: 18rpx;
height: 84rpx;
border-radius: $fc-radius;
background: $fc-bg;
padding: 0 24rpx;
display: flex;
align-items: center;
justify-content: space-between;
}
.create__draft-title {
flex: 1;
font-size: $fc-font;
color: $fc-text;
margin-right: 16rpx;
}
.create__draft-arrow {
font-size: $fc-font-sm;
color: $fc-primary-text;
}
.create__bar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: center;
padding: 16rpx 24rpx;
background: #ffffff;
border-top: 2rpx solid $fc-border;
box-shadow: 0 -6rpx 24rpx rgba(4, 48, 30, 0.08);
}
.create__bar-ghost {
width: 190rpx;
height: 88rpx;
margin-right: 20rpx;
border-radius: $fc-radius-pill;
border: 2rpx solid $fc-primary-text;
display: flex;
align-items: center;
justify-content: center;
color: $fc-primary-text;
font-size: $fc-font;
}
.create__bar-main {
flex: 1;
height: 88rpx;
border-radius: $fc-radius-pill;
background: $fc-gradient-primary;
color: $fc-primary-ink;
font-size: 30rpx;
font-weight: 700;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 8rpx 24rpx rgba(4, 48, 30, 0.3);
&--disabled {
opacity: 0.7;
}
}
</style>