promoProduct.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. <template>
  2. <view class="scrollPage">
  3. <!-- head -->
  4. <view class="scrollPage-header">
  5. <view :style="{height:statusBarHeight+'px'}"></view>
  6. <view class="header-title">
  7. <view class="header-left" @click="goBack">
  8. <image style="width: 16px; height: 16px;margin-right:3px;" mode="aspectFit" src="./static/arrow-left.png"></image>
  9. <text>返回</text>
  10. </view>
  11. <view class="header-title"><text class="ellipsis-one" overflow="ellipsis">{{title}}</text></view>
  12. <view class="header-right"></view>
  13. </view>
  14. </view>
  15. <!-- body -->
  16. <scroll-view
  17. class="scrollPage-body"
  18. :style="{height: (screenHeight - statusBarHeight - safeAreaBottom - 94) + 'px'}"
  19. scroll-y="true"
  20. type="custom"
  21. @scrolltolower="reachBottom">
  22. <!-- 搜索,吸顶 -->
  23. <sticky-header>
  24. <view class="search-head">
  25. <uni-search-bar radius="100" placeholder="请输入产品名称/轮胎规格" clearButton="auto" cancelButton="none" @confirm="searchList" @clear="searchList" />
  26. </view>
  27. </sticky-header>
  28. <!-- 产品列表 -->
  29. <productItem
  30. v-for="(item,index) in list"
  31. :key="index"
  32. :list="item"
  33. :index="index"
  34. :itemWidth="(screenWidth*0.94-screenWidth*0.03)*0.5"
  35. :gap="screenWidth*0.03"
  36. @chooseProduct="chooseProduct"
  37. ></productItem>
  38. <!-- loading -->
  39. <view class="loading-bar" v-if="list.length && (loading||loadEnd)">{{loadText}}</view>
  40. <view class="empty-bar" v-if="total==0&&!loading">
  41. <image mode="aspectFit" :src="empty.imgUrl"></image>
  42. <view>{{empty.tip}}</view>
  43. </view>
  44. </scroll-view>
  45. <!-- footer -->
  46. <view class="scrollPage-footer">
  47. <view>
  48. <view class="bottom-bar-left">
  49. <view class="bottom-bar-left-item">
  50. <view class="bottom-bar-left-item-icon" @click="openChoose">
  51. <image style="width: 20px; height: 20px;" mode="aspectFit" src="./static/cart.png"></image>
  52. <text class="badge" v-if="totalNum">{{totalNum>99?'99+':totalNum}}</text>
  53. </view>
  54. </view>
  55. <view class="bottom-bar-left-item">
  56. <!-- 合计:<view class="price-txt">¥<text>{{totalAmount}}</text></view> -->
  57. </view>
  58. </view>
  59. <view :class="'bottom-bar-right'" @click="submitOrder">
  60. <view class="bottom-bar-right-item">去结算</view>
  61. </view>
  62. </view>
  63. <view :style="{height:safeAreaBottom+'px'}"></view>
  64. </view>
  65. <!-- 已选产品 -->
  66. <page-container
  67. :show="showChoosePopu"
  68. :round="true"
  69. :z-index="10"
  70. position="bottom"
  71. custom-style="height:80%"
  72. @afterenter="openRender"
  73. @afterleave="closePopu">
  74. <view class="popup-body" v-if="showSwipeAction">
  75. <scroll-view
  76. :style="{width:screenWidth + 'px',height:'100%'}"
  77. scroll-y="true"
  78. type="custom"
  79. >
  80. <sticky-header>
  81. <view class="choose-title">
  82. <view class="choose-left">
  83. <view>已选<text class="p-nums">{{chooseLength}}</text>款产品</view>
  84. <button @click="claerChoose" size="mini" type="warn" plain>清空列表</button>
  85. <button class="editBtn" @click="editChoose=!editChoose" size="mini" type="primary" plain>{{editChoose?'完成':'编辑'}}</button>
  86. </view>
  87. <view class="choose-close" @click="showChoosePopu=false">
  88. <icon type="clear" size="24"></icon>
  89. </view>
  90. </view>
  91. </sticky-header>
  92. <chooseProductItem
  93. v-for="(item, index) in showList"
  94. :key="item.id"
  95. :index="index"
  96. :list="item"
  97. :fristLength="fristLength"
  98. :pageSize="choosePageSize"
  99. :edit="editChoose"
  100. @changeQty="changeQty"
  101. @remove="removeChoose">
  102. </chooseProductItem>
  103. <view style="min-height: 180rpx;"></view>
  104. </scroll-view>
  105. <view class="choose-footer">
  106. <view>
  107. <view class="choose-footer-left">
  108. <view class="choose-footer-left-item">
  109. <!-- 总数:<view><text class="p-nums">{{totalNum}}</text></view> -->
  110. </view>
  111. <view class="choose-footer-left-item">
  112. 合计:<view class="price-txt">¥<text>{{totalAmount}}</text></view>
  113. </view>
  114. </view>
  115. <view class="choose-footer-right bg-1" @click="submitOrder">
  116. <view class="choose-footer-right-item">确认采购</view>
  117. </view>
  118. </view>
  119. <view :style="{height:safeAreaBottom+'px'}"></view>
  120. </view>
  121. </view>
  122. </page-container>
  123. </view>
  124. </template>
  125. <script>
  126. import {
  127. mapState,
  128. mapMutations,
  129. } from 'vuex'
  130. import { queryPromoProductByPage } from '@/api/video.js'
  131. import { purchaseSave } from '@/api/purchase.js'
  132. import productItem from '@/pagesB/components/productItemSkline.vue'
  133. import chooseProductItem from '@/pagesB/components/chooseProductItemSkline.vue'
  134. export default {
  135. components:{
  136. productItem,
  137. chooseProductItem
  138. },
  139. data() {
  140. return {
  141. title: '促销产品列表',
  142. pageNo:1,
  143. pageSize:16,
  144. total:0,
  145. list:[],
  146. loading:false,
  147. loadEnd: false,
  148. loadText: '加载中...',
  149. empty: {
  150. tip: '暂无产品',
  151. imgUrl: '/static/nodata.png'
  152. },
  153. queryWord:'',
  154. promoActiveSn:'',
  155. screenWidth: 750,
  156. screenHeight: 0,
  157. statusBarHeight: 44,
  158. safeAreaBottom: 0,
  159. // 已选弹框
  160. showChoosePopu: false,
  161. showSwipeAction: false,
  162. chooseList:[],
  163. editChoose: false,
  164. choosePageSize: 12,
  165. // 数量,合计
  166. chooseLength: 0, //已选产品总款数
  167. totalAmount: 0, //已选产品总金额
  168. totalNum: 0, //已选产品总数量
  169. };
  170. },
  171. computed: {
  172. ...mapState(['hasLogin']),
  173. userInfo(){
  174. return this.$store.state.vuex_userInfo
  175. },
  176. fristLength(){
  177. return this.chooseList.length > 0 ? this.chooseList[0].length : 0
  178. },
  179. showList(){
  180. return this.showSwipeAction ? this.chooseList : []
  181. }
  182. },
  183. onLoad(opts) {
  184. this.title = opts.title||'促销产品列表'
  185. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight
  186. this.screenWidth = uni.getSystemInfoSync().windowWidth
  187. this.screenHeight = uni.getSystemInfoSync().windowHeight
  188. this.safeAreaBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
  189. // 清空临时数据
  190. this.$store.state.vuex_tempData = null
  191. this.promoActiveSn = opts.promoActiveSn
  192. // 查询列表
  193. this.getList()
  194. this.chooseList = this.getCacheList()
  195. this.getChooseHeji()
  196. },
  197. onUnload(){
  198. // 存储已选列表值
  199. const hasCache = this.$store.state.vuex_cartList.find(k => k.sn == this.promoActiveSn)
  200. if(hasCache){
  201. hasCache.list = this.chooseList
  202. }else{
  203. this.$store.state.vuex_cartList.push({sn:this.promoActiveSn,list: this.chooseList})
  204. }
  205. this.$u.vuex('vuex_cartList', this.$store.state.vuex_cartList)
  206. // 清空数据
  207. this.chooseList = null
  208. this.list = null
  209. },
  210. methods: {
  211. goBack() {
  212. uni.navigateBack();
  213. },
  214. // 从缓存获取数据
  215. getCacheList(){
  216. const cacheList = this.$store.state.vuex_cartList.find(k => k.sn == this.promoActiveSn)
  217. return cacheList ? JSON.parse(JSON.stringify(cacheList.list)) : []
  218. },
  219. // 滚动到底部
  220. reachBottom() {
  221. if(!this.loading && !this.loadEnd){
  222. this.pageNo++
  223. this.getList()
  224. }
  225. },
  226. // 搜索
  227. searchList(event){
  228. this.loadEnd = false
  229. this.loadText = '加载中...'
  230. this.queryWord = event.value
  231. this.list = []
  232. this.pageNo = 1
  233. this.getList()
  234. },
  235. // 列表查询
  236. getList(){
  237. this.loading = true
  238. queryPromoProductByPage({
  239. pageNo: this.pageNo,
  240. pageSize: this.pageSize,
  241. queryWord: this.queryWord,
  242. promoActiveSn: this.promoActiveSn
  243. }).then(res => {
  244. this.loading = false
  245. if(res.status == 200){
  246. this.total = res.data.count
  247. if(this.total){
  248. const list = res.data.list || []
  249. const ret = []
  250. // 更新已选状态
  251. list.forEach(k => {
  252. // 如果筛选或分页后,更新页面列表产品数量
  253. const row = this.getChooseItemById(k.id)
  254. const hasChecked = row.p>=0 && row.s>=0
  255. ret.push({
  256. id: k.id,
  257. cost: k.cost,
  258. checked: hasChecked ? true : false,
  259. qty: hasChecked ? this.chooseList[row.p][row.s].qty : 0,
  260. productCode: k.productCode,
  261. productSn: k.productSn,
  262. productImage: k.productImage,
  263. productName: k.productName,
  264. productOrigCode: k.productOrigCode,
  265. promoActiveSn: k.promoActiveSn,
  266. promoRuleSn: k.promoRuleSn,
  267. promoRuleValue: k.promoRuleValue,
  268. promoRuleType: k.promoRuleType,
  269. })
  270. })
  271. // 追加数据
  272. this.list.push(ret)
  273. // 判断是否最后一页
  274. const maxPages = Math.ceil(this.total / this.pageSize)
  275. if(this.pageNo >= maxPages){
  276. this.loadEnd = true
  277. this.loadText = '没有更多了'
  278. }
  279. }else{
  280. this.loadEnd = false
  281. this.loadText = '暂无产品'
  282. }
  283. }
  284. }).catch(err => {
  285. this.loading = false
  286. })
  287. },
  288. // 根据id查找chooseList
  289. getChooseItemById(id){
  290. const a = this.chooseList.findIndex(k => k.find(s=> s.id == id))
  291. const b = a>=0 ? this.chooseList[a].findIndex(s=> s.id == id) : -1
  292. return {p:a,s:b}
  293. },
  294. // 计算总款数、合计、数量
  295. getChooseHeji(){
  296. this.chooseLength = 0
  297. this.totalAmount = 0
  298. this.totalNum = 0
  299. this.chooseList.forEach(key => {
  300. this.chooseLength += key.length // 总款数
  301. key.forEach(item => {
  302. this.totalAmount += item.cost * item.qty // 总金额
  303. this.totalNum += item.qty // 总数量
  304. })
  305. })
  306. },
  307. // 选择产品
  308. chooseProduct(item,index){
  309. const rowItem = this.list[index].find(k => k.id == item.id)
  310. // 新加
  311. if(!rowItem.checked){
  312. rowItem.checked = true
  313. rowItem.qty = 1
  314. // 存入二维数组中
  315. const chooselens = this.chooseList.length
  316. if(chooselens){
  317. const firstRow = this.chooseList[0]
  318. if(firstRow.length<this.choosePageSize){
  319. firstRow.unshift(this.$u.deepClone(rowItem))
  320. }else{
  321. this.chooseList.unshift([this.$u.deepClone(rowItem)])
  322. }
  323. }else{
  324. this.chooseList.unshift([this.$u.deepClone(rowItem)])
  325. }
  326. }else{
  327. rowItem.qty = rowItem.qty + 1
  328. // 已添加,则增加已选列表中的数量
  329. const row = this.getChooseItemById(rowItem.id)
  330. if(row.p>=0 && row.s>=0){
  331. this.chooseList[row.p][row.s].qty = this.chooseList[row.p][row.s].qty + 1
  332. this.chooseList[row.p].splice()
  333. }
  334. }
  335. // 合计
  336. this.getChooseHeji()
  337. },
  338. // 打开已选产品列表
  339. openChoose(){
  340. this.submitOk = false
  341. if(this.chooseLength){
  342. uni.showLoading({
  343. title:'加载中...',
  344. mask: true
  345. })
  346. this.showChoosePopu = true
  347. }else{
  348. this.$u.toast(`请选择产品`);
  349. }
  350. },
  351. // 渲染已选产品列表
  352. openRender(){
  353. this.showSwipeAction = true
  354. setTimeout(()=>{
  355. uni.hideLoading()
  356. },300)
  357. },
  358. // 关闭后
  359. closePopu(){
  360. setTimeout(()=>{
  361. this.showSwipeAction = false
  362. },300)
  363. this.showChoosePopu = false
  364. this.editChoose = false
  365. uni.hideLoading()
  366. // 提交成功
  367. if(this.submitOk){
  368. uni.navigateTo({
  369. url: "/pagesB/procureOrder"
  370. })
  371. }
  372. },
  373. // 确定清空已选列表
  374. claerChoose(){
  375. const _this = this
  376. uni.showModal({
  377. title: '提示',
  378. content: '确定清空已选列表?',
  379. confirmText: '确定',
  380. success(res) {
  381. if(res.confirm){
  382. uni.showLoading({
  383. title: '正在清空...',
  384. mask: true
  385. })
  386. _this.clearChooseData()
  387. }
  388. }
  389. })
  390. },
  391. // 清空已选数据
  392. clearChooseData(){
  393. this.showChoosePopu = false
  394. this.chooseList = []
  395. // 合计
  396. this.getChooseHeji()
  397. // 重置已选状态
  398. this.list.forEach(key => {
  399. key.filter(item => item.checked).forEach(item=>{
  400. item.checked = false
  401. item.qty = 0
  402. })
  403. })
  404. },
  405. // 根据id 获取页面list项
  406. getListItemById(id){
  407. const aindex = this.list.findIndex(key => key.find(a => a.id == id))
  408. const aindex2 = aindex >=0 ? this.list[aindex].findIndex(a => a.id == id) : -1
  409. return {p:aindex,s:aindex2}
  410. },
  411. // 已选产品变更数量
  412. changeQty(val,id){
  413. const crow = this.getChooseItemById(id)
  414. if(crow.p>=0&&crow.s>=0){
  415. this.chooseList[crow.p][crow.s].qty = val
  416. this.chooseList[crow.p].splice()
  417. // 合计
  418. this.getChooseHeji()
  419. }
  420. // 更新页面产品数据
  421. const row = this.getListItemById(id)
  422. if(row.p>=0 && row.s>=0){
  423. this.list[row.p][row.s].qty = val
  424. this.list[row.p].splice()
  425. }
  426. },
  427. //已选产品删除
  428. removeChoose(id){
  429. // 删除项
  430. const crow = this.getChooseItemById(id)
  431. if(crow.p>=0&&crow.s>=0){
  432. this.chooseList[crow.p].splice(crow.s, 1);
  433. // 合计
  434. this.getChooseHeji()
  435. }
  436. // 如果没有数据了
  437. if(this.chooseList.length==0){
  438. this.clearChooseData()
  439. }
  440. // 更新列表已选状态
  441. const row = this.getListItemById(id)
  442. if(row.p>=0 && row.s>=0){
  443. this.list[row.p][row.s].qty = 0
  444. this.list[row.p][row.s].checked = false
  445. this.list[row.p].splice()
  446. }
  447. },
  448. // 去结算
  449. submitOrder(){
  450. if(this.showChoosePopu){
  451. uni.showLoading({
  452. title: '提交中...',
  453. mask: true
  454. })
  455. // 展开成一维数组
  456. const detailList = []
  457. this.chooseList.forEach(key => {
  458. key.forEach(item => {
  459. detailList.push({
  460. promoActiveSn: this.promoActiveSn,
  461. promoRuleSn:item.promoRuleSn,
  462. productSn:item.productSn,
  463. productCode:item.productCode,
  464. qty:item.qty,
  465. price:item.cost,
  466. giveAmount:item.promoRuleValue
  467. })
  468. })
  469. })
  470. purchaseSave({
  471. promoActiveSn: this.promoActiveSn,
  472. detailList: detailList
  473. }).then(res => {
  474. if(res.status == 200){
  475. res.data.detailList = []
  476. this.$store.state.vuex_tempData = res.data
  477. this.clearChooseData()
  478. this.submitOk = true
  479. }else{
  480. uni.showToast({
  481. title: res.message,
  482. icon: 'none'
  483. })
  484. }
  485. })
  486. }else{
  487. this.openChoose()
  488. }
  489. }
  490. }
  491. }
  492. </script>
  493. <style lang="less">
  494. /* 显示一行省略号 */
  495. .ellipsis-one{
  496. white-space: nowrap;
  497. text-overflow: ellipsis;
  498. overflow: hidden;
  499. word-break: break-all;
  500. }
  501. .scrollPage{
  502. height: 100vh;
  503. display: flex;
  504. flex-direction: column;
  505. .loading-bar{
  506. text-align: center;
  507. padding: 10px 0;
  508. color: #999999;
  509. }
  510. .empty-bar{
  511. display: flex;
  512. flex-direction: column;
  513. align-items: center;
  514. justify-content: center;
  515. padding: 20px 0;
  516. image{
  517. width: 100px;
  518. height: 100px;
  519. }
  520. view{
  521. font-size: 14px;
  522. color: #999999;
  523. margin-top: 10px;
  524. }
  525. }
  526. .scrollPage-header{
  527. z-index: 1;
  528. .header-title{
  529. display: flex;
  530. align-items: center;
  531. justify-content: space-between;
  532. padding: 0 10px;
  533. height: 44px;
  534. background-color: #FFFFFF;
  535. box-shadow: 0px 1px 0px 0px #E6E6E6;
  536. .header-title{
  537. font-size: 16px;
  538. color: #333333;
  539. max-width: 70%;
  540. }
  541. .header-left{
  542. display: flex;
  543. align-items: center;
  544. text{
  545. font-size: 14px;
  546. color: #333333;
  547. margin-left: 5px;
  548. }
  549. }
  550. .header-right{
  551. width: 50px;
  552. }
  553. }
  554. }
  555. .scrollPage-body{
  556. flex-grow: 1;
  557. height: 100%;
  558. .search-head{
  559. background: #fff;
  560. }
  561. }
  562. .scrollPage-footer{
  563. background: #fff;
  564. z-index: 200;
  565. > view{
  566. height: 50px;
  567. display: flex;
  568. align-items: center;
  569. justify-content: space-between;
  570. font-size: 32upx;
  571. }
  572. .bottom-bar-left{
  573. padding: 0 30upx;
  574. flex-grow: 1;
  575. display: flex;
  576. align-items: center;
  577. }
  578. .bottom-bar-left-item{
  579. padding-right: 20rpx;
  580. font-size: 24rpx;
  581. display: flex;
  582. align-items: center;
  583. .price-txt{
  584. color: red;
  585. display: flex;
  586. align-items: center;
  587. text{
  588. font-size: 36rpx;
  589. }
  590. }
  591. }
  592. .bottom-bar-left-item-icon{
  593. position: relative;
  594. background: #2196F3;
  595. width: 80rpx;
  596. height: 80rpx;
  597. border-radius: 100%;
  598. display: flex;
  599. align-items: center;
  600. justify-content: center;
  601. .badge{
  602. background: #f44336;
  603. color: #fff;
  604. position: absolute;
  605. left: 50rpx;
  606. top: -3px;
  607. font-size: 12px;
  608. display: flex;
  609. align-items: center;
  610. justify-content: center;
  611. border-radius: 50rpx;
  612. padding: 0 10rpx;
  613. }
  614. }
  615. .bottom-bar-right{
  616. width: 35%;
  617. height: 100%;
  618. background: #066cff;
  619. display: flex;
  620. align-items: center;
  621. justify-content: center;
  622. .bottom-bar-right-item{
  623. color: #fff;
  624. }
  625. }
  626. .bg-0{
  627. background: #066cff;
  628. }
  629. .bg-1{
  630. background: #f44336;
  631. }
  632. }
  633. .choose-title{
  634. display: flex;
  635. align-items:center;
  636. justify-content: space-between;
  637. background: #fff;
  638. width: 100%;
  639. border-bottom: 1px solid #eee;
  640. border-radius: 40rpx 40rpx 0 0;
  641. > view{
  642. padding: 20rpx 30rpx;
  643. }
  644. .choose-left{
  645. display: flex;
  646. align-items:center;
  647. flex-grow: 1;
  648. > view{
  649. display: flex;
  650. align-items:center;
  651. flex-grow: 1;
  652. }
  653. button{
  654. padding: 0 10px;
  655. height: auto;
  656. font-size: 12px;
  657. margin-left: 10px;
  658. }
  659. .editBtn{
  660. color: #2196F3;
  661. border-color: #2196F3;
  662. }
  663. }
  664. .p-nums{
  665. color: #2196F3;
  666. margin: 0 6rpx;
  667. }
  668. }
  669. .popup-body{
  670. height: 100%;
  671. position: relative;
  672. }
  673. .choose-footer{
  674. position: fixed;
  675. bottom: 0;
  676. left: 0;
  677. background: #fff;
  678. z-index: 100;
  679. width: 100%;
  680. > view{
  681. width: 100%;
  682. height: 100rpx;
  683. display: flex;
  684. align-items: center;
  685. justify-content: space-between;
  686. }
  687. .choose-footer-left{
  688. flex-grow: 1;
  689. display: flex;
  690. align-items: center;
  691. padding: 0 30rpx;
  692. > view{
  693. font-size: 28rpx;
  694. }
  695. .choose-footer-left-item{
  696. display: flex;
  697. align-items: center;
  698. margin-right: 10px;
  699. .price-txt{
  700. color: red;
  701. display: flex;
  702. align-items: center;
  703. text{
  704. font-size: 36rpx;
  705. }
  706. }
  707. }
  708. }
  709. .choose-footer-right{
  710. width: 35%;
  711. height: 100%;
  712. background: #2196F3;
  713. display: flex;
  714. align-items: center;
  715. justify-content: center;
  716. .choose-footer-right-item{
  717. color: #fff;
  718. }
  719. }
  720. .bg-1{
  721. background: #f44336;
  722. }
  723. }
  724. }
  725. </style>