util.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. const md5 = require('./md5.js');
  2. const urlsDef = require('./urls.js');
  3. const accessHeader = {
  4. accessKey: '',
  5. appId: ''
  6. };
  7. const notWxApiPath = urlsDef.urls.baseUrl + '/schoolbaby/api/';
  8. const apiPath = urlsDef.urls.baseUrl + '/schoolbaby/api/wx/';
  9. function fun_date(num) {
  10. var date1 = new Date();
  11. //今天时间
  12. var time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate()
  13. var date2 = new Date(date1);
  14. date2.setDate(date1.getDate() + num);
  15. //num是正数表示之后的时间,num负数表示之前的时间,0表示今天
  16. var month = date2.getMonth() + 1
  17. if (month < 10) {
  18. month = "0" + month
  19. }
  20. var date = date2.getDate()
  21. if (date < 10) {
  22. date = "0" + date
  23. }
  24. var time2 = date2.getFullYear() + "-" + (month) + "-" + date;
  25. return time2;
  26. }
  27. function fun_week(datestr) {
  28. var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
  29. var week = weekArray[new Date(datestr).getDay()];
  30. return week;
  31. }
  32. //经纬度算距离
  33. function distance(la1, lo1, la2, lo2) {
  34. var La1 = la1 * Math.PI / 180.0;
  35. var La2 = la2 * Math.PI / 180.0;
  36. var La3 = La1 - La2;
  37. var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
  38. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
  39. s = s * 6378.137;
  40. s = Math.round(s * 10000) / 10000;
  41. s = s.toFixed(2);
  42. return s;
  43. }
  44. const formatTime = date => {
  45. const year = date.getFullYear()
  46. const month = date.getMonth() + 1
  47. const day = date.getDate()
  48. const hour = date.getHours()
  49. const minute = date.getMinutes()
  50. const second = date.getSeconds()
  51. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  52. }
  53. function curTime() {
  54. const date = new Date()
  55. const year = date.getFullYear()
  56. const month = date.getMonth() + 1
  57. const day = date.getDate()
  58. const hour = date.getHours()
  59. const minute = date.getMinutes()
  60. const second = date.getSeconds()
  61. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  62. }
  63. function validDay(val) {
  64. var todayDate = new Date()
  65. var today = todayDate.getFullYear() + '/' + (todayDate.getMonth() + 1) + '/' + todayDate.getDate()
  66. var date = new Date(today);
  67. date.setDate(date.getDate() + val);
  68. var year = date.getFullYear()
  69. var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  70. var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  71. return year + '-' + month + '-' + day;
  72. }
  73. //相隔天数
  74. function getDayBetween(sDate1, sDate2) {
  75. //Date.parse() 解析一个日期时间字符串,并返回1970/1/1 午夜距离该日期时间的毫秒数
  76. var time1 = Date.parse(new Date(sDate1.substring(0, 10)));
  77. var time2 = Date.parse(new Date(sDate2.substring(0, 10)));
  78. var nDays = parseInt((time1 - time2) / 1000 / 3600 / 24);
  79. return nDays;
  80. }
  81. //相隔小时
  82. function validHour(s1, s2) {
  83. s1 = new Date(s1.replace(/-/g, '/'));
  84. s2 = new Date(s2.replace(/-/g, '/'));
  85. var ms = s1.getTime() - s2.getTime();
  86. var hours = (ms / 1000 / 60 / 60).toFixed(1);
  87. return hours;
  88. }
  89. //相隔分钟
  90. function validMinutes(startTime, endTime) {
  91. //定义两个变量time1,time2分别保存开始和结束时间
  92. var time1 = startTime;
  93. var time2 = endTime;
  94. //截取字符串,得到日期部分,用split把字符串分隔成数组
  95. var begin1 = time1.substr(0, 10).split("/");
  96. var end1 = time2.substr(0, 10).split("/");
  97. //将拆分的数组重新组合,并实例成化新的日期对象
  98. var date1 = new Date(begin1[1] + '/' + begin1[2] + '/' + begin1[0]);
  99. var date2 = new Date(end1[1] + '/' + end1[2] + '/' + end1[0]);
  100. //得到两个日期之间的差值m,以分钟为单位
  101. //Math.abs(date2-date1)计算出以毫秒为单位的差值
  102. //Math.abs(date2-date1)/1000得到以秒为单位的差值
  103. //Math.abs(date2-date1)/1000/60得到以分钟为单位的差值
  104. var m = parseInt((date2 - date1) / 1000 / 60);
  105. //小时数和分钟数相加得到总的分钟数
  106. //time1.substr(11,2)截取字符串得到时间的小时数
  107. //parseInt(time1.substr(11,2))*60把小时数转化成为分钟
  108. var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
  109. var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));
  110. //两个分钟数相减得到时间部分的差值,以分钟为单位
  111. var n = min1 - min2;
  112. //将日期和时间两个部分计算出来的差值相加,即得到两个时间相减后的分钟数
  113. var minutes = m + n;
  114. return minutes
  115. }
  116. const formatNumber = n => {
  117. n = n.toString()
  118. return n[1] ? n : '0' + n
  119. }
  120. function getAccessKey(ticket) {
  121. return md5.md5(accessHeader.accessKey + '@' + ticket)
  122. }
  123. function getHeaders() {
  124. // console.log('accessHeader : ' + JSON.stringify(accessHeader));
  125. let ticket = new Date().getTime();
  126. // 将key作为密钥,根据accessKey 生成 加密的 accessKey(暂未实现)
  127. return {
  128. 'Access-Ticket': ticket,
  129. 'Access-Key': getAccessKey(ticket),
  130. 'Access-AppId': accessHeader.appId,
  131. 'SSO-TOKEN': wx.getStorageSync('sso-token')
  132. }
  133. }
  134. function setAccessHeader(accessKey, appId) {
  135. accessHeader.appId = appId;
  136. accessHeader.accessKey = accessKey;
  137. }
  138. function apiPost(url, params, contentType) {
  139. return new Promise((resolve, reject) => {
  140. let headers = getHeaders();
  141. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  142. if (contentType) {
  143. headers['Content-Type'] = contentType;
  144. }
  145. wx.request({
  146. url: url,
  147. header: headers,
  148. data: params,
  149. method: 'POST',
  150. success: res => {
  151. if (res.success > 1 || typeof res.version != undefined) {
  152. resolve(res.data)
  153. } else {
  154. wx.hideLoading({
  155. success: (res) => {
  156. wx.showToast({
  157. title: '加载失败,请重试',
  158. icon: 'none'
  159. })
  160. },
  161. })
  162. }
  163. },
  164. faild: res => {
  165. // reject(res)
  166. wx.hideLoading({
  167. success: (res) => {
  168. wx.showToast({
  169. title: res.errMsg,
  170. })
  171. },
  172. })
  173. }
  174. })
  175. }).catch(err => {
  176. console.log(err)
  177. })
  178. }
  179. function doPost(url, params) {
  180. return new Promise((resolve, reject) => {
  181. let headers = getHeaders();
  182. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  183. wx.request({
  184. url: apiPath + url,
  185. header: headers,
  186. data: params,
  187. method: 'POST',
  188. success: res => {
  189. if (res.success > 1 || typeof res.version != undefined) {
  190. resolve(res.data)
  191. } else {
  192. wx.hideLoading({
  193. success: (res) => {
  194. wx.showToast({
  195. title: '加载失败,请重试',
  196. icon: 'none'
  197. })
  198. },
  199. })
  200. }
  201. },
  202. faild: res => {
  203. // reject(res)
  204. wx.hideLoading({
  205. success: (res) => {
  206. wx.showToast({
  207. title: res.errMsg,
  208. })
  209. },
  210. })
  211. }
  212. })
  213. }).catch(err => {
  214. console.log(err)
  215. })
  216. }
  217. function notWxPost(url, params) {
  218. return new Promise((resolve, reject) => {
  219. let headers = getHeaders();
  220. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  221. wx.request({
  222. url: notWxApiPath + url,
  223. header: headers,
  224. data: params,
  225. method: 'POST',
  226. success: res => {
  227. if (res.success > 1 || typeof res.version != undefined) {
  228. resolve(res.data)
  229. } else {
  230. wx.hideLoading({
  231. success: (res) => {
  232. wx.showToast({
  233. title: '加载失败,请重试',
  234. icon: 'none'
  235. })
  236. },
  237. })
  238. }
  239. },
  240. faild: res => {
  241. // reject(res)
  242. wx.hideLoading({
  243. success: (res) => {
  244. wx.showToast({
  245. title: res.errMsg,
  246. })
  247. },
  248. })
  249. }
  250. })
  251. }).catch(err => {
  252. console.log(err)
  253. })
  254. }
  255. module.exports = {
  256. formatTime: formatTime,
  257. doPost: doPost,
  258. notWxPost: notWxPost,
  259. validDay: validDay,
  260. validMinutes: validMinutes,
  261. getDayBetween: getDayBetween,
  262. validHour: validHour,
  263. curTime: curTime,
  264. setAccessHeader: setAccessHeader,
  265. distance: distance,
  266. apiPost: apiPost,
  267. fun_date: fun_date,
  268. fun_week: fun_week,
  269. getHeaders:getHeaders
  270. }