index.js 7.8 KB

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