w3ctech

thinkjs远程http调用封装

BaseController

module.exports = Controller(function() {
    'use strict';
    return {
        init: function(http) {
            this.super("init", http);
            /*
            // 通过获取session判断是否已经登录
            var self = this;
            return this.session("userInfo").then(function(data) {
                if(isEmpty(data)) {
                    return self.redirect("/login"); 
                } else {
                    self.userInfo = data;
                }
            });*/
        },
        // 远程Http调用,path:请求地址,method:POST/GET,data:json格式数据,success:请求成功的回调方法,error:请求失败的回调方法
        performRequest: function(path, method, data, success, error) {
            var dataString = JSON.stringify(data);
            var headers = {};

            if(method == 'GET') {
                path += '?' + querystring.stringify(data);
            } else {
                headers = {
                    // 'Content-Type': 'application/json',
                    'Accept': 'application/json',
                    'Content-Length': dataString.length
                };
            }
            var options = {
                host: C('api_host'),
                port: C('api_port'),
                path: path,
                method: method,
                headers: headers
            };

            var http = require('http');
            var req = http.request(options, function(res) {
                res.setEncoding('utf-8');

                var responseString = '';

                res.on('data', function(data) {
                    responseString += data;
                });

                res.on('end', function() {
                    console.log(responseString);
                    var responseObject = JSON.parse(responseString);
                    success(responseObject);
                });
            });

            req.on('error', function(e) {
                console.log('Problem with request: ' + e.message);
                error(e);
            });

            req.write(dataString);
            req.end();
        }
    }
});

子类调用实例

module.exports = Controller("Home/BaseController", function() {
    "use strict";
    return {
        indexAction: function() {
            this.action("pickuporder:list", []);
        },
        listAction: function() {
            var self = this;
            var param = {
                iDisplayStart  : self.param("iDisplayStart"),  
                iDisplayLength : self.param("iDisplayLength"),   
                keyword        : self.param("keyword"), 
                text           : self.param("text")
            }

            self.performRequest('/demo-operation-portal/pickupOrderList/demoQuery.action', 'POST', param, function(data) {
                // self.end(JSON.stringify(data));
                self.assign('data', data);
                return self.display();
            }, function(e) {
                self.end('Error');
            });
        },
        detailAction: function() {
            return this.display();
        },
        addAction: function() {

        },
        editAction: function() {

        },
        deleteAction: function() {

        }
    }
});
w3ctech微信

扫码关注w3ctech微信公众号

共收到2条回复

  • request

    回复此楼
  • 看了下 你这个是thinkjs服务器端请求别处远程http的吧?我以为是别处跨域请求thinkjs服务端

    回复此楼