utils.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import convertSolarLunar from './convertSolarLunar'
  2. let systemInfo
  3. export function getSystemInfo() {
  4. if (systemInfo) return systemInfo
  5. systemInfo = wx.getSystemInfoSync()
  6. return systemInfo
  7. }
  8. export function isComponent(target) {
  9. return (
  10. target &&
  11. target.__wxExparserNodeId__ !== void 0 &&
  12. typeof target.setData === 'function'
  13. )
  14. }
  15. export class Logger {
  16. info(msg) {
  17. console.log(
  18. '%cInfo: %c' + msg,
  19. 'color:#FF0080;font-weight:bold',
  20. 'color: #FF509B'
  21. )
  22. }
  23. warn(msg) {
  24. console.log(
  25. '%cWarn: %c' + msg,
  26. 'color:#FF6600;font-weight:bold',
  27. 'color: #FF9933'
  28. )
  29. }
  30. tips(msg) {
  31. console.log(
  32. '%cTips: %c' + msg,
  33. 'color:#00B200;font-weight:bold',
  34. 'color: #00CC33'
  35. )
  36. }
  37. }
  38. export class Slide {
  39. /**
  40. * 上滑
  41. * @param {object} e 事件对象
  42. * @returns {boolean} 布尔值
  43. */
  44. isUp(gesture = {}, touche = {}) {
  45. const { startX, startY } = gesture
  46. const deltaX = touche.clientX - startX
  47. const deltaY = touche.clientY - startY
  48. if (deltaY < -60 && deltaX < 20 && deltaX > -20) {
  49. this.slideLock = false
  50. return true
  51. } else {
  52. return false
  53. }
  54. }
  55. /**
  56. * 下滑
  57. * @param {object} e 事件对象
  58. * @returns {boolean} 布尔值
  59. */
  60. isDown(gesture = {}, touche = {}) {
  61. const { startX, startY } = gesture
  62. const deltaX = touche.clientX - startX
  63. const deltaY = touche.clientY - startY
  64. if (deltaY > 60 && deltaX < 20 && deltaX > -20) {
  65. return true
  66. } else {
  67. return false
  68. }
  69. }
  70. /**
  71. * 左滑
  72. * @param {object} e 事件对象
  73. * @returns {boolean} 布尔值
  74. */
  75. isLeft(gesture = {}, touche = {}) {
  76. const { startX, startY } = gesture
  77. const deltaX = touche.clientX - startX
  78. const deltaY = touche.clientY - startY
  79. if (deltaX < -60 && deltaY < 20 && deltaY > -20) {
  80. return true
  81. } else {
  82. return false
  83. }
  84. }
  85. /**
  86. * 右滑
  87. * @param {object} e 事件对象
  88. * @returns {boolean} 布尔值
  89. */
  90. isRight(gesture = {}, touche = {}) {
  91. const { startX, startY } = gesture
  92. const deltaX = touche.clientX - startX
  93. const deltaY = touche.clientY - startY
  94. if (deltaX > 60 && deltaY < 20 && deltaY > -20) {
  95. return true
  96. } else {
  97. return false
  98. }
  99. }
  100. }
  101. export class GetDate {
  102. /**
  103. * new Date 区分平台
  104. * @param {number} year
  105. * @param {number} month
  106. * @param {number} day
  107. */
  108. newDate(year, month, day) {
  109. let cur = `${+year}-${+month}-${+day}`
  110. if (isIos()) {
  111. cur = `${+year}/${+month}/${+day}`
  112. }
  113. return new Date(cur)
  114. }
  115. /**
  116. * 计算指定月份共多少天
  117. * @param {number} year 年份
  118. * @param {number} month 月份
  119. */
  120. thisMonthDays(year, month) {
  121. return new Date(Date.UTC(year, month, 0)).getUTCDate()
  122. }
  123. /**
  124. * 计算指定月份第一天星期几
  125. * @param {number} year 年份
  126. * @param {number} month 月份
  127. */
  128. firstDayOfWeek(year, month) {
  129. return new Date(Date.UTC(year, month - 1, 1)).getUTCDay()
  130. }
  131. /**
  132. * 计算指定日期星期几
  133. * @param {number} year 年份
  134. * @param {number} month 月份
  135. * @param {number} date 日期
  136. */
  137. dayOfWeek(year, month, date) {
  138. return new Date(Date.UTC(year, month - 1, date)).getUTCDay()
  139. }
  140. todayDate() {
  141. const _date = new Date()
  142. const year = _date.getFullYear()
  143. const month = _date.getMonth() + 1
  144. const date = _date.getDate()
  145. return {
  146. year,
  147. month,
  148. date
  149. }
  150. }
  151. todayTimestamp() {
  152. const { year, month, date } = this.todayDate()
  153. const timestamp = this.newDate(year, month, date).getTime()
  154. return timestamp
  155. }
  156. toTimeStr(dateInfo) {
  157. if (dateInfo.day) {
  158. dateInfo.date = dateInfo.day
  159. }
  160. return `${+dateInfo.year}-${+dateInfo.month}-${+dateInfo.date}`
  161. }
  162. sortDates(dates, sortType) {
  163. return dates.sort(function(a, b) {
  164. const at = getDateTimeStamp(a)
  165. const bt = getDateTimeStamp(b)
  166. if (at < bt && sortType !== 'desc') {
  167. return -1
  168. } else {
  169. return 1
  170. }
  171. })
  172. }
  173. prevMonth(dataInfo) {
  174. const prevMonthInfo =
  175. +dataInfo.month > 1
  176. ? {
  177. year: dataInfo.year,
  178. month: dataInfo.month - 1
  179. }
  180. : {
  181. year: dataInfo.year - 1,
  182. month: 12
  183. }
  184. return prevMonthInfo
  185. }
  186. nextMonth(dataInfo) {
  187. const nextMonthInfo =
  188. +dataInfo.month < 12
  189. ? {
  190. year: dataInfo.year,
  191. month: dataInfo.month + 1
  192. }
  193. : {
  194. year: dataInfo.year + 1,
  195. month: 1
  196. }
  197. return nextMonthInfo
  198. }
  199. convertLunar(dates = []) {
  200. const datesWithLunar = dates.map(date => {
  201. if (date) {
  202. date.lunar = convertSolarLunar.solar2lunar(
  203. +date.year,
  204. +date.month,
  205. +date.day
  206. )
  207. }
  208. return date
  209. })
  210. return datesWithLunar
  211. }
  212. }
  213. export function isIos() {
  214. const sys = getSystemInfo()
  215. return /iphone|ios/i.test(sys.platform)
  216. }
  217. /**
  218. * 浅比较对象是否相等
  219. * @param {Object} origin 对比源
  220. * @param {Object} target 对比目标
  221. * @return {Boolean} true 为相等,false 为不等
  222. */
  223. export function shallowEqual(origin, target) {
  224. if (origin === target) {
  225. return true
  226. } else if (
  227. typeof origin === 'object' &&
  228. origin != null &&
  229. typeof target === 'object' &&
  230. target != null
  231. ) {
  232. if (Object.keys(origin).length !== Object.keys(target).length) return false
  233. for (var prop in origin) {
  234. if (target.hasOwnProperty(prop)) {
  235. if (!shallowEqual(origin[prop], target[prop])) return false
  236. } else return false
  237. }
  238. return true
  239. } else return false
  240. }
  241. /**
  242. * 获取当前页面实例
  243. */
  244. export function getCurrentPage() {
  245. const pages = getCurrentPages()
  246. const last = pages.length - 1
  247. return pages[last]
  248. }
  249. export function getComponent(componentId) {
  250. const logger = new Logger()
  251. let page = getCurrentPage() || {}
  252. if (page.selectComponent && typeof page.selectComponent === 'function') {
  253. if (componentId) {
  254. return page.selectComponent(componentId)
  255. } else {
  256. logger.warn('请传入组件ID')
  257. }
  258. } else {
  259. logger.warn('该基础库暂不支持多个小程序日历组件')
  260. }
  261. }
  262. /**
  263. * 日期数组根据日期去重
  264. * @param {array} array 数组
  265. */
  266. export function uniqueArrayByDate(array = []) {
  267. let uniqueObject = {}
  268. let uniqueArray = []
  269. array.forEach(item => {
  270. uniqueObject[`${item.year}-${item.month}-${item.day}`] = item
  271. })
  272. for (let i in uniqueObject) {
  273. uniqueArray.push(uniqueObject[i])
  274. }
  275. return uniqueArray
  276. }
  277. /**
  278. * 指定可选日期及可选日期数组去重
  279. * @param {array} enableDays 特定可选日期数组
  280. * @param {array} enableArea 可选日期区域数组
  281. */
  282. export function delRepeatedEnableDay(enableDays = [], enableArea = []) {
  283. let _startTimestamp
  284. let _endTimestamp
  285. if (enableArea.length === 2) {
  286. const { startTimestamp, endTimestamp } = convertEnableAreaToTimestamp(
  287. enableArea
  288. )
  289. _startTimestamp = startTimestamp
  290. _endTimestamp = endTimestamp
  291. }
  292. const enableDaysTimestamp = converEnableDaysToTimestamp(enableDays)
  293. const tmp = enableDaysTimestamp.filter(
  294. item => item < _startTimestamp || item > _endTimestamp
  295. )
  296. return tmp
  297. }
  298. /**
  299. * 指定日期区域转时间戳
  300. * @param {array} timearea 时间区域
  301. */
  302. export function convertEnableAreaToTimestamp(timearea = []) {
  303. const getDate = new GetDate()
  304. const start = timearea[0].split('-')
  305. const end = timearea[1].split('-')
  306. const logger = new Logger()
  307. if (start.length !== 3 || end.length !== 3) {
  308. logger.warn('enableArea() 参数格式为: ["2018-2-1", "2018-3-1"]')
  309. return {}
  310. }
  311. const startTimestamp = getDate.newDate(start[0], start[1], start[2]).getTime()
  312. const endTimestamp = getDate.newDate(end[0], end[1], end[2]).getTime()
  313. return {
  314. start,
  315. end,
  316. startTimestamp,
  317. endTimestamp
  318. }
  319. }
  320. /**
  321. * 计算指定日期时间戳
  322. * @param {object} dateInfo
  323. */
  324. export function getDateTimeStamp(dateInfo) {
  325. if (Object.prototype.toString.call(dateInfo) !== '[object Object]') return
  326. const getDate = new GetDate()
  327. return getDate.newDate(dateInfo.year, dateInfo.month, dateInfo.day).getTime()
  328. }
  329. /**
  330. * 指定特定日期数组转时间戳
  331. * @param {array} enableDays 指定时间数组
  332. */
  333. export function converEnableDaysToTimestamp(enableDays = []) {
  334. const logger = new Logger()
  335. const getDate = new GetDate()
  336. const enableDaysTimestamp = []
  337. enableDays.forEach(item => {
  338. if (typeof item !== 'string')
  339. return logger.warn('enableDays()入参日期格式错误')
  340. const tmp = item.split('-')
  341. if (tmp.length !== 3) return logger.warn('enableDays()入参日期格式错误')
  342. const timestamp = getDate.newDate(tmp[0], tmp[1], tmp[2]).getTime()
  343. enableDaysTimestamp.push(timestamp)
  344. })
  345. return enableDaysTimestamp
  346. }
  347. // 同一页面多个日历组件按先后顺序渲染
  348. export const initialTasks = {
  349. flag: 'finished', // process 处理中,finished 处理完成
  350. tasks: []
  351. }