search.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // pages/search/search.js
  2. const app = getApp();
  3. const api = require('../../api/api');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. navbarData: {
  10. showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
  11. title: '搜索', //导航栏 中间的标题,
  12. capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页)
  13. },
  14. news: [], //历史搜索
  15. dingyues: [], //团长列表
  16. show: false,
  17. isShowSearchResult: false,
  18. keywords: '', //搜索关键字
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. let that = this;
  25. // 获取
  26. wx.showLoading({
  27. title: '加载中',
  28. mask: true
  29. })
  30. wx.request({
  31. url: api.Head_list,
  32. header: {
  33. 'Authorization': wx.getStorageSync('token')
  34. },
  35. success(res) {
  36. console.log(res);
  37. if (res.data.code === 1) {
  38. that.setData({
  39. dingyues: res.data.data
  40. })
  41. } else {
  42. wx.showToast({
  43. title: res.data.msg,
  44. mask: true,
  45. icon: 'none'
  46. })
  47. }
  48. },
  49. fail(err) {
  50. wx.showToast({
  51. title: '发起网络请求失败',
  52. icon: 'none',
  53. mask: true
  54. })
  55. },
  56. complete() {
  57. wx.hideLoading()
  58. }
  59. })
  60. },
  61. delhis() {
  62. this.setData({
  63. show: true
  64. })
  65. },
  66. onClose() {
  67. this.setData({
  68. show: false
  69. })
  70. },
  71. /**
  72. * 生命周期函数--监听页面初次渲染完成
  73. */
  74. onReady: function () {
  75. },
  76. /**
  77. * 生命周期函数--监听页面显示
  78. */
  79. onShow: function () {
  80. this.getSearchList()
  81. },
  82. /**
  83. * 生命周期函数--监听页面隐藏
  84. */
  85. onHide: function () {
  86. },
  87. /**
  88. * 生命周期函数--监听页面卸载
  89. */
  90. onUnload: function () {
  91. },
  92. /**
  93. * 页面相关事件处理函数--监听用户下拉动作
  94. */
  95. onPullDownRefresh: function () {
  96. },
  97. /**
  98. * 页面上拉触底事件的处理函数
  99. */
  100. onReachBottom: function () {
  101. },
  102. /**
  103. * 用户点击右上角分享
  104. */
  105. onShareAppMessage: function () {
  106. },
  107. // 获取历史搜索记录
  108. getSearchList() {
  109. let that = this;
  110. // 获取用户历史搜索
  111. wx.showLoading({
  112. title: '加载中',
  113. mask: true
  114. })
  115. wx.request({
  116. url: api.Search_list,
  117. header: {
  118. 'Authorization': wx.getStorageSync('token')
  119. },
  120. success(res) {
  121. console.log(res);
  122. if (res.data.code === 1) {
  123. that.setData({
  124. news: res.data.data
  125. })
  126. } else {
  127. wx.showToast({
  128. title: res.data.msg,
  129. mask: true,
  130. icon: 'none'
  131. })
  132. }
  133. },
  134. fail(err) {
  135. wx.showToast({
  136. title: '发起网络请求失败',
  137. icon: 'none',
  138. mask: true
  139. })
  140. },
  141. complete() {
  142. wx.hideLoading()
  143. }
  144. })
  145. },
  146. inputKeyWords(e) {
  147. console.log(e.detail.value)
  148. if (e.detail.value === '') {
  149. this.setData({
  150. keywords: e.detail.value,
  151. isShowSearchResult: false
  152. })
  153. let that = this;
  154. // 获取
  155. wx.showLoading({
  156. title: '加载中',
  157. mask: true
  158. })
  159. wx.request({
  160. url: api.Head_list,
  161. header: {
  162. 'Authorization': wx.getStorageSync('token')
  163. },
  164. success(res) {
  165. console.log(res);
  166. if (res.data.code === 1) {
  167. that.setData({
  168. dingyues: res.data.data
  169. })
  170. } else {
  171. wx.showToast({
  172. title: res.data.msg,
  173. mask: true,
  174. icon: 'none'
  175. })
  176. }
  177. },
  178. fail(err) {
  179. wx.showToast({
  180. title: '发起网络请求失败',
  181. icon: 'none',
  182. mask: true
  183. })
  184. },
  185. complete() {
  186. wx.hideLoading()
  187. }
  188. })
  189. that.getSearchList()
  190. } else {
  191. this.setData({
  192. keywords: e.detail.value
  193. })
  194. }
  195. },
  196. // 搜索
  197. search() {
  198. let that = this;
  199. if (that.data.keywords !== '') {
  200. // console.log('搜索:' + this.data.keywords);
  201. // 获取订阅团长列表
  202. wx.showLoading({
  203. title: '加载中',
  204. mask: true
  205. })
  206. wx.request({
  207. url: api.Head_list,
  208. header: {
  209. 'Authorization': wx.getStorageSync('token')
  210. },
  211. data: {
  212. name: that.data.keywords
  213. },
  214. success(res) {
  215. console.log(res);
  216. if (res.data.code === 1) {
  217. that.setData({
  218. dingyues: res.data.data,
  219. isShowSearchResult: true
  220. })
  221. } else {
  222. wx.showToast({
  223. title: res.data.msg,
  224. mask: true,
  225. icon: 'none'
  226. })
  227. }
  228. },
  229. fail(err) {
  230. wx.showToast({
  231. title: '发起网络请求失败',
  232. icon: 'none',
  233. mask: true
  234. })
  235. },
  236. complete() {
  237. wx.hideLoading()
  238. }
  239. })
  240. }
  241. },
  242. // 点击历史搜索
  243. clickHistoryItem(e) {
  244. this.setData({
  245. keywords: e.currentTarget.dataset.name
  246. })
  247. },
  248. // 删除所有历史搜索记录
  249. confirmDelete() {
  250. let that = this;
  251. let arr = [];
  252. if(that.data.news.length == 0){
  253. wx.showToast({
  254. title: '暂无记录,无需删除',
  255. icon: 'none',
  256. mask: true
  257. })
  258. return
  259. }
  260. for (let i = 0; i < that.data.news.length; i++) {
  261. arr.push(that.data.news[i].id)
  262. }
  263. wx.showLoading({
  264. title: '删除中',
  265. mask: true
  266. })
  267. wx.request({
  268. url: api.Search_delete,
  269. header: {
  270. 'Authorization': wx.getStorageSync('token')
  271. },
  272. method: 'POST',
  273. data: {
  274. id: arr
  275. },
  276. success(res) {
  277. console.log(res);
  278. wx.hideLoading()
  279. if (res.data.code === 1) {
  280. wx.showToast({
  281. title: '删除成功',
  282. mask: true,
  283. icon: 'none',
  284. success() {
  285. that.setData({
  286. show: false
  287. })
  288. setTimeout(()=>{
  289. that.getSearchList()
  290. }, 1500)
  291. }
  292. })
  293. } else {
  294. wx.showToast({
  295. title: res.data.msg,
  296. mask: true,
  297. icon: 'none'
  298. })
  299. }
  300. },
  301. fail(err) {
  302. wx.hideLoading()
  303. wx.showToast({
  304. title: '发起网络请求失败',
  305. icon: 'none',
  306. mask: true
  307. })
  308. },
  309. complete() {
  310. // wx.hideLoading()
  311. }
  312. })
  313. },
  314. // 团长跳转
  315. navToTuanZhang(e) {
  316. let url = e.currentTarget.dataset.url;
  317. let id = e.currentTarget.dataset.id;
  318. wx.navigateTo({
  319. url: url + '&id=' + id
  320. })
  321. },
  322. })