productManagement.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <template>
  2. <view class="container">
  3. <!-- 搜索栏 -->
  4. <view class="search-bar">
  5. <view class="search-bar-con">
  6. <view class="search-input-wrapper">
  7. <uni-icons class="search-icon" type="search" size="18" color="#999"></uni-icons>
  8. <input
  9. class="search-input"
  10. v-model="searchVal"
  11. placeholder="请输入搜索内容"
  12. placeholder-class="placeholder"
  13. @input="onSearch"
  14. @confirm="onSearch"
  15. :focus="isFocus"
  16. />
  17. <view v-if="searchVal" class="clear-btn" @click="onClear">
  18. <uni-icons type="clear" size="18" color="#999"></uni-icons>
  19. </view>
  20. </view>
  21. </view>
  22. <!-- 分类标签 -->
  23. <up-tabs :list="tabList"
  24. @click="tabChange"
  25. lineColor="#F8C008"
  26. lineWidth="24"
  27. lineHeight="2"
  28. :itemStyle="{
  29. flex:1,
  30. height:'36px',
  31. }"
  32. :activeStyle="{
  33. color: '#F8C008',
  34. fontWeight: 'bold',
  35. transform: 'scale(1.05)'
  36. }"
  37. :inactiveStyle="{
  38. color: '#333333',
  39. transform: 'scale(1)'
  40. }"></up-tabs>
  41. <!-- 管理按钮 -->
  42. <view v-if="goodsList.length" class='nav acea-row row-between-wrapper'>
  43. <view>共 <text class='num font-color'>{{ goodsList.length }}</text>件商品</view>
  44. <view class='administrate acea-row row-center-wrapper' @click='manage'>
  45. {{ footerswitch ? '管理' : '取消' }}
  46. </view>
  47. </view>
  48. </view>
  49. <!-- 商品列表 -->
  50. <view class="product-list">
  51. <up-checkbox-group shape="circle" v-model="selectValue" @change="checkboxChange" class="centent" activeColor="#F8C008">
  52. <view class="product-card" v-for="(item, index) in goodsList" :key="item.id">
  53. <view class="product-header" @click="goDetail(item)">
  54. <!-- 多选框 -->
  55. <up-checkbox
  56. :name="item.id.toString()"
  57. :checked="item.checked"
  58. v-if="!footerswitch"
  59. style="margin-right: 10rpx;"
  60. activeColor="#F8C008"
  61. />
  62. <image class="product-image" :src="item.image" mode="aspectFill" />
  63. <view class="product-info">
  64. <view class="nameweight">
  65. <text class="product-name">{{ item.storeName }}</text>
  66. <text class="product-weight">{{ item.weight }}g</text>
  67. </view>
  68. <view class="nameweight">
  69. <view class="price-info">
  70. <text class="label">工费</text>
  71. <view class="value"><text class="unit">¥</text>{{ item.totalLaborCost }}<text class="unit">/g</text></view>
  72. </view>
  73. <view class="price-info">
  74. <text class="label">附加费</text>
  75. <view class="value"><text class="unit">¥</text>{{ item.additionalAmount }}</view>
  76. </view>
  77. </view>
  78. <view class="product-stats">
  79. <text class="stat">销量:{{ Number(item.sales || 0) + Number(item.ficti || 0) }}</text>
  80. <text class="stat">库存:{{ item.stock }}</text>
  81. </view>
  82. </view>
  83. </view>
  84. <view class="action-buttons">
  85. <button class="btn btn-delete" @click="deleteFn(item,index)">删除</button>
  86. <button class="btn btn-offline" v-show="params.isShow==1" @click="OffShellFn(item,index)">下架</button>
  87. <button class="btn btn-offline" v-show="params.isShow==0" @click="PutOnShellFn(item,index)">上架</button>
  88. <button class="btn btn-edit" v-show="params.isShow==0" @click="toEditProduct(item)">编辑</button>
  89. </view>
  90. </view>
  91. </up-checkbox-group>
  92. <view class="loadingicon acea-row row-center-wrapper" v-if="goodScroll">
  93. <text
  94. class="loading iconfont icon-jiazai"
  95. :hidden="loading == false"
  96. ></text>
  97. </view>
  98. <view class="no-data" v-if="isNoDataState">
  99. <image
  100. src="https://my-go-easy-im.oss-cn-shenzhen.aliyuncs.com/goeasy-im-%E6%B0%B4%E8%B4%9D%E5%95%86%E5%9F%8E/zhanwu_20250827104005_1720_6.png"
  101. />
  102. </view>
  103. <view class="mores-txt flex" v-if="!goodScroll && !isNoDataState">
  104. <text>我是有底线的</text>
  105. </view>
  106. </view>
  107. <!-- 底部操作栏 -->
  108. <view v-if="!footerswitch" class='footer acea-row row-between-wrapper'>
  109. <view>
  110. <up-checkbox-group shape="circle" @change="checkboxAllChange">
  111. <up-checkbox value="all" :checked="!!isAllSelect" activeColor="#F8C008" />
  112. <text class='checkAll'>全选</text>
  113. </up-checkbox-group>
  114. </view>
  115. <view class='button acea-row row-middle'>
  116. <button v-if="params.isShow==1" class='bnt' @click="batchOffShell">批量下架</button>
  117. <button v-if="params.isShow==0" class='bnt' @click="batchPutOnShell">批量上架</button>
  118. <!-- <button class='bnt cart-color delete-btn' @click="batchDelete">批量删除</button>-->
  119. </view>
  120. </view>
  121. </view>
  122. </template>
  123. <script setup>
  124. import {computed, ref,watch,onUnmounted} from 'vue'
  125. import { onLoad, onShow, onReachBottom } from "@dcloudio/uni-app";
  126. import { productsList, productPutOnShell, productOffShell, productDelete, batchStatus } from "@/api/merchant.js";
  127. import { useAppStore } from "@/stores/app";
  128. const appStore = useAppStore();
  129. // 管理相关状态
  130. const footerswitch = ref(true); // true: 显示正常模式, false: 显示管理模式
  131. const isAllSelect = ref(false); // 是否全选
  132. const selectValue = ref([]); // 选中的商品ID数组
  133. const tabList = ref([
  134. {name:'销售中',code:1},
  135. {name:'已下架',code:0}
  136. ])
  137. const searchVal = ref('')
  138. const goodsList = ref([]);
  139. const goodType = ref(1);
  140. // 防抖定时器和请求状态
  141. const searchTimer = ref(null);
  142. const isLoading = ref(false); // 防止重复请求
  143. // Pagination
  144. const params = ref({
  145. page: 1,
  146. limit: 10,
  147. isShow:1,
  148. });
  149. const loading = ref(false);
  150. const goodScroll = ref(true);
  151. const merchantInfo = ref({})
  152. const isFocus = ref(false)
  153. const isNoDataState = computed(() => {
  154. return goodsList.value.length === 0 && !loading.value;
  155. });
  156. onShow(() => {
  157. merchantInfo.value = appStore.userInfo.merchant;
  158. getGroomList()
  159. })
  160. // 切换管理状态
  161. const manage = () => {
  162. footerswitch.value = !footerswitch.value;
  163. // 退出管理状态时,清空选择
  164. if (footerswitch.value) {
  165. resetSelection();
  166. }
  167. }
  168. // 重置选择状态
  169. const resetSelection = () => {
  170. goodsList.value.forEach(item => {
  171. item.checked = false;
  172. });
  173. isAllSelect.value = false;
  174. selectValue.value = [];
  175. }
  176. // 多选框变化
  177. const checkboxChange = (values) => {
  178. goodsList.value.forEach(item => {
  179. item.checked = values.includes(item.id.toString());
  180. });
  181. selectValue.value = values;
  182. isAllSelect.value = goodsList.value.length === values.length;
  183. };
  184. // 全选变化
  185. const checkboxAllChange = (event) => {
  186. if (event.length > 0) {
  187. setAllSelectValue(1);
  188. } else {
  189. setAllSelectValue(0);
  190. }
  191. };
  192. // 设置全选状态
  193. const setAllSelectValue = (status) => {
  194. const selectValues = [];
  195. goodsList.value.forEach(item => {
  196. if (status) {
  197. item.checked = true;
  198. selectValues.push(String(item.id));
  199. isAllSelect.value = true;
  200. } else {
  201. item.checked = false;
  202. isAllSelect.value = false;
  203. }
  204. });
  205. selectValue.value = selectValues;
  206. console.log(selectValue.value)
  207. };
  208. // 检查是否有选中商品
  209. const checkHasSelected = () => {
  210. if (!selectValue.value || selectValue.value.length === 0) {
  211. uni.showToast({ title: '请先选择商品', icon: 'none' });
  212. return false;
  213. }
  214. return true;
  215. }
  216. // 批量下架
  217. const batchOffShell = async () => {
  218. if (!checkHasSelected()) return;
  219. uni.showModal({
  220. title: '提示',
  221. content: `确定要下架选中的${selectValue.value.length}件商品吗?`,
  222. success: async (res) => {
  223. if (res.confirm) {
  224. try {
  225. const { code, message } = await batchStatus({ ids: selectValue.value, status:0 });
  226. if (code === 200) {
  227. uni.showToast({ title: '批量下架成功', icon: 'success' });
  228. // 从列表中移除下架的商品
  229. goodsList.value = goodsList.value.filter(item => !selectValue.value.includes(item.id.toString()));
  230. resetSelection();
  231. footerswitch.value = true; // 退出管理模式
  232. } else {
  233. uni.showToast({ title: message || '操作失败', icon: 'none' });
  234. }
  235. } catch (err) {
  236. uni.showToast({ title: err.message || '操作失败', icon: 'none' });
  237. }
  238. }
  239. }
  240. });
  241. }
  242. // 批量上架
  243. const batchPutOnShell = async () => {
  244. if (!checkHasSelected()) return;
  245. uni.showModal({
  246. title: '提示',
  247. content: `确定要上架选中的${selectValue.value.length}件商品吗?`,
  248. success: async (res) => {
  249. if (res.confirm) {
  250. try {
  251. const { code, message } = await batchStatus({ ids: selectValue.value,status:1 });
  252. if (code === 200) {
  253. uni.showToast({ title: '批量上架成功', icon: 'success' });
  254. // 从列表中移除上架的商品
  255. console.log(selectValue.value)
  256. goodsList.value = goodsList.value.filter(item => !selectValue.value.includes(item.id.toString()));
  257. resetSelection();
  258. footerswitch.value = true; // 退出管理模式
  259. } else {
  260. uni.showToast({ title: message || '操作失败', icon: 'none' });
  261. }
  262. } catch (err) {
  263. uni.showToast({ title: err.message || '操作失败', icon: 'none' });
  264. }
  265. }
  266. }
  267. });
  268. }
  269. // 批量删除
  270. // const batchDelete = async () => {
  271. // if (!checkHasSelected()) return;
  272. //
  273. // uni.showModal({
  274. // title: '警告',
  275. // content: `确定要删除选中的${selectValue.value.length}件商品吗?此操作不可恢复!`,
  276. // success: async (res) => {
  277. // if (res.confirm) {
  278. // try {
  279. // const { code, message } = await batchDeleteApi({ ids: selectValue.value.join(',') });
  280. // if (code === 200) {
  281. // uni.showToast({ title: '批量删除成功', icon: 'success' });
  282. // // 从列表中移除删除的商品
  283. // goodsList.value = goodsList.value.filter(item => !selectValue.value.includes(item.id.toString()));
  284. // resetSelection();
  285. // footerswitch.value = true; // 退出管理模式
  286. // } else {
  287. // uni.showToast({ title: message || '操作失败', icon: 'none' });
  288. // }
  289. // } catch (err) {
  290. // uni.showToast({ title: err.message || '操作失败', icon: 'none' });
  291. // }
  292. // }
  293. // }
  294. // });
  295. // }
  296. const tabChange = (item) => {
  297. console.log('item',item)
  298. goodsList.value = [];
  299. loading.value = false;
  300. goodScroll.value = true;
  301. params.value.isShow = item.code;
  302. params.value.page = 1;
  303. getGroomList();
  304. // 切换标签时退出管理模式
  305. footerswitch.value = true;
  306. resetSelection();
  307. }
  308. const onSearch = (e) => {
  309. console.log(e)
  310. // 如果是input事件,只处理键盘输入,不直接触发搜索
  311. if (e && e.type === 'input') {
  312. clearTimeout(searchTimer.value);
  313. searchTimer.value = setTimeout(() => {
  314. doSearch();
  315. }, 500); // 防抖500ms
  316. } else {
  317. // 如果是confirm事件,立即搜索
  318. clearTimeout(searchTimer.value);
  319. doSearch();
  320. }
  321. }
  322. const doSearch = () => {
  323. if (isLoading.value) return; // 防止重复请求
  324. goodsList.value = [];
  325. loading.value = false;
  326. goodScroll.value = true;
  327. params.value.page = 1;
  328. getGroomList();
  329. // 搜索时退出管理模式
  330. footerswitch.value = true;
  331. resetSelection();
  332. }
  333. // 清除搜索内容
  334. const onClear = () => {
  335. console.log('清除搜索内容');
  336. searchVal.value = '';
  337. clearTimeout(searchTimer.value);
  338. doSearch(); // 直接搜索,不需要防抖
  339. }
  340. const getGroomList = async () => {
  341. if (!goodScroll.value) return;
  342. if (isLoading.value) return; // 防止重复请求
  343. try {
  344. isLoading.value = true;
  345. loading.value = true;
  346. params.value.merchantId = merchantInfo.value.id;
  347. params.value.keyword = searchVal.value;
  348. const { data } = await productsList(params.value);
  349. // 初始化选择状态
  350. const newList = data.list || [];
  351. newList.forEach(item => {
  352. item.checked = false;
  353. });
  354. goodsList.value = [...goodsList.value, ...newList];
  355. goodScroll.value = data.list.length >= params.value.limit;
  356. params.value.page++;
  357. } catch (err) {
  358. console.error(err);
  359. } finally {
  360. loading.value = false;
  361. isLoading.value = false;
  362. }
  363. };
  364. async function OffShellFn(obj,index) {
  365. const {code} = await productOffShell(obj.id);
  366. if(code == 200){
  367. uni.showToast({ title: "操作成功", icon: "none" });
  368. goodsList.value.splice(index,1);
  369. }
  370. }
  371. async function PutOnShellFn(obj,index) {
  372. const {code} = await productPutOnShell(obj.id);
  373. if(code == 200){
  374. uni.showToast({ title: "操作成功", icon: "none" });
  375. goodsList.value.splice(index,1);
  376. }
  377. }
  378. function toEditProduct (obj){
  379. uni.navigateTo({
  380. url:`/pages/merchantCenters/releaseProduct?id=${obj.id}`
  381. })
  382. }
  383. async function deleteFn(obj,index){
  384. uni.showModal({
  385. title: '警告',
  386. content: '确定要删除此商品吗?此操作不可恢复!',
  387. success: async (res) => {
  388. if (res.confirm) {
  389. const {code} = await productDelete(obj.id);
  390. if(code == 200){
  391. uni.showToast({ title: "操作成功", icon: "none" });
  392. goodsList.value.splice(index,1);
  393. }
  394. }
  395. }
  396. });
  397. }
  398. // 跳转商品详情页
  399. const goDetail = async (item) => {
  400. if(!footerswitch.value){
  401. let id = String(item.id);
  402. console.log(id)
  403. if(selectValue.value.includes(id)){
  404. goodsList.value.forEach(item=>{
  405. if(item.id==id){
  406. item.checked = false;
  407. }
  408. })
  409. selectValue.value = selectValue.value.filter(n=>n!=id)
  410. console.log(selectValue.value)
  411. }else{
  412. selectValue.value.push(id)
  413. console.log(selectValue.value)
  414. }
  415. isAllSelect.value = goodsList.value.length === selectValue.value.length;
  416. return
  417. }
  418. if(item.isShow){
  419. uni.navigateTo({
  420. url: "/pages/goods/goods_details/index?id="+item.id
  421. })
  422. }else{
  423. uni.showToast({title:'商品已下架',icon:'none'})
  424. }
  425. };
  426. onReachBottom(() => {
  427. getGroomList();
  428. });
  429. onUnmounted(() => {
  430. clearTimeout(searchTimer.value);
  431. });
  432. </script>
  433. <style scoped>
  434. /* 管理栏样式 */
  435. .nav {
  436. width: 100%;
  437. height: 90rpx;
  438. background-color: #fff;
  439. padding: 0 30rpx;
  440. box-sizing: border-box;
  441. font-size: 28rpx;
  442. color: #282828;
  443. border-top: 1px solid #f0f0f0;
  444. border-bottom: 1px solid #f0f0f0;
  445. margin-top: 20rpx;
  446. }
  447. .nav .administrate {
  448. width: 120rpx;
  449. height: 50rpx;
  450. border-radius: 8rpx;
  451. border: 1px solid #F8C008;
  452. color: #F8C008;
  453. font-size: 24rpx;
  454. }
  455. .num {
  456. color: #F8C008;
  457. font-weight: bold;
  458. margin: 0 4rpx;
  459. }
  460. /* 底部操作栏样式 */
  461. .footer {
  462. z-index: 9;
  463. width: 100%;
  464. height: 96rpx;
  465. background-color: #fff;
  466. position: fixed;
  467. padding: 0 30rpx;
  468. box-sizing: border-box;
  469. border-top: 1rpx solid #eee;
  470. border-bottom: 1px solid #EEEEEE;
  471. bottom: 0rpx;
  472. }
  473. .footer .checkAll {
  474. font-size: 28rpx;
  475. color: #282828;
  476. margin-left: 16rpx;
  477. line-height: 56rpx;
  478. }
  479. .footer .button {
  480. display: flex;
  481. gap: 20rpx;
  482. }
  483. .footer .button .bnt {
  484. font-size: 28rpx;
  485. color: #333333;
  486. border-radius: 8rpx;
  487. background-color: #F8C008;
  488. width: 180rpx;
  489. height: 60rpx;
  490. line-height: 60rpx;
  491. text-align: center;
  492. border: none;
  493. }
  494. .footer .button .delete-btn {
  495. color:#F52929;
  496. background-color: #FEEAEA;
  497. }
  498. /* 商品卡片调整 */
  499. .product-card {
  500. width: 100%;
  501. position: relative;
  502. background-color: #fff;
  503. border-radius: 16rpx;
  504. padding: 20rpx;
  505. margin: 10rpx 20rpx;
  506. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  507. }
  508. .product-header {
  509. display: flex;
  510. align-items: flex-start;
  511. margin-bottom: 20rpx;
  512. }
  513. .product-image {
  514. width: 180rpx;
  515. height: 180rpx;
  516. border-radius: 12rpx;
  517. margin-right: 20rpx;
  518. }
  519. .product-info {
  520. flex: 1;
  521. }
  522. .nameweight {
  523. display: flex;
  524. justify-content: space-between;
  525. align-items: center;
  526. margin-bottom: 20rpx;
  527. }
  528. .product-name {
  529. font-size: 28rpx;
  530. font-weight: bold;
  531. color: #333;
  532. flex: 1;
  533. margin-right: 20rpx;
  534. }
  535. .product-weight {
  536. font-size: 24rpx;
  537. color: #999;
  538. background: #f5f5f5;
  539. padding: 4rpx 12rpx;
  540. border-radius: 8rpx;
  541. }
  542. .price-info {
  543. display: flex;
  544. align-items: center;
  545. margin-bottom: 10rpx;
  546. }
  547. .price-info .label {
  548. font-size: 24rpx;
  549. color: #666;
  550. margin-right: 10rpx;
  551. min-width: 60rpx;
  552. }
  553. .price-info .value {
  554. font-size: 28rpx;
  555. color: #e64340;
  556. font-weight: bold;
  557. }
  558. .price-info .unit {
  559. font-size: 20rpx;
  560. }
  561. .product-stats {
  562. display: flex;
  563. gap: 30rpx;
  564. margin-top: 10rpx;
  565. }
  566. .product-stats .stat {
  567. font-size: 24rpx;
  568. color: #999;
  569. }
  570. .action-buttons {
  571. display: flex;
  572. justify-content: flex-start;
  573. align-items: center;
  574. gap: 20rpx;
  575. padding-top: 10rpx;
  576. //border-top: 1rpx solid #f0f0f0;
  577. }
  578. .btn {
  579. font-size: 28rpx;
  580. border-radius: 8rpx;
  581. background: transparent;
  582. flex: 1;
  583. height: 60rpx;
  584. line-height: 60rpx;
  585. }
  586. .btn-delete {
  587. color: #F52929;
  588. background-color: #FEEAEA;
  589. }
  590. .btn-offline {
  591. color: #333333;
  592. background-color: #F5F7FA;
  593. }
  594. .btn-edit {
  595. color: #333333;
  596. background-color: #F8C008;
  597. }
  598. /* 搜索栏样式 */
  599. .search-bar {
  600. background-color: #fff;
  601. position: sticky;
  602. top: 0;
  603. z-index: 100;
  604. padding: 20rpx 30rpx 0;
  605. }
  606. .search-bar-con {
  607. padding-bottom: 20rpx;
  608. }
  609. .search-input-wrapper {
  610. display: flex;
  611. align-items: center;
  612. background-color: #f5f5f5;
  613. border-radius: 50rpx;
  614. padding: 0 20rpx;
  615. height: 70rpx;
  616. }
  617. .search-icon {
  618. margin-right: 10rpx;
  619. }
  620. .search-input {
  621. flex: 1;
  622. height: 100%;
  623. font-size: 28rpx;
  624. color: #333;
  625. }
  626. .placeholder {
  627. color: #999;
  628. font-size: 28rpx;
  629. }
  630. .clear-btn {
  631. width: 40rpx;
  632. height: 40rpx;
  633. border-radius: 50%;
  634. background: #e0e0e0;
  635. display: flex;
  636. align-items: center;
  637. justify-content: center;
  638. margin-left: 10rpx;
  639. }
  640. /* 加载状态 */
  641. .loadingicon {
  642. text-align: center;
  643. color: #999;
  644. font-size: 24rpx;
  645. padding: 30rpx 0;
  646. }
  647. /* 无数据状态 */
  648. .no-data {
  649. display: flex;
  650. justify-content: center;
  651. padding: 100rpx 0;
  652. }
  653. .no-data image {
  654. width: 300rpx;
  655. height: 300rpx;
  656. }
  657. .mores-txt {
  658. text-align: center;
  659. color: #999;
  660. font-size: 24rpx;
  661. padding: 30rpx 0 60rpx;
  662. border-top: 1rpx solid #f0f0f0;
  663. margin-top: 20rpx;
  664. align-items: center;
  665. justify-content: center;
  666. }
  667. </style>