Authored by 陈峰

Merge branch 'feature/activity-image-upload' into 'master'

Feature/activity image upload



See merge request !18
@@ -22,7 +22,7 @@ const sha1 = (str) => { @@ -22,7 +22,7 @@ const sha1 = (str) => {
22 22
23 // 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 23 // 微信 JS 接口签名校验工具 http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign
24 24
25 -let getAccessToken = function(req, next, appId, secret, prefix) { // 获取access_token 25 +let getAccessToken = function(appId, secret, prefix) { // 获取access_token
26 return request({ 26 return request({
27 url: prefix + 'token', 27 url: prefix + 'token',
28 qs: { 28 qs: {
@@ -33,11 +33,11 @@ let getAccessToken = function(req, next, appId, secret, prefix) { // 获取acces @@ -33,11 +33,11 @@ let getAccessToken = function(req, next, appId, secret, prefix) { // 获取acces
33 json: true 33 json: true
34 }).then(res => { 34 }).then(res => {
35 return res; 35 return res;
36 - }).catch(next); 36 + });
37 37
38 }; 38 };
39 39
40 -let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获取ticket 40 +let getTicket = function(accessToken, appId, secret, prefix) { // 获取ticket
41 return request({ 41 return request({
42 url: prefix + 'ticket/getticket', 42 url: prefix + 'ticket/getticket',
43 qs: { 43 qs: {
@@ -50,7 +50,7 @@ let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获 @@ -50,7 +50,7 @@ let getTicket = function(req, accessToken, next, appId, secret, prefix) { // 获
50 }); 50 });
51 }; 51 };
52 52
53 -let calcSigature = function(ticket_res, res) { // 计算signature 53 +let calcSigature = function(ticket_res) { // 计算signature
54 let appID = ticket_res.appID; 54 let appID = ticket_res.appID;
55 let ticket = ticket_res.access_ticket; 55 let ticket = ticket_res.access_ticket;
56 let nonceStr = Math.random().toString(36).substr(2, 15); 56 let nonceStr = Math.random().toString(36).substr(2, 15);
@@ -59,63 +59,32 @@ let calcSigature = function(ticket_res, res) { // 计算signature @@ -59,63 +59,32 @@ let calcSigature = function(ticket_res, res) { // 计算signature
59 let str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`; 59 let str = `jsapi_ticket=${ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`;
60 let signature = sha1(str); 60 let signature = sha1(str);
61 61
62 - console.log(str + '&signature=' + signature);  
63 -  
64 - let result = { 62 + return {
65 appId: appID, 63 appId: appID,
66 nonceStr, 64 nonceStr,
67 timestamp, 65 timestamp,
68 url, 66 url,
69 signature 67 signature
70 }; 68 };
71 -  
72 - // console.log('result', result);  
73 - return res.jsonp(result);  
74 }; 69 };
75 70
76 -let getResult = function(wechat_res, req, res, next) { // 向微信接口获取access_token及ticket并计算signature 71 +let getResult = function(wechat_res) { // 向微信接口获取access_token及ticket并计算signature
77 let appID = wechat_res.appID; 72 let appID = wechat_res.appID;
78 let appSecret = wechat_res.appSecret; 73 let appSecret = wechat_res.appSecret;
79 let prefix = wechat_res.prefix; 74 let prefix = wechat_res.prefix;
80 let mpPrefix = wechat_res.mpPrefix; 75 let mpPrefix = wechat_res.mpPrefix;
81 - let url = wechat_res.url;  
82 - let type = wechat_res.type;  
83 76
84 - return getAccessToken(req, next, appID, appSecret, prefix) 77 + return getAccessToken(appID, appSecret, prefix)
85 .then(result => { // 先获取accessToken 78 .then(result => { // 先获取accessToken
86 let access_token = result.access_token; 79 let access_token = result.access_token;
87 80
88 - console.log('token:', access_token);  
89 - return getTicket(req, access_token, next, appID, appSecret, mpPrefix) 81 + return getTicket(access_token, appID, appSecret, mpPrefix)
90 .then(ticket_res => { // 通过accessToken获取ticket 82 .then(ticket_res => { // 通过accessToken获取ticket
91 - if (ticket_res && ticket_res.ticket) {  
92 - let access_ticket = ticket_res.ticket;  
93 - let expired = parseInt(ticket_res.expires_in, 10) - 100;  
94 - let create_time = moment().format('YYYY-MM-DD HH:mm:ss');  
95 - let expired_time = moment().add(expired, 'seconds').format('YYYY-MM-DD HH:mm:ss');  
96 -  
97 - // console.log('ticket_res:', ticket_res);  
98 - req.ctx(WechatModel).  
99 - saveAccessKeys(access_token, access_ticket, create_time, expired_time, type)  
100 - .then(() => {  
101 - let access_res = {  
102 - appID: appID,  
103 - access_ticket: access_ticket,  
104 - url: url  
105 - };  
106 -  
107 - calcSigature(access_res, res);  
108 - });  
109 - } else {  
110 - res.jsonp({  
111 - appID: appID,  
112 - nonceStr: '',  
113 - timestamp: '',  
114 - url: url,  
115 - signature: 'get failed'  
116 - });  
117 - }  
118 - }).catch(next); 83 + return Object.assign({
  84 + access_token: access_token,
  85 + appID: appID
  86 + }, ticket_res);
  87 + });
119 }); 88 });
120 }; 89 };
121 90
@@ -132,51 +101,52 @@ const wechatShare = { @@ -132,51 +101,52 @@ const wechatShare = {
132 101
133 req.ctx(WechatModel).getAccessKeys() 102 req.ctx(WechatModel).getAccessKeys()
134 .then(keys_res => { // 先查询数据库中是否有access_token并且未过期 103 .then(keys_res => { // 先查询数据库中是否有access_token并且未过期
135 - console.log(keys_res);  
136 - if (keys_res.length > 0) {  
137 - let access_token = keys_res[0].access_token;  
138 - let access_ticket = keys_res[0].access_ticket;  
139 - let expired = new Date(keys_res[0].access_expired).getTime();  
140 - let now = Date.now();  
141 -  
142 - console.log('data_time!', expired, now, keys_res[0].access_expired);  
143 - if (now > expired) { // 过期则去 取新的token与ticket 并且更新数据库  
144 - return Promise.reject({  
145 - type: 'update'  
146 - });  
147 - } else {  
148 - return Promise.resolve({ // 没过期则直接返回数据库中保存的access_ticket  
149 - access_token,  
150 - access_ticket  
151 - });  
152 - }  
153 -  
154 - } else {  
155 - return Promise.reject({ // 数据库中没有token数据则新增  
156 - type: 'save' 104 +
  105 + if (keys_res.code === 200) { // code=200说明数据库中查找到数据并且未过期
  106 +
  107 + delete keys_res.code;
  108 + return Object.assign({
  109 + appID,
  110 + url
  111 + }, keys_res);
  112 + } else if (keys_res.code === 201) {
  113 + // code=201说明数据库中没有数据,或者数据已过期,根据返回的type判断是添加还是更新
  114 +
  115 + delete keys_res.code;
  116 + keys_res = Object.assign({
  117 + appID,
  118 + appSecret,
  119 + prefix,
  120 + mpPrefix,
  121 + url
  122 + }, keys_res);
  123 +
  124 + return getResult(keys_res).then(ticket_res => {
  125 + let appId = ticket_res.appID;
  126 + let access_token = ticket_res.access_token || '';
  127 + let access_ticket = ticket_res.ticket || '';
  128 + let expired = parseInt(ticket_res.expires_in, 10) - 100 || 0;
  129 + let create_time = moment().format('YYYY-MM-DD HH:mm:ss') || '';
  130 + let expired_time = moment().add(expired, 'seconds').format('YYYY-MM-DD HH:mm:ss') || '';
  131 +
  132 + // 获取到ticket信息之后把相关字段保存到数据库中
  133 + return req.ctx(WechatModel).
  134 + saveAccessKeys(access_token, access_ticket, create_time, expired_time, keys_res.type)
  135 + .then(() => {
  136 + return {
  137 + appID: appId,
  138 + access_ticket: access_ticket,
  139 + url: url
  140 + };
  141 + });
157 }); 142 });
158 } 143 }
159 }) 144 })
160 - .then(access_res => {  
161 - access_res = Object.assign({  
162 - appID,  
163 - url  
164 - }, access_res);  
165 - calcSigature(access_res, res); // 组合参数, 计算signature  
166 - },  
167 - fail_res => {  
168 - fail_res = Object.assign({  
169 - appID: appID,  
170 - appSecret: appSecret,  
171 - prefix: prefix,  
172 - mpPrefix: mpPrefix,  
173 - url: url  
174 - }, fail_res);  
175 -  
176 - getResult(fail_res, req, res, next); // 向微信接口获取access_token及ticket并计算signature  
177 - });  
178 - 145 + .then(result => {
  146 + let calc_res = calcSigature(result);
179 147
  148 + res.jsonp(calc_res);
  149 + }).catch(next);
180 } 150 }
181 }; 151 };
182 152
@@ -13,12 +13,51 @@ class WechatModel extends global.yoho.BaseModel { @@ -13,12 +13,51 @@ class WechatModel extends global.yoho.BaseModel {
13 super(ctx); 13 super(ctx);
14 } 14 }
15 15
  16 + /**
  17 + * 获取微信token与ticket信息,存在并且未过期则返回code=200,
  18 + * 存在且过期返回201,‘update’
  19 + * 不存在返回201,‘save’
  20 + */
16 getAccessKeys() { 21 getAccessKeys() {
17 let strSql = `select * from ${TABLE_WECHAT} limit 0, 1;`; 22 let strSql = `select * from ${TABLE_WECHAT} limit 0, 1;`;
18 23
19 - return mysqlCli.query(strSql); 24 + return mysqlCli.query(strSql).then(res => {
  25 + if (res.length > 0) {
  26 + let access_token = res[0].access_token;
  27 + let access_ticket = res[0].access_ticket;
  28 + let expired = new Date(res[0].access_expired).getTime();
  29 + let now = Date.now();
  30 +
  31 + if (now > expired) { // 过期则去 取新的token与ticket 并且更新数据库
  32 + return Promise.resolve({
  33 + code: 201,
  34 + type: 'update'
  35 + });
  36 + } else {
  37 + return Promise.resolve({ // 没过期则直接返回数据库中保存的access_ticket
  38 + code: 200,
  39 + access_token,
  40 + access_ticket
  41 + });
  42 + }
  43 + } else {
  44 + return Promise.resolve({ // 数据库中没有token数据则新增
  45 + code: 201,
  46 + type: 'save'
  47 + });
  48 + }
  49 + });
20 } 50 }
21 51
  52 + /**
  53 + * 保存或者更新微信的token与ticket
  54 + * @param access_token
  55 + * @param access_ticket
  56 + * @param access_create_time
  57 + * @param access_expired
  58 + * @param type 'save'为添加数据 'update'为更新数据
  59 + * @returns {*}
  60 + */
22 saveAccessKeys(access_token, access_ticket, access_create_time, access_expired, type) { 61 saveAccessKeys(access_token, access_ticket, access_create_time, access_expired, type) {
23 let strSql = ''; 62 let strSql = '';
24 let params = { 63 let params = {
@@ -33,7 +72,6 @@ class WechatModel extends global.yoho.BaseModel { @@ -33,7 +72,6 @@ class WechatModel extends global.yoho.BaseModel {
33 (access_token, access_ticket, access_create_time, access_expired) values 72 (access_token, access_ticket, access_create_time, access_expired) values
34 (:access_token, :access_ticket, :access_create_time, :access_expired);`; 73 (:access_token, :access_ticket, :access_create_time, :access_expired);`;
35 74
36 - console.log(strSql, params);  
37 return mysqlCli.insert(strSql, params); 75 return mysqlCli.insert(strSql, params);
38 } else if (type === 'update') { 76 } else if (type === 'update') {
39 strSql = `update ${TABLE_WECHAT} set 77 strSql = `update ${TABLE_WECHAT} set
@@ -42,7 +80,6 @@ class WechatModel extends global.yoho.BaseModel { @@ -42,7 +80,6 @@ class WechatModel extends global.yoho.BaseModel {
42 access_create_time = :access_create_time, 80 access_create_time = :access_create_time,
43 access_expired = :access_expired`; 81 access_expired = :access_expired`;
44 82
45 - console.log(strSql);  
46 return mysqlCli.update(strSql, params); 83 return mysqlCli.update(strSql, params);
47 } 84 }
48 85