user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import storage from "../utils/storage";
  2. import Api from "./api";
  3. import Route from "./route";
  4. import {liveSources} from "./enum";
  5. import {showLoading, toast} from "../utils/utils";
  6. const app = getApp();
  7. class User {
  8. static async getUserSign(userId) {
  9. const res = await Api.getSig({userId});
  10. return res.data
  11. }
  12. static async loginByPhone({phone, verify}) {
  13. showLoading();
  14. const res = await Api.login(phone, verify);
  15. app.globalData.token = res.data.access_token;
  16. storage.setItem("token", app.globalData.token)
  17. await this.updateUserInfo();
  18. wx.hideLoading();
  19. }
  20. static async loginByWechat() {
  21. showLoading();
  22. const data = await wx.login();
  23. const res = await Api.loginByWX(data.code);
  24. app.globalData.token = res.data.access_token;
  25. storage.setItem("token", app.globalData.token)
  26. await this.updateUserInfo();
  27. wx.hideLoading();
  28. }
  29. static async bindWechat() {
  30. const data = await wx.login();
  31. await Api.binding(data.code);
  32. }
  33. static isLogin() {
  34. const user = storage.getItem('user');
  35. return !!user;
  36. }
  37. static async checkLogin() {
  38. return new Promise((resolve, reject) => {
  39. if (this.isLogin()) {
  40. resolve();
  41. } else {
  42. Route.toLogin({
  43. login: ({isLogin}) => {
  44. if (isLogin) {
  45. resolve();
  46. } else {
  47. reject();
  48. }
  49. }
  50. })
  51. }
  52. });
  53. }
  54. static async logout() {
  55. storage.clearItem('token');
  56. storage.clearItem('user');
  57. app.globalData.token = "";
  58. try {
  59. await Api.appLogout();
  60. return true;
  61. } catch (e) {
  62. console.log(e)
  63. return false;
  64. }
  65. }
  66. static getUserInfoByLocal() {
  67. return storage.getItem('user');
  68. }
  69. static async updateUserInfo(user) {
  70. if (!user) {
  71. const res = await Api.getUserInfo();
  72. user = res.data.student;
  73. }
  74. return storage.setItem('user', user);
  75. }
  76. static async checkLive(id) {
  77. console.log(`分享进来的id${id}`)
  78. return new Promise(async (resolve, reject) => {
  79. if (this.isLogin()) {
  80. try {
  81. const res = await Api.getUserLiveId(true);
  82. if (res.data && res.data.length > 0) {
  83. const schedule = res.data.find(item => {
  84. return item.scheduleId == id;
  85. })
  86. if (schedule) {
  87. await Route.toLive(liveSources.DEFAULT,
  88. schedule.courseCeremoneyName,
  89. id, schedule.eStudentId, schedule.eduStuName)
  90. return;
  91. }
  92. }
  93. } catch (e) {
  94. toast("分享异常")
  95. console.log("分享卡片进入获取直播接口有异常情况", e)
  96. }
  97. const user = User.getUserInfoByLocal();
  98. await Route.toLive(liveSources.GUEST,
  99. "分享直播",
  100. id, user.id, user.name)
  101. resolve(true);
  102. } else {
  103. Route.toLogin({
  104. login: ({isLogin}) => {
  105. resolve(isLogin);
  106. }
  107. })
  108. }
  109. })
  110. }
  111. }
  112. export default User