index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. const https = require("../../utils/serverData");
  2. const dayjs = require("dayjs")
  3. import {
  4. storeBindingsBehavior
  5. } from 'mobx-miniprogram-bindings'
  6. import {
  7. store
  8. } from "../../store/index";
  9. Component({
  10. behaviors: [storeBindingsBehavior],
  11. properties: {
  12. popupType: "",
  13. popupKey: "",
  14. popupTitle: "",
  15. popupValue: "",
  16. popupApi: "",
  17. show: false
  18. },
  19. data: {
  20. popupOptions: [],
  21. loading: false,
  22. value: "",
  23. label: "",
  24. },
  25. storeBindings: {
  26. store: store,
  27. fields: {
  28. token: () => store.token,
  29. },
  30. actions: {
  31. setStore: 'setStore',
  32. }
  33. },
  34. observers: { // 设置组件的侦听属性
  35. show(val) {
  36. // console.log(val)
  37. if (val) {
  38. // wx.setNavigationBarTitle({
  39. // title: this.data.popupTitle
  40. // })
  41. const {
  42. popupType,
  43. popupValue
  44. } = this.data
  45. if (popupType === "radio") {
  46. this.setData({
  47. value: popupValue
  48. })
  49. }
  50. if (popupType === 'checkbox' || popupType === "radio") {
  51. this.getDateList()
  52. } else {
  53. this.setData({
  54. popupOptions: [],
  55. })
  56. }
  57. }
  58. },
  59. },
  60. methods: {
  61. async getDateList() {
  62. let resList = []
  63. const {
  64. popupType,
  65. popupValue
  66. } = this.data
  67. const {
  68. code,
  69. data,
  70. msg
  71. } = await https[this.data.popupApi]({
  72. page: 1,
  73. limit: 999999,
  74. status: '1'
  75. })
  76. if (code === 1) {
  77. const {
  78. list
  79. } = data
  80. // console.log(popupValue);
  81. list.forEach(si => {
  82. if (popupType === 'checkbox') {
  83. let index = popupValue.findIndex((s) => s + '' === si.id + '')
  84. resList.push({
  85. ...si,
  86. selected: index !== -1
  87. })
  88. } else {
  89. resList.push({
  90. ...si,
  91. name: si.name ? si.name : String(si.max) !== '0' ? `${si.min}~${si.max}人` : `${si.min}+人`
  92. })
  93. }
  94. })
  95. console.log(resList);
  96. } else {
  97. await this.setStore("", "");
  98. }
  99. this.setData({
  100. popupOptions: resList,
  101. loading: false
  102. })
  103. },
  104. radioChange(e) {
  105. const index = Number(e.currentTarget.dataset.index)
  106. const options = this.data.popupOptions
  107. const value = index === "" ? "" : options[index].id
  108. const label = index === "" ? "" : options[index].name
  109. this.returnItem({
  110. value,
  111. label
  112. })
  113. },
  114. timeConfirm(e) {
  115. // console.log(e.detail);
  116. let str = "",
  117. time1 = "",
  118. time2 = "",
  119. num = 0;
  120. if (e.detail.length === 2) {
  121. time1 = dayjs(e.detail[0]).format('YYYY-MM-DD');
  122. time2 = dayjs(e.detail[1]).format('YYYY-MM-DD');
  123. let timeStamp1 = new Date(e.detail[0]).valueOf(),
  124. timeStamp2 = new Date(e.detail[1]).valueOf(),
  125. timeType1 = dayjs(e.detail[0]).format('MM.DD'),
  126. timeType2 = dayjs(e.detail[1]).format('MM.DD'),
  127. conver = 1000 * 60 * 60 * 24;
  128. num = ((timeStamp2 - timeStamp1) / conver) + 1
  129. str = `${timeType1}-${timeType2},${num}天`
  130. }
  131. this.returnItem({
  132. value: time1,
  133. label: str,
  134. count: num,
  135. end: time2
  136. })
  137. },
  138. //复选框确定
  139. checkboxSelected() {
  140. const options = this.data.popupOptions
  141. let list = [],
  142. str = "";
  143. options.forEach((s) => {
  144. if (s.selected) {
  145. list.push(s.id)
  146. str += str === '' ? s.name : `+${s.name}`
  147. }
  148. });
  149. this.returnItem({
  150. value: list,
  151. label: str,
  152. })
  153. },
  154. //复选框选择
  155. checkboxChange(e) {
  156. const index = Number(e.currentTarget.dataset.index)
  157. const options = this.data.popupOptions
  158. options[Number(index)].selected = !options[Number(index)].selected
  159. this.setData({
  160. popupOptions: options
  161. });
  162. },
  163. onItemClick(e) {
  164. const {
  165. value,
  166. label
  167. } = e.detail
  168. this.returnItem({
  169. value,
  170. label,
  171. })
  172. },
  173. onClickLeft() {
  174. this.triggerEvent("leftClick")
  175. },
  176. returnItem(model) {
  177. let modal = {
  178. popupType: this.data.popupType,
  179. popupKey: this.data.popupKey,
  180. ...model
  181. }
  182. this.triggerEvent("itemClick", modal)
  183. },
  184. showToastMsg(msg) {
  185. wx.showToast({
  186. title: msg,
  187. icon: 'none',
  188. duration: 2000
  189. })
  190. },
  191. }
  192. })