util.js 9.5 KB

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