index.js 7.9 KB

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