forked from shen100/wemall
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "" | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters