金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

754 lines
20 KiB

  1. <template>
  2. <view>
  3. <view id="store-list">
  4. <view class="commodity-top" catchtouchmove="move">
  5. <view class="sidebar mx-1px-bottom">
  6. <view class="sidebar-left">
  7. <span class="iconfont icon-fenzu" @tap="classF">
  8. </span>
  9. </view>
  10. <view class="sidebar-right">
  11. <view class="right-seek iconfont icon-sousuo" @tap="search">
  12. </view>
  13. <view class="right-cart iconfont icon-gouwuche" @tap="cart">
  14. </view>
  15. <view class="right-cart iconfont icon-qiehuan1" v-if="arrangement == 'rank'" @tap="changeRange">
  16. </view>
  17. <view class="right-cart iconfont icon-qiehuanduotuSVG" v-if="arrangement == 'vertical'" @tap="changeRange">
  18. </view>
  19. </view>
  20. </view>
  21. <view class="filter-box">
  22. <view class="filter-item mx-1px-right" data-type="updated_at" @tap="changeOrderBy">
  23. <view class="left" :class=" orderBy === 'updated_at' ? 'active' : ''">最新</view>
  24. <view class="right" :class="(orderBy === 'updated_at' && sort === 'desc') ? 'desc' : ''"></view>
  25. </view>
  26. <view class="filter-item mx-1px-right" data-type="sell_price" @tap="changeOrderBy">
  27. <view class="left" :class=" orderBy === 'sell_price' ? 'active' : ''">价格</view>
  28. <view class="right" :class="(orderBy === 'sell_price' && sort === 'desc') ? 'desc' : ''"></view>
  29. </view>
  30. <view class="filter-item mx-1px-right" data-type="sale" @tap="changeOrderBy">
  31. <view class="left" :class=" orderBy === 'sale' ? 'active' : ''">销量</view>
  32. <view class="right" :class="(orderBy === 'sale' && sort === 'desc') ? 'desc' : ''"></view>
  33. </view>
  34. <view class="filter-item big-filter" :class="c_id ? '' : 'dis'" @tap="showFilterF">
  35. 筛选
  36. </view>
  37. </view>
  38. </view>
  39. <view class="commodity-bottom" :hidden="showFilter">
  40. <!-- 一排显示1个 -->
  41. <view v-for="(items, idx) in storeList" :key="idx" v-if="arrangement == 'rank'">
  42. <view class="commodity-out" :data-id="item.id" v-for="(item, index) in storeList[idx]" :key="index" @tap="jump">
  43. <view class="commodity-box">
  44. <view class="commodity-img">
  45. <image mode="widthFix" :src="item.img"></image>
  46. </view>
  47. <view class="commodity-name">
  48. {{item.name}}
  49. </view>
  50. <view class="commodity-money">
  51. <view class="money">
  52. {{item.sell_price}}
  53. <view class="origin-money">
  54. {{item.market_price}}
  55. </view>
  56. </view>
  57. <!-- <view class="discount-tags">
  58. <span class="tags-item" v-for="(tags, index) in item.discount_tags" :key="index" >{{tags}}</span>
  59. </view> -->
  60. </view>
  61. </view>
  62. </view>
  63. </view>
  64. <!-- 一排显示一个 -->
  65. <view v-for="(items, idx) in storeList" :key="idx" v-if="arrangement == 'vertical'">
  66. <view class="commodity-out-vertical" :data-id="item.id" v-for="(item, index) in storeList[idx]" :key="index" @tap="jump">
  67. <view class="img-box">
  68. <image mode="widthFix" :src="item.img"></image>
  69. </view>
  70. <view class="right-box">
  71. <view class="goods-name">
  72. {{item.name}}
  73. </view>
  74. <view class="goods-money">
  75. {{item.sell_price}}
  76. <view class="origin-money">
  77. {{item.market_price}}
  78. </view>
  79. </view>
  80. </view>
  81. </view>
  82. </view>
  83. <view class="loadingbox" :hidden="!show">
  84. 正在加载下一页数据
  85. </view>
  86. </view>
  87. </view>
  88. <view class="right-filter-box" :hidden="!showFilter">
  89. <view class="box">
  90. <view>
  91. <view class="title">
  92. 价格区间
  93. </view>
  94. <view class="list">
  95. <view class="list-block">
  96. <view data-num="200-500" :class="shadows.price == '200-500' ? 'selected' : ''" @tap="checkPrice">200-500元</view>
  97. </view>
  98. <view class="list-block">
  99. <view data-num="501-1000" :class="shadows.price == '501-1000' ? 'selected' : ''" @tap="checkPrice">501-1000元</view>
  100. </view>
  101. <view class="list-block">
  102. <view data-num="1001-1500" :class="shadows.price == '1001-1500' ? 'selected' : ''" @tap="checkPrice">1001-1500元</view>
  103. </view>
  104. <view class="list-block">
  105. <view data-num="1501-2000" :class="shadows.price == '1501-2000' ? 'selected' : ''" @tap="checkPrice">1501-2000元</view>
  106. </view>
  107. <view class="list-block">
  108. <view data-num="2000-" :class="shadows.price == '2000-' ? 'selected' : ''" @tap="checkPrice">2000-元以上</view>
  109. </view>
  110. <!-- <view class="price-inputs">
  111. <input type="number" data-type="0" bindinput="modifyPrice" />
  112. <view class="minus">
  113. -
  114. </view>
  115. <input type="number" data-type="1" bindinput="modifyPrice" />
  116. </view> -->
  117. </view>
  118. </view>
  119. <!-- #ifdef APP-PLUS || MP-WEIXIN -->
  120. <view v-for="(item, index) in filter" :key="index">
  121. <view class="title">
  122. {{item.key}}
  123. </view>
  124. <view class="list">
  125. <view class="list-block" v-for="(obj, index) in item.values" :key="index">
  126. <view :class="!!selections[obj.id] ? 'selected' : ''" :data-id="obj.id" :data-type="obj" @tap="check">
  127. {{obj.text}}
  128. </view>
  129. </view>
  130. </view>
  131. </view>
  132. <!-- #endif -->
  133. <!-- #ifdef H5 -->
  134. <view v-for="(item, index) in filter" :key="index">
  135. <view class="title">
  136. {{item.key}}
  137. </view>
  138. <view class="list">
  139. <view class="list-block" v-for="(obj, index) in item.values" :key="index">
  140. <view :class="!!selections[obj.id] ? 'selected' : ''" :data-id="obj.id" :data-type="obj" @tap="check(obj)">
  141. {{obj.text}}
  142. </view>
  143. </view>
  144. </view>
  145. </view>
  146. <!-- #endif -->
  147. <view class="button-box">
  148. <button type="default" @tap="cancel">取消</button>
  149. <button type="primary" @tap="confirm">确定</button>
  150. </view>
  151. </view>
  152. </view>
  153. </view>
  154. </template>
  155. <script>
  156. var app = getApp();
  157. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  158. import Animation from '@/common/js/animation.js';
  159. export default {
  160. data() {
  161. return {
  162. page: 1,
  163. storeList: [],
  164. orderBy: '',
  165. sort: '',
  166. c_id: '',
  167. meta: '',
  168. show: false,
  169. showFilter: false,
  170. filter: null,
  171. price:{},
  172. priceCache:{},
  173. shadows: {},
  174. selections:{},
  175. arrangement:'rank',//判断当前的排列方式,是横列 还是 竖列
  176. };
  177. },
  178. onReachBottom() {
  179. var hasMore = this.meta.pagination.total_pages > this.meta.pagination.current_page;
  180. if (!this.showFilter) {
  181. if (hasMore) {
  182. this.setData({
  183. show: true
  184. });
  185. var query = {
  186. sort: this.sort,
  187. orderBy: this.orderBy,
  188. c_id: this.c_id
  189. };
  190. var page = this.meta.pagination.current_page + 1;
  191. this.queryCommodityList(query, page);
  192. } else {
  193. wx.showToast({
  194. image: '../../../static/error.png',
  195. title: '再拉也没有啦'
  196. });
  197. }
  198. }
  199. },
  200. onReady() {
  201. var query = {
  202. sort: this.sort,
  203. orderBy: this.orderBy,
  204. c_id: this.c_id
  205. };
  206. this.queryCommodityList(query);
  207. },
  208. onLoad(e) {
  209. if (e.c_id) {
  210. this.setData({
  211. c_id: e.c_id
  212. });
  213. }
  214. if (e.orderBy) {
  215. this.setData({
  216. orderBy: e.orderBy,
  217. sort: e.sort
  218. });
  219. }
  220. var price = {
  221. min: '',
  222. max: ''
  223. };
  224. var shadows = {
  225. attr: {},
  226. specs: {}
  227. };
  228. if (e.attr) {
  229. e.attr.forEach(v => shadows.attr[v] = true);
  230. }
  231. Object.keys(e).forEach(key => {
  232. let ret = /^specs\[([^\]]+)]$/.exec(key);
  233. if (ret) {
  234. let name = ret[1];
  235. shadows.specs[name] = e[key];
  236. }
  237. });
  238. var priceList = ['200-500', '501-1000', '1001-1500', '1501-2000', '2000-'];
  239. shadows.price = e.price || '';
  240. if (!~priceList.indexOf(shadows.price)) {
  241. var parts = shadows.price.split(/\s*\-\s*/);
  242. price.min = parts[0] || '';
  243. price.max = parts[1] || '';
  244. }
  245. // this.setData({
  246. // price: price,
  247. // shadows: shadows,
  248. // selections: {},
  249. // priceList: priceList,
  250. // priceCache: {}
  251. // });
  252. this.price=price;
  253. this.shadows=shadows;
  254. this.selections={};
  255. this.priceList=priceList;
  256. this.priceCache={};
  257. },
  258. onShareAppMessage() {
  259. var url = decodeURIComponent(getUrl());
  260. return {
  261. title: '商品列表',
  262. path: '/' + url
  263. };
  264. },
  265. methods: {
  266. //点击切换排列方式
  267. changeRange(){
  268. if(this.arrangement == 'rank'){
  269. this.arrangement = 'vertical'
  270. } else if(this.arrangement == 'vertical'){
  271. this.arrangement = 'rank'
  272. }
  273. },
  274. // 价格点击
  275. checkPrice(e) {
  276. var num = e.currentTarget.dataset.num;
  277. if (this.shadows.price == num) {
  278. console.log("gaehgae");
  279. this.setData({
  280. 'priceCache.value': num
  281. });
  282. if (this.priceCache.min !== undefined) {
  283. this.setData({
  284. 'price.min': this.priceCache.min
  285. });
  286. }
  287. if (this.priceCache.max !== undefined) {
  288. this.setData({
  289. 'price.min': this.priceCache.max
  290. });
  291. }
  292. // this.setData({
  293. // 'shadows.price': ''
  294. // });
  295. this.shadows={price:''}
  296. } else {
  297. console.log(46846846846846);
  298. if (this.price.min !== '') {
  299. // this.setData({
  300. // 'priceCache.min': this.priceCache.min
  301. // });
  302. this.priceCache.min=this.priceCache.min;
  303. }
  304. if (this.price.max !== '') {
  305. // this.setData({
  306. // 'priceCache.max': this.priceCache.max
  307. // });
  308. this.priceCache.max=this.priceCache.max;
  309. }
  310. // this.setData({
  311. // 'price.min': '',
  312. // 'price.max': '',
  313. // 'shadows.price': num
  314. // });
  315. //
  316. this.price.min='';
  317. this.price.max='';
  318. this.shadows.price=num;
  319. }
  320. },
  321. // 价格输入
  322. modifyPrice(e) {
  323. var type = e.currentTarget.dataset.type;
  324. var val = this.price[type ? 'max' : 'min'];
  325. val = parseFloat(val);
  326. if (isNaN(val)) val = '';
  327. if (this.price[type] == 'max') {
  328. // this.setData({
  329. // 'price.max': val
  330. // });
  331. this.price={max:val};
  332. } else {
  333. // this.setData({
  334. // 'price.min': val
  335. // });
  336. this.price={min:val};
  337. }
  338. // this.setData({
  339. // 'shadows.price': ''
  340. // });
  341. //
  342. this.shadows={price:''}
  343. },
  344. // 点击筛选条件
  345. // #ifdef APP-PLUS || MP-WEIXIN
  346. check(e) {
  347. var type = e.currentTarget.dataset.type;
  348. var id = type.id;
  349. var selections = Object.assign({}, this.selections);
  350. if (!selections[id]) {
  351. for (let k in selections) {
  352. if (selections.hasOwnProperty(k)) {
  353. let o = selections[k]
  354. if (o.type === type.type && o.key === type.key) {
  355. delete selections[k];
  356. }
  357. }
  358. }
  359. selections[id] = {
  360. key: type.key,
  361. type: type.type,
  362. value: type.value,
  363. field: type.field
  364. }
  365. } else {
  366. delete selections[id];
  367. }
  368. this.setData({
  369. selections: Object.assign({}, selections)
  370. })
  371. },
  372. // #endif
  373. // #ifdef H5
  374. check(e) {
  375. var type = e;
  376. var id = type.id;
  377. var selections = Object.assign({}, this.selections);
  378. if (!selections[id]) {
  379. for (let k in selections) {
  380. if (selections.hasOwnProperty(k)) {
  381. let o = selections[k];
  382. if (o.type === type.type && o.key === type.key) {
  383. delete selections[k];
  384. }
  385. }
  386. }
  387. selections[id] = {
  388. key: type.key,
  389. type: type.type,
  390. value: type.value,
  391. field: type.field
  392. };
  393. } else {
  394. delete selections[id];
  395. }
  396. this.setData({
  397. selections: Object.assign({}, selections)
  398. });
  399. /* this.selections=Object.assign({}, selections); */
  400. },
  401. // #endif
  402. // 取消
  403. cancel() {
  404. // var animation = new Animation('show');
  405. // animation.right().then(() => {
  406. this.setData({
  407. showFilter: false,
  408. selections: {},
  409. shadows: {}
  410. });
  411. // });
  412. },
  413. // 确定
  414. confirm() {
  415. var attr = [],
  416. specs = {};
  417. for (let k in this.selections) {
  418. if (this.selections.hasOwnProperty(k)) {
  419. let o = this.selections[k];
  420. if (o.type === 'attr') {
  421. attr.push(o.value);
  422. } else {
  423. specs['specs[' + o.field + ']'] = o.value;
  424. }
  425. }
  426. }
  427. attr = attr.join(',');
  428. var price = this.shadows.price;
  429. if (attr.length) {
  430. var query = Object.assign({}, {
  431. attr
  432. }, {
  433. c_id: this.c_id,
  434. orderBy: this.orderBy,
  435. sort: this.sort,
  436. price: price
  437. }, specs);
  438. } else {
  439. var query = Object.assign({}, {
  440. c_id: this.c_id,
  441. orderBy: this.orderBy,
  442. sort: this.sort,
  443. price: price
  444. }, specs);
  445. }
  446. console.log(query);
  447. this.queryCommodityList(query);
  448. },
  449. changeOrderBy(e) {
  450. var field = e.currentTarget.dataset.type;
  451. if (this.orderBy === field) {
  452. this.setData({
  453. sort: this.sort === 'desc' ? 'asc' : 'desc'
  454. });
  455. } else {
  456. this.setData({
  457. orderBy: field,
  458. sort: 'desc'
  459. });
  460. }
  461. wx.redirectTo({
  462. url: '/pages/store/list/list?orderBy=' + this.orderBy + '&sort=' + this.sort + '&c_id=' + this.c_id
  463. });
  464. },
  465. jump(e) {
  466. var id = e.currentTarget.dataset.id;
  467. wx.navigateTo({
  468. url: '/pages/store/detail/detail?id=' + id
  469. });
  470. },
  471. classF() {
  472. wx.switchTab({
  473. url: '/pages/index/classification/classification'
  474. });
  475. },
  476. search() {
  477. wx.navigateTo({
  478. url: '/pages/store/search/search'
  479. });
  480. },
  481. cart() {
  482. wx.switchTab({
  483. url: '/pages/store/cart/cart'
  484. });
  485. },
  486. showFilterF() {
  487. if (this.c_id) {
  488. this.setData({
  489. showFilter: true
  490. });
  491. // var animation = new Animation('show');
  492. // animation.Pullleft();
  493. }
  494. },
  495. move() {},
  496. loadMore() {
  497. this.$http.get({
  498. api: "api/store/list?page=" + (this.page + 1)
  499. });
  500. },
  501. // 查询商品列表
  502. queryCommodityList(query = {}, page = 1) {
  503. var token = this.$cookieStorage.get('user_token') || '';
  504. var params = Object.assign({}, query, {
  505. page
  506. });
  507. wx.showLoading({
  508. title: '加载中',
  509. mask: true
  510. });
  511. this.$http.get({
  512. api: 'api/store/list',
  513. data: params,
  514. header: {
  515. Authorization: token
  516. }
  517. }).then(res => {
  518. if (res.statusCode == 200) {
  519. res = res.data;
  520. if (res.status) {
  521. // 商品列表页赋值
  522. this.setData({
  523. [`storeList.${page - 1}`]: res.data,
  524. meta: res.meta
  525. }); // 右侧筛选赋值
  526. if (res.meta && res.meta.filter) {
  527. if (Array.isArray(res.meta.filter)) {
  528. this.setData({
  529. filter: null
  530. });
  531. } else {
  532. let filter = res.meta.filter;
  533. let list = [];
  534. if (filter.attr && filter.attr.keys) {
  535. let type = 'attr';
  536. filter.attr.keys.forEach(key => {
  537. let arr = [];
  538. let arrText = [];
  539. for (let attr in filter.attr.values[key]) {
  540. !!attr && arrText.push(filter.attr.values[key][attr]);
  541. !!attr && arr.push(attr);
  542. }
  543. list.push({
  544. key,
  545. values: arr.map((v, index) => {
  546. return {
  547. id: [type, key, v].join('-'),
  548. key,
  549. type,
  550. value: v,
  551. text: arrText[index]
  552. };
  553. })
  554. });
  555. });
  556. }
  557. if (filter.specs && filter.specs.keys) {
  558. let type = 'specs';
  559. filter.specs.keys.forEach(key => {
  560. let entries = key.split(':');
  561. let field = entries[1];
  562. key = entries[0];
  563. var newKey = key + ':' + field;
  564. let specs = [];
  565. let specsText = [];
  566. for (let spec in filter.specs.values[newKey]) {
  567. !!spec && specsText.push(filter.specs.values[newKey][spec]);
  568. !!spec && specs.push(spec);
  569. }
  570. list.push({
  571. key,
  572. values: specs.map((v, index) => {
  573. return {
  574. id: [type, key, v].join('-'),
  575. key,
  576. type,
  577. field,
  578. value: v,
  579. text: specsText[index]
  580. };
  581. })
  582. });
  583. });
  584. }
  585. this.setData({
  586. filter: list
  587. });
  588. }
  589. }
  590. this.setData({
  591. showFilter: false
  592. });
  593. } else {
  594. wx.showModal({
  595. title: '',
  596. content: res.message,
  597. showCancel: false
  598. });
  599. }
  600. } else {
  601. wx.showModal({
  602. title: '',
  603. content: "请求失败",
  604. showCancel: false
  605. });
  606. }
  607. this.setData({
  608. show: false
  609. });
  610. wx.hideLoading();
  611. }).catch(rej => {
  612. wx.showModal({
  613. title: '',
  614. content: '请求失败',
  615. success: res => {
  616. if (res.confirm) {}
  617. }
  618. });
  619. this.setData({
  620. show: false
  621. });
  622. wx.hideLoading();
  623. });
  624. },
  625. setData: function (obj) {
  626. let that = this;
  627. let keys = [];
  628. let val, data;
  629. Object.keys(obj).forEach(function (key) {
  630. keys = key.split('.');
  631. val = obj[key];
  632. data = that.$data;
  633. keys.forEach(function (key2, index) {
  634. if (index + 1 == keys.length) {
  635. that.$set(data, key2, val);
  636. } else {
  637. if (!data[key2]) {
  638. that.$set(data, key2, {});
  639. }
  640. }
  641. data = data[key2];
  642. });
  643. });
  644. }
  645. },
  646. computed: {},
  647. watch: {}
  648. };
  649. </script>
  650. <style rel="stylesheet/less" lang="less">
  651. @import "list";
  652. </style>