productAdd.vue 12 KB

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