user.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { userDetail, timestamp } from "../requests/user";
  2. import { formatBirthday, formatSeconds, formatTime } from "./index";
  3. import utils from "./index";
  4. import user_store from '../store/index'
  5. //刷新用户信息
  6. export function reload_userinfo($this) {
  7. wx.showLoading({
  8. title: '加载中',
  9. mask: true
  10. }),
  11. setTimeout(function () {
  12. wx.hideLoading()
  13. }, 4000)
  14. let $errcode = [
  15. 1001, 1002, 1011, 1012, 1021, 1031
  16. ]
  17. userDetail().then((res) => {
  18. // console.log("数据:", $this.userinfo['validity_unformattedTime']);
  19. let _data = res.data.data.validity_time;
  20. console.log("有效时间:" + _data);
  21. let $data = res.data;
  22. if ($errcode.indexOf($data.code) != -1) {
  23. do_logout($this)
  24. }
  25. // if ($this.userinfo['validity_time'] != -1) {
  26. // do_logout($this)
  27. // }
  28. $this.userinfo = $data.data;
  29. $this.userinfo.birthday = formatBirthday($this.userinfo.birthday)
  30. $this.userinfo['validity_unformattedTime'] = $this.userinfo['validity_time']
  31. let nowTime = timestamp()
  32. //判断是否过期
  33. if (nowTime > $this.userinfo['validity_unformattedTime']) {
  34. $this.userinfo.isActive = false
  35. $this.userinfo.isntActive = true
  36. } else {
  37. $this.userinfo.isActive = true
  38. $this.userinfo.isntActive = false
  39. }
  40. //当天具体日期
  41. // let date = new Date();
  42. // let day = date.getDate();
  43. // let Y = date.getFullYear() + "."
  44. // let M = (date.getMonth() + 1 < 10 ?
  45. // "0" + (date.getMonth() + 1) :
  46. // date.getMonth() + 1) + ".";
  47. // let D = (day < 10 ? "0" + day : day);
  48. // $this.userinfo.certainTime = Y + M + D;
  49. $this.userinfo['validity_time'] = formatTime($this.userinfo['validity_time'])
  50. // console.log("数据:", $this.userinfo['validity_unformattedTime']);
  51. $this.userinfo['level_name'] = utils.get_level_name($this.userinfo['level'])
  52. $this.userinfo['free_time'] = formatSeconds($this.userinfo['free_time'])
  53. //设置邀请码
  54. user_store.setters.set_invite_code($this.userinfo['invite_id'])
  55. $this.invite_code = wx.getStorageSync('code') ? wx.getStorageSync('code') : $this.userinfo['invite_id']
  56. user_store.setters.set_userinfo($this.userinfo)
  57. //停止下拉刷新
  58. wx.stopPullDownRefresh()
  59. wx.hideLoading()
  60. })
  61. }
  62. function do_logout($this) {
  63. $this.is_login = false
  64. wx.showToast({
  65. title: "用户信息过期,请重新登录",
  66. icon: 'none',
  67. mask: true,
  68. success() {
  69. wx.clearStorageSync()
  70. wx.setStorageSync('is_login', false)
  71. mpvue.reLaunch({
  72. url: "/pages/index/main"
  73. })
  74. try {
  75. $this.change_login_status()
  76. } catch
  77. (e) {
  78. console.log(e)
  79. }
  80. }
  81. })
  82. }
  83. export default {
  84. do_logout
  85. }