回复5上传临时图片报错
对Promise不懂,不知道怎么检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | "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 |