index.js 8.5 KB

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