index.js 12 KB

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