Skip to content

Instantly share code, notes, and snippets.

@stenote
Created September 7, 2017 06:19
Show Gist options
  • Save stenote/73cc1d4d8269357c33cf22459a69b479 to your computer and use it in GitHub Desktop.
Save stenote/73cc1d4d8269357c33cf22459a69b479 to your computer and use it in GitHub Desktop.
// eslint-disable-next-line
const config = require('/etc/dove.json').providers.lmobile;
const request = require('request-promise');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const parseXMLPromise = promisify(parseString);
// 发送数据
async function sender(data) {
const body = await request.post(config.url, {
form: {
sname: config.sname,
spwd: config.spwd,
scorpid: '', // scorpid 必填, 但是为空, 详细请查看接口文档
sprdid: config.sprdid,
sdst: data.phone,
smsg: `${data.content}【免费无线】`, // 需要增加签名 【免费无线】
},
});
const result = await parseXMLPromise(body, {
explicitArray: false,
ignoreAttrs: true,
});
// 处理成功, 返回的 XML 数据结构如下, 其中 State 表示处理结果
// 当 State === '0' 表示处理成功 (XML 本身无法存储强类型, JS 弱类型, 解析后结果为 '0')
/*
<?xml version="1.0" encoding="utf-8"?>
<CSubmitState xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<State>0</State>
<MsgID>1709061509240038262</MsgID>
<MsgState>提交成功</MsgState>
<Reserve>1</Reserve>
</CSubmitState>
*/
if (result.CSubmitState.State === '0') {
return body;
}
// 其他都属于错误, 返回 body 即可
throw new Error(body);
}
module.exports = sender;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment