文章标签 ‘sails’
201511 月20

Node.js:集成百度编辑器的上传功能

Ueditor1.4.3.1版本,仅贴了图片上传代码,其他自己加。。。

/**
 * Created by root on 11/16/15.
 */
var fs = require('fs-extra');
var moment = require("moment");
module.exports = {
  index: function (req, res) {
    if (req.session.auth && !req.session.user.disabled) {
      var action = req.query.action;
      //加载配置文件
      fs.readFile(sails.config.appPath + '/assets/plugins/ueditor/node/config.json', 'utf8', function (err, config_txt) {
        var config = JSON.parse(config_txt);
        switch (action) {
          case 'config':
            return res.send(config_txt);
            break;
          case 'uploadimage':

            req.file('Filedata').upload({
              maxBytes: config.imageMaxSize
            }, function (err, uploadedFiles) {
              if (err)return res.json({state: sails.__('file.upload.err')});
              var filename = uploadedFiles[0].filename;
              var type = uploadedFiles[0].type;
              var fd = uploadedFiles[0].fd;
              var size = uploadedFiles[0].size;
              console.log('uploadimage:::' + JSON.stringify(uploadedFiles));
              if(config.imageAllowFiles.indexOf(fd.substring(fd.lastIndexOf('.')))<0)
                return res.json({state: sails.__('file.upload.err')});
              var file = fd.substring(fd.lastIndexOf('/'));
              var newPath = sails.config.system.AppBase + sails.config.system.UploadPath + "/image/" + moment().format("YYYYMMDD") + file;

              fs.copy(fd, sails.config.appPath + newPath, function (err) {
                if (err)return res.json({state: sails.__('file.upload.err')});
                return res.json({state: 'SUCCESS', url: newPath, title: filename, original: filename, type: type, size: size});
              })

            });
            break;
        }
      });
    } else {
      return res.json({state: sails.__('private.forbidden'), url: '', title: '', original: '', type: '', size: 0});
    }
  }
};
201510 月27

Node.js:微信消息回复、推送的实现

适应场景:Sails框架、微信公众号数据配置在数据库中,支持多个公众号。
1、Model定义(Waterline)
文件路径:/api/models/wx/Wx_config.js

/**
 * Created by root on 10/25/15.
 */
var moment = require('moment');
module.exports = {
  schema: true,
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true
    },
    appname: {
      type: 'string',
      required: true
    },
    ghid: {//原始ID
      type: 'string',
      unique: true,
      required: true
    },
    appid: {
      type: 'string',
      unique: true,
      required: true
    },
    appsecret: {
      type: 'string',
      unique: true,
      required: true
    },
    encodingAESKey:{
      type: 'string',
      unique: true,
      required: true
    },
    token: {
      type: 'string',
      unique: true,
      required: true
    },
    access_token: {
      type: 'string'
    },
    expire_time: {
      type:'integer',
      defaultsTo:function(){
        return 0;
      }
    },
    createdBy:{
      type: 'integer'
    },
    createdAt:{
      type:'integer',
      defaultsTo:function(){
        return moment().format('X');
      }
    }
  }
};

2、Sails默认不支持微信xml post,改造之
文件路径:/config/http.js

rawParser: function(req, res, next) {
      switch(req.headers['content-type']) {
        case 'text/xml':
          require('body-parser').raw({ type: 'text/xml' })(req, res, next);
          break;
        default :
          next();
      }
    },
    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'myRequestLogger',
      'rawParser',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ],

3、控制类
文件路径:/api/controllers/open/WeixinController.js
微信公众号后台配置路径:http://127.0.0.1/open/weixin/api?wxid=1

/**
 * Created by root on 10/25/15.
 */
module.exports = {
  api: function (req, res) {
    var id = req.query.wxid;
    Wx_config.findOne(id).exec(function (err, conf) {
      if (err)return res.send(200, 'fail');
      if (req.body) {
        WeixinService.loop(req, function (data) {
          if (data.type = 'text') {//用户发送纯文本
            var msg = {toUserName: data.openid, fromUserName: conf.ghid, content: 'Node.js Test...'};
            WeixinService.sendTextMsg(res, msg);//向用户回复消息
          }
        });
      }
      //签名
      if (WeixinService.checkSignature(req, conf.token)) {
        res.send(200, req.query.echostr);
      } else {
        res.send(200, 'fail');
      }
    });
  }
};

4、微信服务工具类
文件路径:/api/services/WeixinService.js

/**
 * Created by root on 10/26/15.
 */
var sha1 = require('sha1'), xml2js = require('xml2js');
module.exports = {
  /**
   * 签名验证
   * @param req
   * @param token
   * @returns {boolean}
   */
  checkSignature: function (req, token) {
    var signature = req.query.signature,
      timestamp = req.query.timestamp,
      nonce = req.query.nonce,
      echostr = req.query.echostr;
    // 按照字典排序
    var array = [token, timestamp, nonce];
    array.sort();
    // 连接
    var str = sha1(array.join(""));
    // 对比签名
    return str == signature;
  },
  /**
   * 监听用户消息
   * @param req
   * @param callback
   */
  loop: function (req, callback) {
    var body = req.body.toString('utf-8');
    var data = {};
    xml2js.parseString(body, function (err, json) {
      if (err) {
      } else {
        console.log('json::' + JSON.stringify(json));
        data.type = json.xml.MsgType;
        data.openid = json.xml.FromUserName;
        if (data.type == 'text') {
          data.txt = json.xml.Content;
        } else if (data.type == 'image') {
          data.pic = json.xml.PicUrl;
        }
        return callback(data);
      }
    });
  },
  /**
   * 发送文本消息
   * @param res
   * @param msg
   */
  sendTextMsg: function (res, msg) {
    var time = Math.round(new Date().getTime() / 1000);

    var funcFlag = msg.funcFlag ? msg.funcFlag : 0;

    var output = "" +
      "<xml>" +
      "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" +
      "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" +
      "<CreateTime>" + time + "</CreateTime>" +
      "<MsgType><![CDATA[text]]></MsgType>" +
      "<Content><![CDATA[" + msg.content + "]]></Content>" +
      "<FuncFlag>" + funcFlag + "</FuncFlag>" +
      "</xml>";

    res.type('xml');
    res.send(output);
  },
  /**
   * 发送图文消息
   * @param res
   * @param msg
   */
  sendNewsMsg: function (res, msg) {
    var time = Math.round(new Date().getTime() / 1000);
    var articlesStr = "";
    for (var i = 0; i < msg.articles.length; i++) {
      articlesStr += "<item>" +
        "<Title><![CDATA[" + msg.articles[i].title + "]]></Title>" +
        "<Description><![CDATA[" + msg.articles[i].description + "]]></Description>" +
        "<PicUrl><![CDATA[" + msg.articles[i].picUrl + "]]></PicUrl>" +
        "<Url><![CDATA[" + msg.articles[i].url + "]]></Url>" +
        "</item>";
    }

    var funcFlag = msg.funcFlag ? msg.funcFlag : 0;
    var output = "" +
      "<xml>" +
      "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" +
      "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" +
      "<CreateTime>" + time + "</CreateTime>" +
      "<MsgType><![CDATA[news]]></MsgType>" +
      "<ArticleCount>" + msg.articles.length + "</ArticleCount>" +
      "<Articles>" + articlesStr + "</Articles>" +
      "<FuncFlag>" + funcFlag + "</FuncFlag>" +
      "</xml>";

    res.type('xml');
    res.send(output);
  }
};

Nodejs版本:0.12.7
Sails版本:0.11.0
require相关组件,记得安装。
参考项目1:https://github.com/node-webot/wechat-api
参考项目2:https://github.com/JeremyWei/weixin_api

20159 月11

Sails:Waterline多对多查询

Sys_user.js

module.exports = {
  schema: true,
  autoCreatedAt: false,
  autoUpdatedAt: false,
  attributes: {
    id: {
      type: 'integer',
      autoIncrement: true,
      primaryKey: true
    },
    roles: {
      collection: 'Sys_role',
      via: 'users'
    }
  }
};

Sys_role.js

users: {
      collection: 'Sys_user',
      via: 'roles'
      //dominant: true 
    },

 

使用populate查询

    Sys_role.findOne(1).populate('users').populate('menus').exec(function(err, role) {
      console.log(':::'+JSON.stringify(role.users));
      console.log(':::'+JSON.stringify(role.menus));
    });

 

20159 月10

Node:加盐salt密码验证

npm install bcrypt –save

 

var salt = bcrypt.genSaltSync(10);
var hash = bcrypt.hashSync(‘pass’, salt);
console.log(‘加密::’+hash);
console.log(‘解密::’+bcrypt.compareSync(‘pass’,hash));

20159 月9

Node:搭建sails.js开发环境

友情提示:do not 在windows下开发node.js应用,否则你会浪费很多时间并且会pia撞墙的……
1.CentOS6.5 安装 nodejs 并配置到环境
———————————–
>>gedit /etc/profile
export NODE_HOME=/soft/node-v0.12.7-linux-x64
export PATH=$NODE_HOME/bin:$PATH
>>source /etc/profile
>>node -v

 

2.npm 设置为国内源
———————————–
>>npm config set registry https://registry.npm.taobao.org

 

3.安装必备模块 node-gyp (CentOS6.5 自带Python2.6.6)
———————————–
>>npm install node-gyp -g

 

4.安装WebStorm 并安装JDK7
———————————–
>>rpm -ivh jdk-7u79-linux-x64.rpm
>>gedit /etc/profile
JAVA_HOME=/usr/java/jdk1.7.0_79
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
>>source /etc/profile

 

5.安装Git 并生成ssh密钥,github帐号设置中添加密钥
———————————–
>>yum install git
>>cd ~/.ssh
>>ssh-keygen -t rsa -C “wizzer@qq.com”
>>cat ~/.ssh/id_rsa.pub
>>git clone git@github.com:Wizzercn/nodeshop.git

>>git pull
>>git commit -am ‘note’
>>git push

 

6.验证码组件ccap,需安装python2.7
————————————
http://blog.csdn.net/tiantiandjava/article/details/17242345
>>npm install ccap –save