import hs256 from 'crypto-js/hmac-sha256' ; import base64 from 'crypto-js/enc-base64' ; import utf8 from 'crypto-js/enc-utf8' ; import { KV } from '../types' ; export const getQuery = function ( name , url ? ) { if ( typeof window === 'undefined' ) { return false ; } let u = url || window . location . search ; let reg = new RegExp ( '(^|&)' + name + '=([^&]*)(&|$)' ) ; let r = u . substr ( u . indexOf ( '?' ) + 1 ) . match ( reg ) ; return r != null ? r [ 2 ] : '' ; } ; export const getHash = function ( name ) { if ( typeof window === 'undefined' ) { return '' ; } const matches = window . location . hash . match ( new RegExp ( `[#\?&\/]${ name }=([^&#]*)` ) ) ; return matches ? matches [ 1 ] : '' ; } ; export const removeParam = function ( key , sourceURL ) { let rtn = sourceURL . split ( '?' ) [ 0 ] ; let param ; let params_arr = [ ] ; let queryString = sourceURL . indexOf ( '?' ) !== - 1 ? sourceURL . split ( '?' ) [ 1 ] : '' ; if ( queryString !== '' ) { params_arr = queryString . split ( '&' ) ; for ( let i = params_arr . length - 1 ; i >= 0 ; i -= 1 ) { param = params_arr [ i ] . split ( '=' ) [ 0 ] ; if ( param === key ) { params_arr . splice ( i , 1 ) ; } } rtn = rtn + '?' + params_arr . join ( '&' ) ; } return rtn ; } ; export const createPromiseCallback = ( ) => { let cb ; if ( ! Promise ) { cb = ( ) => { } ; cb . promise = { } ; const throwPromiseNotDefined = ( ) => { throw new Error ( 'Your Node runtime does support ES6 Promises. ' + 'Set "global.Promise" to your preferred implementation of promises.' ) ; } ; Object . defineProperty ( cb . promise , 'then' , { get : throwPromiseNotDefined } ) ; Object . defineProperty ( cb . promise , 'catch' , { get : throwPromiseNotDefined } ) ; return cb ; } const promise = new Promise ( ( resolve , reject ) => { cb = ( err , data ) => { if ( err ) return reject ( err ) ; return resolve ( data ) ; } ; } ) ; cb . promise = promise ; return cb ; } ; export const getWeixinCode = function ( ) { return getQuery ( 'code' ) || getHash ( 'code' ) ; } ; export const getMiniAppCode = function ( ) : Promise < string > { return new Promise ( ( resolve , reject ) => { wx . login ( { success ( res ) { resolve ( res . code ) ; } , fail ( err ) { reject ( err ) ; } } ) ; } ) ; } ; export function isArray ( val ) { return Object . prototype . toString . call ( val ) === '[object Array]' ; } export function isString ( val ) { return typeof val === 'string' ; } export function isUndefined ( val ) { return typeof val === 'undefined' ; } export function isNull ( val ) { return Object . prototype . toString . call ( val ) === '[object Null]' ; } export function isInstanceOf ( instance , construct ) { return instance instanceof construct ; } export function isFormData ( val ) { return Object . prototype . toString . call ( val ) === '[object FormData]' ; } export function genSeqId ( ) { return Math . random ( ) . toString ( 16 ) . slice ( 2 ) ; } export function getArgNames ( fn ) { const funStr = fn . toString ( ) ; return funStr . slice ( funStr . indexOf ( '(' ) + 1 , funStr . indexOf ( ')' ) ) . match ( /([^\s,]+)/g ) ; } export function formatUrl ( protocol , url , query = { } ) { const urlHasQuery = /\?/ . test ( url ) ; let queryString = '' ; for ( let key in query ) { if ( queryString === '' ) { ! urlHasQuery && ( url += '?' ) ; } else { queryString += '&' ; } queryString += `${ key }=${ encodeURIComponent ( query [ key ] ) }` ; } url += queryString ; if ( /^http(s)?\:\/\// . test ( url ) ) { return url ; } return `${ protocol }${ url }` ; } export function toQueryString ( query = { } ) { let queryString = [ ] ; for ( let key in query ) { queryString . push ( `${ key }=${ encodeURIComponent ( query [ key ] ) }` ) ; } return queryString . join ( '&' ) ; } function base64url ( source ) { let encodedSource = base64 . stringify ( source ) ; encodedSource = encodedSource . replace ( /=+$/ , '' ) ; encodedSource = encodedSource . replace ( /\+/g , '-' ) ; encodedSource = encodedSource . replace ( /\//g , '_' ) ; return encodedSource ; } export function createSign ( payload , secret ) { const header = { alg : 'HS256' , typ : 'JWT' } ; const headerStr = base64url ( utf8 . parse ( JSON . stringify ( header ) ) ) ; const payloadStr = base64url ( utf8 . parse ( JSON . stringify ( payload ) ) ) ; const token = `${ headerStr }.${ payloadStr }` ; const sign = base64url ( hs256 ( token , secret ) ) ; return `${ token }.${ sign }` ; }