user.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import storage from "../utils/storage";
  2. import Api from "./api";
  3. import Route from "./route";
  4. import {liveSources} from "./enum";
  5. import {showLoading} 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. if (!user) {
  36. // 如果没有user信息,将token也删除
  37. storage.clearItem('token');
  38. }
  39. if(user && user.isLiveGuest) {
  40. //如果是直播游客(未实际登录的)
  41. return false;
  42. }
  43. return !!user;
  44. }
  45. static async checkLogin() {
  46. return new Promise((resolve, reject) => {
  47. if (this.isLogin()) {
  48. resolve();
  49. } else {
  50. Route.toLogin({
  51. login: ({isLogin}) => {
  52. if (isLogin) {
  53. resolve();
  54. } else {
  55. reject();
  56. }
  57. }
  58. })
  59. }
  60. });
  61. }
  62. static async logout() {
  63. storage.clearItem('token');
  64. storage.clearItem('user');
  65. app.globalData.token = "";
  66. try {
  67. await Api.appLogout();
  68. return true;
  69. } catch (e) {
  70. console.log(e)
  71. return false;
  72. }
  73. }
  74. static getUserInfoByLocal() {
  75. return storage.getItem('user');
  76. }
  77. static async updateUserInfo(user) {
  78. if (!user) {
  79. const res = await Api.getUserInfo();
  80. user = res.data.student;
  81. if (user.wechatOpenId && !user.msWechatOpenId) {
  82. // 如果绑定了微信,但是发送消息的openid是空的,执行绑定微信方法 -- 对应在app中完成微信绑定的情况
  83. this.bindWechat();
  84. }
  85. }
  86. return storage.setItem('user', user);
  87. }
  88. static async gotoLive(id) {
  89. console.log(`分享进来的id${id}`)
  90. return new Promise(async (resolve, reject) => {
  91. // const {data: courseInfo} = await Api.getCourseInfo(id);
  92. // if (!courseInfo) {
  93. // toast('课程不存在!');
  94. // resolve(true);
  95. // return;
  96. // }
  97. // if (courseInfo.liveStatus == liveStatus.NOLIVE) {
  98. // toast('课程还未开始直播');
  99. // resolve(true);
  100. // return;
  101. // } else if (courseInfo.liveStatus == liveStatus.LIVEEND) {
  102. // toast('直播已结束!');
  103. // resolve(true);
  104. // return;
  105. // }
  106. // const isLogin = await this.isLogin();
  107. // // console.log('isLogin ->', isLogin);
  108. // if(!isLogin) {
  109. // resolve('NOT_LOGIN');
  110. // return;
  111. // }
  112. //判断学员是否为在直播课班级内的
  113. const res = await Api.getUserLiveId(true);
  114. if (res.data && res.data.length > 0) {
  115. const schedule = res.data.find(item => {
  116. return item.scheduleId == id;
  117. });
  118. // console.log('schedule --> ', schedule);
  119. if (schedule) {
  120. Route.toLive(liveSources.DEFAULT,
  121. schedule.courseCeremoneyName,
  122. id, schedule.eStudentId, schedule.eduStuName,schedule.courseThumbnailUrl)
  123. resolve(true);
  124. return;
  125. }
  126. }
  127. const user = await User.getUserInfoByLocal();
  128. // console.log('user -> ', user);
  129. Route.toLive(liveSources.GUEST,
  130. "分享直播",
  131. id, user.id, user.name,"")
  132. resolve(true);
  133. })
  134. }
  135. }
  136. export default User