Skip to content

Commit

Permalink
提交最新代码
Browse files Browse the repository at this point in the history
  • Loading branch information
ldjking committed Apr 20, 2015
1 parent 7887e30 commit 49cca59
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 201 deletions.
8 changes: 6 additions & 2 deletions handler/upload/reciever.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module.exports = function(req, res, cb) { //上传文件能力处理
var multer=require("multer");
var path=require("path");
var fs=require("fs");
var md5=require("MD5");
var fn=multer({
dest: './web/upload/wechat_img/',
// limits:{
Expand Down Expand Up @@ -32,11 +35,12 @@ module.exports = function(req, res, cb) { //上传文件能力处理

fn(req,res,function(req2,res2,next2){
//console.log("next");
//console.log(req2);
//console.log(req.files);
console.log(req);
console.log(req.files);
for(var p in req.files){
var file=req.files[p];
var ori_name=file.originalname;
console.log("ori_name",ori_name);
var filepath=path.resolve(process.cwd(),file.path);
var filedir=path.dirname(filepath);
var extension=file.extension;
Expand Down
98 changes: 0 additions & 98 deletions lib/weixin/uploadFileTest.js

This file was deleted.

102 changes: 32 additions & 70 deletions lib/weixin/uploadImg.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,83 +16,45 @@ function uploadImage(cb) {
var url = "https://api.weixin.qq.com/cgi-bin/menu/create"
url += "?access_token=" + token;

var body = {
"button": [{
"type": "view",
"name": "我要投资",
"key": "V1001_TODAY_MUSIC",
"url": "http://www.51blb.com"
}, {
"type": "view",
"name": "福利专区",
"key": "V1002_TODAY_MUSIC",
"url": "http://www.51blb.com"
}, {
"name": "客服中心",
"sub_button": [{
"type": "click",
"name": "文本消息",
"key": "CMD1"
}, {
"type": "click",
"name": "图文消息",
"key": "CMD2"
}, {
"type": "click",
"name": "图片",
"key": "CMD3",
}, {
"type": "click",
"name": "音乐",
"key": "CMD4",
}]
}]
var formData = {
// Pass a simple key-value pair
//my_field: 'my_value',
// Pass data via Buffers
//my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/../../web/src/favicon.ico'),
// Pass multiple values /w an Array
//attachments: [
// fs.createReadStream(__dirname + '/attachment1.jpg'),
// fs.createReadStream(__dirname + '/attachment2.jpg')
//],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
//custom_file: {
// value: fs.createReadStream('/dev/urandom'),
// options: {
// filename: 'topsecret.jpg',
// contentType: 'image/jpg'
// }
//}
};
var opt = {

//var url = "http://service.com/upload";
//var url = "http://127.0.0.1:3000/handler/upload/reciever";
request.post({
url: url,
json: true,
body: body
}
request.post(opt, function(err, res, body) {
//var jsonObj = JSON.parse(body);
console.log("body", body);
//jsonObj.timeStamp = timeStamp;
//fs.writeFileSync(path.resolve(__dirname,"./data.json"), JSON.stringify(jsonObj), encoding);
//cb && cb(jsonObj);
formData: formData
}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
});
//https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
}

var formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + '/attachment1.jpg'),
fs.createReadStream(__dirname + '/attachment2.jpg')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/felixge/node-form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
options: {
filename: 'topsecret.jpg',
contentType: 'image/jpg'
}
}
};
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});


uploadImage();
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"colors": "^1.0.3",
"cookie-parser": "^1.3.3",
"dot": "^1.0.3",
"expect.js": "^0.3.1",
"express": "^4.10.5",
"express-session": "^1.10.4",
"express-xml-bodyparser": "0.0.7",
Expand All @@ -33,6 +34,7 @@
"grunt-contrib-watch": "^0.6.1",
"js-beautify": "^1.5.4",
"matchdep": "^0.3.0",
"muk": "^0.3.2",
"multer": "^0.1.7",
"node-js-beautify": "^0.1.0",
"node-rsa": "^0.2.13",
Expand All @@ -46,5 +48,8 @@
"watch": "^0.13.0",
"wechat": "^1.2.4"
},
"homepage": "https://github.com/91blb/weixin"
"homepage": "https://github.com/91blb/weixin",
"devDependencies": {
"wechat-api": "^1.8.1"
}
}
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var xmlparser = require('express-xml-bodyparser');
var session = require('express-session');
var favicon = require('serve-favicon');

app.use(favicon(__dirname + '/src/favicon.ico'));
app.use(favicon(__dirname + '/web/src/favicon.ico'));

var colors = require('colors');
process.on('uncaughtException', function(e) {
Expand Down Expand Up @@ -110,7 +110,7 @@ function handler(req, res) { //处理所有服务请求

console.log(("method=" + method).green);

var fn = require("./handler/" + method + ".js");
var fn = require("./handler" + method + ".js");
var result = fn(req, res); //也允许异步返回
//console.log("result=["+result+"]");
} catch (e) {
Expand Down
36 changes: 36 additions & 0 deletions test/wechat/getAccessToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var request = require("request");
var path = require("path");
var now = new Date();
var timeStamp = parseInt(now.getTime() / 1000); /*秒数*/
var fs = require("fs");
var encoding = {
encoding: "utf8"
};
var url = "";


function getAccessToken(cb) {
var app_id = "wx4b6e962611f5e662";
var app_secret = "78f0744a1d73bbbd423859840fd1255d";

console.log("timestamp", timeStamp);

//if (dataObj.timeStamp + 7200 < timeStamp) { //已经超时 需要重新获取
var url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
url += "&appid=" + app_id;
url += "&secret=" + app_secret;

//var url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3eb0718281190cf6&secret=6ff53b417eb39296fc92134c0e0d04bd";
request.get(url, function(err, res, body) {
var jsonObj = JSON.parse(body);
console.log("body", body);
//jsonObj.timeStamp = timeStamp;
//fs.writeFileSync(path.resolve(__dirname,"./data.json"), JSON.stringify(jsonObj), encoding);
cb && cb(jsonObj);
});
//} else {
//cb && cb();
//}
}
module.exports=getAccessToken;
//getAccessToken();
Binary file added test/wechat/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 4 additions & 28 deletions test/wechat/test1.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
require("should");
var wechat=require("wechat");
console.log("wechat",wechat);

var name = "zhaojian";

describe("Name", function() {
it("The name should be zhaojian", function() {
name.should.eql("zhaojian");
});
});

var Person = function(name) {
this.name = name;
};
var zhaojian = new Person(name);

describe("InstanceOf", function() {
it("Zhaojian should be an instance of Person", function() {
zhaojian.should.be.an.instanceof(Person);
});

it("Zhaojian should be an instance of Object", function() {
zhaojian.should.be.an.instanceof(Object);
});
});
describe("Property", function() {
it("Zhaojian should have property name", function() {
zhaojian.should.have.property("name");
});
});
var fn=wechat("",function(){});
console.log(fn);
Loading

0 comments on commit 49cca59

Please sign in to comment.