index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import Logger from './logger'
  2. import WxData from './wxData'
  3. let systemInfo
  4. export function getSystemInfo() {
  5. if (systemInfo) return systemInfo
  6. systemInfo = wx.getSystemInfoSync()
  7. return systemInfo
  8. }
  9. export function isIos() {
  10. const sys = getSystemInfo()
  11. return /iphone|ios/i.test(sys.platform)
  12. }
  13. class Gesture {
  14. /**
  15. * 左滑
  16. * @param {object} e 事件对象
  17. * @returns {boolean} 布尔值
  18. */
  19. isLeft(gesture = {}, touche = {}) {
  20. const { startX, startY } = gesture
  21. const deltaX = touche.clientX - startX
  22. const deltaY = touche.clientY - startY
  23. if (deltaX < -60 && deltaY < 20 && deltaY > -20) {
  24. return true
  25. } else {
  26. return false
  27. }
  28. }
  29. /**
  30. * 右滑
  31. * @param {object} e 事件对象
  32. * @returns {boolean} 布尔值
  33. */
  34. isRight(gesture = {}, touche = {}) {
  35. const { startX, startY } = gesture
  36. const deltaX = touche.clientX - startX
  37. const deltaY = touche.clientY - startY
  38. if (deltaX > 60 && deltaY < 20 && deltaY > -20) {
  39. return true
  40. } else {
  41. return false
  42. }
  43. }
  44. }
  45. class DateUtil {
  46. newDate(year, month, date) {
  47. let cur = `${+year}-${+month}-${+date}`
  48. if (isIos()) {
  49. cur = `${+year}/${+month}/${+date}`
  50. }
  51. return new Date(cur)
  52. }
  53. /**
  54. * 计算指定日期时间戳
  55. * @param {object} date
  56. */
  57. getTimeStamp(dateInfo) {
  58. if (typeof dateInfo === 'string') {
  59. dateInfo = this.transformDateRow2Dict(dateInfo)
  60. }
  61. if (Object.prototype.toString.call(dateInfo) !== '[object Object]') return
  62. const dateUtil = new DateUtil()
  63. return dateUtil
  64. .newDate(dateInfo.year, dateInfo.month, dateInfo.date)
  65. .getTime()
  66. }
  67. /**
  68. * 计算指定月份共多少天
  69. * @param {number} year 年份
  70. * @param {number} month 月份
  71. */
  72. getDatesCountOfMonth(year, month) {
  73. return new Date(Date.UTC(year, month, 0)).getUTCDate()
  74. }
  75. /**
  76. * 计算指定月份第一天星期几
  77. * @param {number} year 年份
  78. * @param {number} month 月份
  79. */
  80. firstDayOfWeek(year, month) {
  81. return new Date(Date.UTC(year, month - 1, 1)).getUTCDay()
  82. }
  83. /**
  84. * 计算指定日期星期几
  85. * @param {number} year 年份
  86. * @param {number} month 月份
  87. * @param {number} date 日期
  88. */
  89. getDayOfWeek(year, month, date) {
  90. return new Date(Date.UTC(year, month - 1, date)).getUTCDay()
  91. }
  92. todayFMD() {
  93. const _date = new Date()
  94. const year = _date.getFullYear()
  95. const month = _date.getMonth() + 1
  96. const date = _date.getDate()
  97. return {
  98. year: +year,
  99. month: +month,
  100. date: +date
  101. }
  102. }
  103. todayTimestamp() {
  104. const { year, month, date } = this.todayFMD()
  105. const timestamp = this.newDate(year, month, date).getTime()
  106. return timestamp
  107. }
  108. toTimeStr(dateInfo = {}) {
  109. return `${+dateInfo.year}-${+dateInfo.month}-${+dateInfo.date}`
  110. }
  111. transformDateRow2Dict(dateStr) {
  112. if (typeof dateStr === 'string' && dateStr.includes('-')) {
  113. const [year, month, date] = dateStr.split('-')
  114. return this.tranformStr2NumOfDate({
  115. year,
  116. month,
  117. date
  118. })
  119. }
  120. return {}
  121. }
  122. tranformStr2NumOfDate(date = {}) {
  123. const target = { ...date }
  124. // 可能传入字符串
  125. target.year = +target.year
  126. target.month = +target.month
  127. target.date = +target.date
  128. return target
  129. }
  130. sortDatesByTime(dates = [], sortType) {
  131. return dates.sort((a, b) => {
  132. const at = this.getTimeStamp(a)
  133. const bt = this.getTimeStamp(b)
  134. if (at < bt && sortType !== 'desc') {
  135. return -1
  136. } else {
  137. return 1
  138. }
  139. })
  140. }
  141. getPrevMonthInfo(date = {}) {
  142. const prevMonthInfo =
  143. Number(date.month) > 1
  144. ? {
  145. year: +date.year,
  146. month: Number(date.month) - 1
  147. }
  148. : {
  149. year: Number(date.year) - 1,
  150. month: 12
  151. }
  152. return prevMonthInfo
  153. }
  154. getNextMonthInfo(date = {}) {
  155. const nextMonthInfo =
  156. Number(date.month) < 12
  157. ? {
  158. year: +date.year,
  159. month: Number(date.month) + 1
  160. }
  161. : {
  162. year: Number(date.year) + 1,
  163. month: 1
  164. }
  165. return nextMonthInfo
  166. }
  167. getPrevYearInfo(date = {}) {
  168. return {
  169. year: Number(date.year) - 1,
  170. month: +date.month
  171. }
  172. }
  173. getNextYearInfo(date = {}) {
  174. return {
  175. year: Number(date.year) + 1,
  176. month: +date.month
  177. }
  178. }
  179. findDateIndexInArray(target, dates) {
  180. return dates.findIndex(
  181. item => dateUtil.toTimeStr(item) === dateUtil.toTimeStr(target)
  182. )
  183. }
  184. calcDates(year, month) {
  185. const datesCount = this.getDatesCountOfMonth(year, month)
  186. const dates = []
  187. const today = dateUtil.todayFMD()
  188. for (let i = 1; i <= datesCount; i++) {
  189. const week = dateUtil.getDayOfWeek(+year, +month, i)
  190. const date = {
  191. year: +year,
  192. id: i - 1,
  193. month: +month,
  194. date: i,
  195. week,
  196. isToday:
  197. +today.year === +year && +today.month === +month && i === +today.date
  198. }
  199. dates.push(date)
  200. }
  201. return dates
  202. }
  203. /**
  204. * 日期数组根据日期去重
  205. * @param {array} array 数组
  206. */
  207. uniqueArrayByDate(array = []) {
  208. let uniqueObject = {}
  209. let uniqueArray = []
  210. array.forEach(item => {
  211. uniqueObject[dateUtil.toTimeStr(item)] = item
  212. })
  213. for (let i in uniqueObject) {
  214. uniqueArray.push(uniqueObject[i])
  215. }
  216. return uniqueArray
  217. }
  218. /**
  219. * 筛选指定年月日期
  220. * @param {object} target 指定年月
  221. * @param {array} dates 待筛选日期
  222. */
  223. filterDatesByYM(target, dates) {
  224. if (target) {
  225. const { year, month } = target
  226. const _dates = dates.filter(
  227. item => +item.year === +year && +item.month === +month
  228. )
  229. return _dates
  230. }
  231. return dates
  232. }
  233. getWeekHeader(firstDayOfWeek) {
  234. let weeksCh = ['日', '一', '二', '三', '四', '五', '六']
  235. if (firstDayOfWeek === 'Mon') {
  236. weeksCh = ['一', '二', '三', '四', '五', '六', '日']
  237. }
  238. return weeksCh
  239. }
  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 getComponentById(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. export const logger = new Logger()
  263. export const calendarGesture = new Gesture()
  264. export const dateUtil = new DateUtil()
  265. export const getCalendarData = (key, component) =>
  266. new WxData(component).getData(key)
  267. export const setCalendarData = (data, component) =>
  268. new WxData(component).setData(data)
  269. export const getCalendarConfig = component =>
  270. getCalendarData('config', component)
  271. export const setCalendarConfig = (config, component) =>
  272. setCalendarData(
  273. {
  274. config
  275. },
  276. component
  277. )