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.

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