Commit d604327494e3d920d6e45d83647646414d8fef14
1 parent
e483beb0
购好物,对接新版电商API签名校验规则~
Showing
1 changed file
with
51 additions
and
36 deletions
src/main.js
| ... | ... | @@ -156,17 +156,40 @@ Vue.prototype.SIGN = { |
| 156 | 156 | |
| 157 | 157 | /* 根据模式,获取相关请求参数 开始 */ |
| 158 | 158 | Vue.prototype.getParams = function(mode) { |
| 159 | + | |
| 160 | + // 随机生成字符串 | |
| 161 | + function randomString(e) { | |
| 162 | + e = e || 8; | |
| 163 | + let t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678"; | |
| 164 | + let a = t.length; | |
| 165 | + let n = ""; | |
| 166 | + for (let i = 0; i < e; i++) { | |
| 167 | + n += t.charAt(Math.floor(Math.random() * a)); | |
| 168 | + } | |
| 169 | + return n | |
| 170 | + } | |
| 171 | + | |
| 159 | 172 | // 返回参数 |
| 160 | 173 | let params = {}; |
| 161 | 174 | |
| 175 | + // 请求头 | |
| 176 | + let app_key = "769_jnjxdh3w53"; | |
| 177 | + let timestamp = new Date().getTime().toString().substr(0, 10); | |
| 178 | + let nonce = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpeLgK0vk5POU5PQ"; | |
| 179 | + let nonce_str = randomString(8); | |
| 180 | + | |
| 162 | 181 | switch (mode.toLowerCase()) { |
| 163 | 182 | case "test": |
| 164 | 183 | params.shop = { |
| 165 | 184 | apiAddress: "https://wjjshop.cnlive.com", |
| 166 | 185 | headers: { |
| 167 | - "content-type": "application/x-www-form-urlencoded", | |
| 186 | + "App-Key": app_key, | |
| 187 | + "Timestamp": timestamp, | |
| 188 | + "Signature": sha1(app_key + nonce + nonce_str + timestamp), | |
| 189 | + "Content-Type": "application/x-www-form-urlencoded", | |
| 168 | 190 | "Phone-Model": "Wap", |
| 169 | - Version: "1.0.1" | |
| 191 | + "Version": "1.0.1", | |
| 192 | + "Nonce-Str": nonce_str | |
| 170 | 193 | }, |
| 171 | 194 | sellerCategory: { |
| 172 | 195 | url: "http://wjjh5test.cnlive.com/hqyw.html" |
| ... | ... | @@ -179,12 +202,8 @@ Vue.prototype.getParams = function(mode) { |
| 179 | 202 | appKey: "9c5619e9be747bb3b925dd215ca5923d86f12c09b9394f", |
| 180 | 203 | secret: "1e6380cb347255e777f72b5aa60f41daa68443a132aaea", |
| 181 | 204 | ver: "1.0", |
| 182 | - page_url: | |
| 183 | - "https://wjjshop.cnlive.com/html/votes/1/#/pages/redirect/redirect", | |
| 184 | - timestamp: new Date() | |
| 185 | - .getTime() | |
| 186 | - .toString() | |
| 187 | - .substr(0, 10) | |
| 205 | + // page_url: "https://wjjshop.cnlive.com/html/votes/1/#/pages/redirect/redirect", | |
| 206 | + timestamp: new Date().getTime().toString().substr(0, 10) | |
| 188 | 207 | }, |
| 189 | 208 | headers: { |
| 190 | 209 | "content-type": "application/x-www-form-urlencoded" |
| ... | ... | @@ -192,24 +211,16 @@ Vue.prototype.getParams = function(mode) { |
| 192 | 211 | }; |
| 193 | 212 | break; |
| 194 | 213 | case "prod": |
| 195 | - // 请求头 | |
| 196 | - let app_key = "769_jnjxdh3w53"; | |
| 197 | - let timestamp = new Date() | |
| 198 | - .getTime() | |
| 199 | - .toString() | |
| 200 | - .substr(0, 10); | |
| 201 | - let nonce = | |
| 202 | - "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCpeLgK0vk5POU5PQ"; | |
| 203 | - | |
| 204 | 214 | params.shop = { |
| 205 | 215 | apiAddress: "https://managemall.cnlive.com", |
| 206 | 216 | headers: { |
| 207 | 217 | "App-Key": app_key, |
| 208 | - Timestamp: timestamp, | |
| 209 | - Signature: sha1(app_key + nonce + timestamp), | |
| 210 | - "content-type": "application/x-www-form-urlencoded", | |
| 218 | + "Timestamp": timestamp, | |
| 219 | + "Signature": sha1(app_key + nonce + nonce_str + timestamp), | |
| 220 | + "Content-Type": "application/x-www-form-urlencoded", | |
| 211 | 221 | "Phone-Model": "Wap", |
| 212 | - Version: "1.0.1" | |
| 222 | + "Version": "1.0.1", | |
| 223 | + "Nonce-Str": nonce_str | |
| 213 | 224 | }, |
| 214 | 225 | sellerCategory: { |
| 215 | 226 | url: "http://wjjh5.cnlive.com/hqyw.html" |
| ... | ... | @@ -222,12 +233,8 @@ Vue.prototype.getParams = function(mode) { |
| 222 | 233 | appKey: "0d57b045ec0c3988a682d94c644e66b9b8e0d2b8ef937d", |
| 223 | 234 | secret: "275e8dc29274a1186d82f65bc607c231d02bcb3bfd8976", |
| 224 | 235 | ver: "1.0", |
| 225 | - page_url: | |
| 226 | - "https://managemall.cnlive.com/html/votes/1/#/pages/redirect/redirect", | |
| 227 | - timestamp: new Date() | |
| 228 | - .getTime() | |
| 229 | - .toString() | |
| 230 | - .substr(0, 10) | |
| 236 | + // page_url: "https://managemall.cnlive.com/html/votes/1/#/pages/redirect/redirect", | |
| 237 | + timestamp: new Date().getTime().toString().substr(0, 10) | |
| 231 | 238 | }, |
| 232 | 239 | headers: { |
| 233 | 240 | "content-type": "application/x-www-form-urlencoded" |
| ... | ... | @@ -239,7 +246,13 @@ Vue.prototype.getParams = function(mode) { |
| 239 | 246 | params.shop = { |
| 240 | 247 | apiAddress: "https://wjjshop.cnlive.com", |
| 241 | 248 | headers: { |
| 242 | - "content-type": "application/x-www-form-urlencoded" | |
| 249 | + "App-Key": app_key, | |
| 250 | + "Timestamp": timestamp, | |
| 251 | + "Signature": sha1(app_key + nonce + nonce_str + timestamp), | |
| 252 | + "Content-Type": "application/x-www-form-urlencoded", | |
| 253 | + "Phone-Model": "Wap", | |
| 254 | + "Version": "1.0.1", | |
| 255 | + "Nonce-Str": nonce_str | |
| 243 | 256 | }, |
| 244 | 257 | sellerCategory: { |
| 245 | 258 | url: "http://wjjh5test.cnlive.com/hqyw.html" |
| ... | ... | @@ -252,12 +265,8 @@ Vue.prototype.getParams = function(mode) { |
| 252 | 265 | appKey: "9c5619e9be747bb3b925dd215ca5923d86f12c09b9394f", |
| 253 | 266 | secret: "1e6380cb347255e777f72b5aa60f41daa68443a132aaea", |
| 254 | 267 | ver: "1.0", |
| 255 | - page_url: | |
| 256 | - "http://172.16.2.171:8083/html/votes/1/#/pages/redirect/redirect", | |
| 257 | - timestamp: new Date() | |
| 258 | - .getTime() | |
| 259 | - .toString() | |
| 260 | - .substr(0, 10) | |
| 268 | + // page_url: "http://172.16.2.171:8083/html/votes/1/#/pages/redirect/redirect", | |
| 269 | + timestamp: new Date().getTime().toString().substr(0, 10) | |
| 261 | 270 | }, |
| 262 | 271 | headers: { |
| 263 | 272 | "content-type": "application/x-www-form-urlencoded" |
| ... | ... | @@ -498,6 +507,12 @@ Vue.prototype.http = function(path, data, callback, ext) { |
| 498 | 507 | window.location.href = |
| 499 | 508 | params[api].apiAddress + path + "?" + data.join("&"); |
| 500 | 509 | } else { |
| 510 | + | |
| 511 | + // shop接口,如果请求接口参数有uid,在headers中加入Uid参数 | |
| 512 | + if (api == 'shop' && data && data.uid) { | |
| 513 | + params.shop.headers.Uid = data.uid; | |
| 514 | + } | |
| 515 | + | |
| 501 | 516 | // 判断请求类型 |
| 502 | 517 | switch (method) { |
| 503 | 518 | case "POST": |
| ... | ... | @@ -508,7 +523,7 @@ Vue.prototype.http = function(path, data, callback, ext) { |
| 508 | 523 | data, |
| 509 | 524 | callback, |
| 510 | 525 | ext, |
| 511 | - params.shop.headers | |
| 526 | + params[api].headers | |
| 512 | 527 | ); |
| 513 | 528 | break; |
| 514 | 529 | case "GET": |
| ... | ... | @@ -520,7 +535,7 @@ Vue.prototype.http = function(path, data, callback, ext) { |
| 520 | 535 | data, |
| 521 | 536 | callback, |
| 522 | 537 | ext, |
| 523 | - params.shop.headers | |
| 538 | + params[api].headers | |
| 524 | 539 | ); |
| 525 | 540 | break; |
| 526 | 541 | } | ... | ... |