前提
有個需求,需要限制小程序的抽獎只能在某個群內(nèi),需要知道誰在群里面,但是微信并沒有提供誰在群里面的方法,不過提供了獲取群id的方法,這樣加上限制分享就能保證群里的參加,即時分享出去了,判斷來源的時候也不是來自于群了,就不允許用戶參與。
需要的方法
wx.login
wx.getGroupEnterInfo
加密解密數(shù)據(jù)
解密解密官方代碼c++、php、node、python
沒有java和c#的代碼,目前需要c#代碼,java的加密解密參考https://blog.csdn.net/qq_36437991/article/details/135553304
解密流程
需要的nuget包
skit.flurlhttpclient.wechat.api.2.35.0
實現(xiàn)代碼
微信小程序
wxml
<view style="width: 80%;margin: 0 auto;margin-top: 30rpx;">
<text>微信群id:</text>
<text>{{groupId}}</text>
<view>
<button bind:tap="copyGroupId">復(fù)制</button>
</view>
</view>
js
// pages/wechatgroupinfo/wechatgroupinfo.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
groupId: ""
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad(options) {
wx.setNavigationBarTitle({
title: '獲取微信群信息',
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady() {
let that = this;
wx.login({
success: (res) => {
if (res.errMsg == "login:ok") {
const code = res.code;
wx.getGroupEnterInfo({
success(resData) {
if (resData.errMsg === "getGroupEnterInfo:ok") {
const iv = resData.iv;
const encryptedData = resData.encryptedData;
that.getWechatGroupId(code, iv, encryptedData);
} else {
wx.showToast({
title: 'wx.getGroupEnterInfo出錯',
})
}
},
fail() {
wx.showToast({
title: 'wx.getGroupEnterInfo:' + resData.errMsg,
})
}
})
} else {
wx.showToast({
title: 'wx.login:' + res.errMsg,
})
}
},
fail: () => {
wx.showToast({
title: 'wx.login出錯',
})
}
})
},
/**
* 獲取微信群id
* @param {String} code 登錄的id
* @param {String} iv 偏移量
* @param {String} encryptData 加密的數(shù)據(jù)
* @returns 微信群id
*/
getWechatGroupId(code, iv, encryptData) {
let that = this;
wx.request({
method: "POST",
url: 'http://localhost:5000/v1/user/GetWechatGroupId',
data: JSON.stringify({
code,
iv,
encryptData
}),
success: (response) => {
console.log(response);
if (response.data && response.data.success) {
that.setData({
groupId: response.data.response
})
} else {
wx.showToast({
title: response.data.msg,
})
}
},
fail: () => {
wx.showToast({
title: '后臺獲取微信群id失敗',
})
}
})
},
/**
* 復(fù)制微信群組
*/
copyGroupId() {
wx.setClipboardData({
data: this.data.groupId,
success(res) {
wx.showToast({
title: '復(fù)制成功',
})
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload() {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
*/
onPullDownRefresh() {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom() {
},
/**
* 用戶點擊右上角分享
*/
onShareAppMessage() {
}
})
C#后臺
獲取微信群id接口文章來源:http://www.zghlxwxcb.cn/news/detail-793838.html
/// <summary>
/// 獲取微信的群組id
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<string>> GetWechatGroupId([FromBody] GetWechatGroupIdParamModel param)
{
var result = new MessageModel<string>();
var res = await weChatMiniProgramHelper.GetWechatGroupId(param.Code, param.Iv, param.EncryptData);
result.Success = !string.IsNullOrEmpty(res);
result.Msg = result.Success ? "成功" : "失敗";
result.Response = res;
return result;
}
/// <summary>
/// 單例模式注入
/// </summary>
public class WeChatMiniProgramClient
{
public WechatApiClient Instance { get; private set; }
/// <summary>
/// 構(gòu)造函數(shù)注入
/// </summary>
/// <param name="wechatConfig"></param>
public WeChatMiniProgramClient(IOptions<WechatConfigModel> wechatConfig)
{
var options = new WechatApiClientOptions()
{
AppId = wechatConfig.Value.MiniProgramAppId,
AppSecret = wechatConfig.Value.MiniProgramAppSecret,
MidasAppKey = "",//米大師相關(guān)服務(wù) AppKey,不用則不填
ImmeDeliveryAppKey = "",//即時配送相關(guān)服務(wù) AppKey,不用則不填
ImmeDeliveryAppSecret = ""//即時配送相關(guān)服務(wù) AppSecret,不用則不填
};
Instance = new WechatApiClient(options);
}
}
//weChatMiniProgramHelper下的方法
/// <summary>
/// 獲取微信群id
/// </summary>
/// <param name="code"></param>
/// <param name="iv"></param>
/// <param name="encryptData"></param>
/// <returns></returns>
public async Task<string> GetWechatGroupId(string code, string iv, string encryptData)
{
if (string.IsNullOrEmpty(iv) || iv.Length != 24)
{
return string.Empty;
}
if (string.IsNullOrEmpty(encryptData) || encryptData.Length < 1)
{
return string.Empty;
}
//WechatApiClient客戶端
var client = weChatMiniProgramClient.Instance;
try
{
var userLoginRequest = new SnsJsCode2SessionRequest();
userLoginRequest.JsCode = code;
var loginInfo = await client.ExecuteSnsJsCode2SessionAsync(userLoginRequest);
if (!loginInfo.IsSuccessful())
{
return string.Empty;
}
var sessionKey = loginInfo.SessionKey;
if (string.IsNullOrEmpty(sessionKey) || sessionKey.Length != 24)
{
return string.Empty;
}
var encryptDataDecode = Convert.FromBase64String(encryptData);
var jsonStr = AESUtility.DecryptWithCBC(sessionKey, iv, encryptData);
if (string.IsNullOrEmpty(jsonStr))
{
return string.Empty;
}
var openGidDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonStr);
return openGidDic["opengid"];
}
catch (Exception ex)
{
logger.LogError($"獲取微信群id失敗\r\n{ex.Message}");
throw new Exception(ex.Message, ex.InnerException);
}
}
測試
如果直接加密解密會報錯Specified initialization vector (IV) does not match the block size for this algorithm
,需要先base64解碼一下
AESUtility.cs源代碼,來自skit.flurlhttpclient.wechat文章來源地址http://www.zghlxwxcb.cn/news/detail-793838.html
/// <summary>
/// 基于 CBC 模式解密數(shù)據(jù)。
/// </summary>
/// <param name="keyBytes">AES 密鑰字節(jié)數(shù)組。</param>
/// <param name="ivBytes">加密使用的初始化向量字節(jié)數(shù)組。</param>
/// <param name="cipherBytes">待解密數(shù)據(jù)字節(jié)數(shù)組。</param>
/// <returns>解密后的數(shù)據(jù)字節(jié)數(shù)組。</returns>
public static byte[] DecryptWithCBC(byte[] keyBytes, byte[] ivBytes, byte[] cipherBytes)
{
if (keyBytes == null) throw new ArgumentNullException(nameof(keyBytes));
if (ivBytes == null) throw new ArgumentNullException(nameof(ivBytes));
if (cipherBytes == null) throw new ArgumentNullException(nameof(cipherBytes));
using (SymmetricAlgorithm aes = Aes.Create())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
aes.IV = ivBytes;
using ICryptoTransform transform = aes.CreateDecryptor();
return transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
}
}
/// <summary>
/// 基于 CBC 模式解密數(shù)據(jù)。
/// </summary>
/// <param name="encodingKey">經(jīng) Base64 編碼后的 AES 密鑰。</param>
/// <param name="encodingIV">經(jīng) Base64 編碼后的 AES 初始化向量。</param>
/// <param name="encodingCipherText">經(jīng) Base64 編碼后的待解密數(shù)據(jù)。</param>
/// <returns>解密后的文本數(shù)據(jù)。</returns>
public static string DecryptWithCBC(string encodingKey, string encodingIV, string encodingCipherText)
{
if (encodingKey == null) throw new ArgumentNullException(nameof(encodingKey));
if (encodingCipherText == null) throw new ArgumentNullException(nameof(encodingCipherText));
byte[] plainBytes = DecryptWithCBC(
keyBytes: Convert.FromBase64String(encodingKey),
ivBytes: Convert.FromBase64String(encodingIV),
cipherBytes: Convert.FromBase64String(encodingCipherText)
);
return Encoding.UTF8.GetString(plainBytes);
}
/// <summary>
/// 基于 CBC 模式加密數(shù)據(jù)。
/// </summary>
/// <param name="keyBytes">AES 密鑰字節(jié)數(shù)組。</param>
/// <param name="ivBytes">加密使用的初始化向量字節(jié)數(shù)組。</param>
/// <param name="plainBytes">待加密數(shù)據(jù)字節(jié)數(shù)組。</param>
/// <returns>加密后的數(shù)據(jù)字節(jié)數(shù)組。</returns>
public static byte[] EncryptWithCBC(byte[] keyBytes, byte[] ivBytes, byte[] plainBytes)
{
if (keyBytes == null) throw new ArgumentNullException(nameof(keyBytes));
if (ivBytes == null) throw new ArgumentNullException(nameof(ivBytes));
if (plainBytes == null) throw new ArgumentNullException(nameof(plainBytes));
using (SymmetricAlgorithm aes = Aes.Create())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
aes.IV = ivBytes;
using ICryptoTransform transform = aes.CreateEncryptor();
return transform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
}
}
/// <summary>
/// 基于 CBC 模式加密數(shù)據(jù)。
/// </summary>
/// <param name="encodingKey">經(jīng) Base64 編碼后的 AES 密鑰。</param>
/// <param name="encodingIV">經(jīng) Base64 編碼后的 AES 初始化向量。</param>
/// <param name="plainText">待加密文本。</param>
/// <returns>經(jīng) Base64 編碼的加密后的數(shù)據(jù)。</returns>
public static string EncryptWithCBC(string encodingKey, string encodingIV, string plainText)
{
if (encodingKey == null) throw new ArgumentNullException(nameof(encodingKey));
if (plainText == null) throw new ArgumentNullException(nameof(plainText));
byte[] plainBytes = EncryptWithCBC(
keyBytes: Convert.FromBase64String(encodingKey),
ivBytes: Convert.FromBase64String(encodingIV),
plainBytes: Encoding.UTF8.GetBytes(plainText)
);
return Convert.ToBase64String(plainBytes);
}
到了這里,關(guān)于C# 微信小程序獲取群id的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!