shangtang - 过期验证.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view>
  3. <web-view :src="href"></web-view>
  4. </view>
  5. </template>
  6. <script>
  7. import "@/common/time.js"
  8. // 返回指定日期距今多少天
  9. const daysComputed = time => {
  10. let oldTimeFormat = new Date(time)
  11. let nowDate = new Date()
  12. if (nowDate.getTime() - oldTimeFormat.getTime() > 0) {
  13. let times = nowDate.getTime() - oldTimeFormat.getTime()
  14. let days = parseInt(times / (60 * 60 * 24 * 1000))
  15. return days
  16. } else {
  17. throw ('传入时间不能大于当前日期')
  18. }
  19. }
  20. // console.log(daysComputed('2022-02-26'))
  21. export default {
  22. data() {
  23. return {
  24. href: "",
  25. staff_num:"",
  26. mine_code:""
  27. };
  28. },
  29. onLoad() {
  30. this.staff_num = uni.getStorageSync('user').staff_num
  31. this.mine_code = uni.getStorageSync('mine_code')
  32. console.log(this.staff_num,this.mine_code)
  33. let shangtang_login_time = uni.getStorageSync('shangtang_login_time')
  34. console.log(shangtang_login_time)
  35. if (!uni.getStorageSync('shangtang_login_time')) {
  36. console.log('第一次登录')
  37. // 第一次登录
  38. this.get_token()
  39. } else {
  40. // 有登录记录
  41. console.log('有登录记录')
  42. // 判断距离登录日期过去多少天
  43. console.log(daysComputed(shangtang_login_time))
  44. // 如果大于5天 重新登录
  45. if (daysComputed(shangtang_login_time) > 2) {
  46. console.log("大于2天")
  47. this.get_token()
  48. } else {
  49. console.log("小于2天")
  50. // 小于5天,直接进入对应页面
  51. this.href = "http://shangtang.nxjiewei.com:8011/?token=" + uni.getStorageSync('shangtang_token')
  52. }
  53. }
  54. },
  55. methods:{
  56. get_token(){
  57. uni.request({
  58. // 登录 获取token
  59. method:"POST",
  60. url: "http://shangtang.nxjiewei.com:8011/v1/app/token/get",
  61. header:{
  62. "Content-Type": "application/json"
  63. },
  64. data: {
  65. staff_num: this.staff_num,
  66. area_code: this.mine_code
  67. },
  68. success: (res) => {
  69. console.log(res.data.data)
  70. // uni.setStorageSync('shangtang_token',res.data.data.access_token)
  71. // uni.setStorageSync('shangtang_login_time',new Date().format("yyyy-MM-dd"))
  72. // 登录成功后进入对应页面
  73. // 商汤链接+token
  74. this.href = "http://shangtang.nxjiewei.com:8011/?token=" + uni.getStorageSync('shangtang_token')
  75. // this.href = "http://shangtang.nxjiewei.com:8011/?token=" + res.data.data.access_token
  76. },
  77. fail: (err) => {
  78. uni.showToast({
  79. icon:"none",
  80. title:err
  81. })
  82. }
  83. })
  84. }
  85. }
  86. }
  87. </script>
  88. <style lang="scss">
  89. </style>