import fs from 'fs';
import fetch from 'node-fetch';
import formData from 'form-data';
import config from '../../server/config';


class live {

    constructor(options){

    }

    post(url, options, callback){
        callback = callback || function(){};
        let form = [];
        for(name in options){
          form.push(`${name}=${options[name]}`);
        }
        fetch(url, { method: 'POST', body: form.join("&") })
        .then(function(res) {
            return res.json();
        }).then(function(data) {
            callback(data)
        });
    }

    sendMessage(options, callback){
        callback = callback || function(){};
        let form = new formData();
        for(name in options){
            if(typeof options[name] == 'object'){
                for (var i = 0; i < options[name].length; i++) {
                    form.append(name+"[]", options[name][i]);
                };
            }else{
                form.append(name, options[name]);
            }
        }
        fetch(config.MANAGER_HOST, { method: 'POST', body: form })
        .then(function(res) {
            return res.text();
        }).then(function(body) {
            //console.info(options)
            //console.info(body)
            try{
                var data = JSON.parse(body);
                callback(null, data.message, data.result)
            }catch(e){
                callback(true, e, null)
            }
        });
    }
 
}


module.exports = new live()

@@ -729,15 +729,8 @@
  if(
-typeof 
 opti
@@ -743,19 +743,24 @@
 me%5D 
-== 'object'
+instanceof Array
 )%7B%0A 

add redpacket +3