Skip to content

Commit

Permalink
对接微信解密
Browse files Browse the repository at this point in the history
  • Loading branch information
shen100 committed Jun 11, 2017
1 parent 785e0c2 commit 82f92e1
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 44 deletions.
52 changes: 52 additions & 0 deletions go/utils/security.go
Original file line number Diff line number Diff line change
@@ -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)]
}
47 changes: 6 additions & 41 deletions wexin/app.js
Original file line number Diff line number Diff line change
@@ -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: ""
}
})
71 changes: 71 additions & 0 deletions wexin/common/login.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion wexin/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var config = {
weappLogin: '/weappLogin',
reqCategoryList: '/categories',
reqProductList: '/products',
reqProductDetail: '/product/:id'
reqProductDetail: '/product/:id',
addToCart: '/cart/create'
}
};

Expand Down
4 changes: 3 additions & 1 deletion wexin/pages/mine/mine.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var login = require('../../common/login.js');

Page({
data: {
userInfo: null
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion wexin/pages/product/product.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</view>
<text class="cart-text">购物车</text>
</view>
<view class="tabbar-item tabbar-add-cart"><text>加入购物车</text></view>
<view bindtap="onAddToCartTap" class="tabbar-item tabbar-add-cart"><text>加入购物车</text></view>
<view class="tabbar-item tabbar-buy-now"><text>立即购买</text></view>
</view>
</view>

0 comments on commit 82f92e1

Please sign in to comment.