login.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import Store from './store/index';
  2. import Http from './http/index';
  3. import Error from './untils/error/index';
  4. import Config from './config/index';
  5. import Router from './untils/router/index'
  6. class UserLogin {
  7. constructor () {
  8. this.state = {};
  9. this._hasShowLoginTip = false;
  10. this.instance = null;
  11. }
  12. // 静态方法作为广为人知的接口
  13. static getInstance () {
  14. if (!this.instance) {
  15. this.instance = new UserLogin();
  16. }
  17. return this.instance;
  18. }
  19. /**
  20. * 登录
  21. * @param {object} objs { mobile: '', password: '' }
  22. * @returns {boolean} 如果登录成功返回true,登录失败返回false
  23. *
  24. * @example login({mobile: '13412341234', password: '123456'})
  25. */
  26. async login (objs = { account: '', password: '' }) {
  27. // ------------------------------------------------发送登录请求
  28. let res = await Http.post({
  29. url: Http.urlMap.login,
  30. data: {
  31. account: objs.account,
  32. password: objs.password
  33. }
  34. });
  35. // 根据登录结果判断是否登录成功
  36. // ①:如果成功
  37. if (res.code === 1) {
  38. // ------------------------------------------------将用户保存到vuex中
  39. Store.commit('setUserInfo', { userinfo: res.data.userinfo });
  40. // ------------------------------------------------保存该用户的token数据
  41. // 返回true
  42. return true;
  43. } else {
  44. // ②:如果失败
  45. // 返回false
  46. return false;
  47. }
  48. }
  49. /**
  50. * 退出登录
  51. * @param {object} objs
  52. * @returns {boolean} 如果退出登录成功返回true,否则返回false
  53. */
  54. async logout (objs = {}) {
  55. Store.commit('setUserInfo', { userinfo: {} });
  56. return true;
  57. }
  58. /**
  59. * 检查是否登录
  60. * @param {object} objs
  61. * @returns {boolean} 如果登录状态正常,如果正常,返回true,否则返回false
  62. */
  63. checkIfLogin (objs = {}) {
  64. // 检查是否登录
  65. // ①:检查用户数据是否存在
  66. // ②:检查token是否过期
  67. if (this.getToken()) {
  68. return true; // 登录态正常
  69. }
  70. // 退出登录
  71. this.logout();
  72. // this.showLoginTips();
  73. return false; // 登录失效
  74. }
  75. /**
  76. * 显示登录提示
  77. * @param {object} objs
  78. */
  79. showLoginTips (objs = {}) {
  80. if (!this._hasShowLoginTip) {
  81. this._hasShowLoginTip = true;
  82. Error.errorNotLoggedIn(
  83. // success
  84. () => {
  85. this._hasShowLoginTip = false;
  86. },
  87. // fail
  88. () => {
  89. this._hasShowLoginTip = false;
  90. }
  91. );
  92. }
  93. }
  94. /**
  95. * 获取token
  96. * @returns {boolean} 如果登录状态正常返回token的值,否则返回false
  97. */
  98. getToken () {
  99. let userinfo = this.getUserInfo();
  100. if (userinfo.token) {
  101. return userinfo.token;
  102. }
  103. return false;
  104. }
  105. /**
  106. * 获取当前用户信息
  107. * @returns {object} 如果登录状态正常返回用户信息,否则返回空对象{}
  108. */
  109. getUserInfo () {
  110. // console.log(Store.state.userinfo)
  111. let userinfo = Store.state.userinfo;
  112. if (userinfo) {
  113. if (userinfo.token) {
  114. return userinfo;
  115. }
  116. }
  117. // 退出登录
  118. this.logout();
  119. return {};
  120. }
  121. //是否是vip
  122. async getSubvip(id){
  123. let res = await Http.post({
  124. url:Http.urlMap.checkvip,
  125. data:{
  126. subject_id:id
  127. },
  128. needLogin:true,
  129. })
  130. if(res.code == 1){
  131. let is_vip = res.data.is_vip
  132. Store.commit('setSubjectVipInfo', is_vip)
  133. return is_vip
  134. }
  135. }
  136. async getUnitVipInfo(type,unit_id){
  137. // type // 1:章节练习 2历年真题
  138. let res = await Http.post({
  139. url:type == 1 ? Http.urlMap.unitVip : Http.urlMap.zhentiVip,
  140. data:{
  141. id:unit_id
  142. },
  143. needLogin:true
  144. })
  145. if(res.code == 1){
  146. return res.data
  147. }
  148. }
  149. async getExamVipInfo(exam_id){
  150. console.log(exam_id)
  151. let res = await Http.post({
  152. url:Http.urlMap.examVip,
  153. data:{
  154. id:exam_id
  155. },
  156. needLogin:true
  157. })
  158. if(res.code == 1){
  159. return res.data
  160. }
  161. }
  162. }
  163. let _userLogin = new UserLogin();
  164. export default _userLogin;