index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // pages/yuezhan/search/index.js
  2. const { request, serverName, imgServer } = require('../../../utils/services');
  3. const {
  4. newRequestFns,
  5. newServerName
  6. } = require('../../../utils/newServices.js');
  7. const { Toast } = require('../../../utils/util');
  8. const { defaultImg, noExhibitionImg, noExhibitionHallImg } = require('../../../utils/images');
  9. const app = getApp();
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. tabs: ['展览', '展馆'],
  16. testImg: '../../../imgs/testImg/thumbSmallImg2.jpg',
  17. avatar: '../../../imgs/testImg/1.jpg',
  18. activeIndex:0,
  19. animationData: "",
  20. isLike:false,
  21. loading:false,
  22. currentPage:1,
  23. exhibitionList:[],
  24. pavilionList:[],
  25. totalElements:''
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. this.setData({
  32. searchText: options.searchText,
  33. serverName,
  34. imgServer
  35. });
  36. // 检查 globalData 是否已有位置信息
  37. if (app.globalData.latitude && app.globalData.longitude) {
  38. // 使用 globalData 中的经纬度
  39. this.setData({
  40. latitude: app.globalData.latitude,
  41. longitude: app.globalData.longitude
  42. });
  43. // 处理 city 设置
  44. if (app.globalData.locationName) {
  45. app.globalData.city = app.globalData.locationName;
  46. } else {
  47. app.globalData.city = "北京";
  48. }
  49. this.loadData();
  50. }
  51. // this.getKeywordCode()
  52. this.setData({
  53. noExhibitionImg,
  54. noExhibitionHallImg,
  55. defaultImg
  56. })
  57. },
  58. keywordSearch:function(e){
  59. let { keyword } = e.currentTarget.dataset;
  60. this.setData({
  61. searchText: keyword
  62. })
  63. console.log("s",e)
  64. this.loadData();
  65. },
  66. getKeywordCode:function(){
  67. newRequestFns["getKeywordCode"]({}, "", res => {
  68. if (res.data.code > -1) {
  69. let {data:keyword} = res.data;
  70. this.setData({
  71. keyword
  72. })
  73. wx.stopPullDownRefresh();
  74. console.log(res)
  75. }
  76. }, err => {
  77. }, complete => {
  78. })
  79. },
  80. loadData: function () {
  81. // 通过activeIndex 判断应该load哪部分的数据
  82. const { activeIndex } = this.data;
  83. this.setData({
  84. exhibitionList: [],
  85. pavilionList: []
  86. });
  87. if (activeIndex == 0) {
  88. this.getExhibitionSearch(1);
  89. if (this.data.exhibitionList.length < 5){
  90. this.popular();
  91. }
  92. }
  93. else if (activeIndex == 1) {
  94. this.getPavilionSearch(1);
  95. }
  96. else {
  97. return
  98. }
  99. },
  100. addLike: function (e) {
  101. let { type, id, idx } = e.currentTarget.dataset;
  102. console.log(idx, type, id)
  103. let { collectedArr, collectedChange } = app.globalData, hasItem = true;
  104. let exhibitionList = this.data.exhibitionList;
  105. console.log('type', type)
  106. Toast.showToast2('loading');
  107. let loginSessionKey = wx.getStorageSync('token') || "";
  108. // if (loginSessionKey){
  109. newRequestFns['isCollect']({
  110. loginSessionKey,
  111. exhibitionId: id,
  112. type: Number(type),
  113. }, "post", res => {
  114. if (res.data.code > -1) {
  115. for (let i = 0; i < collectedArr.length; i++) {
  116. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  117. console.log("true")
  118. collectedArr[i] = {
  119. collectedId: id,
  120. status: res.data.data.hasCollect,
  121. }
  122. hasItem = false;
  123. }
  124. }
  125. if (hasItem) {
  126. collectedArr.push({
  127. collectedId: id,
  128. status: res.data.data.hasCollect,
  129. })
  130. }
  131. app.globalData.collectedArr = collectedArr;
  132. app.globalData.collectedChange = true;
  133. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  134. this.setData({
  135. exhibitionList: exhibitionList
  136. })
  137. }
  138. }, err => {
  139. }, complete => {
  140. Toast.hideLoading();
  141. })
  142. // }
  143. console.log(this.data.exhibitionList)
  144. },
  145. getExhibitionSearch:function(page){
  146. let name = this.data.searchText||"";
  147. this.setData({
  148. loading: true
  149. })
  150. let { latitude: lat, longitude: lng } = this.data;
  151. newRequestFns["getExhibitionSearch"]({
  152. name,
  153. lat,
  154. lng,
  155. page
  156. },"",res=>{
  157. if(res.data.code>-1){
  158. let tempContent = this.data.exhibitionList
  159. ? this.data.exhibitionList
  160. : [];
  161. let { pageData: exhibitionList, total } = res.data.data;
  162. // 判断是否为最后一页(返回空数组表示没有更多数据)
  163. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  164. if (isLastPage) {
  165. this.setData({
  166. loading: false,
  167. lastPage: true, // 标记为最后一页
  168. totalElements: total || 0
  169. });
  170. wx.stopPullDownRefresh();
  171. return;
  172. }
  173. exhibitionList.forEach((currentValue) => {
  174. currentValue.distance = Math.ceil(currentValue.distance);
  175. currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
  176. })
  177. // 拼接新数据
  178. let newExhibitionList = tempContent.concat(exhibitionList);
  179. // 去重
  180. newExhibitionList = newExhibitionList.reduce((acc, curr) => {
  181. if (!acc.some(item => item.id === curr.id)) {
  182. acc.push(curr);
  183. }
  184. return acc;
  185. }, []);
  186. this.setData({
  187. currentPage: page,
  188. lastPage: false, // 有数据时设为false
  189. loading: false,
  190. exhibitionList: newExhibitionList,
  191. totalElements: total
  192. });
  193. console.log(exhibitionList)
  194. wx.stopPullDownRefresh();
  195. console.log(res)
  196. }
  197. },err=>{
  198. }, complete => {
  199. })
  200. },
  201. getPavilionSearch: function (page) {
  202. let name = this.data.searchText || "";
  203. this.setData({
  204. loading: true
  205. })
  206. let { latitude: lat, longitude: lng } = this.data;
  207. newRequestFns["getPavilionSearch"]({
  208. name,
  209. lat,
  210. lng,
  211. page
  212. }, "", res => {
  213. if (res.data.code > -1) {
  214. let tempContent = this.data.pavilionList
  215. ? this.data.pavilionList
  216. : [];
  217. let { pageData: pavilionList, total } = res.data.data;
  218. // 判断是否为最后一页(返回空数组表示没有更多数据)
  219. let isLastPage = !pavilionList || pavilionList.length === 0;
  220. if (isLastPage) {
  221. this.setData({
  222. loading: false,
  223. lastPage: true
  224. });
  225. wx.stopPullDownRefresh();
  226. return;
  227. }
  228. // 拼接新数据
  229. let newPavilionList = tempContent.concat(pavilionList);
  230. // 去重
  231. newPavilionList = newPavilionList.reduce((acc, curr) => {
  232. if (!acc.some(item => item.id === curr.id)) {
  233. acc.push(curr);
  234. }
  235. return acc;
  236. }, []);
  237. this.setData({
  238. currentPage: page,
  239. lastPage: false, // 有数据时设为false
  240. loading: false,
  241. pavilionList: newPavilionList,
  242. });
  243. wx.stopPullDownRefresh();
  244. console.log(res)
  245. }
  246. }, err => {
  247. }, complete => {
  248. })
  249. },
  250. searchByText: function () {
  251. this.loadData()
  252. },
  253. bindInput: function (event) {
  254. var obj = {},
  255. key = event.target.dataset['key'];
  256. // Toast.showToast('success', event.detail.value);
  257. obj[key] = event.detail.value;
  258. this.setData(obj);
  259. console.log(this.data.searchText)
  260. },
  261. loadMore: function () {
  262. const { activeIndex } = this.data;
  263. // const loadDataFn =
  264. // activeIndex == 0 ? this.loadOneToOrderData : this.loadClassroomData;
  265. let loadDataFn;
  266. if ( activeIndex == 0) {
  267. loadDataFn = this.getExhibitionSearch;
  268. }
  269. else if ( activeIndex == 1) {
  270. loadDataFn = this.getPavilionSearch;
  271. }
  272. else {
  273. return
  274. }
  275. if (!this.data.lastPage) {
  276. this.setData({
  277. loading: true
  278. });
  279. console.log(this.data.currentPage + 1)
  280. loadDataFn(this.data.currentPage + 1);
  281. } else {
  282. return;
  283. }
  284. },
  285. popular : function(){
  286. let { latitude: lat, longitude: lng } = this.data;
  287. newRequestFns["recommend"]({
  288. lat,
  289. lng,
  290. }, "", res => {
  291. this.setData({
  292. ExhibitionPopular: res.data.data
  293. })
  294. }, err => {
  295. }, complete => {
  296. })
  297. },
  298. onReachBottom: function () {
  299. if (!this.data.loading) {
  300. this.loadMore();
  301. console.log('reach Bottom');
  302. }
  303. },
  304. tabClick: function (e) {
  305. let { activeIndex } = this.data;
  306. this.setData({
  307. loading: true,
  308. activeIndex: e.currentTarget.id
  309. })
  310. this.loadData();
  311. },
  312. /**
  313. * 生命周期函数--监听页面初次渲染完成
  314. */
  315. onReady: function () {
  316. },
  317. /**
  318. * 生命周期函数--监听页面显示
  319. */
  320. onShow: function () {
  321. let { collectedArr, collectedChange } = app.globalData;
  322. let { exhibitionList } = this.data
  323. let isLogin = app.globalData.isLogin;
  324. // this.setData({
  325. // exhibitionList: []
  326. // });
  327. // this.getBanner();
  328. // this.getList(1);
  329. this.setData({
  330. isLogin
  331. })
  332. if (collectedChange) {
  333. for (let i = 0; i < exhibitionList.length; i++) {
  334. for (let j = 0; j < collectedArr.length; j++) {
  335. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  336. exhibitionList[i].hasCollect = collectedArr[j].status
  337. if (exhibitionList[i].hasCollect) {
  338. exhibitionList[i].collectionsCount += 1;
  339. }
  340. else {
  341. exhibitionList[i].collectionsCount -= 1;
  342. }
  343. }
  344. }
  345. // console.log(collectedArr.status, collectedArr.collectedId)
  346. }
  347. this.setData({
  348. exhibitionList,
  349. })
  350. }
  351. // app.globalData.clickToSelect = false;
  352. // app.globalData.collectedChange = false;
  353. },
  354. /**
  355. * 生命周期函数--监听页面隐藏
  356. */
  357. onHide: function () {
  358. },
  359. /**
  360. * 生命周期函数--监听页面卸载
  361. */
  362. onUnload: function () {
  363. },
  364. /**
  365. * 页面相关事件处理函数--监听用户下拉动作
  366. */
  367. onPullDownRefresh: function () {
  368. this.loadData();
  369. },
  370. /**
  371. * 用户点击右上角分享
  372. */
  373. onShareAppMessage: function () {
  374. }
  375. })