selectDate_two.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. var app = getApp()
  2. var that,yearIndex;
  3. // 设置默认年份
  4. app.globalData.yearArr.forEach((item,index)=>{
  5. if(item==app.globalData.year){
  6. yearIndex=index
  7. }
  8. })
  9. Component({
  10. mixins: [],
  11. data: {
  12. dateIndex: "1",
  13. day: app.globalData.day,//日
  14. year: app.globalData.year,//年
  15. month: app.globalData.month,//月
  16. jdYear: app.globalData.year,//季度年
  17. jdji: 0,//季度季
  18. jdArr: ['一季度', '二季度', '三季度', '四季度'],
  19. yearIndex:yearIndex,
  20. yearArr:app.globalData.yearArr,
  21. jdYearIndex:yearIndex,
  22. isShowArr:[true,true,true],
  23. showSelect:false,
  24. customIndOn: app.globalData.day,
  25. customIndTw: app.globalData.day,
  26. // custom: '',
  27. },
  28. props: {
  29. onClose: (data) => console.log(data),
  30. onConfirm: (data) => console.log(data),
  31. showSelectP:false,
  32. dateObj:{
  33. type:'3',
  34. date:app.globalData.month
  35. },
  36. },
  37. didMount() {
  38. this.setData({isShow: this.props.isShow,showSelect:this.props.showSelectP})
  39. if(this.props.dateObj.type){
  40. this.setData({
  41. dateIndex:this.props.dateObj.type,
  42. })
  43. if(this.props.dateObj.type==1){
  44. this.data.yearArr.forEach((item,index)=>{
  45. if(this.props.dateObj.date==item){
  46. this.setData({
  47. yearIndex:index,
  48. })
  49. }
  50. })
  51. }else if(this.props.dateObj.type==2){
  52. var data=String(this.props.dateObj.date);
  53. this.data.yearArr.forEach((item,index)=>{
  54. if(data.substr(0,4)==item){
  55. this.setData({
  56. jdYearIndex:index,
  57. })
  58. }
  59. })
  60. this.setData({
  61. jdji:data.substr(data.length-1,1)-1
  62. })
  63. }else if(this.props.dateObj.type==4){
  64. this.setData({
  65. customIndOn: this.props.dateObj.date.start_date,
  66. customIndTw: this.props.dateObj.date.end_date,
  67. })
  68. }else{
  69. this.setData({
  70. month:this.props.dateObj.date,
  71. })
  72. }
  73. }
  74. },
  75. didUpdate() { },
  76. didUnmount() { },
  77. methods: {
  78. selectYear2(e) {
  79. this.setData({
  80. jdYearIndex: e.detail.value,
  81. });
  82. },
  83. selectYear(e) {
  84. this.setData({
  85. yearIndex: e.detail.value,
  86. });
  87. },
  88. setjdji(e) {
  89. this.setData({
  90. jdji: e.detail.value,
  91. });
  92. },
  93. onConfirm() {
  94. var obj;
  95. switch (this.data.dateIndex) {
  96. case "1":
  97. obj = {
  98. type: this.data.dateIndex,
  99. date: this.data.yearArr[this.data.yearIndex]
  100. }
  101. break
  102. case "2":
  103. var year=this.data.yearArr[this.data.jdYearIndex];
  104. var num=this.data.jdji;
  105. num++;
  106. let arrNew=new Array()
  107. arrNew.push(year)
  108. arrNew.push(num)
  109. let strNew=arrNew.join('')
  110. obj = {
  111. type: this.data.dateIndex,
  112. date:parseInt(strNew)
  113. }
  114. break
  115. case "3":
  116. obj = {
  117. type: this.data.dateIndex,
  118. date: this.data.month
  119. }
  120. break
  121. case "4":
  122. function tsTamp(ydm) {
  123. return new Date(ydm).getTime()
  124. }
  125. let timeC = (tsTamp(this.data.customIndOn) - tsTamp(this.data.customIndTw)) / (60 * 60 * 24 * 1000)
  126. if(timeC > 0){
  127. app.globalData.showToast("开始时间不能大于结束时间")
  128. return
  129. }
  130. // if(Math.abs(timeC) > 30) {
  131. // app.globalData.showToast("自定义事件区间不能大于一个月")
  132. // return
  133. // }
  134. obj = {
  135. type: this.data.dateIndex,
  136. date: {
  137. start_date: this.data.customIndOn,
  138. end_date: this.data.customIndTw
  139. }
  140. }
  141. break
  142. };
  143. this.props.onConfirm(obj);
  144. this.props.onClose();
  145. },
  146. onClose() {
  147. this.props.onClose();
  148. },
  149. selectDay() {
  150. dd.datePicker({
  151. format: 'yyyy',
  152. currentDate: this.data.jdYear,
  153. success: (res) => {
  154. this.setData({
  155. jdYear: res.date
  156. })
  157. },
  158. });
  159. },
  160. selectDay2() {
  161. },
  162. selectDate() {
  163. dd.datePicker({
  164. format: 'yyyy-MM',
  165. currentDate: this.data.month,
  166. success: (res) => {
  167. this.setData({
  168. month: res.date
  169. })
  170. },
  171. });
  172. },
  173. customDate(e) {
  174. console.log(e)
  175. let cust = e.target.dataset.index
  176. dd.datePicker({
  177. format: 'yyyy-MM-dd',
  178. currentDate: this.data[cust],
  179. success : res =>{
  180. console.log(res)
  181. this.setData({
  182. [cust]: res.date
  183. })
  184. },
  185. })
  186. },
  187. activeDate(e) {
  188. this.setData({ dateIndex: e.target.dataset.index })
  189. },
  190. },
  191. });