index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. const _Http = getApp().globalData.http;
  2. import {
  3. getHeight
  4. } from "../../utils/getHeight";
  5. import {
  6. formatTime
  7. } from "../../utils/getTime"
  8. Component({
  9. options: {
  10. multipleSlots: true
  11. },
  12. properties: {
  13. list: {
  14. type: {
  15. type: Array
  16. },
  17. value: [{
  18. label: "筛选1",
  19. index: null,
  20. showName: "name", //显示字段
  21. valueKey: "name", //返回Key
  22. selectKey: "id", //传参 代表选着字段 不传参返回整个选择对象
  23. value: "", //选中值
  24. default: "", //默认索引
  25. list: [{
  26. name: "a1",
  27. id: 0
  28. }, {
  29. name: "a2",
  30. id: 1
  31. }]
  32. }]
  33. },
  34. show: {
  35. type: Boolean
  36. },
  37. handle: {
  38. type: Function
  39. }, //按钮回调函数
  40. dateRange: {
  41. type: Boolean
  42. }, //是否开启日期范围筛选
  43. dateRangeName: {
  44. type: String,
  45. value: "日期范围"
  46. },
  47. dateRange1: {
  48. type: Boolean
  49. }, //是否开启日期范围筛选
  50. zIndex: {
  51. type: String,
  52. value: 99999,
  53. },
  54. interrupt: {
  55. type: Function
  56. },
  57. isReset: {
  58. type: Boolean,
  59. value: true
  60. },
  61. minus: {
  62. type: [Number, String],
  63. value: 80
  64. }
  65. },
  66. data: {
  67. startdate: "", //开始时间
  68. enddate: "", //结束时间
  69. periodstart: "",
  70. periodend: "",
  71. },
  72. observers: {
  73. "show"(newVal) {
  74. if (newVal) getHeight('.head', this).then(res => this.setData({
  75. listHeight: res - this.data.minus
  76. }));
  77. }
  78. },
  79. lifetimes: {
  80. attached() {
  81. getApp().globalData.Language.getLanguagePackage(this)
  82. }
  83. },
  84. methods: {
  85. /* 选择 */
  86. onSelect(e) {
  87. const {
  88. item, //被选项
  89. index, //列表下标
  90. i //被选项下标
  91. } = e.currentTarget.dataset;
  92. if (this.data.list[index].index == i) {
  93. this.setData({
  94. [`list[${index}].value`]: "",
  95. [`list[${index}].index`]: null
  96. });
  97. } else {
  98. this.setData({
  99. [`list[${index}].value`]: this.data.list[index].selectKey ? item[this.data.list[index].selectKey] : item,
  100. [`list[${index}].index`]: i
  101. });
  102. };
  103. if (this.data.list[index].interrupt) this.triggerEvent("interrupt", {
  104. item,
  105. index,
  106. name: this.data.list[index].selectKey,
  107. data: this.data.list[index],
  108. list: this.data.list
  109. })
  110. },
  111. onSelectCheckbox(e) {
  112. const {
  113. item, //被选项
  114. index, //列表下标
  115. i //被选项下标
  116. } = e.currentTarget.dataset,
  117. list = this.data.list;
  118. if (list[index].index == "" || list[index].index == null) {
  119. list[index].index = [];
  120. list[index].value = [];
  121. }
  122. let arrIndex = list[index].index.indexOf(i)
  123. if (arrIndex == -1) {
  124. list[index].index.push(i)
  125. list[index].value.push(list[index].selectKey ? item[list[index].selectKey] : item)
  126. } else {
  127. list[index].index.splice(arrIndex, 1)
  128. list[index].value.splice(arrIndex, 1)
  129. };
  130. this.setData({
  131. list
  132. })
  133. if (this.data.list[index].interrupt) this.triggerEvent("interrupt", {
  134. item,
  135. index,
  136. name: this.data.list[index].selectKey,
  137. list
  138. })
  139. },
  140. /* 点击按钮 */
  141. onClick(e) {
  142. const {
  143. name
  144. } = e.target.dataset;
  145. if (name == 'reset') {
  146. this.setData({
  147. startdate: '',
  148. enddate: ''
  149. })
  150. this.setData({
  151. list: this.data.list.map(v => {
  152. let value = "",
  153. index = null;
  154. try {
  155. if ((v.default+'').length) {
  156. value = v.selectKey ? v.list[v.default][v.selectKey] : v.list[v.default]
  157. }
  158. } catch (error) {
  159. }
  160. v.value = value;
  161. v.index = index;
  162. if (index != null && v.relevance == 'dateRange') this.queryMonths(v.list[index].queryMonths)
  163. return v;
  164. })
  165. })
  166. let MultilevelClass = this.selectComponent("#MultilevelClass");
  167. if (MultilevelClass) MultilevelClass.clearChild()
  168. };
  169. let obj = {};
  170. this.data.list.forEach(v => {
  171. if (v.type == 'multilevelClass') {
  172. obj[v.valueKey] = getApp().globalData.temporaryId;
  173. delete(getApp().globalData.temporaryId);
  174. } else if (v.type == 'checkbox') {
  175. obj[v.valueKey] = v.value || [];
  176. } else {
  177. try {
  178. if (v.value != null && v.value != undefined && String(v.value).length) {
  179. obj[v.valueKey] = v.value
  180. } else {
  181. obj[v.valueKey] = v.list[v.default][v.selectKey] || ''
  182. }
  183. } catch (error) {
  184. obj[v.valueKey] = v.value
  185. }
  186. }
  187. });
  188. if (this.data.dateRange) {
  189. obj.startdate = this.data.startdate;
  190. obj.enddate = this.data.enddate;
  191. };
  192. if (this.data.dateRange1) {
  193. obj.periodstart = this.data.periodstart;
  194. obj.periodend = this.data.periodend;
  195. };
  196. obj.name = name;
  197. this.triggerEvent("handle", obj);
  198. this.onClose();
  199. },
  200. /* 筛选日期范围 */
  201. changeDate(e) {
  202. const name = e.currentTarget.dataset.name,
  203. value = e.detail.value;
  204. let data = this.data.list.find(v => v.relevance == "dateRange")
  205. if (data) {
  206. data.index = null;
  207. this.setData({
  208. list: this.data.list
  209. })
  210. }
  211. this.setData({
  212. [name]: value
  213. })
  214. },
  215. setTimeCycle(startdate = "", enddate = "") {
  216. console.log(startdate,enddate)
  217. if (typeof startdate == 'object') {
  218. startdate = formatTime(startdate, '-').split(' ')[0]
  219. }
  220. if (typeof enddate == 'object') {
  221. enddate = formatTime(enddate, '-').split(' ')[0];
  222. }
  223. this.setData({
  224. startdate,
  225. enddate,
  226. })
  227. },
  228. //本周
  229. getWeekRange(date = new Date()) {
  230. const startOfWeek = new Date(date);
  231. const endOfWeek = new Date(date);
  232. // 获取本周的第一天(周一)
  233. const dayOfWeek = startOfWeek.getDay();
  234. const diffToMonday = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
  235. startOfWeek.setDate(startOfWeek.getDate() + diffToMonday);
  236. startOfWeek.setHours(0, 0, 0, 0);
  237. // 获取本周的最后一天(周日)
  238. endOfWeek.setDate(endOfWeek.getDate() + (7 - dayOfWeek));
  239. endOfWeek.setHours(23, 59, 59, 999);
  240. this.setTimeCycle(startOfWeek, endOfWeek)
  241. },
  242. //本月
  243. getMonthRange(date = new Date()) {
  244. const startOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
  245. const endOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  246. endOfMonth.setHours(23, 59, 59, 999);
  247. this.setTimeCycle(startOfMonth, endOfMonth)
  248. },
  249. //上月
  250. getLastMonthRange(date = new Date()) {
  251. const year = date.getFullYear();
  252. const month = date.getMonth(); // Current month (0-based index)
  253. const startOfLastMonth = new Date(year, month - 1, 1); // First day of last month
  254. const endOfLastMonth = new Date(year, month, 0); // Last day of last month
  255. endOfLastMonth.setHours(23, 59, 59, 999); // Set end time to the last millisecond of the day
  256. this.setTimeCycle(startOfLastMonth, endOfLastMonth);
  257. },
  258. //本年
  259. getYearRange(date = new Date()) {
  260. const startOfYear = new Date(date.getFullYear(), 0, 1);
  261. const endOfYear = new Date(date.getFullYear(), 11, 31);
  262. endOfYear.setHours(23, 59, 59, 999);
  263. this.setTimeCycle(startOfYear, endOfYear)
  264. },
  265. queryDays(num) {
  266. if (num == -1) return this.setTimeCycle()
  267. let now = new Date().getTime(),
  268. end = now,
  269. beg = end - (num * 86400000);
  270. this.setTimeCycle(formatTime(new Date(beg), '-').split(' ')[0], formatTime(new Date(end), '-').split(' ')[0])
  271. },
  272. queryMonths(period) {
  273. if (period == -1) return this.setTimeCycle()
  274. let startdate = formatTime(new Date(), '-').split(' ')[0],
  275. enddate = formatTime(new Date(), '-').split(' ')[0].split('-');
  276. for (let i = period; i != 0; i--) {
  277. enddate[1] = Number(enddate[1]) - 1;
  278. if (enddate[1] == 0) {
  279. enddate[1] = 12;
  280. enddate[0] = enddate[0] - 1;
  281. }
  282. if (i == 1 && enddate[1] < 10) enddate[1] = "0" + enddate[1]
  283. }
  284. let lastDay = new Date(enddate[0], enddate[1], 0).getDate();
  285. if (enddate[2] > lastDay) enddate[2] = lastDay;
  286. this.setTimeCycle(enddate.join("-"), startdate)
  287. },
  288. onClose() {
  289. this.setData({
  290. show: false
  291. })
  292. }
  293. }
  294. })