123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import store from '../store';
- import {
- checkLogin
- } from './login';
- import {
- login,
- routineLogin,
- silenceAuth
- } from '../api/public';
- import Cache from '../utils/cache';
- import {
- STATE_R_KEY,
- USER_INFO,
- EXPIRES_TIME,
- LOGIN_STATUS
- } from './../config/cache';
- import {
- mapGetters
- } from "vuex";
- class Routine {
- constructor() {
- this.scopeUserInfo = 'scope.userInfo';
- }
- async getUserCode() {
- let isAuth = await this.isAuth(),
- code = '';
- if (isAuth)
- code = await this.getCode();
- return code;
- }
-
- getUserInfo() {
- let that = this,
- code = this.getUserCode();
- return new Promise((resolve, reject) => {
- uni.getUserInfo({
- lang: 'zh_CN',
- success(user) {
- if (code) user.code = code;
- resolve({
- userInfo: user,
- islogin: false
- });
- },
- fail(res) {
- reject(res);
- }
- })
- })
- }
-
- getUserProfile(code) {
- return new Promise((resolve, reject) => {
- uni.getUserProfile({
- lang: 'zh_CN',
- desc: '用于完善会员资料',
- success(user) {
- if (code) user.code = code;
- resolve({
- userInfo: user,
- islogin: false
- });
- },
- fail(res) {
- reject(res);
- }
- })
- })
- }
-
- authorize() {
- let that = this;
- return new Promise((resolve, reject) => {
- if (checkLogin())
- return resolve({
- userInfo: Cache.get(USER_INFO, true),
- islogin: true,
- });
- uni.authorize({
- scope: that.scopeUserInfo,
- success() {
- resolve({
- islogin: false
- });
- },
- fail(res) {
- reject(res);
- }
- })
- })
- }
- async getCode() {
- let provider = await this.getProvider();
- return new Promise((resolve, reject) => {
-
-
-
- uni.login({
- provider: provider,
- success(res) {
- if (res.code) Cache.set(STATE_R_KEY, res.code, 10800);
- return resolve(res.code);
- },
- fail() {
- return reject(null);
- }
- })
- })
- }
-
- getProvider() {
- return new Promise((resolve, reject) => {
- uni.getProvider({
- service: 'oauth',
- success(res) {
- resolve(res.provider);
- },
- fail() {
- resolve(false);
- }
- });
- });
- }
-
- isAuth() {
- let that = this;
- return new Promise((resolve, reject) => {
- uni.getSetting({
- success(res) {
- if (!res.authSetting[that.scopeUserInfo]) {
- resolve(true)
- } else {
- resolve(true);
- }
- },
- fail() {
- resolve(false);
- }
- });
- });
- }
-
-
- compareVersion(v1, v2) {
- v1 = v1.split('.')
- v2 = v2.split('.')
- const len = Math.max(v1.length, v2.length)
- while (v1.length < len) {
- v1.push('0')
- }
- while (v2.length < len) {
- v2.push('0')
- }
- for (let i = 0; i < len; i++) {
- const num1 = parseInt(v1[i])
- const num2 = parseInt(v2[i])
- if (num1 > num2) {
- return 1
- } else if (num1 < num2) {
- return -1
- }
- }
- return 0
- }
- authUserInfo(data) {
- return new Promise((resolve, reject) => {
- routineLogin(data).then(res => {
- if (res.data.key !== undefined && res.data.key) {} else {
- store.commit('UPDATE_USERINFO', res.data.userInfo);
- store.commit('SETUID', res.data.userInfo.uid);
- Cache.set(USER_INFO, res.data.userInfo);
- }
- return resolve(res);
- }).catch(res => {
- return reject(res);
- })
- })
- }
- }
- export default new Routine();
|