• uni-app基于vue实现商城小程序


    目录

    一、前言

    二、功能效果图

    1.首页

    2.分类

    ​3.活动

    4.我的

    ​5.商品详情

    6.购物车

    三、代码实现

    1.项目结构截图 uni-app,Hbuilder

    2.首页源码

    3.数据模拟通讯

    四、总结


    一、前言

    参考“网易严选”小程序

    项目采用传统vue项目结构,即uni-app打包和运行成小程序,使用HBuilder开发工具开发项目,通过运行启动“微信开发者工具”完成项目启动。

    二、功能效果图

    1.首页

     

    2.分类

     3.活动

    4.我的

     5.商品详情

    6.购物车

    代码动态实现商品购物车的增删改查等操作。

    三、代码实现

    1.项目结构截图 uni-app,Hbuilder

    2.首页源码

    1. <template>
    2. <view class="Header">
    3. <view class="myHeader">
    4. <view class="logos">
    5. 网易严选
    6. view>
    7. <view class="searchs">
    8. <uni-icons type="search" size="30">uni-icons><input type="text" name="" id=""
    9. placeholder="请输入想搜索的商品..." @tap="goShowContract">
    10. view>
    11. <view class="search-button">
    12. <view class="button" @click="onSearch">取消view>
    13. view>
    14. view>
    15. <view class="backgroundBanner">
    16. <view class="banner">
    17. <swiper autoplay="true" :interval="2000" :duration="500" circular="true" indicator-active-color="#fff"
    18. easing-function="true" indicator-dots='true'>
    19. <swiper-item v-for="(item, index) in bannerList" :key="index">
    20. <img :src="item.url">img>
    21. swiper-item>
    22. swiper>
    23. view>
    24. view>
    25. <view class="cates">
    26. <view class="cate-item" v-for="(cateItem,cateIndex) in cateList" :key="cateIndex" @tap="selPage(cateItem)">
    27. <image :src="cateItem.cateUrl">image>
    28. <text>{{cateItem.cateName}}text>
    29. view>
    30. view>
    31. <hr>
    32. <view class="time">
    33. <view class="timeTop">
    34. <view class="logo">
    35. <image src="../../static/zhong.jpg" mode="">image>
    36. <text>限时秒购text>
    37. view>
    38. <view class="timeFinish">
    39. <text>距结束text>
    40. <uni-countdown :day="1" :hour="2" :minute="30" :second="0" color="#FFFFFF"
    41. background-color="#007AFF" />
    42. view>
    43. view>
    44. <view class="timeBottom">
    45. <view class="timeItem" v-for="(timeItem,timeIndex) in timeList" :key="timeIndex">
    46. <image :src="timeItem.timeUrl">image>
    47. <view class="timeText">
    48. <text class="timeText1">¥{{timeItem.timeText1}}text>
    49. <text class="timeText2">¥{{timeItem.timeText2}}text>
    50. view>
    51. view>
    52. view>
    53. <hr>
    54. <scroll-view scroll-x class="myNav">
    55. <view v-for="(item,index) in indexTypes" :key="index" @tap="selType(item)" :class="'item '+(typeId===item.id?'active':'')" style='font-size: 30rpx;'>{{item.name}}view>
    56. scroll-view>
    57. <index-type ref="indexType">index-type>
    58. view>
    59. view>
    60. view>
    61. template>
    62. <script>
    63. import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
    64. import indexType from '@/components/index_type.vue'
    65. export default {
    66. components:{
    67. indexType,
    68. },
    69. data() {
    70. return {
    71. typeId: 0,
    72. cateList: [{
    73. cateId: 0,
    74. cateUrl: "../../static/zhong.jpg",
    75. cateName: '限时秒杀',
    76. path:''
    77. },
    78. {
    79. cateId: 0,
    80. cateUrl: '../../static/che.jpg',
    81. cateName: '断货清仓',
    82. path:'./okoko'
    83. },
    84. {
    85. cateId: 0,
    86. cateUrl: "../../static/yi.jpg",
    87. cateName: '宝藏女装',
    88. },
    89. {
    90. cateId: 0,
    91. cateUrl: "../../static/b3.jpg",
    92. cateName: '多多果园',
    93. },
    94. {
    95. cateId: 0,
    96. cateUrl: "../../static/9.9.jpg",
    97. cateName: '9块9特卖',
    98. },
    99. {
    100. cateId: 0,
    101. cateUrl: "../../static/1a.jpg",
    102. cateName: '签到',
    103. },
    104. {
    105. cateId: 0,
    106. cateUrl: "../../static/yao.jpg",
    107. cateName: '医药馆',
    108. },
    109. {
    110. cateId: 0,
    111. cateUrl: "../../static/54e.jpg",
    112. cateName: '省钱月卡',
    113. },
    114. ],
    115. // //商品实例图
    116. timeList: [{
    117. time: 0,
    118. timeUrl: '../../static/7b.jpg',
    119. timeText1: '388',
    120. timeText2: '720',
    121. },
    122. {
    123. time: 0,
    124. timeUrl: '../../static/345d.jpg',
    125. timeText1: '18',
    126. timeText2: '39',
    127. },
    128. {
    129. time: 0,
    130. timeUrl: '../../static/3d.jpg',
    131. timeText1: '3888',
    132. timeText2: '3889',
    133. },
    134. {
    135. time: 0,
    136. timeUrl: '../../static/bing.jpg',
    137. timeText1: '12.9',
    138. timeText2: '18',
    139. },
    140. ],
    141. bannerList: [{
    142. id: 1,
    143. url: '../../static/temp1.jpg'
    144. },
    145. {
    146. id: 2,
    147. url: '../../static/temp2.jpg'
    148. },
    149. {
    150. id: 3,
    151. url: '../../static/temp3.jpg'
    152. },
    153. {
    154. id: 4,
    155. url: '../../static/temp4.jpg'
    156. },
    157. {
    158. id: 5,
    159. url: '../../static/temp5.jpg'
    160. },
    161. ],
    162. }
    163. },
    164. computed:{
    165. ...mapState([
    166. 'products',
    167. 'indexTypes'
    168. ])
    169. },
    170. mounted() {
    171. this.typeId = this.indexTypes[0].id;
    172. this.$refs.indexType.initProduct(this.indexTypes[0]);
    173. },
    174. methods: {
    175. goShowContract() {
    176. uni.navigateTo({
    177. url: '/pages/index/show-contract'
    178. })
    179. },
    180. selPage(cateItem){
    181. // uni.navigateTo({
    182. // url:cateItem.path,
    183. // })
    184. },
    185. //选择分类
    186. selType(item){
    187. this.typeId = item.id;
    188. this.$refs.indexType.initProduct(item);
    189. }
    190. }
    191. }
    192. script>
    193. <style lang="scss" scoped>
    194. * {
    195. margin: 0;
    196. padding: 0;
    197. }
    198. .myHeader {
    199. display: flex;
    200. padding: 10rpx;
    201. align-items: center;
    202. .logos {
    203. width: 140rpx;
    204. height: 40rpx;
    205. }
    206. .searchs {
    207. border: 1px solid black;
    208. border-radius: 10rpx;
    209. flex: 1;
    210. margin: 0 10rpx;
    211. width: 80rpx;
    212. padding-left: 14rpx;
    213. display: flex;
    214. align-items: center;
    215. padding: 4rpx 10rpx;
    216. vertical-align: top;
    217. }
    218. .btn {
    219. width: 80rpx;
    220. text-align: center;
    221. }
    222. }
    223. .myNav {
    224. white-space: nowrap;
    225. display: flex;
    226. color: #333;
    227. box-sizing: border-box;
    228. height: 80rpx;
    229. .item {
    230. flex: 1;
    231. margin: 10rpx 20rpx;
    232. display: inline-block;
    233. &.active {
    234. color: #ff5555;
    235. border-bottom: 2px solid #ff5555;
    236. font-size: 24px;
    237. }
    238. }
    239. .item:hover {
    240. display: inline-block;
    241. color: #ff5555;
    242. border-bottom: 2px solid #ff5555;
    243. }
    244. }
    245. .backgroundBanner {
    246. padding: 0 10rpx;
    247. box-sizing: border-box;
    248. }
    249. .banner {
    250. width: 100%;
    251. margin: 0 auto;
    252. }
    253. .banner swiper {
    254. height: calc(750rpx / 1.9); //calc(屏幕宽度 / (图片宽度 / 图片高度))
    255. }
    256. .banner img {
    257. width: 100%;
    258. height: 100%;
    259. }
    260. .cates {
    261. width: 100%;
    262. height: 200rpx;
    263. background-color: #fff;
    264. display: flex;
    265. align-items: center;
    266. justify-content: space-around;
    267. .cate-item {
    268. width: 20%;
    269. height: 160rpx;
    270. display: flex;
    271. flex-direction: column;
    272. align-items: center;
    273. justify-content: space-around;
    274. image {
    275. width: 100rpx;
    276. height: 100rpx;
    277. }
    278. }
    279. }
    280. .cates {
    281. width: 100%;
    282. height: 400rpx;
    283. background-color: #fff;
    284. display: flex;
    285. align-items: center;
    286. justify-content: space-around;
    287. flex-wrap: wrap;
    288. .cate-item {
    289. width: 22%;
    290. height: 160rpx;
    291. display: flex;
    292. flex-direction: column;
    293. align-items: center;
    294. justify-content: space-around;
    295. // flex-flow: wrap;
    296. image {
    297. width: 100rpx;
    298. height: 100rpx;
    299. }
    300. }
    301. }
    302. .time {
    303. width: 100%;
    304. background-color: #fff;
    305. margin-top: 20rpx;
    306. .timeTop {
    307. height: 80rpx;
    308. display: flex;
    309. justify-content: space-between;
    310. align-items: center;
    311. margin-left: 20rpx;
    312. .logo {
    313. image {
    314. width: 30rpx;
    315. height: 30rpx;
    316. margin-right: 10rpx;
    317. }
    318. text {
    319. font-size: 16px;
    320. }
    321. }
    322. .timeFinish {
    323. margin-right: 20rpx;
    324. display: flex;
    325. align-items: center;
    326. text {
    327. font-size: 14px;
    328. }
    329. }
    330. }
    331. .timeBottom {
    332. width: 100%;
    333. display: flex;
    334. justify-content: space-around;
    335. flex-wrap: wrap;
    336. .timeItem {
    337. width: 25%;
    338. justify-content: space-around;
    339. display: flex;
    340. flex-direction: column;
    341. align-items: center;
    342. box-shadow: rgba(99, 99, 99, 0.2) 0px 2px 8px 0px;
    343. image {
    344. width: 160rpx;
    345. height: 160rpx;
    346. }
    347. .timeText {
    348. display: flex;
    349. justify-content: space-between;
    350. font-size: 14px;
    351. .timeText2 {
    352. color: #9F9F9F;
    353. text-decoration: line-through;
    354. }
    355. }
    356. }
    357. }
    358. }
    359. style>

    3.数据模拟通讯

    通过vuex实现数据通讯共享和管理

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. Vue.use(Vuex)
    4. const store = new Vuex.Store({
    5. state: {
    6. //公共的变量,这里的变量不能随便修改,只能通过触发mutations的方法才能改变
    7. //首页分类
    8. indexTypes:[
    9. {id: 1,name: '商品推荐'},
    10. {id: 2,name: '配饰'},
    11. {id: 3,name: '包包'},
    12. {id: 4,name: '鞋子'},
    13. {id: 5,name: '裙子'},
    14. {id: 6,name: '饮品'},
    15. {id: 7,name: '衣服'}
    16. ],
    17. //分类菜单
    18. allTypes:[
    19. {id: 11,name: '推荐'},
    20. {id: 12,name: '箱包'},
    21. {id: 13,name: '男装'},
    22. {id: 14,name: '女装'},
    23. {id: 15,name: '鞋子'},
    24. {id: 16,name: '裤子'},
    25. {id: 17,name: '食品'},
    26. {id: 18,name: '维修'},
    27. {id: 19,name: '机票'},
    28. {id: 20,name: '图书'},
    29. {id: 21,name: '宠物'},
    30. ],
    31. //活动商品 商品主键id
    32. activeProducts:[
    33. 1,2,3,4,5
    34. ],
    35. //全部商品
    36. products: [{
    37. id:1,
    38. typeIds:[1,11],
    39. cover: require('../../static/0fd.jpg'),
    40. imgList: [
    41. 'https://img11.360buyimg.com/n1/s450x450_jfs/t1/71831/14/25245/39959/6487c621Fca760cf2/769dd8bcf43d82c1.jpg',
    42. 'https://img.pddpic.com/mms-material-img/2022-08-11/cd9c638d-dac2-474a-aad2-0f141c146102.jpeg.a.jpeg?imageMogr2/quality/90/format/webp',
    43. 'https://img.pddpic.com/mms-material-img/2022-08-11/b0827f91-083a-4b5b-9461-64e6d6789fcf.jpeg.a.jpeg?imageMogr2/quality/90/format/webp'
    44. ],
    45. name: '联想拯救者Y9000P i7/RTX3060 2022款16英寸电竞游戏笔记本电脑',
    46. title: '电脑',
    47. price: 2000,
    48. oldprice: 5000,
    49. saleNumMonth: 120,
    50. saleNumTotal: 2000,
    51. types: [{
    52. name: '类型',
    53. items: [{
    54. id: 1,
    55. name: '13.3英寸 M1芯片 8+7核 8G+256G'
    56. },
    57. {
    58. id: 2,
    59. name: '13.3英寸 M1芯片 8+7核 8G+512G'
    60. },
    61. {
    62. id: 3,
    63. name: '13.3英寸 M1芯片 8+7核 16G+256G'
    64. },
    65. {
    66. id:4,
    67. name: '13.3英寸 M1芯片 8+7核 16G+512G'
    68. }
    69. ],
    70. },
    71. {
    72. name: '数量',
    73. items: [{
    74. id: 1,
    75. name: '英特尔官方21代'
    76. }, {
    77. id: 2,
    78. name: '英特尔官方18代'
    79. }, {
    80. id: 3,
    81. name: '英特尔官方19代'
    82. }]
    83. }
    84. ]
    85. },
    86. {
    87. id:2,
    88. typeIds:[1,11],
    89. cover: require('../../static/13d.jpg'),
    90. imgList: [
    91. "https://img.pddpic.com/mms-material-img/2021-12-10/6b514989-49fb-4112-b636-33fc82cb1641.jpeg.a.jpeg?imageMogr2/quality/90/format/webp",
    92. "https://img.pddpic.com/mms-material-img/2022-01-05/6fbb05da-e1ba-4bee-985a-300cdd7ad2e9.jpeg.a.jpeg?imageMogr2/quality/90/format/webp",
    93. "https://img.pddpic.com/mms-material-img/2021-12-10/21e55b15-a17d-4668-b8a9-9b8616f11cb2.jpeg.a.jpeg?imageMogr2/quality/90/format/webp"
    94. ],
    95. name: '保鲜膜([食品级认证)',
    96. title:'保鲜膜',
    97. price: 12.99,
    98. oldprice: 20.80,
    99. saleNumMonth: 120,
    100. saleNumTotal: 2000,
    101. types: [{
    102. name: '型号',
    103. items: [{
    104. id: 1,
    105. name: '1卷宽30厘米6.25斤'
    106. },
    107. {
    108. id: 2,
    109. name: '1卷宽50厘米7斤'
    110. },
    111. {
    112. id: 3,
    113. name: '整箱宽50厘米4卷28斤'
    114. },
    115. {
    116. id:4,
    117. name: '整箱4卷宽50厘米重40斤约720米'
    118. }
    119. ],
    120. },
    121. {
    122. name: '数量',
    123. items: [{
    124. id: 1,
    125. name: '3包'
    126. }, {
    127. id: 2,
    128. name: '5包'
    129. }, {
    130. id: 3,
    131. name: '10包'
    132. }]
    133. }
    134. ]
    135. },
    136. {
    137. id:3,
    138. typeIds:[1,11],
    139. cover: require('../../static/8537c.jpg'),
    140. imgList: [
    141. "https://img.pddpic.com/mms-material-img/2022-05-28/3329312a-b971-4990-af80-6e15113b0600.jpeg.a.jpeg?imageMogr2/quality/90/format/webp",
    142. "https://img.pddpic.com/mms-material-img/2022-05-28/e12d134d-c2d8-4aec-8dc3-0e9ff80befd5.jpeg.a.jpeg?imageMogr2/quality/90/format/webp",
    143. "https://img.pddpic.com/mms-material-img/2022-05-28/1510e448-2d5b-4398-a692-ff679e2587e8.jpeg.a.jpeg?imageMogr2/quality/90/format/webp",
    144. "https://img.pddpic.com/mms-material-img/2022-05-28/248a3ffb-b4ce-4529-a7a4-8f0c97a4b331.jpeg.a.jpeg?imageMogr2/quality/90/format/webp"
    145. ],
    146. name: '男士斜挎包潮牌ins工装风日系休闲男包大容量单肩包个性邮差包潮',
    147. title:'斜挎包',
    148. price: 33.00,
    149. oldprice: 40.75,
    150. saleNumMonth: 120,
    151. saleNumTotal: 2000,
    152. types: [{
    153. name: '颜色',
    154. items: [
    155. {
    156. id: 1,
    157. name: '大板黑色'
    158. },
    159. {
    160. id: 2,
    161. name: '灰色'
    162. },
    163. {
    164. id: 3,
    165. name: '蓝色'
    166. },
    167. {
    168. id:4,
    169. name: '白色'
    170. }
    171. ],
    172. },
    173. ]
    174. },
    175. {
    176. id:4,
    177. typeIds:[1,11],
    178. cover: require('../../static/fa98.jpg'),
    179. imgList: [
    180. "https://img.pddpic.com/openapi/images/2020-04-27/4cd766b88b0a4e9e8fbebaa221de7b58.jpg?imageMogr2/quality/90/format/webp",
    181. "https://img.pddpic.com/openapi/images/2020-04-27/c2090bace7f938f02819fca05e17c4e7.jpg?imageMogr2/quality/90/format/webp",
    182. "https://img.pddpic.com/openapi/images/2020-04-27/1492ab8ba1e76cca387583c80180d65e.jpg?imageMogr2/quality/90/format/webp",
    183. "https://img.pddpic.com/openapi/images/2020-04-27/13b1af9176580b3e7a035871f4456594.jpg?imageMogr2/quality/90/format/webp",
    184. "https://img.pddpic.com/openapi/images/2020-04-27/793db583c0d598c13124af5991f7794e.jpg?imageMogr2/quality/90/format/webp"
    185. ],
    186. name: '高级感韩版原创ins个性多层斜挎包2023夏季新款包包女单肩链条包',
    187. title:'休闲 仙女',
    188. price: 38.39,
    189. oldprice: 57.80,
    190. saleNumMonth: 120,
    191. saleNumTotal: 2000,
    192. types: [{
    193. name: '颜色',
    194. items: [
    195. {
    196. id: 1,
    197. name: '大板黑色'
    198. },
    199. {
    200. id: 2,
    201. name: '灰色'
    202. },
    203. {
    204. id: 3,
    205. name: '红色'
    206. },
    207. {
    208. id:4,
    209. name: '粉色'
    210. },
    211. {
    212. id:5,
    213. name: '白色'
    214. },
    215. ],
    216. },
    217. ]
    218. },
    219. {
    220. id:5,
    221. typeIds:[1,11],
    222. cover: 'https://img.pddpic.com/goods/images/2020-06-10/52fd0f70-671c-4ab0-a867-094d8301d4e5.jpeg?imageMogr2/quality/90/format/webp',
    223. imgList: [
    224. "https://img.pddpic.com/goods/images/2020-06-10/52fd0f70-671c-4ab0-a867-094d8301d4e5.jpeg?imageMogr2/quality/90/format/webp",
    225. "https://img.pddpic.com/mms-material-img/2020-06-04/a12fe65d-ee77-4b1c-b183-7ce735fb1342.jpeg?imageMogr2/quality/90/format/webp",
    226. "https://img.pddpic.com/mms-material-img/2020-06-04/cf0eb9da-efb7-4f24-9703-6450b792b4ec.jpeg?imageMogr2/quality/90/format/webp",
    227. "https://img.pddpic.com/mms-material-img/2020-06-04/911b4c63-1212-46e7-a1eb-038b983553b0.jpeg?imageMogr2/quality/90/format/webp",
    228. "https://img.pddpic.com/goods/images/2020-06-10/f7e45e61-0a72-4ac9-8f56-f422327799b9.jpeg?imageMogr2/quality/90/format/webp"
    229. ],
    230. name: '【驯龙士】夏季新款冰丝套装男休闲运动短袖男士大码七分裤两件套',
    231. title:'驯龙士',
    232. price: 99.99,
    233. oldprice: 120.80,
    234. saleNumMonth: 120,
    235. saleNumTotal: 2000,
    236. types: [{
    237. name: '颜色',
    238. items: [
    239. {
    240. id: 1,
    241. name: '军绿色'
    242. },
    243. {
    244. id: 2,
    245. name: '灰色'
    246. },
    247. {
    248. id: 3,
    249. name: '黑色'
    250. },
    251. ],
    252. },
    253. ]
    254. }
    255. ],
    256. },
    257. mutations: {
    258. //相当于同步的操作
    259. getarray(state, count) {
    260. var shuffled = state.timesList.slice(0),
    261. i = state.timesList.length,
    262. min = i - count,
    263. temp,
    264. index;
    265. while (i-- > min) {
    266. index = Math.floor((i + 1) * Math.random());
    267. temp = shuffled[index];
    268. shuffled[index] = shuffled[i];
    269. shuffled[i] = temp;
    270. }
    271. return shuffled.slice(min);
    272. }
    273. },
    274. actions: {
    275. //相当于异步的操作,不能直接改变state的值,只能通过触发mutations的方法才能改变
    276. }
    277. })
    278. export default store

    四、总结

    项目功能完整,后续可能将不断升级。

    关注作者,及时了解更多好项目!

    作者主页也有更多好项目分享!

    获取源码或如需帮助,可通过博客后面名片+作者即可!

     其他作品集合

    1. 《Springboot+Spring Security+OAuth2+redis+mybatis-plus+mysql+vue+elementui实现请假考勤系统》
    2. 《vue+element实现电商商城礼品代发网,商品、订单管理》
    3. 《vue+vant2完美实现香奈儿移动端商城网站》
    4. 《vue+elementui实现联想购物商城,样式美观大方》
    5. 《vue+elementui实现英雄联盟道具城》
    6. 《vue+elementui实现app布局小米商城,样式美观大方,功能完整》
    7. 《vue完美模拟pc版快手,实现短视频,含短视频详情播放》
    8. 《vue+element实现美观大方好看的音乐网站,仿照咪咕音乐网》     
  • 相关阅读:
    风力发电机液压偏航控制系统设计
    JavaScript-操作BOM对象
    错字修改 | 布署1个中文文文本拼蟹纠错模型
    深入浅出排序算法之归并排序
    华为OD机试 - 滑动窗口最大和
    VMware虚拟机如何设置网络
    JavaScript中的基础知识挑战
    工作中何如来合理分配核心线程数?
    F1C100S自制开发板调试过程
    使用docker安装本地pdf工具集合Stirling-PDF
  • 原文地址:https://blog.csdn.net/lucky_fang/article/details/134269531