util.js 7.4 KB

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