From 82f92e1f87974f24c5a6221ba80afd1c411cb208 Mon Sep 17 00:00:00 2001 From: shen100 Date: Sun, 11 Jun 2017 15:41:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E5=BE=AE=E4=BF=A1=E8=A7=A3?= =?UTF-8?q?=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/utils/security.go | 52 +++++++++++++++++++++++ wexin/app.js | 47 +++------------------ wexin/common/login.js | 71 ++++++++++++++++++++++++++++++++ wexin/config/config.js | 3 +- wexin/pages/mine/mine.js | 4 +- wexin/pages/product/product.wxml | 2 +- 6 files changed, 135 insertions(+), 44 deletions(-) create mode 100644 go/utils/security.go create mode 100644 wexin/common/login.js diff --git a/go/utils/security.go b/go/utils/security.go new file mode 100644 index 00000000..a04bc693 --- /dev/null +++ b/go/utils/security.go @@ -0,0 +1,52 @@ +package utils + +import ( + "encoding/base64" + "crypto/aes" + "crypto/cipher" + "fmt" +) + +// DecodeWeAppUserInfo 解密微信小程序用户信息 +func DecodeWeAppUserInfo(encryptedData string, sessionKey string, iv string) (string, error) { + cipher, err := base64.StdEncoding.DecodeString(encryptedData) + if err != nil { + fmt.Println("encryptedData: ", encryptedData, "\n", err.Error()) + return "", err + } + + key, keyErr := base64.StdEncoding.DecodeString(sessionKey) + if keyErr != nil { + fmt.Println("sessionKey: ", sessionKey, "\n", keyErr.Error()) + return "", keyErr + } + + theIV, ivErr := base64.StdEncoding.DecodeString(iv) + if ivErr != nil { + fmt.Println("iv: ", iv, "\n", ivErr.Error()) + return "", ivErr + } + + result, resultErr := AESDecrypt(cipher, key, theIV) + resultStr := string(result) + fmt.Println(resultStr) + return string(result), resultErr +} + +func AESDecrypt(ciphertext, key, iv []byte) ([]byte, error) { + block, err := aes.NewCipher(key) //选择加密算法 + if err != nil { + return nil, err + } + blockModel := cipher.NewCBCDecrypter(block, iv) + plantText := make([]byte, len(ciphertext)) + blockModel.CryptBlocks(plantText, ciphertext) + plantText = PKCS7UnPadding(plantText, block.BlockSize()) + return plantText, nil +} + +func PKCS7UnPadding(plantText []byte, blockSize int) []byte { + length := len(plantText) + unpadding := int(plantText[length-1]) + return plantText[:(length - unpadding)] +} diff --git a/wexin/app.js b/wexin/app.js index 33ee7100..2bff293c 100644 --- a/wexin/app.js +++ b/wexin/app.js @@ -1,49 +1,14 @@ var config = require('./config/config.js'); - -var userInfoCallbacks = []; +var login = require('./common/login.js'); App({ onLaunch: function() { - var self = this; - wx.login({ - success: function(res) { - console.log(res); - - if (res.code) { - wx.request({ - url: config.api.weappLogin, - data: { - code: res.code - }, - success: function(res) { - try { - wx.setStorageSync(config.wemallSession, res.data.data.sid); - } catch (err) { - console.log(err); - } - } - }); - - wx.getUserInfo({ - success: function(res) { - for (var i = 0; i < userInfoCallbacks.length; i++) { - userInfoCallbacks[i](res.userInfo); - } - userInfoCallbacks = []; - self.globalData.userInfo = res.userInfo; - }, - fail: function(data) { - console.log(data); - } - }); - } - } - }); - }, - addUserInfoCallback: function(callback) { - userInfoCallbacks.push(callback); + login.login(); }, globalData: { - userInfo: null + userInfo: null, + encryptedData: "", + iv: "", + sid: "" } }) \ No newline at end of file diff --git a/wexin/common/login.js b/wexin/common/login.js new file mode 100644 index 00000000..86c8770c --- /dev/null +++ b/wexin/common/login.js @@ -0,0 +1,71 @@ +var login = { + loginResponders: [], + addLoginResponder: function(responder) { + this.loginResponders.push(responder); + }, + login: function() { + var self = this; + var resData = {}; + var app = App(); + + function setUserInfo() { + wx.request({ + url: config.api.setWeAppUser, + data: { + encryptedData : resData.encryptedData, + iv : resData.iv + }, + header: { + 'content-type' : 'application/json', + 'Cookie' : resData.sid + }, + success: function(res) { + app.globalData.userInfo = resData.userInfo; + app.globalData.encryptedData = resData.encryptedData; + app.globalData.iv = resData.iv; + app.globalData.sid = resData.sid; + for (var i = 0; i < self.loginResponders.length; i++) { + self.loginResponders[i](); + } + } + }); + } + + wx.login({ + success: function(res) { + if (res.code) { + wx.request({ + url: config.api.weappLogin, + data: { + code: res.code + }, + success: function(res) { + resData.sid = res.data.data.sid; + jsCodeDone = true; + jsCodeDone && userInfoDone && setUserInfo(); + } + }); + + wx.getUserInfo({ + success: function(res) { + resData.userInfo = res.userInfo; + resData.encryptedData = res.encryptedData; + resData.iv = res.iv; + userInfoDone = true; + jsCodeDone && userInfoDone && setUserInfo(); + }, + fail: function(data) { + console.log(data); + } + }); + } + } + }); + }, + + logout: function() { + + } +} + +module.exports = login; \ No newline at end of file diff --git a/wexin/config/config.js b/wexin/config/config.js index 60a7f2ec..494b1ab2 100644 --- a/wexin/config/config.js +++ b/wexin/config/config.js @@ -10,7 +10,8 @@ var config = { weappLogin: '/weappLogin', reqCategoryList: '/categories', reqProductList: '/products', - reqProductDetail: '/product/:id' + reqProductDetail: '/product/:id', + addToCart: '/cart/create' } }; diff --git a/wexin/pages/mine/mine.js b/wexin/pages/mine/mine.js index 6d3025f0..d076c6c7 100644 --- a/wexin/pages/mine/mine.js +++ b/wexin/pages/mine/mine.js @@ -1,3 +1,5 @@ +var login = require('../../common/login.js'); + Page({ data: { userInfo: null @@ -11,7 +13,7 @@ Page({ var app = getApp(); var userInfo = app.globalData.userInfo; if (!userInfo) { - app.addUserInfoCallback(this.onUserInfoCallback.bind(this)); + login.addLoginResponder(this.onUserInfoCallback.bind(this)); } else { this.setData({ userInfo: userInfo diff --git a/wexin/pages/product/product.wxml b/wexin/pages/product/product.wxml index bad9d5c7..6ed390e2 100644 --- a/wexin/pages/product/product.wxml +++ b/wexin/pages/product/product.wxml @@ -54,7 +54,7 @@ 购物车 - 加入购物车 + 加入购物车 立即购买 \ No newline at end of file