LimitPickers.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { PickerView, PickerViewColumn, View, Text } from "@tarojs/components";
  2. import { forwardRef, useEffect, useImperativeHandle, useState } from "react";
  3. import './LimitPickers.scss'
  4. import Taro from "@tarojs/taro";
  5. // export default function Component(props: { limit: number, onChange: Function, onCancel: Function,isRealTime?:boolean,limitDay?:number,time?:number,ref?:any }) {
  6. const Component = forwardRef((props: {
  7. limit: number, onChange: Function, onCancel: Function,
  8. isRealTime?: boolean, limitDay?: number, time?: number, themeColor?: string, title?: string
  9. }, ref) => {
  10. const days: string[] = [];
  11. const today = new Date();
  12. var color = props.themeColor ? props.themeColor : '#ff0000'
  13. var alpha = alphaToHex(0.4)
  14. const [values, setValues] = useState([props.limitDay ? props.limitDay - 1 : 6, today.getHours(), today.getMinutes()])
  15. const [isDisableConfirm, setIsDisableConfirm] = useState(false)
  16. function alphaToHex(alpha) {
  17. var alphaValue = Math.round(alpha * 255); // 将透明度乘以255并四舍五入
  18. var hexValue = alphaValue.toString(16); // 将整数转换为十六进制字符串
  19. if (hexValue.length === 1) {
  20. hexValue = "0" + hexValue; // 如果十六进制字符串只有一位,补零
  21. }
  22. return hexValue;
  23. }
  24. useEffect(() => {
  25. global.picker_time = global.set_time
  26. if (props.time) {
  27. var date = new Date(props.time)
  28. setValues([(props.limitDay ? props.limitDay - 1 : 6) - getDaysDiff(date), date.getHours(), date.getMinutes()])
  29. }
  30. }, [])
  31. // useEffect(() => {
  32. // setValues([6, today.getHours(), today.getMinutes()])
  33. // }, [props.limit])
  34. useEffect(() => {
  35. var date = new Date();
  36. if (!props.isRealTime) {
  37. date = new Date(global.set_time);
  38. }
  39. date.setDate(today.getDate() - ((props.limitDay ? props.limitDay - 1 : 6) - values[0]));
  40. date.setHours(values[1])
  41. date.setMinutes(values[2])
  42. global.picker_time = date.getTime()
  43. }, [values])
  44. for (let i = props.limitDay ? props.limitDay - 1 : 6; i >= 0; i--) {
  45. const date = new Date();
  46. date.setDate(today.getDate() - i);
  47. const month = date.getMonth() + 1;
  48. const day = date.getDate();
  49. const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()];
  50. const formattedDate = `${month}月${day}日 ${weekday}`;
  51. if (i == 0) {
  52. days.push('今天 ');
  53. }
  54. else if (i == 1) {
  55. days.push('昨天 ');
  56. }
  57. else {
  58. days.push(formattedDate);
  59. }
  60. }
  61. const hours: number[] = [];
  62. for (let i = 0; i <= 23; i++) {
  63. hours.push(i);
  64. }
  65. const minutes: number[] = [];
  66. for (let i = 0; i <= 59; i++) {
  67. minutes.push(i);
  68. }
  69. function getTimestamp(dateTimeString: string): number {
  70. const timestamp = Date.parse(dateTimeString);
  71. return timestamp;
  72. }
  73. function getDaysDiff(date: Date): number {
  74. const today = new Date();
  75. today.setHours(0, 0, 0, 0);
  76. const targetDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  77. targetDate.setHours(0, 0, 0, 0);
  78. const timeDiff = today.getTime() - targetDate.getTime();
  79. const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
  80. return daysDiff;
  81. }
  82. function onPickerChange(e) {
  83. var list = e.detail.value
  84. const date = new Date();
  85. date.setDate(today.getDate() - ((props.limitDay ? props.limitDay - 1 : 6) - list[0]));
  86. const year = date.getFullYear();
  87. const month = date.getMonth() + 1;
  88. const day = date.getDate();
  89. const time = `${year}-${expandZero(month)}-${expandZero(day)}T${expandZero(hours[list[1]])}:${expandZero(minutes[list[2]])}:00`;
  90. // if (getTimestamp(time) > today.getTime()) {
  91. // setValues([list[0], today.getHours(), today.getMinutes()])
  92. // }
  93. if (getTimestamp(time) > global.set_time) {
  94. Taro.showToast({
  95. icon: 'none',
  96. title: '不能更大了',
  97. })
  98. setValues([list[0], (new Date(global.set_time)).getHours(), (new Date(global.set_time)).getMinutes()])
  99. disableConfirm()
  100. }
  101. else {
  102. var limitDate = new Date(props.limit)
  103. // const date = new Date();
  104. // date.setDate(today.getDate() - (6 - list[0]));
  105. // const year = date.getFullYear();
  106. // const month = date.getMonth() + 1;
  107. // const day = date.getDate();
  108. // debugger
  109. // const time = `${year}-${expandZero(month)}-${expandZero(day)}T${expandZero(hours[list[1]])}:${expandZero(minutes[list[2]])}:59`;
  110. if (getTimestamp(time) < props.limit) {
  111. // console.log(6 - getDaysDiff(limitDate))
  112. Taro.showToast({
  113. icon: 'none',
  114. title: '不能更小了',
  115. })
  116. setValues([(props.limitDay ? props.limitDay - 1 : 6) - getDaysDiff(limitDate), limitDate.getHours(), limitDate.getMinutes()])
  117. disableConfirm()
  118. return
  119. }
  120. setValues(e.detail.value)
  121. //把picker变动时间存储起来
  122. setPickerTime();
  123. }
  124. }
  125. function disableConfirm(){
  126. setIsDisableConfirm(true)
  127. setTimeout(() => {
  128. setIsDisableConfirm(false)
  129. },2000);
  130. }
  131. function expandZero(num: number): string {
  132. return num < 10 ? `0${num}` : `${num}`;
  133. }
  134. function cancel() {
  135. props.onCancel()
  136. }
  137. function confirm() {
  138. if (isDisableConfirm){
  139. return;
  140. }
  141. var date = new Date();
  142. if (!props.isRealTime) {
  143. date = new Date(global.set_time);
  144. }
  145. date.setDate(today.getDate() - ((props.limitDay ? props.limitDay - 1 : 6) - values[0]));
  146. date.setHours(values[1])
  147. date.setMinutes(values[2])
  148. props.onChange(date.getTime())
  149. }
  150. function setPickerTime() {
  151. }
  152. function getConfirmData() {
  153. var date = new Date();
  154. if (!props.isRealTime) {
  155. date = new Date(global.set_time);
  156. }
  157. date.setDate(today.getDate() - ((props.limitDay ? props.limitDay - 1 : 6) - values[0]));
  158. date.setHours(values[1])
  159. date.setMinutes(values[2])
  160. return date.getTime()
  161. }
  162. useImperativeHandle(ref, () => ({
  163. getConfirmData: getConfirmData
  164. }));
  165. return <View style={{display:'flex',flexDirection:'column'}}>
  166. <Text className='modal_title' style={{ color: color }}>{props.title ? props.title : '测试标题 '}</Text>
  167. <View style={{ backgroundColor: 'transparent', position: 'relative' }}>
  168. <PickerView value={values}
  169. className="picker"
  170. maskClass="picker-mask"
  171. // indicatorClass = 'picker-sel'
  172. onChange={onPickerChange}
  173. immediateChange={true}
  174. indicatorStyle='height: 50px;color:red;'>
  175. <PickerViewColumn style='flex:0 0 45%'>
  176. {days.map(item => {
  177. return (
  178. <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item}</View>
  179. );
  180. })}
  181. </PickerViewColumn>
  182. <PickerViewColumn>
  183. {hours.map(item => {
  184. return (
  185. <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item < 10 ? `0${item}` : item}</View>
  186. );
  187. })}
  188. </PickerViewColumn>
  189. <PickerViewColumn>
  190. {minutes.map(item => {
  191. return (
  192. <View style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{item < 10 ? `0${item}` : item}</View>
  193. );
  194. })}
  195. </PickerViewColumn>
  196. </PickerView>
  197. {/* <View className="point_bg1 ">
  198. <Text style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>:</Text>
  199. </View> */}
  200. </View>
  201. <View className='modal_operate'>
  202. <View className='modal_btn' style={{ backgroundColor: color + alpha }} onClick={cancel}>
  203. <Text className='modal_cancel_text' style={{ color: color }}>取消</Text>
  204. </View>
  205. <View className='btn_space' />
  206. <View className='modal_btn' style={{ backgroundColor: color }} onClick={confirm}>
  207. <Text className='modal_confirm_text' style={{ color: '#000' }}>确定</Text>
  208. </View>
  209. </View>
  210. </View>
  211. })
  212. export default Component;