buy.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { get,post } from "../../common/request.js"
  2. import stylemap from "../../common/stylemap"
  3. import share from "../../common/share"
  4. import pathmap from "../../common/pathmap"
  5. import { retCartData } from "../../data/car"
  6. import bindSearch from "../../module/search/search.js"
  7. const app = getApp();
  8. // pages/buy/buy.js
  9. Page(Object.assign({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. list:false,
  15. limit:10,
  16. pageindex:0,
  17. lock:false,
  18. searchfouce:false,
  19. selectbar:null,
  20. hasloadall:false,
  21. filters:[
  22. {
  23. title:"排序",
  24. name:"sortby"
  25. },
  26. {
  27. title:"品牌",
  28. name:"brand"
  29. },
  30. {
  31. title:"价格",
  32. name:"price"
  33. },
  34. {
  35. title:"更多",
  36. name:"more"
  37. }
  38. ],
  39. sortbymap:[
  40. {
  41. title:"默认排序",
  42. value:""
  43. },
  44. {
  45. title:"价格最低",
  46. value:"price&asc"
  47. },
  48. {
  49. title:"价格最高",
  50. value:"price&desc"
  51. },
  52. {
  53. title:"车龄最短",
  54. value:"car_age&desc"
  55. },
  56. {
  57. title:"里程最少",
  58. value:"mileage&asc"
  59. },
  60. {
  61. title:"最新发布",
  62. value:"new&desc"
  63. }
  64. ],
  65. searchdata:{
  66. sortby:"",
  67. price:"",
  68. keys:"",
  69. car_age:"",
  70. mileage:"",
  71. transmission:"",
  72. displacement:"",
  73. emission:"",
  74. colour:"",
  75. filterlock:true
  76. },
  77. keylist:[],
  78. morebtheight:app.globalData.isIpx?120:98
  79. },
  80. /**
  81. * 生命周期函数--监听页面加载
  82. */
  83. onLoad: function (options) {
  84. bindSearch(this);
  85. //initialheight = initialheight-initialheight%2;
  86. var searchdata = Object.assign(this.data.searchdata,options);
  87. this.setData(stylemap);
  88. this.getWheres();
  89. this.getList(this.data.searchdata);
  90. this.loadBrandRecommend()
  91. },
  92. transformSearchData:function(searchdata){
  93. var para = Object.assign({},searchdata);
  94. var sortby = para.sortby;
  95. if(sortby){
  96. var sortinfo = sortby.split("&");
  97. para.order_key = sortinfo[0];
  98. para.order_val = sortinfo[1];
  99. }
  100. delete para.sortby;
  101. return para;
  102. },
  103. //获取筛选信息
  104. getWheres:function(){
  105. var _self = this;
  106. var wheres = wx.getStorageSync("wheres");
  107. if(wheres){
  108. _self.setData({
  109. wheres: wheres
  110. })
  111. }else{
  112. get(pathmap.wheres, {
  113. no_brand_series:1
  114. }, function (json) {
  115. _self.setData({
  116. wheres: json.data
  117. })
  118. wx.setStorageSync("wheres",json.data);
  119. })
  120. }
  121. },
  122. loadBrandRecommend:function(){
  123. var _self = this;
  124. get(pathmap.brandrecommend, {
  125. }, function (json) {
  126. _self.setData({
  127. brandrecommend:json.data
  128. })
  129. })
  130. },
  131. getList:function(searchdata){
  132. var _self = this;
  133. var para = this.transformSearchData(searchdata);
  134. if (searchdata.keys){
  135. searchdata.keylist = searchdata.keys.split(/\W+/);
  136. }
  137. para.offset = 0;
  138. para.limit = this.data.limit;
  139. if(!this.data.lock){
  140. this.setData({
  141. lock:true
  142. })
  143. get(pathmap.searchcar, para, function (json) {
  144. var list = retCartData(json.data&&json.data.list);
  145. var key = _self.data.searchdata.keys.replace(/(^\s*)|(\s*$)/g, "");
  146. var keylist = key?key.split(/\s+/):[];
  147. var obj = {
  148. lock:false,
  149. list:list,
  150. searchdata:searchdata,
  151. offset:_self.data.limit,
  152. keylist: keylist
  153. }
  154. if(json.data.total<=_self.data.limit){
  155. obj.hasloadall=true;
  156. }else{
  157. obj.hasloadall=false;
  158. }
  159. _self.setData(obj);
  160. if(list.length==0){
  161. _self.loadLikeCar()
  162. }
  163. })
  164. }
  165. },
  166. loadListMore:function(){
  167. var _self = this;
  168. var para = this.transformSearchData(this.data.searchdata);
  169. para.offset = this.data.offset;
  170. para.limit = this.data.limit;
  171. if(!this.data.lock&&!this.data.hasloadall){
  172. this.setData({
  173. lock:true
  174. })
  175. get(pathmap.searchcar, para, function (json) {
  176. var list = retCartData(json.data&&json.data.list);
  177. var nextoffset = _self.data.offset+_self.data.limit
  178. var obj = {
  179. lock:false,
  180. list:_self.data.list.concat(list),
  181. offset:nextoffset
  182. }
  183. if(json.data.total<=nextoffset){
  184. obj.hasloadall=true;
  185. }else{
  186. obj.hasloadall=false;
  187. }
  188. _self.setData(obj);
  189. })
  190. }
  191. },
  192. showFilter:function(e){
  193. var el = e.currentTarget;
  194. var name = el.dataset.name;
  195. if(name==this.data.selectbar){
  196. name=null;
  197. }
  198. this.setData({
  199. selectbar:name
  200. })
  201. },
  202. changeSort:function(e){
  203. var el = e.currentTarget;
  204. var name = el.dataset.sort;
  205. var searchdata = Object.assign(this.data.searchdata,{sortby:name});
  206. this.setData({
  207. selectbar:""
  208. })
  209. this.getList(searchdata);
  210. },
  211. changePrice:function(e){
  212. var el = e.currentTarget;
  213. var id = el.dataset.id;
  214. var searchdata = Object.assign(this.data.searchdata,{price:id});
  215. this.setData({
  216. selectbar:"" //关闭筛选卡
  217. })
  218. this.getList(searchdata);
  219. },
  220. loadLikeCar:function(){
  221. var _self = this;
  222. if(!this.data.like){
  223. get(pathmap.like, {
  224. offset: 0,
  225. limit: 10
  226. }, function (json) {
  227. var list = retCartData(json.data.list)
  228. _self.setData({
  229. like: list
  230. })
  231. })
  232. }
  233. },
  234. getMoreFilter:function(e){
  235. var info = e.currentTarget.dataset;
  236. var obj = {};
  237. obj[info.type]=info.value;
  238. this.setData({searchdata:Object.assign(this.data.searchdata,obj)})
  239. },
  240. resetFilter:function(){
  241. this.setData({searchdata:Object.assign(this.data.searchdata,{
  242. car_age:"",
  243. mileage:"",
  244. transmission:"",
  245. displacement:"",
  246. emission:"",
  247. colour:""
  248. })})
  249. },
  250. moreSearch:function(){
  251. this.setData({selectbar:""})
  252. this.getList(this.data.searchdata);
  253. },
  254. lockFilter:function(e){
  255. if(e.detail&&!this.data.filterlock){
  256. this.setData({
  257. filterlock:true
  258. })
  259. }else if(!e.detail&&this.data.filterlock){
  260. this.setData({
  261. filterlock:false
  262. })
  263. }
  264. },
  265. submitsearchinfo:function(obj){
  266. this.getList(Object.assign(this.data.searchdata,obj));
  267. this.setData({searchshow:false})
  268. },
  269. closeBrandList:function(){
  270. this.setData({selectbar:""});
  271. },
  272. filterBack:function(){
  273. this.setData({selectbar:""});
  274. },
  275. selectSeries:function(e){
  276. var series = e.detail.series;
  277. this.setData({
  278. selectbar:""
  279. })
  280. this.getList(Object.assign(this.data.searchdata,{car_series:series.car_series_id}));
  281. },
  282. checkAll:function(e){
  283. this.getList({
  284. sortby:"",
  285. price:"",
  286. car_age:"",
  287. mileage:"",
  288. transmission:"",
  289. displacement:"",
  290. emission:"",
  291. colour:"",
  292. filterlock:true,
  293. keys:""
  294. });
  295. },
  296. deletekey:function(e){
  297. var index = e.currentTarget.dataset.index;
  298. this.data.keylist.splice(index,1)
  299. var keys = this.data.keylist.join(" ");
  300. this.submitsearchinfo({keys:keys});
  301. }
  302. },share))