util.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. wx.hideLoading({
  169. success: (res) => {},
  170. })
  171. if(res.data.message == "请先登录系统!"){
  172. wx.showModal({
  173. title: '提示',
  174. content: '请重新登录学生端',
  175. showCancel: false, //是否显示取消按钮
  176. cancelText: "否", //默认是“取消”
  177. cancelColor: '#999999', //取消文字的颜色
  178. confirmText: "我知道了", //默认是“确定”
  179. // confirmColor: 'skyblue', //确定文字的颜色
  180. success(res){
  181. if(res.confirm){
  182. wx.redirectTo({
  183. url: '/pages/login/login',
  184. })
  185. }
  186. }
  187. })
  188. }
  189. }
  190. })
  191. }).catch(err => {
  192. console.log(err)
  193. })
  194. }
  195. function doPost(url, params) {
  196. return new Promise((resolve, reject) => {
  197. wx.showLoading({
  198. title: '加载中',
  199. mask: true
  200. })
  201. let headers = getHeaders();
  202. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  203. wx.request({
  204. url: apiPath + url,
  205. header: headers,
  206. data: params,
  207. method: 'POST',
  208. success: res => {
  209. if (res.success > 1 || typeof res.version != undefined) {
  210. resolve(res.data)
  211. } else {
  212. wx.showToast({
  213. title: '加载失败,请重试',
  214. icon: 'none'
  215. })
  216. }
  217. },
  218. faild: res => {
  219. // reject(res)
  220. wx.showToast({
  221. title: res.errMsg,
  222. icon: 'none'
  223. })
  224. },
  225. complete: res =>{
  226. wx.hideLoading({
  227. success: (res) => {},
  228. })
  229. if(res.data.message == "请先登录系统!"){
  230. wx.showModal({
  231. title: '提示',
  232. content: '请重新登录学生端',
  233. showCancel: false, //是否显示取消按钮
  234. cancelText: "否", //默认是“取消”
  235. cancelColor: '#999999', //取消文字的颜色
  236. confirmText: "我知道了", //默认是“确定”
  237. // confirmColor: 'skyblue', //确定文字的颜色
  238. success(res){
  239. if(res.confirm){
  240. wx.redirectTo({
  241. url: '/pages/login/login',
  242. })
  243. }
  244. }
  245. })
  246. }
  247. }
  248. })
  249. }).catch(err => {
  250. console.log(err)
  251. })
  252. }
  253. function notWxPost(url, params) {
  254. return new Promise((resolve, reject) => {
  255. wx.showLoading({
  256. title: '加载中',
  257. mask: true
  258. })
  259. let headers = getHeaders();
  260. headers['Content-Type'] = 'application/x-www-form-urlencoded';
  261. wx.request({
  262. url: notWxApiPath + url,
  263. header: headers,
  264. data: params,
  265. method: 'POST',
  266. success: res => {
  267. if (res.success > 1 || typeof res.version != undefined) {
  268. resolve(res.data)
  269. } else {
  270. wx.showToast({
  271. title: '加载失败,请重试',
  272. icon: 'none'
  273. })
  274. }
  275. },
  276. faild: res => {
  277. // reject(res)
  278. wx.showToast({
  279. title: res.errMsg,
  280. icon: 'none'
  281. })
  282. },
  283. complete: res =>{
  284. wx.hideLoading({
  285. success: (res) => {},
  286. })
  287. if(res.data.message == "请先登录系统!"){
  288. wx.showModal({
  289. title: '提示',
  290. content: '请重新登录学生端',
  291. showCancel: false, //是否显示取消按钮
  292. cancelText: "否", //默认是“取消”
  293. cancelColor: '#999999', //取消文字的颜色
  294. confirmText: "我知道了", //默认是“确定”
  295. // confirmColor: 'skyblue', //确定文字的颜色
  296. success(res){
  297. if(res.confirm){
  298. wx.redirectTo({
  299. url: '/pages/login/login',
  300. })
  301. }
  302. }
  303. })
  304. }
  305. }
  306. })
  307. }).catch(err => {
  308. console.log(err)
  309. })
  310. }
  311. module.exports = {
  312. formatTime: formatTime,
  313. doPost: doPost,
  314. notWxPost: notWxPost,
  315. validDay: validDay,
  316. validMinutes: validMinutes,
  317. getDayBetween: getDayBetween,
  318. validHour: validHour,
  319. curTime: curTime,
  320. setAccessHeader: setAccessHeader,
  321. distance: distance,
  322. apiPost: apiPost,
  323. fun_date: fun_date,
  324. fun_week: fun_week,
  325. getHeaders:getHeaders
  326. }