'use strict' const AntCloudClient = require('./base.js') const b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const b64pad = "="; const BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz"; function int2char(n) { return BI_RM.charAt(n); } // convert a base64 string to hex function b64tohex(s) { let ret = "" let i; let k = 0; // b64 state, 0-3 let slop; let v; for(i = 0; i < s.length; ++i) { if(s.charAt(i) == b64pad) break; v = b64map.indexOf(s.charAt(i)); if(v < 0) continue; if(k == 0) { ret += int2char(v >> 2); slop = v & 3; k = 1; } else if(k == 1) { ret += int2char((slop << 2) | (v >> 4)); slop = v & 0xf; k = 2; } else if(k == 2) { ret += int2char(slop); ret += int2char(v >> 2); slop = v & 3; k = 3; } else { ret += int2char((slop << 2) | (v >> 4)); ret += int2char(v & 0xf); k = 0; } } if(k == 1) ret += int2char(slop << 2); return ret; } module.exports = async (ctx) => { const args = ctx.args || {} const bizid = args.bizid || '' const method = args.method || '' const param = args.param || {} const user = await ctx.basement.openapi.alipay.exec('alipay.user.info.share') const userId = user.userId // 初始化客户端 const client = new AntCloudClient({ endpoint: 'https://prodapigw.cloud.alipay.com/gateway.do', accessKey: '${accessKey}', accessSecret: '${accessSecret}' }) // 构建请求 const request = { version: '1.0', // product_instance_id: 'baas-account-prod', // region_name: 'CN-HANGZHOU-FINANCE', } if (method === 'create') { return client.execute({ account: { bizid: bizid, entity_info_type: '0', entity_info_value: userId, full_name: userId, status: '1', target_name: userId, type: '0', uid: userId, }, method: 'baas.account.mapping.start', product_instance_id: 'baas-account-prod', region_name: 'CN-HANGZHOU-FINANCE', ...request }) } // 验证用户是否有账户映射 return client.execute({ bizid: bizid, uid: userId, method: 'baas.account.mapping.query', product_instance_id: 'baas-account-prod', region_name: 'CN-HANGZHOU-FINANCE', ...request }).then((resp) => { if (resp.result_code === 'ACCOUNT_NOT_MAPPING' || (resp.result_code === 'OK' && resp.data && resp.data.binding_status !== 2)) { // 给用户创建账户映射 return client.execute({ account: { bizid: bizid, entity_info_type: '0', entity_info_value: userId, full_name: userId, status: '1', target_name: userId, type: '0', uid: userId, }, method: 'baas.account.mapping.start', product_instance_id: 'baas-account-prod', region_name: 'CN-HANGZHOU-FINANCE', ...request }) } return resp }).then(() => { const sdkData = param.data || {params: {transaction: null}} const transaction = sdkData.params.transaction let P = null if (param.isSign && transaction) { // 通过账户映射签名 P = client.execute({ bizid: bizid, uid: userId, algorithm: 'ecdsa', data: transaction.hash || '', method: 'baas.account.signature.start', product_instance_id: 'baas-account-prod', region_name: 'CN-HANGZHOU-FINANCE', ...request }).then((resp) => { if (resp.result_code === 'OK') { transaction.signature = [b64tohex(resp.signature)] sdkData.params.transaction = transaction } return sdkData }) } else { P = new Promise((resolve) => { resolve(sdkData) }) } return P }) .then((sdkData) => { // 调用合约代理接口 return client.execute({ bizid: bizid, body: JSON.stringify(sdkData), method: 'baas.contract.delegate.exec', product_instance_id: 'contract-dev-prod', // region_name: 'CN-HANGZHOU-FINANCE', ...request }) }) .then((resp) => { return resp }) }