util.js 8.2 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.showToast({
  155. title: '加载失败,请重试',
  156. icon: 'none'
  157. })
  158. }
  159. },
  160. faild: res => {
  161. // reject(res)
  162. wx.showToast({
  163. title: res.errMsg,
  164. icon: 'none'
  165. })
  166. },
  167. complete: res =>{
  168. }
  169. })
  170. }).catch(err => {
  171. console.log(err)
  172. })
  173. }
  174. function doPost(url, params) {
  175. return new Promise((resolve, reject) => {
  176. wx.showLoading({
  177. title: '加载中',
  178. mask: true
  179. })
  180. let headers = getHeaders();
  181. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  182. wx.request({
  183. url: apiPath + url,
  184. header: headers,
  185. data: params,
  186. method: 'POST',
  187. success: res => {
  188. if (res.success > 1 || typeof res.version != undefined) {
  189. resolve(res.data)
  190. } else {
  191. wx.showToast({
  192. title: '加载失败,请重试',
  193. icon: 'none'
  194. })
  195. }
  196. },
  197. faild: res => {
  198. // reject(res)
  199. wx.showToast({
  200. title: res.errMsg,
  201. icon: 'none'
  202. })
  203. },
  204. complete: res =>{
  205. wx.hideLoading({
  206. success: (res) => {},
  207. })
  208. }
  209. })
  210. }).catch(err => {
  211. console.log(err)
  212. })
  213. }
  214. function notWxPost(url, params) {
  215. return new Promise((resolve, reject) => {
  216. wx.showLoading({
  217. title: '加载中',
  218. mask: true
  219. })
  220. let headers = getHeaders();
  221. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  222. wx.request({
  223. url: notWxApiPath + url,
  224. header: headers,
  225. data: params,
  226. method: 'POST',
  227. success: res => {
  228. if (res.success > 1 || typeof res.version != undefined) {
  229. resolve(res.data)
  230. } else {
  231. wx.showToast({
  232. title: '加载失败,请重试',
  233. icon: 'none'
  234. })
  235. }
  236. },
  237. faild: res => {
  238. // reject(res)
  239. wx.showToast({
  240. title: res.errMsg,
  241. icon: 'none'
  242. })
  243. },
  244. complete: res =>{
  245. wx.hideLoading({
  246. success: (res) => {},
  247. })
  248. }
  249. })
  250. }).catch(err => {
  251. console.log(err)
  252. })
  253. }
  254. module.exports = {
  255. formatTime: formatTime,
  256. doPost: doPost,
  257. notWxPost: notWxPost,
  258. validDay: validDay,
  259. validMinutes: validMinutes,
  260. getDayBetween: getDayBetween,
  261. validHour: validHour,
  262. curTime: curTime,
  263. setAccessHeader: setAccessHeader,
  264. distance: distance,
  265. apiPost: apiPost,
  266. fun_date: fun_date,
  267. fun_week: fun_week,
  268. getHeaders:getHeaders
  269. }