productManagement.vue 17 KB

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