珠宝小程序前端代码
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.

411 lines
9.2 KiB

3 months ago
5 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 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. ### config.js
  61. 项目核心配置文件,包含以下配置:
  62. **1. 环境配置**
  63. ```javascript
  64. // 当前环境
  65. const type = 'prod'
  66. // 环境配置
  67. const config = {
  68. dev: {
  69. baseUrl: 'http://h5.xzaiyp.top/jewelry-admin',
  70. },
  71. prod: {
  72. baseUrl: 'https://jewelry-admin.hhlm1688.com/jewelry-admin',
  73. }
  74. }
  75. ```
  76. **2. 默认配置**
  77. ```javascript
  78. const defaultConfig = {
  79. // 腾讯地图Key
  80. mapKey: 'XMBBZ-BCPCV-SXPPQ-5Y7MY-PHZXK-YFFVU',
  81. // 阿里云OSS配置
  82. aliOss: {
  83. url: 'https://image.hhlm1688.com/',
  84. config: {
  85. region: 'oss-cn-guangzhou',
  86. accessKeyId: '***',
  87. accessKeySecret: '***',
  88. bucket: 'hanhaiimage',
  89. endpoint: 'oss-cn-shenzhen.aliyuncs.com',
  90. }
  91. }
  92. }
  93. ```
  94. **3. UI框架配置**
  95. ```javascript
  96. uni.$uv.setConfig({
  97. config: {
  98. unit: 'rpx' // 设置默认单位
  99. },
  100. })
  101. ```
  102. ## 核心模块详解
  103. ### 1. Mixins 混入
  104. #### 1.1 list.js - 列表数据加载混入
  105. 提供列表数据的加载、分页、下拉刷新、上拉加载更多等功能。
  106. **主要功能:**
  107. - 统一的分页参数处理
  108. - 下拉刷新和上拉加载更多
  109. - 数据加载状态管理
  110. **使用示例:**
  111. ```javascript
  112. // 在页面中使用list混入
  113. import listMixin from '@/mixins/list.js'
  114. export default {
  115. mixins: [listMixin],
  116. data() {
  117. return {
  118. // 指定API接口
  119. mixinsListApi: 'product.list'
  120. }
  121. }
  122. }
  123. ```
  124. #### 1.2 configList.js - 全局配置混入
  125. 已全局引入的配置管理混入,无需手动引入即可使用。
  126. **主要功能:**
  127. - 统一的分享配置
  128. - 全局配置管理
  129. - 用户信息关联
  130. **配置参数:**
  131. ```javascript
  132. // 分享配置示例
  133. this.Gshare.title = '分享标题'
  134. this.Gshare.path = '分享路径'
  135. ```
  136. ### 2. API 模块
  137. #### 2.1 http.js - 请求封装
  138. 统一的HTTP请求处理,包含:
  139. - 请求拦截器
  140. - 响应拦截器
  141. - 统一的错误处理
  142. - Token管理
  143. #### 2.2 api.js - 接口管理
  144. 统一管理API接口,支持模块化组织。API模块采用分层结构,便于维护和扩展。
  145. **目录结构:**
  146. ```
  147. api/
  148. ├── api.js # API统一出口
  149. ├── http.js # HTTP请求封装
  150. └── model/ # 业务模块API
  151. ├── product.js # 商品相关接口
  152. ├── order.js # 订单相关接口
  153. └── user.js # 用户相关接口
  154. ```
  155. **接口定义示例:**
  156. ```javascript
  157. // api/model/product.js
  158. export default {
  159. // GET请求示例
  160. list: {
  161. url: '/api/product/list',
  162. method: 'GET',
  163. loading: true // 显示加载提示
  164. },
  165. // POST请求示例
  166. create: {
  167. url: '/api/product/create',
  168. method: 'POST',
  169. loading: true // 显示加载提示
  170. auth : true,//效验登录
  171. debounce : 1000,//接口防抖,1s
  172. limit : 500,//接口限流,0.5s
  173. },
  174. }
  175. ```
  176. **调用接口示例:**
  177. ```javascript
  178. // 第一种写法:callback方式处理响应
  179. this.$api('product.list', {
  180. pageNo: 1,
  181. pageSize: 10,
  182. categoryId: '123'
  183. }, res => {
  184. // 处理列表数据
  185. })
  186. // 第二种写法:Promise方式处理响应
  187. this.$api('product.create', {
  188. name: '商品名称',
  189. price: 99.99,
  190. description: '商品描述'
  191. }).then(res => {
  192. if (res.code === 200) {
  193. // 创建成功
  194. uni.showToast({ title: '创建成功' })
  195. }
  196. })
  197. ```
  198. ### 3. 公共代码
  199. #### 3.1 工具函数 (utils)
  200. - authorize.js: 授权处理
  201. - pay.js: 微信网页支付相关
  202. - utils.js: 通用工具函数
  203. - timeUtils.js: 时间处理
  204. - position.js: 定位与位置计算
  205. - oss-upload: 阿里云OSS上传模块
  206. **使用示例:**
  207. ```javascript
  208. // 授权处理
  209. async preservationImg(img) {
  210. await this.$authorize('scope.writePhotosAlbum')
  211. //在执行$authorize之后,await下面的代码都是确保授权完成的情况下执行
  212. },
  213. // 时间格式化
  214. const formattedTime = this.$timeUtils.formatTime(new Date())
  215. // 微信网页支付调用
  216. import { wxPay } from '@/utils/pay'
  217. wxPay(orderData)
  218. ```
  219. #### 3.2 公共组件
  220. - navbar.vue: 自定义导航栏
  221. - tabbar.vue: 底部导航栏
  222. - productItem.vue: 商品列表项
  223. **使用示例:**
  224. ```html
  225. <template>
  226. <view>
  227. <navbar title="商品列表" />
  228. <product-item
  229. v-for="item in list"
  230. :key="item.id"
  231. :product="item"
  232. />
  233. </view>
  234. </template>
  235. ```
  236. #### 3.3 OSS上传模块
  237. **配置说明:**
  238. 项目使用阿里云OSS进行文件存储,相关配置位于config.js中:
  239. **使用示例:**
  240. 1. 单文件上传
  241. ```javascript
  242. export default {
  243. methods: {
  244. onUpload(file) {
  245. this.$Oss.ossUpload(file.path).then(url => {
  246. this.filePath = url
  247. })
  248. }
  249. }
  250. }
  251. ```
  252. 2. 在uv-upload组件中使用
  253. ```html
  254. <template>
  255. <uv-upload
  256. :fileList="fileList"
  257. @afterRead="afterRead"
  258. @delete="deleteImage"
  259. name="1"
  260. multiple
  261. :maxCount="maxCount"
  262. ></uv-upload>
  263. </template>
  264. <script>
  265. export default {
  266. data() {
  267. return {
  268. fileList: [],
  269. maxCount: 9
  270. }
  271. },
  272. methods: {
  273. // 新增图片
  274. afterRead(e) {
  275. e.file.forEach(file => {
  276. this.$Oss.ossUpload(file.url).then(url => {
  277. this.fileList.push({
  278. url
  279. })
  280. })
  281. })
  282. },
  283. // 删除图片
  284. deleteImage(e) {
  285. this.fileList.splice(e.index, 1)
  286. },
  287. }
  288. }
  289. </script>
  290. ```
  291. **注意事项:**
  292. 1. 上传前请确保OSS配置正确
  293. 2. 建议对上传文件大小进行限制
  294. 3. 支持的文件类型:图片、视频、文档等
  295. 4. 上传失败时会抛出异常,请做好错误处理
  296. ## 最佳实践
  297. ### 1. 列表页面开发
  298. ```javascript
  299. // pages/product/list.vue
  300. import listMixin from '@/mixins/list.js'
  301. export default {
  302. mixins: [listMixin],
  303. data() {
  304. return {
  305. mixinsListApi: 'product.list',
  306. }
  307. },
  308. methods: {
  309. // 分类切换
  310. onCategoryChange(categoryId) {
  311. this.queryParams.categoryId = categoryId
  312. this.getData()
  313. }
  314. }
  315. }
  316. ```
  317. ### 2. 详情页面开发
  318. ```javascript
  319. // pages/product/detail.vue
  320. import configMixin from '@/mixins/configList.js'
  321. export default {
  322. mixins: [configMixin],
  323. data() {
  324. return {
  325. productId: '',
  326. detail: {}
  327. }
  328. },
  329. onLoad(options) {
  330. this.productId = options.id
  331. this.getDetail()
  332. },
  333. methods: {
  334. getDetail() {
  335. this.$api('product.detail', {
  336. id: this.productId
  337. }, res => {
  338. this.detail = res.result
  339. // 设置分享信息
  340. this.Gshare.title = this.detail.name
  341. this.Gshare.path = `/pages/product/detail?id=${this.productId}`
  342. })
  343. }
  344. }
  345. }
  346. ```
  347. ## 注意事项
  348. 1. 使用mixins时注意命名冲突
  349. 2. API调用建议统一使用this.$api方式
  350. 3. 页面开发建议继承相应的混入来复用通用功能
  351. ## 常见问题
  352. 1. 列表加载失败
  353. - 检查mixinsListApi是否正确配置
  354. - 确认网络请求是否正常
  355. - 查看请求参数格式是否正确