NodeJS 发送 POST 请求 curl -d & JS 类的静态属性使用
'use strict';
const Service = require('../lib/ServiceBase')
const tables = require('../lib/tables')
var http = require('http')
var querystring = require('querystring');
/**
* 授权绑定淘宝服务 @author Jack
* @class
* @extends Canary.Service
*/
class WealtimePersonBindService extends Service {
constructor(ctx) {
super(ctx)
WealtimePersonBindService.ctx = ctx;
}
get table() {
return tables.wealtimePersonBind
}
/**
* 淘宝授权绑定
* @param item
* @returns {null}
*/
* add(item) {
const me = this
const user = this.user
console.log(`user ===> ${JSON.stringify(user)}`)
console.log(`item ===> ${JSON.stringify(item)}`)
var code = item['code']
console.log(`code ===> ${JSON.stringify(code)}`)
var bindtype = item['bindtype']
// curl -d "code=QOwnL6ukr3LJzz2r2uVJgFnz12673853&grant_type=authorization_code&client_id=24779841&client_secret=971a06012b81382350a26ae48c2ebe0d&redirect_uri=https://welare.alibaba-inc.com/#/profile" https://oauth.taobao.com/token
var contents = querystring.stringify({
code: code,
grant_type: 'authorization_code',
client_id: '24779841',
client_secret: '971a06012b81382350a26ae48c2ebe0d',
redirect_uri: 'https://welare.alibaba-inc.com/#/profile'
});
console.log(`post contents ===> ${contents}`);
var options = {
host: 'oauth.taobao.com',
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
//只有post时,这个才有用
'Content-Length': contents.length
}
}
var requestCallback = (response) => {
response.setEncoding('utf-8');
var receiveData = "";
response.on('data', (chunk) => {
receiveData += chunk;
}).on('end', () => {
console.log('getAccessToken body 分隔线---------------------------------\r\n');
console.log(`accessToken ===> ${JSON.stringify(receiveData)}`);
var accessToken = JSON.parse(receiveData)
var taobao_user_id = accessToken.taobao_user_id
var taobao_user_nick = accessToken.taobao_user_nick
console.log(`taobao_user_id = ${taobao_user_id}`)
console.log(`taobao_user_nick = ${taobao_user_nick}`)
if (taobao_user_id && taobao_user_nick) {
try {
// 写入活动记录
console.log('WealtimePersonBindService.ctx =========================>')
console.dir(WealtimePersonBindService.ctx)
console.log('WealtimePersonBindService.ctx.app.mysql =========================>')
var mysql = WealtimePersonBindService.ctx.app.mysql
console.dir(mysql)
var ret = mysql.insert(me.table, {
taobao_user_id: taobao_user_id,
taobao_user_nick: taobao_user_nick,
// 人员信息
work_no: user['workid'],
emp_name: user['lastName'],
nick_name: user['name'],
bind_type: bindtype,
gmt_create: new Date(),
gmt_modified: new Date(),
});
return ret.insertId
} catch (e) {
console.log(`[service.WealtimePersonBindService.add] ${e}`);
throw e
}
}
});
};
// post 请求 AccessToken
var req = http.request(options, requestCallback).on('error', function (e) {
console.log(e.message);
});
req.on('error', function (e) {
console.log(`getAccessToken error ===> ${JSON.stringify(e)}`)
});
req.write(contents);
req.end();
}
}
WealtimePersonBindService.ctx;
module.exports = WealtimePersonBindService