敢为人鲜小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

461 lines
11 KiB

2 months ago
4 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. # 珠宝商城项目文档
  2. ## 项目概述
  3. 本项目是一个基于uni-app开发的珠宝商城小程序,采用Vue框架开发,集成了完整的商城功能模块。
  4. ## 目录结构
  5. ```
  6. ├── api # API接口目录
  7. │ ├── api.js # API统一出口
  8. │ ├── http.js # HTTP请求封装
  9. │ └── model # 业务模块API
  10. ├── components # 公共组件
  11. ├── mixins # 混入文件
  12. ├── pages # 页面文件
  13. ├── static # 静态资源
  14. ├── store # Vuex状态管理
  15. ├── utils # 工具函数
  16. └── uni_modules # uni-app插件模块
  17. ```
  18. ## 分包结构说明
  19. ### pages_order分包
  20. 分包是小程序优化加载性能的重要手段,pages_order作为独立分包,包含以下模块:
  21. ```
  22. ├── auth # 认证相关页面
  23. │ ├── loginAndRegisterAndForgetPassword.vue # 登录注册
  24. │ ├── wxLogin.vue # 微信登录
  25. │ └── wxUserInfo.vue # 微信用户信息
  26. ├── components # 分包内公共组件
  27. │ ├── address/ # 地址选择组件
  28. │ ├── areaSelector/ # 区域选择器
  29. │ └── product/ # 商品相关组件
  30. ├── home # 首页相关
  31. │ ├── addEnterprise.vue # 添加企业
  32. │ ├── contact.vue # 联系我们
  33. │ ├── introduce.vue # 介绍页面
  34. │ ├── journalism.vue # 新闻资讯
  35. │ └── notice.vue # 公告
  36. ├── mine # 我的模块
  37. │ ├── address.vue # 收货地址
  38. │ ├── balance.vue # 余额
  39. │ ├── commission.vue # 佣金
  40. │ ├── coupon.vue # 优惠券
  41. │ ├── memberCenter.vue # 会员中心
  42. │ └── more... # 更多功能页面
  43. ├── order # 订单模块
  44. │ ├── createOrder.vue # 创建订单
  45. │ ├── orderDetail.vue # 订单详情
  46. │ └── giftList.vue # 礼品列表
  47. ├── product # 商品模块
  48. │ └── productDetail.vue # 商品详情
  49. └── static # 分包静态资源
  50. ├── address/ # 地址相关图片
  51. ├── auth/ # 认证相关图片
  52. ├── coupon/ # 优惠券图片
  53. └── more... # 其他静态资源
  54. ```
  55. **分包特点:**
  56. - 静态资源就近原则:分包相关的图片等静态资源存放在分包目录下,避免主包体积过大
  57. - 模块化组织:按功能模块划分目录,便于维护和管理
  58. - 组件复用:分包内的通用组件集中管理,提高代码复用性
  59. ## 核心模块详解
  60. ### 1. Mixins 混入
  61. #### 1.1 list.js - 列表数据加载混入
  62. 提供列表数据的加载、分页、下拉刷新、上拉加载更多等功能。
  63. **主要功能:**
  64. - 统一的分页参数处理
  65. - 下拉刷新和上拉加载更多
  66. - 数据加载状态管理
  67. **使用示例:**
  68. ```javascript
  69. // 在页面中使用list混入
  70. import listMixin from '@/mixins/list.js'
  71. export default {
  72. mixins: [listMixin],
  73. data() {
  74. return {
  75. // 指定API接口
  76. mixinsListApi: 'product.list'
  77. }
  78. }
  79. }
  80. ```
  81. #### 1.2 configList.js - 全局配置混入
  82. 已全局引入的配置管理混入,无需手动引入即可使用。
  83. **主要功能:**
  84. - 统一的分享配置
  85. - 全局配置管理
  86. - 用户信息关联
  87. **配置参数:**
  88. ```javascript
  89. // 分享配置示例
  90. this.Gshare.title = '分享标题'
  91. this.Gshare.path = '分享路径'
  92. ```
  93. ### 2. API 模块
  94. #### 2.1 http.js - 请求封装
  95. 统一的HTTP请求处理,包含:
  96. - 请求拦截器
  97. - 响应拦截器
  98. - 统一的错误处理
  99. - Token管理
  100. #### 2.2 api.js - 接口管理
  101. 统一管理API接口,支持模块化组织。API模块采用分层结构,便于维护和扩展。
  102. **目录结构:**
  103. ```
  104. api/
  105. ├── api.js # API统一出口
  106. ├── http.js # HTTP请求封装
  107. └── model/ # 业务模块API
  108. ├── product.js # 商品相关接口
  109. ├── order.js # 订单相关接口
  110. └── user.js # 用户相关接口
  111. ```
  112. **接口定义示例:**
  113. ```javascript
  114. // api/model/product.js
  115. export default {
  116. // GET请求示例
  117. list: {
  118. url: '/api/product/list',
  119. method: 'GET',
  120. loading: true // 显示加载提示
  121. },
  122. // POST请求示例
  123. create: {
  124. url: '/api/product/create',
  125. method: 'POST',
  126. loading: true // 显示加载提示
  127. auth : true,//效验登录
  128. debounce : 1000,//接口防抖,1s
  129. limit : 500,//接口限流,0.5s
  130. },
  131. }
  132. ```
  133. **调用接口示例:**
  134. ```javascript
  135. // 第一种写法:callback方式处理响应
  136. this.$api('product.list', {
  137. pageNo: 1,
  138. pageSize: 10,
  139. categoryId: '123'
  140. }, res => {
  141. // 处理列表数据
  142. })
  143. // 第二种写法:Promise方式处理响应
  144. this.$api('product.create', {
  145. name: '商品名称',
  146. price: 99.99,
  147. description: '商品描述'
  148. }).then(res => {
  149. if (res.code === 200) {
  150. // 创建成功
  151. uni.showToast({ title: '创建成功' })
  152. }
  153. })
  154. ```
  155. ### 3. 公共代码
  156. #### 3.1 工具函数 (utils)
  157. - authorize.js: 授权处理
  158. - pay.js: 微信网页支付相关
  159. - utils.js: 通用工具函数
  160. - timeUtils.js: 时间处理
  161. - position.js: 定位与位置计算
  162. - oss-upload: 阿里云OSS上传模块
  163. #### 3.2 OSS上传模块
  164. **配置说明:**
  165. 项目使用阿里云OSS进行文件存储,相关配置位于config.js中:
  166. ```javascript
  167. const defaultConfig = {
  168. aliOss: {
  169. url: 'https://image.hhlm1688.com/',
  170. config: {
  171. region: 'oss-cn-guangzhou',
  172. accessKeyId: '***',
  173. accessKeySecret: '***',
  174. bucket: 'hanhaiimage',
  175. endpoint: 'oss-cn-shenzhen.aliyuncs.com',
  176. }
  177. }
  178. }
  179. ```
  180. **使用示例:**
  181. 1. 单文件上传
  182. ```javascript
  183. // 在组件中使用
  184. import { uploadFile } from '@/utils/oss-upload'
  185. export default {
  186. methods: {
  187. async onUpload(file) {
  188. try {
  189. const result = await uploadFile(file)
  190. console.log('上传成功:', result.url)
  191. } catch (error) {
  192. console.error('上传失败:', error)
  193. }
  194. }
  195. }
  196. }
  197. ```
  198. 2. 多文件上传
  199. ```javascript
  200. // 在组件中使用
  201. import { uploadFiles } from '@/utils/oss-upload'
  202. export default {
  203. methods: {
  204. async onUploadMultiple(files) {
  205. try {
  206. const results = await uploadFiles(files)
  207. results.forEach(result => {
  208. console.log('文件上传成功:', result.url)
  209. })
  210. } catch (error) {
  211. console.error('上传失败:', error)
  212. }
  213. }
  214. }
  215. }
  216. ```
  217. 3. 在uv-upload组件中使用
  218. ```html
  219. <template>
  220. <uv-upload
  221. :fileList="fileList"
  222. @afterRead="afterRead"
  223. @delete="deletePic"
  224. name="1"
  225. multiple
  226. :maxCount="maxCount"
  227. ></uv-upload>
  228. </template>
  229. <script>
  230. import { uploadFile } from '@/utils/oss-upload'
  231. export default {
  232. data() {
  233. return {
  234. fileList: [],
  235. maxCount: 9
  236. }
  237. },
  238. methods: {
  239. // 新增图片
  240. async afterRead(event) {
  241. let lists = [].concat(event.file)
  242. let fileListLen = this.fileList.length
  243. lists.map((item) => {
  244. this.fileList.push({
  245. ...item,
  246. status: 'uploading',
  247. message: '上传中'
  248. })
  249. })
  250. for (let i = 0; i < lists.length; i++) {
  251. const result = await uploadFile(lists[i])
  252. let item = this.fileList[fileListLen + i]
  253. this.fileList.splice(fileListLen + i, 1, Object.assign(item, {
  254. status: 'success',
  255. message: '',
  256. url: result
  257. }))
  258. }
  259. },
  260. // 删除图片
  261. deletePic(event) {
  262. this.fileList.splice(event.index, 1)
  263. }
  264. }
  265. }
  266. </script>
  267. ```
  268. **注意事项:**
  269. 1. 上传前请确保OSS配置正确
  270. 2. 建议对上传文件大小进行限制
  271. 3. 支持的文件类型:图片、视频、文档等
  272. 4. 上传失败时会抛出异常,请做好错误处理
  273. **使用示例:**
  274. ```javascript
  275. // 授权处理
  276. async preservationImg(img) {
  277. await this.$authorize('scope.writePhotosAlbum')
  278. //在执行$authorize之后,await下面的代码都是确保授权完成的情况下执行
  279. },
  280. // 时间格式化
  281. const formattedTime = this.$timeUtils.formatTime(new Date())
  282. // 微信网页支付调用
  283. import { wxPay } from '@/utils/pay'
  284. wxPay(orderData)
  285. ```
  286. #### 3.2 公共组件
  287. - navbar.vue: 自定义导航栏
  288. - tabbar.vue: 底部导航栏
  289. - productItem.vue: 商品列表项
  290. **使用示例:**
  291. ```html
  292. <template>
  293. <view>
  294. <navbar title="商品列表" />
  295. <product-item
  296. v-for="item in list"
  297. :key="item.id"
  298. :product="item"
  299. />
  300. </view>
  301. </template>
  302. ```
  303. ## 最佳实践
  304. ### 1. 列表页面开发
  305. ```javascript
  306. // pages/product/list.vue
  307. import listMixin from '@/mixins/list.js'
  308. export default {
  309. mixins: [listMixin],
  310. data() {
  311. return {
  312. mixinsListApi: 'product.list',
  313. }
  314. },
  315. methods: {
  316. // 分类切换
  317. onCategoryChange(categoryId) {
  318. this.queryParams.categoryId = categoryId
  319. this.getData()
  320. }
  321. }
  322. }
  323. ```
  324. ### 2. 详情页面开发
  325. ```javascript
  326. // pages/product/detail.vue
  327. import configMixin from '@/mixins/configList.js'
  328. export default {
  329. mixins: [configMixin],
  330. data() {
  331. return {
  332. productId: '',
  333. detail: {}
  334. }
  335. },
  336. onLoad(options) {
  337. this.productId = options.id
  338. this.getDetail()
  339. },
  340. methods: {
  341. getDetail() {
  342. this.$api('product.detail', {
  343. id: this.productId
  344. }, res => {
  345. this.detail = res.result
  346. // 设置分享信息
  347. this.Gshare.title = this.detail.name
  348. this.Gshare.path = `/pages/product/detail?id=${this.productId}`
  349. })
  350. }
  351. }
  352. }
  353. ```
  354. ## 注意事项
  355. 1. 使用mixins时注意命名冲突
  356. 2. API调用建议统一使用this.$api方式
  357. 3. 页面开发建议继承相应的混入来复用通用功能
  358. ## 常见问题
  359. 1. 列表加载失败
  360. - 检查mixinsListApi是否正确配置
  361. - 确认网络请求是否正常
  362. - 查看请求参数格式是否正确
  363. ## 配置文件说明
  364. ### config.js
  365. 项目核心配置文件,包含以下配置:
  366. **1. 环境配置**
  367. ```javascript
  368. // 当前环境
  369. const type = 'prod'
  370. // 环境配置
  371. const config = {
  372. dev: {
  373. baseUrl: 'http://h5.xzaiyp.top/jewelry-admin',
  374. },
  375. prod: {
  376. baseUrl: 'https://jewelry-admin.hhlm1688.com/jewelry-admin',
  377. }
  378. }
  379. ```
  380. **2. 默认配置**
  381. ```javascript
  382. const defaultConfig = {
  383. // 腾讯地图Key
  384. mapKey: 'XMBBZ-BCPCV-SXPPQ-5Y7MY-PHZXK-YFFVU',
  385. // 阿里云OSS配置
  386. aliOss: {
  387. url: 'https://image.hhlm1688.com/',
  388. config: {
  389. region: 'oss-cn-guangzhou',
  390. accessKeyId: '***',
  391. accessKeySecret: '***',
  392. bucket: 'hanhaiimage',
  393. endpoint: 'oss-cn-shenzhen.aliyuncs.com',
  394. }
  395. }
  396. }
  397. ```
  398. **3. UI框架配置**
  399. ```javascript
  400. uni.$uv.setConfig({
  401. config: {
  402. unit: 'rpx' // 设置默认单位
  403. },
  404. })
  405. ```