回复5上传临时图片promise问题
来源:
慕粉3173836
2016-04-19
回复5上传临时图片报错
对Promise不懂,不知道怎么检查
"use strict" var Promise = require("bluebird") // 对request进行promise化(可以使用then) var request = Promise.promisify(require("request")) var util = require("./util") var fs = require("fs") var prefix = "https://api.weixin.qq.com/cgi-bin/" var api = { //access_token获取地址 accessToken: prefix + "token?grant_type=client_credential", upload: "media/upload?" } function Wechat(opts) { // 将appjs里的config配置赋值给自身 var that = this this.appID = opts.appID this.appSecret = opts.appSecret this.getAccessToken = opts.getAccessToken this.saveAccessToken = opts.saveAccessToken this.fetchAccessToken() } // 判断access_token是否可用 Wechat.prototype.isValidAccessToken = function (data) { // access_token是否合法 if (!data || !data.access_token || data.expires_in) { return false } var access_token = data.access_token var expires_in = data.expires_in var now = (new Date().getTime()) if (now < expires_in) { // 有效期内 return ture } else { return false } } // 更新access_token Wechat.prototype.updateAccessToken = function () { var appID = this.appID var appSecret = this.appSecret var url = api.accessToken + "&appid=" + appID + "&secret=" + appSecret // 发送get请求,本身是没有then的,我们用Promise进行了封装 return new Promise(function(resolve, reject){ request({url :url, json: true}).then(function(response) { var data = response[1] var now = (new Date().getTime()) // 提前20秒刷新,考虑网络延迟 var expires_in = now + (data.expires_in - 20) * 1000 data.expires_in = expires_in resolve(data) }) }) } // 获取access_token Wechat.prototype.fetchAccessToken = function (data) { var that = this if (this.access_token && this.expires_in){ if (this.isValidAccessToken(this)){ return Promise.resolve(this) } } this.getAccessToken() .then(function(data) { try { data = JSON.parse(data) } catch(e) { return that.updateAccessToken() } // 拿到票据后依然判断是否有效 if (that.isValidAccessToken(data)){ return Promise.resolve(data) } else { return that.updateAccessToken() } }) .then(function (data) { that.access_token = data.access_token that.expires_in = data.expires_in // 将票据写入文件中 that.saveAccessToken(data) return Promise.resolve(data) }) } // 新增临时素材 Wechat.prototype.uploadMaterial = function (type, filepath) { var that = this var form = { media: fs.createReadStream(filepath) } // 发送get请求,本身是没有then的,我们用Promise进行了封装 return new Promise(function(resolve, reject){ that .fetchAccessToken() .then(function(data) { var url = prefix + api.upload + "&access_token=" + data.access_token + "&type=" + type request({method: "POST", url :url, formData: form, json: true}) .then(function(response) { var _data = response[1] if (_data) { resolve(_data) } else { throw new Error("upload material fails") } }) .catch(function (err) { reject(err) }) }) }) } Wechat.prototype.reply = function() { // 上下文已经改变,可以拿到this的变量 var content = this.body //console.log("content内容是::" + this.body) var message = this.weixin var xml = util.tpl(content, message) this.status = 200 this.type = "application/xml" this.body = xml } module.exports = Wechat
写回答
3回答
-
Wechat.prototype.fetchAccessToken = function (data) { var that = this if (this.access_token && this.expires_in){ if (this.isValidAccessToken(this)){ return Promise.resolve(this) } } this.getAccessToken() .then(function(data) { try { data = JSON.parse(da
this.getAccessToken() 前面需要加一个 return,fetchAccessToken() 执行后,会返回一个 promise.
012016-04-19 -
青了葱的老王
2017-02-04
Wechat.prototype.fetchAccessToken = function (data) { var that = this if (this.access_token && this.expires_in){ if (this.isValidAccessToken(this)){ return Promise.resolve(this) } } return this.getAccessToken() .then(function(data) { try { data = JSON.parse(da
是这样弄么?
然而并没有什么用。。。。。。
012017-04-30 -
qq_老虎_daniu
2016-07-02
我也碰到这问题了,折腾了好久
022017-05-22
相似问题