萤火小程序商城---二次开发
萤火小程序商城是一款非常不错的小程序商城系统,我购买过他们的商业版,而且他们非常厉害,还开源了商城小程序。由于客户额外的功能需求,需要对商城进行二次开发。
下面是记录开发的过程:
1、uniapp网页版跨域问题(vue3)
安装好程序后,使用uniapp来做前端开发,运行后报跨域错误:

这个问题,折腾了好久,最后使用前端反向代理来完美实现:
首先看:config.js 里面的配置:
export default { // 系统名称 name: "云咖商城", /** * 后端api地址 (必填; 斜杠/结尾; 请确保能访问) * 例如: https://www.你的域名.com/index.php?s=/api/ */ apiUrl: "http://www.xxx.cn/index.php?s=/api/", /** * 是否启用商城设置缓存 * 将减少用户端重复请求; 正式运营时请设为true, 开启后商城设置同步前端需10分钟缓存 */ enabledSettingCache: true }
调整:
// config.js const env = process.env.NODE_ENV let apiUrl // 注意末尾必须带斜杠 if(env === 'development'){ // H5本地调试,走代理 apiUrl = '/proxy-api/index.php?s=/api/' }else{ // 小程序 / H5生产环境,写真实完整地址 apiUrl = 'http://www.xxx.cn/index.php?s=/api/' } export default { // 系统名称 name: "云咖商城", /** * 后端api地址 (必填; 斜杠/结尾; 请确保能访问) * 例如: https://www.你的域名.com/index.php?s=/api/ */ // apiUrl: "http://www.xxx.cn/index.php?s=/api/", apiUrl: apiUrl, /** * 是否启用商城设置缓存 * 将减少用户端重复请求; 正式运营时请设为true, 开启后商城设置同步前端需10分钟缓存 */ enabledSettingCache: true }
然后在vite.config.js 里面配置代理
配置前:
import path from 'path' import { defineConfig } from 'vite' import { multipleEntryFilePlugin } from 'vite-plugin-multiple-entries2' import uni from '@dcloudio/vite-plugin-uni' /** * @type {import('vite').UserConfig} */ export default defineConfig({ plugins: [ uni(), process.env.UNI_PLATFORM === 'h5' ? multipleEntryFilePlugin({ chunkName: 'config', entryPath: path.resolve(__dirname, './config.js'), entryFileName: 'config.js', // injectTo: 'body-prepend', crossorigin: true, }) : undefined, ] })
配置后:
import path from 'path' import { defineConfig } from 'vite' import { multipleEntryFilePlugin } from 'vite-plugin-multiple-entries2' import uni from '@dcloudio/vite-plugin-uni' /** * @type {import('vite').UserConfig} */ export default defineConfig({ plugins: [ uni(), process.env.UNI_PLATFORM === 'h5' ? multipleEntryFilePlugin({ chunkName: 'config', entryPath: path.resolve(__dirname, './config.js'), entryFileName: 'config.js', // injectTo: 'body-prepend', crossorigin: true, }) : undefined, ], server: { proxy: { "/proxy-api": { target: "http://www.xxx.com", changeOrigin: true, rewrite: path => path.replace(/^\/proxy-api/, '') } } } })
参考文档:
https://blog.csdn.net/qq_33240556/article/details/145589023
问题完美解决。
打完收工!

浙公网安备 33010602011771号