Addcommodity.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <template>
  2. <view class="center">
  3. <view class="input-box flex" @click="clickSearch">
  4. <view class=" input-content flex">
  5. <view class="iconfont iconsearch"></view>
  6. <view class="input"><input type="text" disabled value="商品搜索" /></view>
  7. </view>
  8. </view>
  9. <view class="mp-height"></view>
  10. <scroll-view scroll-x="true" class="class-box flex" :scroll-left="scrollLeft" :scroll-with-animation="true">
  11. <view @click="tabClass(item, idx)" class="item" v-for="(item, idx) in indexList" :key="idx">
  12. <view class="time" :class="{ action: idx == classIndex }">{{ item }}</view>
  13. </view>
  14. </scroll-view>
  15. <view class="line"></view>
  16. <swiper class="list-Box" :current="classIndex" @change="swiperChange" duration="500">
  17. <swiper-item class="list-item" v-for="(ls, idx) in indexList">
  18. <!-- 空白页 -->
  19. <scroll-view scroll-y="true" class="list">
  20. <view class="goodsList-item flex" v-for="(lss, ind) in ls.dataList" @click="check('item', ind, idx)">
  21. <view class="iconfont iconroundcheckfill checkbox" :class="{ checked: lss.checked }"></view>
  22. <view class="img"><image :src="lss.image" mode="scaleToFill"></image></view>
  23. <view class="text">
  24. <view class="spname">{{ lss.store_name }}</view>
  25. <view class="price">
  26. <text>¥</text>
  27. {{ lss.price }}
  28. </view>
  29. </view>
  30. <view class="yishou">已售{{ lss.sales }}</view>
  31. </view>
  32. </scroll-view>
  33. </swiper-item>
  34. </swiper>
  35. <view class="btn flex">
  36. <view class="checkAllbox" @click="check('all')">
  37. <view class="iconfont iconroundcheckfill checkalls" :class="{ checked: allChecked }"></view>
  38. <view class="quanxuan">全选</view>
  39. </view>
  40. <view class="btn-box flex"><view class="button" style="margin-left: 20rpx;" @click="submit()">添加</view></view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import { getProducts, choseProducts } from '@/api/product.js';
  46. import { getList } from '@/api/category.js';
  47. import { mapState, mapMutations } from 'vuex';
  48. import empty from '@/components/empty';
  49. export default {
  50. components: {
  51. empty
  52. },
  53. data() {
  54. return {
  55. chose: [], //选择的商品id,
  56. allChecked: false,
  57. indexList: ['家居','服装','首饰','食品'],
  58. topNavWidth: '12%', //设置导航默认宽度
  59. classIndex: 0, //当前进行中的活动
  60. itemWidht: 0, //顶部分类宽度
  61. loadingType: 'more', //加载更多状态
  62. actionClassInd: 0,
  63. page: 1,
  64. limit: 10
  65. };
  66. },
  67. computed: {
  68. ...mapState(['hasLogin', 'userInfo', 'baseURL', 'urlFile']),
  69. // 计算左侧距离
  70. scrollLeft() {
  71. if (this.classIndex > 1) {
  72. return this.itemWidht * (this.classIndex - 1.5);
  73. } else {
  74. return 0;
  75. }
  76. }
  77. },
  78. onLoad() {
  79. // 载入分类
  80. this.loadData();
  81. },
  82. methods: {
  83. async loadData(type = 'add', loading) {
  84. // let obj = this;
  85. // let data = {
  86. // page: obj.page,
  87. // limit: obj.limit
  88. // };
  89. // //没有更多直接返回
  90. // if (type === 'add') {
  91. // if (obj.loadingType === 'nomore') {
  92. // return;
  93. // }
  94. // obj.loadingType = 'loading';
  95. // } else {
  96. // obj.loadingType = 'more';
  97. // }
  98. // if (type === 'refresh') {
  99. // // 清空数组
  100. // obj.page = 1;
  101. // }
  102. // getList({})
  103. // .then(({ data }) => {
  104. // console.log('------',data);
  105. // obj.indexList = data.map(e => {
  106. // e.dataList = [];
  107. // e.loadingType = 'more';
  108. // e.page = 1;
  109. // e.limit = 10;
  110. // return e;
  111. // });
  112. // obj.classIndex = 0;
  113. // obj.getDataList(obj.indexList[obj.classIndex]);
  114. // if (obj.indexList.length <= 4) {
  115. // // 当数量小于等于4的时候自适应宽度
  116. // obj.topNavWidth = 100 / obj.indexList.length + '%';
  117. // } else {
  118. // // 页面渲染完毕后加载scroll-view左侧距离
  119. // obj.$nextTick(() => {
  120. // obj.numClassWidht();
  121. // });
  122. // }
  123. // })
  124. // .catch(err => {
  125. // console.log(err);
  126. // })
  127. },
  128. getDataList(item, type = 'add') {
  129. const obj = this;
  130. const requestData = {
  131. page: item.page,
  132. limit: item.limit,
  133. cid: item.id,
  134. can_chose: 1
  135. };
  136. // 判断数据是否加载中
  137. if (item.loadingType == 'nomore' || item.loadingType == 'loading') {
  138. return;
  139. }
  140. // 判断是否重新加载数据
  141. if (type == 'refresh') {
  142. item.dataList = [];
  143. }
  144. // 设置数据加载中
  145. item.loadingType = 'loading';
  146. return new Promise((ok, erro) => {
  147. getProducts(requestData)
  148. .then(({ data }) => {
  149. console.log(item.dataList);
  150. if (item.limit == data.length) {
  151. item.page++;
  152. item.loadingType = 'more';
  153. } else {
  154. item.loadingType = 'nomore';
  155. }
  156. if (type === 'refresh') {
  157. if (loading == 1) {
  158. uni.hideLoading();
  159. } else {
  160. uni.stopPullDownRefresh();
  161. }
  162. }
  163. this.$set(item.dataList, 'loaded', true);
  164. })
  165. .catch(err => {
  166. console.log(err);
  167. });
  168. });
  169. },
  170. swiperChange(e) {
  171. const obj = this;
  172. obj.classIndex = e.target.current;
  173. obj.getDataList(obj.indexList[obj.classIndex], 'tabChange');
  174. obj.allChecked = true;
  175. if (obj.indexList[obj.classIndex].dataList.length > 0) {
  176. let list = obj.indexList[obj.classIndex].dataList;
  177. for (let i = 0; i < list.length; i++) {
  178. if (!list[i].checked) {
  179. obj.allChecked = false;
  180. break;
  181. }
  182. }
  183. } else {
  184. obj.allChecked = false;
  185. }
  186. },
  187. tabClass(item, index) {
  188. const obj = this;
  189. obj.classIndex = index;
  190. obj.actionClassInd = index;
  191. let ls = obj.indexList[obj.actionClassInd];
  192. obj.allChecked = true;
  193. if (obj.indexList[obj.classIndex].dataList.length > 0) {
  194. let list = obj.indexList[obj.classIndex].dataList;
  195. for (let i = 0; i < list.length; i++) {
  196. if (!list[i].checked) {
  197. obj.allChecked = false;
  198. break;
  199. }
  200. }
  201. } else {
  202. obj.allChecked = false;
  203. }
  204. // 判断当前数据是否已经加载完毕
  205. if (ls.loadingType == 'more') {
  206. // this.getDataList(ls);
  207. }
  208. },
  209. check(type, index, erindex) {
  210. const obj = this;
  211. if (type === 'item') {
  212. const list = obj.indexList[erindex].dataList;
  213. list[index].checked = !list[index].checked;
  214. if (list[index].checked) {
  215. if (!obj.chose.includes(list[index].id)) {
  216. obj.chose.push(list[index].id);
  217. }
  218. } else {
  219. if (obj.chose.includes(list[index].id)) {
  220. // console.log(list[index].id);
  221. let aid = list[index];
  222. console.log(aid);
  223. let index = obj.chose.indexOf(aid);
  224. obj.chose.splice(index, 1);
  225. }
  226. }
  227. this.allChecked = true;
  228. for (let i = 0; i < list.length; i++) {
  229. if (!list[i].checked) {
  230. this.allChecked = false;
  231. }
  232. }
  233. } else {
  234. const checked = !obj.allChecked;
  235. const list = obj.indexList[obj.classIndex].dataList;
  236. list.forEach(item => {
  237. item.checked = checked;
  238. });
  239. obj.allChecked = checked;
  240. if (obj.allChecked) {
  241. list.forEach(item => {
  242. if (!obj.chose.includes(item.id)) {
  243. obj.chose.push(item.id);
  244. }
  245. });
  246. } else {
  247. list.forEach(item => {
  248. if (obj.chose.includes(item.id)) {
  249. let index = obj.chose.indexOf(item.id);
  250. obj.chose.splice(index, 1);
  251. }
  252. });
  253. }
  254. }
  255. },
  256. numClassWidht() {
  257. uni.createSelectorQuery()
  258. .select('.center')
  259. .fields(
  260. {
  261. size: true
  262. },
  263. data => {
  264. // 设置项目宽度
  265. // console.log(data);
  266. this.itemWidht = Math.floor((data.width / 750) * 187.5);
  267. }
  268. )
  269. .exec();
  270. },
  271. // clickSearch() {
  272. // uni.navigateTo({
  273. // url: '/pages/product/search'
  274. // });
  275. // },
  276. navTo(url) {
  277. uni.navigateTo({
  278. url
  279. });
  280. },
  281. submit() {
  282. const obj = this;
  283. // obj.chose = [];
  284. // let indexCd = obj.indexList.length;
  285. // for(let i = 0; i < indexCd; i++){
  286. // let dataCd = obj.indexList[i].dataList.length;
  287. // for(let j = 0; j < dataCd; j++){
  288. // if(obj.indexList[i].dataList[j].checked){
  289. // obj.chose=obj.chose.concat(obj.indexList[i].dataList[j].id);
  290. // }
  291. // }
  292. // }
  293. console.log(obj.chose);
  294. if (obj.chose.length != 0) {
  295. choseProducts({ ids: obj.chose })
  296. .then(() => {
  297. obj.$api.msg('添加商品成功');
  298. this.loadData();
  299. })
  300. .catch(err => {
  301. console.log(err);
  302. });
  303. } else {
  304. obj.$api.msg('请选择要添加的数据');
  305. }
  306. }
  307. }
  308. };
  309. </script>
  310. <style lang="scss">
  311. page {
  312. height: 100%;
  313. margin: 0;
  314. padding: 0;
  315. background-color: #ffffff;
  316. }
  317. .center {
  318. height: 100%;
  319. }
  320. .class-box {
  321. background: #ffffff;
  322. white-space: nowrap;
  323. height: 78rpx;
  324. .item {
  325. text-align: center;
  326. display: inline-block;
  327. color: $font-color-base;
  328. margin: 26rpx 34rpx 15rpx 29rpx;
  329. background-color: #fff;
  330. line-height: 1;
  331. .time {
  332. font-family: PingFang SC;
  333. font-weight: bold;
  334. font-size: 30rpx;
  335. padding-bottom: 19rpx;
  336. &.action {
  337. border-bottom: 4rpx solid #52C696;
  338. }
  339. }
  340. }
  341. }
  342. .input-box {
  343. margin-top: -5px;
  344. padding: 25rpx;
  345. background-color: #ffffff;
  346. height: 100rpx;
  347. .input-content {
  348. border-radius: 99rpx;
  349. flex-grow: 1;
  350. padding: 5rpx 30rpx;
  351. background: #ebebeb;
  352. .iconsearch {
  353. font-size: 50rpx;
  354. color: #999999;
  355. }
  356. .input {
  357. margin-left: 19rpx;
  358. flex-grow: 1;
  359. color: #999999;
  360. input {
  361. font-size: 26rpx;
  362. color: #999999;
  363. }
  364. }
  365. }
  366. .input-button {
  367. padding-left: 20rpx;
  368. font-size: $font-base;
  369. height: 100%;
  370. }
  371. }
  372. .line {
  373. width: 100%;
  374. height: 24rpx;
  375. background-color: #f8f8f8;
  376. }
  377. .list-Box {
  378. height: calc(100vh - 200px);
  379. .list-item {
  380. height: 100%;
  381. .list {
  382. height: 100%;
  383. }
  384. }
  385. }
  386. .goodsList-item {
  387. position: relative;
  388. justify-content: flex-start;
  389. width: 100%;
  390. padding: 30rpx;
  391. .checkbox {
  392. display: flex;
  393. flex-shrink: 0;
  394. font-size: 32rpx;
  395. color: #eeeeee;
  396. background: #eeeeee;
  397. border-radius: 50px;
  398. }
  399. .img {
  400. margin-left: 28rpx;
  401. width: 146rpx;
  402. height: 146rpx;
  403. background: #f0f0f0;
  404. border-radius: 10px;
  405. image {
  406. width: 146rpx;
  407. height: 146rpx;
  408. }
  409. }
  410. .text {
  411. position: relative;
  412. padding-left: 35rpx;
  413. height: 146rpx;
  414. text-overflow: ellipsis;
  415. overflow: hidden;
  416. white-space: nowrap;
  417. .spname {
  418. font-size: 28rpx;
  419. font-family: PingFang SC;
  420. font-weight: bold;
  421. color: #333333;
  422. text-overflow: ellipsis;
  423. overflow: hidden;
  424. }
  425. .earning {
  426. width: 275rpx;
  427. padding: 7rpx;
  428. margin-top: 12rpx;
  429. background: #fee1d7;
  430. border-radius: 5rpx;
  431. text-align: center;
  432. font-size: 20rpx;
  433. font-family: PingFang SC;
  434. font-weight: 500;
  435. color: #fc4141;
  436. line-height: 1;
  437. }
  438. .price {
  439. margin-top: 19rpx;
  440. font-family: PingFang SC;
  441. font-weight: bold;
  442. color: #ff0000;
  443. font-size: 36rpx;
  444. text {
  445. font-size: 24rpx;
  446. }
  447. }
  448. }
  449. .yishou {
  450. position: absolute;
  451. right: 29rpx;
  452. bottom: 40rpx;
  453. }
  454. }
  455. .btn {
  456. position: fixed;
  457. bottom: 0;
  458. width: 100%;
  459. height: 100rpx;
  460. justify-content: space-between;
  461. padding: 0 39rpx 0 54rpx;
  462. line-height: 1;
  463. border-top: 1rpx solid #f0f0f0;
  464. .checkAllbox {
  465. display: flex;
  466. .checkalls {
  467. font-size: 32rpx;
  468. color: #eeeeee;
  469. background: #eeeeee;
  470. border-radius: 50px;
  471. }
  472. .quanxuan {
  473. margin-left: 16rpx;
  474. font-size: 30rpx;
  475. font-family: PingFang SC;
  476. font-weight: 400;
  477. color: #1d2023;
  478. }
  479. }
  480. .button {
  481. width: 160rpx;
  482. height: 64rpx;
  483. background: #52C696;
  484. border-radius: 32rpx;
  485. color: #ffffff;
  486. text-align: center;
  487. line-height: 64rpx;
  488. font-size: 30rpx;
  489. font-family: PingFang SC;
  490. font-weight: bold;
  491. }
  492. }
  493. .checkAllbox .checkalls.checked,
  494. .goodsList-item .checkbox.checked {
  495. color: #52C696;
  496. background: #ffffff;
  497. }
  498. </style>