searchList.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // searchList.js
  2. //获取应用实例
  3. // pages/goodlist/goodlist.js
  4. // var request = require('../../utils/https.js')
  5. // var uri = 'goods/api/goodslist' //商品列表的的uri
  6. // var app = getApp();
  7. // var id = '';
  8. var navlist = [
  9. { id: " ", title: "综合", icon: "../../images/search_list_zh_pre.png" },
  10. { id: "price", title: "价格", icon: "../../images/search_list_zh_n.png" }
  11. ];
  12. // 热销新品
  13. var contentList = [
  14. ];
  15. Page({
  16. data: {
  17. placeholder: "输入搜索关键字",
  18. pageNo: 1,
  19. activeIndex: 0,
  20. navList: navlist,
  21. contentList: contentList,
  22. navId : 0,
  23. systemInfo: [],
  24. loadingHidden: false,
  25. list: [],
  26. num: 1,
  27. limt: 20,
  28. tab: '',
  29. pre0: "../../images/search_list_zh_pre.png",
  30. n0: "../../images/search_list_zh_n.png",
  31. pre: "../../images/search_list_zh_pre.png",
  32. n: "../../images/search_list_zh_n.png",
  33. noActPrice: '/images/search_jt_price_up.png',
  34. dataVal : "0",
  35. hidden: false,
  36. page: 0,
  37. size: 20,
  38. hasMore: true,
  39. hasRefesh: false,
  40. commodity_name: '',
  41. host: '',
  42. searchPageNum: 0, // 设置加载的第几次,默认是第一次
  43. callbackcount: 10, //返回数据的个数
  44. searchLoading: false, //"上拉加载"的变量,默认false,隐藏
  45. searchLoadingComplete: false, //“没有数据”的变量,默认false,隐藏
  46. tab : '',
  47. tabVal : ''
  48. },
  49. onLoad: function (options) {
  50. var commodity_name = options.commodity_name;
  51. var that = this;
  52. var host = getApp().globalData.servsers;
  53. //检索商品列表
  54. wx.request({
  55. url: host+"commodityapi/findAllforseach",
  56. data: {
  57. commodity_name: commodity_name,
  58. offset: 0,
  59. limit: 10,
  60. },
  61. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  62. header: {
  63. 'Accept': 'application/json'
  64. },
  65. success: function (res) {
  66. that.setData({
  67. contentList: res.data.rows,
  68. commodity_name: commodity_name,
  69. host: host,
  70. searchPageNum: 0, //第一次加载,设置1
  71. searchSongList: [], //放置返回数据的数组,设为空
  72. isFromSearch: true, //第一次加载,设置true
  73. searchLoading: true, //把"上拉加载"的变量设为true,显示
  74. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  75. });
  76. console.log(res.data.rows);
  77. if (res.data.rows.length < 10) {
  78. that.setData({
  79. searchPageNum: 0, //第一次加载,设置1
  80. searchSongList: [], //放置返回数据的数组,设为空
  81. isFromSearch: true, //第一次加载,设置true
  82. searchLoading: false, //把"上拉加载"的变量设为true,显示
  83. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  84. });
  85. }
  86. console.log(res.data.rows)
  87. if (res == null || res.data == null) {
  88. console.error('网络请求失败');
  89. return;
  90. }
  91. }
  92. });
  93. },
  94. //切换TAB
  95. tagChoose: function (options) {
  96. var that = this
  97. var id = options.currentTarget.dataset.id;
  98. var index = e.currentTarget.dataset.index;
  99. var type = e.currentTarget.dataset.dataType;
  100. if (id == "price" && type == '1'){//价格
  101. that.setData({
  102. activeIndex: index,
  103. pageNo: 1,
  104. pre: "../../images/search_list_zh_n.png"
  105. })
  106. }else{//综合
  107. //设置当前样式
  108. if (type == '0') {//选择综合
  109. that.setData({
  110. noActPrice: '/images/search_jt_price_up.png'
  111. })
  112. }
  113. that.setData({
  114. activeIndex: index,
  115. pageNo: 1
  116. })
  117. }
  118. },
  119. onTapTag: function (e) {
  120. var that = this;
  121. var tab = e.currentTarget.id;
  122. var index = e.currentTarget.dataset.index;
  123. var tabType = e.currentTarget.dataset.type;
  124. var tabVal = e.currentTarget.dataset.val;//标志是否是一个tab点击多次(价格)
  125. that.setData({
  126. tab: tab,
  127. tabVal: tabVal,
  128. searchPageNum: 0
  129. })
  130. if (tab == "price" && tabVal == '0') {//切换数据 为 从低到高 (箭头朝上)
  131. wx.request({
  132. url: that.data.host+"commodityapi/findAllforseachpricedown",
  133. data: {
  134. commodity_name: that.data.commodity_name,
  135. offset: 0,
  136. limit: 10,
  137. },
  138. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  139. header: {
  140. 'Accept': 'application/json'
  141. },
  142. success: function (res) {
  143. that.setData({
  144. contentList: res.data.rows,
  145. commodity_name: that.data.commodity_name,
  146. activeIndex: index,
  147. tab: tab,
  148. pageNo: 1,
  149. dataVal: "1",
  150. pre: "../../images/search_list_zh_pre.png",
  151. searchPageNum: 0, //第一次加载,设置1
  152. searchSongList: [], //放置返回数据的数组,设为空
  153. isFromSearch: true, //第一次加载,设置true
  154. searchLoading: true, //把"上拉加载"的变量设为true,显示
  155. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  156. });
  157. if (res.data.rows.length < 10) {
  158. that.setData({
  159. searchPageNum: 0, //第一次加载,设置1
  160. searchSongList: [], //放置返回数据的数组,设为空
  161. isFromSearch: true, //第一次加载,设置true
  162. searchLoading: false, //把"上拉加载"的变量设为true,显示
  163. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  164. });
  165. }
  166. console.log(res.data.rows)
  167. if (res == null || res.data == null) {
  168. console.error('网络请求失败');
  169. return;
  170. }
  171. }
  172. });
  173. } else if (tab == "price" && tabVal == '1') {//切换数据 为 从高到低 (箭头朝下)
  174. wx.request({
  175. url: that.data.host +"commodityapi/findAllforseachpriceup",
  176. data: {
  177. commodity_name: that.data.commodity_name,
  178. offset: 0,
  179. limit: 10,
  180. },
  181. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  182. header: {
  183. 'Accept': 'application/json'
  184. },
  185. success: function (res) {
  186. that.setData({
  187. contentList: res.data.rows,
  188. commodity_name: that.data.commodity_name,
  189. activeIndex: index,
  190. tab: tab,
  191. pageNo: 1,
  192. dataVal: "0",
  193. pre: "../../images/search_list_zh_n.png",
  194. searchPageNum: 0, //第一次加载,设置1
  195. searchSongList: [], //放置返回数据的数组,设为空
  196. isFromSearch: true, //第一次加载,设置true
  197. searchLoading: true, //把"上拉加载"的变量设为true,显示
  198. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  199. });
  200. if (res.data.rows.length < 10) {
  201. that.setData({
  202. searchPageNum: 0, //第一次加载,设置1
  203. searchSongList: [], //放置返回数据的数组,设为空
  204. isFromSearch: true, //第一次加载,设置true
  205. searchLoading: false, //把"上拉加载"的变量设为true,显示
  206. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  207. });
  208. }
  209. if (res == null || res.data == null) {
  210. console.error('网络请求失败');
  211. return;
  212. }
  213. }
  214. });
  215. }else {
  216. var that = this;
  217. var host = getApp().globalData.servsers;
  218. //检索商品列表
  219. wx.request({
  220. url: host + "commodityapi/findAllforseach",
  221. data: {
  222. commodity_name: that.data.commodity_name,
  223. offset: 0,
  224. limit: 10,
  225. },
  226. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  227. header: {
  228. 'Accept': 'application/json'
  229. },
  230. success: function (res) {
  231. that.setData({
  232. contentList: res.data.rows,
  233. commodity_name: that.data.commodity_name,
  234. host: host, searchPageNum: 0, //第一次加载,设置1
  235. searchSongList: [], //放置返回数据的数组,设为空
  236. isFromSearch: true, //第一次加载,设置true
  237. searchLoading: true, //把"上拉加载"的变量设为true,显示
  238. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  239. });
  240. if (res.data.rows.length < 10) {
  241. that.setData({
  242. searchPageNum: 0, //第一次加载,设置1
  243. searchSongList: [], //放置返回数据的数组,设为空
  244. isFromSearch: true, //第一次加载,设置true
  245. searchLoading: false, //把"上拉加载"的变量设为true,显示
  246. searchLoadingComplete: false //把“没有数据”设为false,隐藏
  247. });
  248. }
  249. if (res == null || res.data == null) {
  250. console.error('网络请求失败');
  251. return;
  252. }
  253. }
  254. });
  255. that.setData({
  256. activeIndex: index,
  257. tab: tab,
  258. pageNo: 1,
  259. pre: "../../images/search_list_zh_pre.png"
  260. })
  261. }
  262. },
  263. //刷新处理
  264. // refesh: function (e) {
  265. // var that = this;
  266. // that.setData({
  267. // hasRefesh: true,
  268. // });
  269. // //检索商品列表
  270. // wx.request({
  271. // url: that.data.host+"commodityapi/findAllforseach",
  272. // data: {
  273. // commodity_name: this.data.commodity_name,
  274. // offset: 0,
  275. // limit: 1000
  276. // },
  277. // method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  278. // header: {
  279. // 'Accept': 'application/json'
  280. // },
  281. // success: function (res) {
  282. // that.setData({
  283. // contentList: res.data.rows
  284. // });
  285. // if (res == null || res.data == null) {
  286. // console.error('网络请求失败');
  287. // return;
  288. // }
  289. // }
  290. // });
  291. // },
  292. fetchSearchList: function () {//加载更多
  293. var that = this;
  294. var host = getApp().globalData.servsers;
  295. var forid = that.data.forid;
  296. var tab = that.data.tab;
  297. var tabVal = that.data.tabVal;
  298. if (tab == "price" && tabVal == '0') {//切换数据 为 从低到高 (箭头朝上)
  299. wx.request({
  300. url: that.data.host + "commodityapi/findAllforseachpricedown",
  301. data: {
  302. commodity_name: that.data.commodity_name,
  303. offset: parseInt(that.data.searchPageNum)*10,
  304. limit: 10
  305. },
  306. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  307. header: {
  308. 'Accept': 'application/json'
  309. },
  310. success: function (res) {
  311. if (res.data.rows.length != 0) {
  312. let contentList = [];
  313. //如果isFromSearch是true从data中取出数据,否则先从原来的数据继续添加
  314. that.data.isFromSearch ? contentList = res.data.rows : contentList = that.data.contentList.concat(res.data.rows)
  315. that.setData({
  316. contentList: contentList, //获取数据数组
  317. searchLoading: true //把"上拉加载"的变量设为false,显示
  318. });
  319. //没有数据了,把“没有数据”显示,把“上拉加载”隐藏
  320. } else {
  321. that.setData({
  322. searchLoadingComplete: true, //把“没有数据”设为true,显示
  323. searchLoading: false //把"上拉加载"的变量设为false,隐藏
  324. });
  325. }
  326. if (res == null || res.data == null) {
  327. console.error('网络请求失败');
  328. return;
  329. }
  330. wx.hideNavigationBarLoading(
  331. that.setData({
  332. hidden: 'hidden'
  333. })
  334. )
  335. }
  336. });
  337. } else if (tab == "price" && tabVal == '1') {//切换数据 为 从高到低 (箭头朝下)
  338. wx.request({
  339. url: that.data.host + "commodityapi/findAllforseachpriceup",
  340. data: {
  341. commodity_name: that.data.commodity_name,
  342. offset: parseInt(that.data.searchPageNum)*10,
  343. limit: 10
  344. },
  345. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  346. header: {
  347. 'Accept': 'application/json'
  348. },
  349. success: function (res) {
  350. if (res.data.rows.length != 0) {
  351. let contentList = [];
  352. //如果isFromSearch是true从data中取出数据,否则先从原来的数据继续添加
  353. that.data.isFromSearch ? contentList = res.data.rows : contentList = that.data.contentList.concat(res.data.rows)
  354. that.setData({
  355. contentList: contentList, //获取数据数组
  356. searchLoading: true //把"上拉加载"的变量设为false,显示
  357. });
  358. //没有数据了,把“没有数据”显示,把“上拉加载”隐藏
  359. } else {
  360. that.setData({
  361. searchLoadingComplete: true, //把“没有数据”设为true,显示
  362. searchLoading: false //把"上拉加载"的变量设为false,隐藏
  363. });
  364. }
  365. if (res == null || res.data == null) {
  366. console.error('网络请求失败');
  367. return;
  368. }
  369. wx.hideNavigationBarLoading(
  370. that.setData({
  371. hidden: 'hidden'
  372. })
  373. )
  374. }
  375. });
  376. }else{
  377. //检索商品列表
  378. wx.request({
  379. url: host + "commodityapi/findAllforseach",
  380. data: {
  381. commodity_name: that.data.commodity_name,
  382. offset: parseInt(that.data.searchPageNum),
  383. limit: 10
  384. },
  385. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  386. header: {
  387. 'Accept': 'application/json'
  388. },
  389. success: function (res) {
  390. if (res.data.rows.length != 0) {
  391. let contentList = [];
  392. //如果isFromSearch是true从data中取出数据,否则先从原来的数据继续添加
  393. that.data.isFromSearch ? contentList = res.data.rows : contentList = that.data.contentList.concat(res.data.rows)
  394. that.setData({
  395. contentList: contentList, //获取数据数组
  396. searchLoading: true //把"上拉加载"的变量设为false,显示
  397. });
  398. //没有数据了,把“没有数据”显示,把“上拉加载”隐藏
  399. } else {
  400. that.setData({
  401. searchLoadingComplete: true, //把“没有数据”设为true,显示
  402. searchLoading: false //把"上拉加载"的变量设为false,隐藏
  403. });
  404. }
  405. if (res == null || res.data == null) {
  406. console.error('网络请求失败');
  407. return;
  408. }
  409. wx.hideNavigationBarLoading(
  410. that.setData({
  411. hidden: 'hidden'
  412. })
  413. )
  414. }
  415. });
  416. }
  417. },
  418. //滚动到底部触发事件
  419. searchScrollLower: function () {
  420. var that = this;
  421. if (that.data.searchLoading && !that.data.searchLoadingComplete) {
  422. that.setData({
  423. searchPageNum: that.data.searchPageNum + 1, //每次触发上拉事件,把searchPageNum+1
  424. isFromSearch: false //触发到上拉事件,把isFromSearch设为为false
  425. });
  426. that.fetchSearchList();
  427. }
  428. }
  429. })
  430. var that;
  431. var Util = require('../../utils/util.js');