util.js 8.8 KB

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