C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 line
5.5 KiB

  1. // pages/index/sw/index.js
  2. let config = require("../../../config/config.js");
  3. let Http = require("../../../utils/HttpBasics");
  4. let app = getApp();
  5. // 请求数据
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. loading: {
  12. type: Boolean,
  13. value: ''
  14. },
  15. loadingtext: {
  16. type: String,
  17. value: ''
  18. },
  19. },
  20. /**
  21. * 组件的初始数据
  22. */
  23. data: {
  24. actUrl: wx.getStorageSync('imgurl').act.url,
  25. loadingUrl: wx.getStorageSync('imgurl').loading.url,
  26. tabs: [],
  27. list: [],
  28. flag: "",
  29. loading: true, //"上拉加载"的变量,默认false,隐藏
  30. content: "",
  31. return_list: [],
  32. current: 0,
  33. current_scroll: 0,
  34. pageNum: 1, // 设置加载的第几次,默认是第一次
  35. pageSize: 10, //返回数据的个数
  36. searchLoadingComplete: false, //“没有数据”的变量,默认false,隐藏
  37. allow_load: true, // 是否允许继续加载标识 默认 true 允许,false 加载完成
  38. },
  39. /**
  40. * 组件的方法列表
  41. */
  42. methods: {
  43. handleChange({
  44. detail
  45. }) {
  46. this.setData({
  47. current: detail.key
  48. });
  49. },
  50. handleChangeScroll({
  51. detail
  52. }) {
  53. let that = this;
  54. // 切换tab时重新初始化下list 和 allow_load
  55. that.setData({
  56. current_scroll: detail.key,
  57. flag: "click",
  58. list: [],
  59. allow_load: true
  60. });
  61. this.getList(detail.key, 1);
  62. var val = detail.key;
  63. //通过这个传递数据
  64. var myEventDetail = {
  65. val: val,
  66. pageNum: 1
  67. };
  68. // detail对象,提供给事件监听函数
  69. this.triggerEvent("myevent", myEventDetail);
  70. },
  71. getList(key, pageNum, from) {
  72. var that = this;
  73. console.log("pageNum------>"+pageNum);
  74. /**
  75. * from==refresh
  76. * 表示是从下拉刷新进来的
  77. */
  78. app.couponListCallback = token => {
  79. Http.setToken(token);
  80. // 券list获取
  81. if (from == 'refresh') {
  82. that.setData({
  83. allow_load: true,
  84. list: [],
  85. })
  86. console.log(that.data.list);
  87. }
  88. if (that.data.allow_load) {
  89. that.setData({
  90. loading: true,
  91. content: '小主,我在玩命加载中...'
  92. })
  93. // 根据 key == 0 区分全部或其它tab,决定是否传参数 business
  94. if (key == 0) {
  95. var param = {
  96. pageNum: pageNum,
  97. pageSize: 6,
  98. targetAd: 1
  99. };
  100. } else {
  101. var param = {
  102. pageNum: pageNum,
  103. pageSize: 6,
  104. business: key,
  105. targetAd: 1
  106. };
  107. }
  108. // 请求接口
  109. Http.get({
  110. url: config.api.couponChannelList,
  111. data: param
  112. }).then(res => {
  113. /**
  114. * 加载完成
  115. */
  116. if (pageNum >= res.data.pages) {
  117. if (res.data.pages == 0 || res.data.pages == 1) {
  118. that.setData({
  119. allow_load: true,
  120. loading: false,
  121. content: ""
  122. });
  123. }
  124. else {
  125. that.setData({
  126. allow_load: false,
  127. loading: true,
  128. content: "——— 再拉裤子就掉了啦 ———",
  129. });
  130. }
  131. }
  132. if (pageNum == 1) {
  133. that.setData({
  134. list: [],
  135. })
  136. }
  137. var tmpArr = that.data.list;
  138. tmpArr.push.apply(tmpArr, res.data.list);
  139. that.setData({
  140. list: tmpArr
  141. })
  142. })
  143. .catch(err => {
  144. wx.showToast({
  145. title: err.errMsg,
  146. icon: 'none',
  147. duration: 2000,
  148. mask: false
  149. });
  150. })
  151. setTimeout(function () {
  152. that.setData({
  153. loading: false,
  154. })
  155. }, 1400);
  156. }
  157. else {
  158. that.setData({
  159. loading: true,
  160. content: "——— 再拉裤子就掉了啦 ———"
  161. })
  162. setTimeout(function () {
  163. that.setData({
  164. loading: false,
  165. })
  166. }, 1400)
  167. }
  168. };
  169. if (app.globalData.token && app.globalData.token != null) {
  170. app.couponListCallback(app.globalData.token);
  171. }
  172. }
  173. },
  174. ready() {
  175. let that = this;
  176. app.businessListCallback = token => {
  177. Http.setToken(token);
  178. // business获取
  179. Http.get({
  180. url: config.api.businessList,
  181. data: {
  182. pageNum: 1,
  183. pageSize: 15,
  184. type: 1
  185. }
  186. }).then(res => {
  187. console.log(res)
  188. let businessObj = [{id:0,title: "全部",type: 1}];
  189. that.setData({
  190. tabs: res.data.list.concat(businessObj).sort(compare("id"))
  191. });
  192. console.log(that.data.tabs)
  193. })
  194. .catch(err => {
  195. wx.showToast({
  196. title: err.errMsg,
  197. icon: 'none',
  198. duration: 2000,
  199. mask: false
  200. });
  201. })
  202. };
  203. if (app.globalData.token && app.globalData.token != null) {
  204. app.businessListCallback(app.globalData.token);
  205. }
  206. that.setData({
  207. list: []
  208. })
  209. that.getList(0, 1);
  210. }
  211. });
  212. function compare(pro) {
  213. return function (obj2, obj1) {
  214. var val1 = obj1[pro];
  215. var val2 = obj2[pro];
  216. if (val1 < val2) {
  217. return 1;
  218. } else if (val1 > val2) {
  219. return -1;
  220. } else {
  221. return 0;
  222. }
  223. }
  224. }