import {BasicCustomerInstance, CustomerParams} from '../Controller'; import {stringify} from 'querystring'; import {compileExpressionString, isExpression} from '../../../util/vm'; import * as _ from 'lodash'; import {parseExpressString} from '../../../parser/index'; export interface PassCustomerExecConfig { /** * 跳转的地址 */ href: string; /** * 跳转带的参数 */ params?: Object | string; } export class LocationCustomer implements BasicCustomerInstance { async exec(config: PassCustomerExecConfig, customParams: CustomerParams) { let { runTime } = customParams; let targetHref = config.href; let locationParams = config.params; if (!targetHref) { console.error('targetHref is necessary for location jumping'); } else if (typeof targetHref === 'string' && isExpression(targetHref)) { targetHref = parseExpressString(targetHref, runTime); } if (locationParams) { if (typeof locationParams === 'string' && isExpression(locationParams)) { locationParams = parseExpressString(locationParams, runTime); } if (_.isPlainObject(locationParams)) { locationParams = compileExpressionString(locationParams, runTime); } locationParams = (targetHref.indexOf('?') === -1 ? '?' : '&') + stringify(locationParams); } else { locationParams = ''; } location.href = targetHref + locationParams; } }