index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // index.js
  2. // 获取应用实例
  3. import {
  4. config,
  5. formData
  6. } from './columns.js'
  7. const dataHttps = require("../../utils/serverData");
  8. const https = require("../../utils/serverResource");
  9. import {
  10. createStoreBindings
  11. } from 'mobx-miniprogram-bindings'
  12. import {
  13. store
  14. } from "../../store/index";
  15. Page({
  16. data: {
  17. hasData: true,
  18. popupOptions: [],
  19. popupType: "city",
  20. popupKey: "city",
  21. popupTitle: "活动城市",
  22. popupApi: "",
  23. show: false,
  24. length: 0,
  25. hasRefund: false,
  26. resourceList: [],
  27. formData: {
  28. ...formData
  29. },
  30. type_id_option: [],
  31. area_id_option: [],
  32. attendance_id_option: [],
  33. store_spec_id_option: [],
  34. },
  35. onLoad() {
  36. this.storeBindings = createStoreBindings(this, {
  37. store,
  38. fields: ["token"],
  39. actions: ["setStore"],
  40. });
  41. this.getResourceList()
  42. this.getListAll()
  43. },
  44. onUnload() {
  45. this.storeBindings.destroyStoreBindings();
  46. },
  47. onShow() {
  48. if (typeof this.getTabBar === 'function' &&
  49. this.getTabBar()) {
  50. this.getTabBar().setData({
  51. selected: 1
  52. })
  53. }
  54. },
  55. async getListAll() {
  56. const list1 = await this.getDateList('type_id');
  57. const list2 = await this.getDateList('area_id');
  58. const list3 = await this.getDateList('attendance_id');
  59. const list4 = await this.getDateList('store_spec_id');
  60. this.setData({
  61. type_id_option: list1,
  62. area_id_option: list2,
  63. attendance_id_option: list3,
  64. store_spec_id_option: list4,
  65. })
  66. // console.log(this.data.type_id_option);
  67. },
  68. async getDateList(key) {
  69. const {
  70. api,
  71. isok,
  72. unit
  73. } = config[key]
  74. let resList = [{
  75. value: '0',
  76. text: '不限'
  77. }]
  78. const {
  79. code,
  80. data
  81. } = await dataHttps[api]({
  82. page: 1,
  83. limit: 99999,
  84. status: 1
  85. })
  86. if (code === 1) {
  87. const {
  88. list
  89. } = data
  90. list.forEach(si => {
  91. resList.push({
  92. value: si.id + "",
  93. min: si.min,
  94. max: si.max,
  95. text: isok ? si.name : String(si.max) === '0' ? `${si.min}+${unit}` : `${si.min}~${si.max}${unit}`
  96. })
  97. })
  98. } else {
  99. resList = []
  100. await this.setStore("", "");
  101. }
  102. return resList
  103. },
  104. typeChange(e) {
  105. const type = e.currentTarget.dataset.type;
  106. const value = e.detail
  107. let list = this.data[type + '_option'],
  108. titleVlue = ""
  109. const index = list.findIndex(
  110. (item) => item.value == value
  111. )
  112. titleVlue = list[index].text
  113. const {
  114. formData,
  115. } = this.data
  116. let model = {
  117. ...formData
  118. }
  119. model[type] = value
  120. model[type + '_title'] = titleVlue
  121. model.page = 1
  122. this.setData({
  123. formData: model,
  124. })
  125. // console.log(formData);
  126. // console.log(this.data.formData);
  127. this.getResourceList()
  128. },
  129. onReachBottom() {
  130. // console.log(' onReachBottom');
  131. const {
  132. formData,
  133. } = this.data
  134. let model = {
  135. ...formData
  136. }
  137. model.page = model.page + 1
  138. this.setData({
  139. formData: model,
  140. })
  141. // console.log(this.data.formData)
  142. this.getResourceList()
  143. },
  144. // 事件处理函数
  145. bindViewTap(e) {
  146. const type = e.currentTarget.dataset.type;
  147. // console.log(type);
  148. wx.navigateTo({
  149. url: '../../userResource/pages/resource-details/index?id=' + type
  150. })
  151. },
  152. onSearch(e) {
  153. const value = e.detail && e.detail.value !== undefined ? e.detail.value : e.detail
  154. // console.log(value);
  155. const {
  156. formData,
  157. } = this.data
  158. let model = {
  159. ...formData
  160. }
  161. model.page = 1
  162. model.store_name = value
  163. this.setData({
  164. formData: model,
  165. })
  166. this.getResourceList()
  167. },
  168. aaa() {
  169. this.setData({
  170. show: true
  171. })
  172. // console.log('aaa');
  173. },
  174. popupLeft() {
  175. this.setData({
  176. show: false,
  177. });
  178. },
  179. onConfirm() {
  180. this.selectComponent('#item').toggle();
  181. },
  182. async getResourceList() {
  183. const {
  184. formData,
  185. resourceList,
  186. hasData
  187. } = this.data
  188. const {
  189. page,
  190. size
  191. } = formData
  192. if (page !== 1 && hasData === false) {
  193. this.setData({
  194. formData: {
  195. ...formData,
  196. page: page-1
  197. }
  198. })
  199. return
  200. }
  201. if (page === 1) {
  202. wx.pageScrollTo({
  203. scrollTop: 0,
  204. duration: 500
  205. })
  206. }
  207. let arr = [];
  208. let model = {
  209. ...formData
  210. }
  211. for (let key in model) {
  212. if (model[key] === "0") {
  213. model[key] = ""
  214. }
  215. }
  216. const {
  217. code,
  218. data
  219. } = await https.list(model)
  220. if (code == 1) {
  221. const {
  222. list,
  223. count
  224. } = data
  225. arr = page == 1 ? list : resourceList.concat(list)
  226. const total = page * size
  227. this.setData({
  228. resourceList: arr,
  229. length: arr.length,
  230. hasData: count > total
  231. })
  232. } else {
  233. await this.setStore("", "");
  234. }
  235. }
  236. })