routine.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import store from '../store';
  2. import { checkLogin } from './login';
  3. import { login } from '../api/public';
  4. import Cache from '../utils/cache';
  5. import { STATE_R_KEY, USER_INFO, EXPIRES_TIME, LOGIN_STATUS} from './../config/cache';
  6. class Routine
  7. {
  8. constructor()
  9. {
  10. this.scopeUserInfo = 'scope.userInfo';
  11. }
  12. async getUserCode(){
  13. let isAuth = await this.isAuth(), code = '' ;
  14. if(isAuth)
  15. code = await this.getCode();
  16. return code;
  17. }
  18. /**
  19. * 获取用户信息
  20. */
  21. getUserInfo(){
  22. let that = this , code = this.getUserCode();
  23. return new Promise( (resolve,reject) => {
  24. uni.getUserInfo({
  25. lang: 'zh_CN',
  26. success(user) {
  27. if(code) user.code = code;
  28. resolve({userInfo:user,islogin:false});
  29. },
  30. fail(res){
  31. reject(res);
  32. }
  33. })
  34. })
  35. }
  36. /**
  37. * 获取用户信息
  38. */
  39. authorize()
  40. {
  41. let that = this;
  42. return new Promise((resolve,reject)=>{
  43. if(checkLogin())
  44. return resolve({
  45. userInfo:Cache.get(USER_INFO,true),
  46. islogin:true,
  47. });
  48. uni.authorize({
  49. scope: that.scopeUserInfo,
  50. success() {
  51. resolve({islogin:false});
  52. },
  53. fail(res){
  54. reject(res);
  55. }
  56. })
  57. })
  58. }
  59. async getCode(){
  60. let provider = await this.getProvider();
  61. return new Promise((resolve,reject)=>{
  62. if(Cache.has(STATE_R_KEY)){
  63. return resolve(Cache.get(STATE_R_KEY));
  64. }
  65. uni.login({
  66. provider:provider,
  67. success(res) {
  68. if (res.code) Cache.set(STATE_R_KEY, res.code ,10800);
  69. return resolve(res.code);
  70. },
  71. fail(){
  72. return reject(null);
  73. }
  74. })
  75. })
  76. }
  77. /**
  78. * 获取服务供应商
  79. */
  80. getProvider()
  81. {
  82. return new Promise((resolve,reject)=>{
  83. uni.getProvider({
  84. service:'oauth',
  85. success(res) {
  86. resolve(res.provider);
  87. },
  88. fail() {
  89. resolve(false);
  90. }
  91. });
  92. });
  93. }
  94. /**
  95. * 是否授权
  96. */
  97. isAuth(){
  98. let that = this;
  99. return new Promise((resolve,reject)=>{
  100. uni.getSetting({
  101. success(res) {
  102. if (!res.authSetting[that.scopeUserInfo]) {
  103. resolve(true)
  104. } else {
  105. resolve(true);
  106. }
  107. },
  108. fail(){
  109. resolve(false);
  110. }
  111. });
  112. });
  113. }
  114. authUserInfo(data)
  115. {
  116. return new Promise((resolve, reject)=>{
  117. login(data).then(res=>{
  118. let time = res.data.expires_time - Cache.time();
  119. store.commit('UPDATE_USERINFO', res.data.userInfo);
  120. store.commit('LOGIN', {token:res.data.token, time:time});
  121. store.commit('SETUID', res.data.userInfo.uid);
  122. Cache.set(EXPIRES_TIME,res.data.expires_time,time);
  123. Cache.set(USER_INFO,res.data.userInfo,time);
  124. return resolve(res);
  125. }).catch(res=>{
  126. return reject(res);
  127. })
  128. })
  129. }
  130. }
  131. export default new Routine();