routine.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import store from '../store';
  2. import {
  3. checkLogin
  4. } from './login';
  5. import {
  6. login,
  7. routineLogin,
  8. silenceAuth
  9. } from '../api/public';
  10. import Cache from '../utils/cache';
  11. import {
  12. STATE_R_KEY,
  13. USER_INFO,
  14. EXPIRES_TIME,
  15. LOGIN_STATUS
  16. } from './../config/cache';
  17. import {
  18. mapGetters
  19. } from "vuex";
  20. class Routine {
  21. constructor() {
  22. this.scopeUserInfo = 'scope.userInfo';
  23. }
  24. async getUserCode() {
  25. let isAuth = await this.isAuth(),
  26. code = '';
  27. if (isAuth)
  28. code = await this.getCode();
  29. return code;
  30. }
  31. // 小程序静默授权
  32. // silenceAuth(code) {
  33. // const app = getApp();
  34. // let that = this;
  35. // let spread = app.globalData.spid ? app.globalData.spid : '';
  36. // return new Promise((resolve, reject) => {
  37. // silenceAuth({
  38. // code: code,
  39. // spread_spid: spread,
  40. // spread_code: app.globalData.code
  41. // })
  42. // .then(res => {
  43. // if (res.data && res.data.token !== undefined) {
  44. // uni.hideLoading();
  45. // let time = res.data.expires_time - Math.round(new Date() / 1000);
  46. // store.commit('LOGIN', {
  47. // token: res.data.token,
  48. // time: time
  49. // });
  50. // store.commit('SETUID', res.data.userInfo.uid);
  51. // store.commit('UPDATE_USERINFO', res.data.userInfo);
  52. // resolve(res)
  53. // } else {
  54. // reject()
  55. // uni.navigateTo({
  56. // url: '/pages/users/wechat_login/index'
  57. // })
  58. // }
  59. // })
  60. // .catch(err => {
  61. // reject(err)
  62. // });
  63. // })
  64. // }
  65. /**
  66. * 获取用户信息
  67. */
  68. getUserInfo() {
  69. let that = this,
  70. code = this.getUserCode();
  71. return new Promise((resolve, reject) => {
  72. uni.getUserInfo({
  73. lang: 'zh_CN',
  74. success(user) {
  75. if (code) user.code = code;
  76. resolve({
  77. userInfo: user,
  78. islogin: false
  79. });
  80. },
  81. fail(res) {
  82. reject(res);
  83. }
  84. })
  85. })
  86. }
  87. /**
  88. * 新版小程序获取用户信息 2021 4.13微信小程序开始正式启用
  89. */
  90. getUserProfile(code) {
  91. return new Promise((resolve, reject) => {
  92. uni.getUserProfile({
  93. lang: 'zh_CN',
  94. desc: '用于用户注册,登录', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  95. success(user) {
  96. if (code) user.code = code;
  97. resolve({
  98. userInfo: user,
  99. islogin: false
  100. });
  101. },
  102. fail(res) {
  103. reject(res);
  104. }
  105. })
  106. })
  107. }
  108. /**
  109. * 获取用户信息
  110. */
  111. authorize() {
  112. let that = this;
  113. return new Promise((resolve, reject) => {
  114. if (checkLogin())
  115. return resolve({
  116. userInfo: Cache.get(USER_INFO, true),
  117. islogin: true,
  118. });
  119. uni.authorize({
  120. scope: that.scopeUserInfo,
  121. success() {
  122. resolve({
  123. islogin: false
  124. });
  125. },
  126. fail(res) {
  127. reject(res);
  128. }
  129. })
  130. })
  131. }
  132. async getCode() {
  133. // let provider = await this.getProvider();
  134. return new Promise((resolve, reject) => {
  135. // if(Cache.has(STATE_R_KEY)){
  136. // return resolve(Cache.get(STATE_R_KEY));
  137. // }
  138. uni.login({
  139. provider: 'weixin',
  140. success(res) {
  141. if (res.code) Cache.set(STATE_R_KEY, res.code, 10800);
  142. return resolve(res.code);
  143. },
  144. fail() {
  145. return reject(null);
  146. }
  147. })
  148. })
  149. }
  150. /**
  151. * 获取服务供应商
  152. */
  153. getProvider() {
  154. return new Promise((resolve, reject) => {
  155. uni.getProvider({
  156. service: 'oauth',
  157. success(res) {
  158. resolve(res.provider);
  159. },
  160. fail() {
  161. resolve(false);
  162. }
  163. });
  164. });
  165. }
  166. /**
  167. * 是否授权
  168. */
  169. isAuth() {
  170. let that = this;
  171. return new Promise((resolve, reject) => {
  172. uni.getSetting({
  173. success(res) {
  174. if (!res.authSetting[that.scopeUserInfo]) {
  175. resolve(true)
  176. } else {
  177. resolve(true);
  178. }
  179. },
  180. fail() {
  181. resolve(false);
  182. }
  183. });
  184. });
  185. }
  186. authUserInfo(data) {
  187. return new Promise((resolve, reject) => {
  188. routineLogin(data).then(res => {
  189. if (res.data.key !== undefined && res.data.key) {} else {
  190. store.commit('UPDATE_USERINFO', res.data.userInfo);
  191. // store.commit('SETUID', res.data.userInfo.id);
  192. Cache.set(USER_INFO, res.data.userInfo);
  193. }
  194. return resolve(res);
  195. }).catch(res => {
  196. return reject(res);
  197. })
  198. })
  199. }
  200. }
  201. export default new Routine();