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 合并会把密码永久写入本仓库历史,故采用快照方式。
这个提交包含在:
59
uniapp/common/utils/format.js
普通文件
59
uniapp/common/utils/format.js
普通文件
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 格式化工具集
|
||||
*/
|
||||
|
||||
/** 金额分转元 */
|
||||
export function fen2yuan(fen) {
|
||||
return (Number(fen || 0) / 100).toFixed(2)
|
||||
}
|
||||
|
||||
/** 金额元转分 */
|
||||
export function yuan2fen(yuan) {
|
||||
return Math.round(Number(yuan || 0) * 100)
|
||||
}
|
||||
|
||||
/** 统一解析时间入参,避免 ISO / '-' / '/' 混用导致 Invalid Date */
|
||||
function toDate(input) {
|
||||
if (input instanceof Date) return input
|
||||
const s = String(input)
|
||||
let d = new Date(s)
|
||||
if (isNaN(d.getTime())) d = new Date(s.replace(/-/g, '/'))
|
||||
return d
|
||||
}
|
||||
|
||||
/** 时间戳/日期串 -> YYYY-MM-DD HH:mm */
|
||||
export function formatTime(input) {
|
||||
if (!input) return ''
|
||||
const d = toDate(input)
|
||||
if (isNaN(d.getTime())) return String(input)
|
||||
const p = (n) => String(n).padStart(2, '0')
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`
|
||||
}
|
||||
|
||||
/** 相对时间:刚刚 / N分钟前 / N小时前 / N天前 */
|
||||
export function fromNow(input) {
|
||||
if (!input) return ''
|
||||
const d = toDate(input)
|
||||
if (isNaN(d.getTime())) return String(input)
|
||||
const diff = Math.floor((Date.now() - d.getTime()) / 1000)
|
||||
if (diff < 60) return '刚刚'
|
||||
if (diff < 3600) return `${Math.floor(diff / 60)}分钟前`
|
||||
if (diff < 86400) return `${Math.floor(diff / 3600)}小时前`
|
||||
if (diff < 86400 * 30) return `${Math.floor(diff / 86400)}天前`
|
||||
return formatTime(input)
|
||||
}
|
||||
|
||||
/** 手机号脱敏 */
|
||||
export function maskPhone(phone) {
|
||||
const s = String(phone || '')
|
||||
return s.length === 11 ? s.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : s
|
||||
}
|
||||
|
||||
/** 距离格式化(米) */
|
||||
export function formatDistance(meters) {
|
||||
const m = Number(meters || 0)
|
||||
if (m < 1000) return `${Math.round(m)}m`
|
||||
return `${(m / 1000).toFixed(1)}km`
|
||||
}
|
||||
|
||||
export default { fen2yuan, yuan2fen, formatTime, fromNow, maskPhone, formatDistance }
|
||||
61
uniapp/common/utils/icon.js
普通文件
61
uniapp/common/utils/icon.js
普通文件
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* 图标地址解析(纯函数,便于 Node 单测)
|
||||
* ------------------------------------------------------------
|
||||
* 优先级:后端下发的覆盖地址 > static/icons/ 下的内置默认图
|
||||
* 后端可通过 /admin/setting/ui 按 key 覆盖任意图标与 LOGO。
|
||||
*/
|
||||
import { ICON_BASE, ICON_DEFAULTS, CATEGORY_ICON_KEY, DEFAULT_LOGO } from '../assets/icons.js'
|
||||
|
||||
let remote = { logo: '', icons: {} }
|
||||
|
||||
/** 注入后端下发的 UI 配置 */
|
||||
export function setRemoteUi(cfg = {}) {
|
||||
remote = {
|
||||
logo: cfg.logo || '',
|
||||
icons: { ...(cfg.icons || {}) },
|
||||
}
|
||||
}
|
||||
|
||||
/** 读取当前生效的后端配置 */
|
||||
export function getRemoteUi() {
|
||||
return { logo: remote.logo, icons: { ...remote.icons } }
|
||||
}
|
||||
|
||||
/** 恢复默认(后端未配置时) */
|
||||
export function resetRemoteUi() {
|
||||
remote = { logo: '', icons: {} }
|
||||
}
|
||||
|
||||
/** 取图标地址:key -> url */
|
||||
export function iconUrl(key) {
|
||||
if (!key) return ''
|
||||
const override = remote.icons[key]
|
||||
if (override) return override
|
||||
const file = ICON_DEFAULTS[key]
|
||||
return file ? ICON_BASE + file : ''
|
||||
}
|
||||
|
||||
/** 取 LOGO 地址 */
|
||||
export function logoUrl() {
|
||||
return remote.logo || DEFAULT_LOGO
|
||||
}
|
||||
|
||||
/** 分类 ID -> 分类图标地址 */
|
||||
export function categoryIconUrl(categoryId) {
|
||||
return iconUrl(CATEGORY_ICON_KEY[categoryId] || 'cat-job')
|
||||
}
|
||||
|
||||
/** 判断某图标是否已被后台自定义 */
|
||||
export function isIconOverridden(key) {
|
||||
return !!remote.icons[key]
|
||||
}
|
||||
|
||||
export default {
|
||||
setRemoteUi,
|
||||
getRemoteUi,
|
||||
resetRemoteUi,
|
||||
iconUrl,
|
||||
logoUrl,
|
||||
categoryIconUrl,
|
||||
isIconOverridden,
|
||||
}
|
||||
39
uniapp/common/utils/pay.js
普通文件
39
uniapp/common/utils/pay.js
普通文件
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 统一支付封装
|
||||
* 支持:微信支付(uni.requestPayment)/ 余额支付(服务端直接扣减)
|
||||
*/
|
||||
import config from '../config/index.js'
|
||||
|
||||
/**
|
||||
* 发起支付
|
||||
* @param {Object} order 订单信息 { orderNo, amount, payType }
|
||||
* @returns {Promise<{success:boolean, payType:string, orderNo:string}>}
|
||||
*/
|
||||
export function pay(order = {}) {
|
||||
const payType = order.payType || config.constants.PAY_TYPE.WXPAY
|
||||
|
||||
// 余额支付:直接调用服务端确认接口(此处由调用方传入回调)
|
||||
if (payType === config.constants.PAY_TYPE.BALANCE) {
|
||||
return Promise.resolve({ success: true, payType, orderNo: order.orderNo })
|
||||
}
|
||||
|
||||
// 微信支付
|
||||
return new Promise((resolve, reject) => {
|
||||
if (typeof uni === 'undefined' || !uni.requestPayment) {
|
||||
// 非小程序环境(如 H5/测试)直接视为成功,便于联调
|
||||
return resolve({ success: true, payType, orderNo: order.orderNo, mock: true })
|
||||
}
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
timeStamp: order.timeStamp,
|
||||
nonceStr: order.nonceStr,
|
||||
package: order.package,
|
||||
signType: order.signType || 'RSA',
|
||||
paySign: order.paySign,
|
||||
success: () => resolve({ success: true, payType, orderNo: order.orderNo }),
|
||||
fail: (err) => reject(Object.assign(new Error('支付已取消'), { raw: err, code: 'PAY_CANCEL' })),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default { pay }
|
||||
150
uniapp/common/utils/request.js
普通文件
150
uniapp/common/utils/request.js
普通文件
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* 全链路统一请求层
|
||||
* - 统一拼接 baseURL / 版本号
|
||||
* - 自动注入 token
|
||||
* - 统一响应结构解包:{ code, message, data }
|
||||
* - 401 自动清理登录态并跳转登录
|
||||
* - 支持 mock 降级(无后端 / 联调前)
|
||||
*
|
||||
* 该文件为纯 ES Module,Node 测试脚本可直接 import(需先注入 globalThis.uni)。
|
||||
*/
|
||||
import config from '../config/index.js'
|
||||
import storage from './storage.js'
|
||||
import { matchMock } from '../mock/index.js'
|
||||
|
||||
// 便于测试注入的运行时环境(默认使用全局 uni)
|
||||
export function getRuntime() {
|
||||
return {
|
||||
request: typeof uni !== 'undefined' && uni.request ? uni.request.bind(uni) : null,
|
||||
showToast: typeof uni !== 'undefined' && uni.showToast ? uni.showToast.bind(uni) : () => {},
|
||||
navigateTo: typeof uni !== 'undefined' && uni.navigateTo ? uni.navigateTo.bind(uni) : () => {},
|
||||
showLoading: typeof uni !== 'undefined' && uni.showLoading ? uni.showLoading.bind(uni) : () => {},
|
||||
hideLoading: typeof uni !== 'undefined' && uni.hideLoading ? uni.hideLoading.bind(uni) : () => {},
|
||||
}
|
||||
}
|
||||
|
||||
/** 拼接完整地址 */
|
||||
export function buildUrl(url) {
|
||||
if (/^https?:\/\//i.test(url)) return url
|
||||
const path = url.startsWith('/') ? url : `/${url}`
|
||||
return `${config.baseURL}/${config.apiVersion}${path}`
|
||||
}
|
||||
|
||||
/** 组装请求头 */
|
||||
export function buildHeaders(extra = {}) {
|
||||
const token = storage.get(config.storageKeys.token, '')
|
||||
const headers = {
|
||||
'Content-Type': 'application/json;charset=UTF-8',
|
||||
'X-Client': 'fenleixinxi',
|
||||
...extra,
|
||||
}
|
||||
if (token) headers.Authorization = `Bearer ${token}`
|
||||
return headers
|
||||
}
|
||||
|
||||
/**
|
||||
* 底层请求(返回 Promise<data>)
|
||||
* @param {Object} options { url, method, data, header, silent, mock }
|
||||
*/
|
||||
export function request(options = {}) {
|
||||
const {
|
||||
url,
|
||||
method = 'GET',
|
||||
data = {},
|
||||
header = {},
|
||||
silent = false, // 失败是否静默(不弹 toast)
|
||||
mock = config.forceMock, // 强制走 mock
|
||||
} = options
|
||||
|
||||
const runtime = getRuntime()
|
||||
const finalUrl = buildUrl(url)
|
||||
const headers = buildHeaders(header)
|
||||
|
||||
const doMock = () =>
|
||||
matchMock({ url, method: method.toUpperCase(), data }).then((res) => res)
|
||||
|
||||
// 强制 mock 或运行环境无 uni.request
|
||||
if (mock || !runtime.request) {
|
||||
return doMock()
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
runtime.request({
|
||||
url: finalUrl,
|
||||
method: method.toUpperCase(),
|
||||
data,
|
||||
header: headers,
|
||||
timeout: config.timeout,
|
||||
success(res) {
|
||||
// HTTP 层
|
||||
if (res.statusCode === 401) {
|
||||
onUnauthorized(runtime)
|
||||
return reject(toErr('登录已过期,请重新登录', 401))
|
||||
}
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
return handleFail(
|
||||
{ message: `网络异常(${res.statusCode})`, statusCode: res.statusCode },
|
||||
{ url, method, data, silent, runtime, resolve, reject }
|
||||
)
|
||||
}
|
||||
const body = res.data || {}
|
||||
// 业务层:约定 code===0 或 code===200 为成功
|
||||
if (body.code === 0 || body.code === 200) {
|
||||
resolve(body.data !== undefined ? body.data : body)
|
||||
} else {
|
||||
const err = toErr(body.message || body.msg || '请求失败', body.code)
|
||||
if (!silent) runtime.showToast({ title: err.message, icon: 'none' })
|
||||
reject(err)
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
handleFail(err, { url, method, data, silent, runtime, resolve, reject })
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败处理:
|
||||
* 1) 若开启 mock 降级,则使用本地 mock 路由兜底(保证离线可演示)
|
||||
* 2) 否则抛出异常
|
||||
*/
|
||||
function handleFail(err, ctx) {
|
||||
const { url, method, data, silent, runtime, resolve, reject } = ctx
|
||||
if (config.enableMockFallback) {
|
||||
// 尝试用本地 mock 路由兜底(离线可演示;无匹配路由时 mock 会 reject)
|
||||
matchMock({ url, method: String(method).toUpperCase(), data })
|
||||
.then(resolve)
|
||||
.catch(() => {
|
||||
const e = toErr(err && (err.errMsg || err.message) ? err.errMsg || err.message : '网络请求失败')
|
||||
if (!silent) runtime.showToast({ title: e.message, icon: 'none' })
|
||||
reject(e)
|
||||
})
|
||||
return
|
||||
}
|
||||
const e = toErr(err && (err.errMsg || err.message) ? err.errMsg || err.message : '网络请求失败')
|
||||
if (!silent) runtime.showToast({ title: e.message, icon: 'none' })
|
||||
reject(e)
|
||||
}
|
||||
|
||||
/** 401 处理 */
|
||||
function onUnauthorized(runtime) {
|
||||
storage.remove(config.storageKeys.token)
|
||||
storage.remove(config.storageKeys.userInfo)
|
||||
runtime.showToast({ title: '登录已过期', icon: 'none' })
|
||||
runtime.navigateTo({ url: '/pages/auth/login' })
|
||||
}
|
||||
|
||||
function toErr(message, code = -1) {
|
||||
const e = new Error(message)
|
||||
e.code = code
|
||||
return e
|
||||
}
|
||||
|
||||
/** 语法糖 */
|
||||
export const get = (url, data, opts = {}) => request({ url, method: 'GET', data, ...opts })
|
||||
export const post = (url, data, opts = {}) => request({ url, method: 'POST', data, ...opts })
|
||||
export const put = (url, data, opts = {}) => request({ url, method: 'PUT', data, ...opts })
|
||||
export const del = (url, data, opts = {}) => request({ url, method: 'DELETE', data, ...opts })
|
||||
|
||||
export default request
|
||||
50
uniapp/common/utils/storage.js
普通文件
50
uniapp/common/utils/storage.js
普通文件
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 本地存储封装(兼容 uni-app 与 Node 测试环境)
|
||||
* Node 环境下自动降级为内存 Map,便于单元测试。
|
||||
*/
|
||||
const memory = new Map()
|
||||
|
||||
function hasUni() {
|
||||
return typeof uni !== 'undefined' && uni && typeof uni.getStorageSync === 'function'
|
||||
}
|
||||
|
||||
export const storage = {
|
||||
get(key, def = '') {
|
||||
try {
|
||||
if (hasUni()) {
|
||||
const v = uni.getStorageSync(key)
|
||||
return v === '' || v === undefined || v === null ? def : v
|
||||
}
|
||||
return memory.has(key) ? memory.get(key) : def
|
||||
} catch (e) {
|
||||
return def
|
||||
}
|
||||
},
|
||||
set(key, value) {
|
||||
try {
|
||||
if (hasUni()) uni.setStorageSync(key, value)
|
||||
else memory.set(key, value)
|
||||
} catch (e) {
|
||||
/* 忽略存储异常 */
|
||||
}
|
||||
return value
|
||||
},
|
||||
remove(key) {
|
||||
try {
|
||||
if (hasUni()) uni.removeStorageSync(key)
|
||||
else memory.delete(key)
|
||||
} catch (e) {
|
||||
/* noop */
|
||||
}
|
||||
},
|
||||
clear() {
|
||||
memory.clear()
|
||||
try {
|
||||
if (hasUni() && uni.clearStorageSync) uni.clearStorageSync()
|
||||
} catch (e) {
|
||||
/* noop */
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
export default storage
|
||||
43
uniapp/common/utils/upload.js
普通文件
43
uniapp/common/utils/upload.js
普通文件
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 图片上传封装
|
||||
* - 真实环境:POST /admin/upload(multipart),返回 { url }
|
||||
* - Mock/无 uni 环境:直接返回本地临时路径,便于离线演示
|
||||
*/
|
||||
import config from '../config/index.js'
|
||||
import storage from './storage.js'
|
||||
|
||||
/**
|
||||
* 上传单张图片
|
||||
* @param {String} filePath 本地临时路径
|
||||
* @returns {Promise<{url:String, mock?:Boolean}>}
|
||||
*/
|
||||
export function uploadImage(filePath) {
|
||||
if (config.forceMock || typeof uni === 'undefined' || !uni.uploadFile) {
|
||||
return Promise.resolve({ url: filePath, mock: true })
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.uploadFile({
|
||||
url: `${config.baseURL}/${config.apiVersion}/admin/upload`,
|
||||
filePath,
|
||||
name: 'file',
|
||||
header: {
|
||||
Authorization: `Bearer ${storage.get(config.storageKeys.token, '')}`,
|
||||
'X-Client': 'fenleixinxi',
|
||||
},
|
||||
success(res) {
|
||||
try {
|
||||
const body = JSON.parse(res.data)
|
||||
if (body.code === 0 || body.code === 200) resolve(body.data || {})
|
||||
else reject(new Error(body.message || '上传失败'))
|
||||
} catch (e) {
|
||||
reject(new Error('上传响应解析失败'))
|
||||
}
|
||||
},
|
||||
fail(err) {
|
||||
reject(new Error((err && err.errMsg) || '上传失败'))
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default { uploadImage }
|
||||
在新工单中引用
屏蔽一个用户