util.js 8.3 KB

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