import graphql from '../utils/graphql' ; import { removeUnlockToken } from './unlock-token' ; import * as globalData from '../../common/global-data' ; export const saveTokenToCookie = ( { access_token } ) => { return ( dispatch , getState ) => { return new Promise ( async ( resolve , reject ) => { if ( process . env . NODE_ENV === 'test' ) { return ; } $ . ajax ( { url : '/sign/in' , type : 'post' , data : { access_token } , async : false , success : ( res ) => { if ( res && res . success ) { resolve ( res ) ; } else { reject ( res ) ; } } , error : ( err ) => { console . log ( err ) ; } } ) ; } ) ; } ; } ; export const signIn = ( { data } ) => { return ( dispatch , getState ) => { return new Promise ( async ( resolve , reject ) => { let [ err , res ] = await graphql ( { apis : [ { api : 'signIn' , args : data , fields : `
            user_id
            access_token
          ` } ] } ) ; if ( err ) { reject ( err ? err . message : '账号或密码错误' ) ; } else { resolve ( res ) ; if ( res && res . access_token && typeof document != 'undefined' ) { await saveTokenToCookie ( res ) ( dispatch , getState ) ; globalData . get ( 'service-worker' ) . uninstall ( ) ; setTimeout ( ( ) => { window . location . reload ( ) ; } , 300 ) ; } } } ) ; } ; } ; export const signOut = ( ) => { return ( dispatch , getState ) => { removeUnlockToken ( ) ( dispatch , getState ) ; $ . ajax ( { url : '/sign/out' , type : 'post' , success : async ( res ) => { if ( res && res . success ) { if ( res . success && typeof window != 'undefined' ) { globalData . get ( 'service-worker' ) . uninstall ( ) ; setTimeout ( ( ) => { window . location . reload ( ) ; } , 300 ) ; } } else { alert ( '退出失败' ) ; } } } ) ; } ; } ; export const signUp = ( args ) => { return ( dispatch , getState ) => { return new Promise ( async ( resolve ) => { let [ err , res ] = await graphql ( { type : 'mutation' , apis : [ { api : 'addUser' , args , fields : `success` } ] } ) ; if ( err ) { resolve ( [ err ] ) ; } else { resolve ( [ null , res ] ) ; } } ) ; } ; } ;