using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
public class JsSdkTicket : MonoBehaviour
{
? ? [DllImport("__Internal")]
? ? private static extern void OpenWechatShare(string encodeURIPtr);
? ? [DllImport("__Internal")]
? ? private static extern void OpenWechatShareMenu(string appId, string time_stamp, string noncestring, string signatureStr);
? ? [DllImport("__Internal")]
? ? private static extern string GetCurWebUrl();
? ? public class AccessToken
? ? {
? ? ? ? public string access_token { get; set; }
? ? ? ? public double expires_in { get; set; }
? ? }
? ? public class JSTicket
? ? {
? ? ? ? public int errcode { get; set; }
? ? ? ? public string errmsg { get; set; }
? ? ? ? public string ticket { get; set; }
? ? ? ? public string expires_in { get; set; }
? ? }
? ? private string createNonceStr()
? ? {
? ? ? ? int length = 16;
? ? ? ? string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
? ? ? ? string str = "";
? ? ? ? System.Random rad = new System.Random();
? ? ? ? for (int i = 0; i < length; i++)
? ? ? ? {
? ? ? ? ? ? str += chars.Substring(rad.Next(0, chars.Length - 1), 1);
? ? ? ? }
? ? ? ? return str;
? ? }
? ? //SHA1哈希加密算法 ?
? ? public static string SHA1_Hash(string str_sha1_in)
? ? {
? ? ? ? SHA1 sha1 = new SHA1CryptoServiceProvider();
? ? ? ? byte[] bytes_sha1_in = System.Text.UTF8Encoding.Default.GetBytes(str_sha1_in);
? ? ? ? byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
? ? ? ? string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
? ? ? ? str_sha1_out = str_sha1_out.Replace("-", "").ToLower();
? ? ? ? return str_sha1_out;
? ? }
? ? public string GetSha1(string str)
? ? {
? ? ? ? //建立SHA1對(duì)象
? ? ? ? SHA1 sha = new SHA1CryptoServiceProvider();
? ? ? ? //將mystr轉(zhuǎn)換成byte[]
? ? ? ? ASCIIEncoding enc = new ASCIIEncoding();
? ? ? ? byte[] dataToHash = enc.GetBytes(str);
? ? ? ? //Hash運(yùn)算
? ? ? ? byte[] dataHashed = sha.ComputeHash(dataToHash);
? ? ? ? //將運(yùn)算結(jié)果轉(zhuǎn)換成string
? ? ? ? string hash = BitConverter.ToString(dataHashed).Replace("-", "").ToLower();
? ? ? ? return hash;
? ? }
? ? public IEnumerator GetWechatToken()
? ? {
? ? ? ? string appID = "wx22db6c710f661f68";
? ? ? ? string appSecret = "ba47bf18ad036518310145743cdc7a04";
? ? ? ? string url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appID}&secret={appSecret}";
? ? ? ? using UnityWebRequest webRequest = UnityWebRequest.Get(url);
? ? ? ? yield return webRequest.SendWebRequest();
? ? ? ? if (webRequest.result == UnityWebRequest.Result.Success)
? ? ? ? {
? ? ? ? ? ? var json = webRequest.downloadHandler.text;
? ? ? ? ? ? if (!string.IsNullOrEmpty(json))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? AccessToken atoken = JsonUtility.FromJson<AccessToken>(json);
? ? ? ? ? ? ? ? tip = atoken.access_token;
? ? ? ? ? ? ? ? //json={"access_token":"71_VWNQS5lobipBuw0cpMXycMlhN5l-cP0lCWyVCAd8xfbxbMLID6ku2Tttw6ivwyjSkp1HIuTw-29bVFCmuHl78GvoPnaJYhsRVO5_Tslr1HJa3Ayp4G8tzf5y74MKPEhAIAVAP","expires_in":7200};
? ? ? ? ? ? ? ? string jsapi_url = $"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={atoken.access_token}&type=jsapi";
? ? ? ? ? ? ? ? using UnityWebRequest jsapiRequest = UnityWebRequest.Get(url);
? ? ? ? ? ? ? ? yield return jsapiRequest.SendWebRequest();
? ? ? ? ? ? ? ? if (jsapiRequest.result == UnityWebRequest.Result.Success)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //{"errcode":0,"errmsg":"ok","ticket":"LIKLckvwlJT9cWIhEQTwfCAOltWVYohgnqfFqTJCnBfBw3-pEXDs3v7UrlsEJte7DaGDvYoxD4znvTviQQM5IA","expires_in":7200}
? ? ? ? ? ? ? ? ? ? var tickitJson = webRequest.downloadHandler.text;
? ? ? ? ? ? ? ? ? ? JSTicket jSTicket = JsonUtility.FromJson<JSTicket>(tickitJson);
? ? ? ? ? ? ? ? ? ? Debug.Log("gcj: " + jSTicket);
? ? ? ? ? ? ? ? ? ? string JSTicketTicket = jSTicket.ticket;
? ? ? ? ? ? ? ? ? ? tip = "JSTicketTicket: "+ JSTicketTicket;
? ? ? ? ? ? ? ? ? ? TimeSpan span = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
? ? ? ? ? ? ? ? ? ? string timestamp = Convert.ToInt64(span.TotalSeconds).ToString();
? ? ? ? ? ? ? ? ? ? string nonceStr = createNonceStr();
? ? ? ? ? ? ? ? ? ? //簽名用的url必須是調(diào)用JS接口頁面的完整URL。
? ? ? ? ? ? ? ? ? ? string UrlMsg = "";
? ? ? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? UrlMsg = GetCurWebUrl();
? ? ? ? ? ? ? ? ? ? ? ? // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 ?
? ? ? ? ? ? ? ? ? ? ? ? string rawstring = "jsapi_ticket=" + JSTicketTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + UrlMsg + "";
? ? ? ? ? ? ? ? ? ? ? ? //string signature = FormsAuthentication.HashPasswordForStoringInConfigFile(rawstring, "SHA1").ToLower();
? ? ? ? ? ? ? ? ? ? ? ? string signature = GetSha1(rawstring);
? ? ? ? ? ? ? ? ? ? ? ? tip = "signature: " + signature;
? ? ? ? ? ? ? ? ? ? ? ? //Hashtable signPackage = new Hashtable();
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("appId", "wx22db6c710f661f68");
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("nonceStr", nonceStr);
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("timestamp", timestamp);
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("url", url);
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("signature", signature);
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("rawString", rawstring);
? ? ? ? ? ? ? ? ? ? ? ? //signPackage.Add("JSTicketTicket", JSTicketTicket);
? ? ? ? ? ? ? ? ? ? ? ? OpenWechatShareMenu(appID, timestamp, nonceStr, signature);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? catch (System.Exception e)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? UnityEngine.Debug.Log(e.Message);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? string tip = "";
? ? private void OnGUI()
? ? {
? ? ? ? GUI.Label(new Rect(200, 200, 500, 500), tip);
? ? }
? ? void Start()
? ? {
? ? ? ? //StartCoroutine( GetWechatToken());
? ? ? ? //TimeSpan span = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
? ? ? ? //string timestamp = "1692415454"; //Convert.ToInt64(span.TotalSeconds).ToString();
? ? ? ? //string nonceStr = "yCPqfcy4DRVu3QCH"; //createNonceStr();
? ? ? ? //Debug.Log("timestamp: "+timestamp);
? ? ? ? //Debug.Log("nonceStr: "+nonceStr);
? ? ? ? //string JSTicketTicket = "LIKLckvwlJT9cWIhEQTwfCAOltWVYohgnqfFqTJCnBdvECHdP06M2ysW3sQqCTMv8tgUxU9av9Dn8ghivMZ0PQ";
? ? ? ? //Debug.Log("JSTicketTicket: " + JSTicketTicket);
? ? ? ? 簽名用的url必須是調(diào)用JS接口頁面的完整URL。
? ? ? ? //string UrlMsg = "";
? ? ? ? //try
? ? ? ? //{
? ? ? ? // ? ?UrlMsg = "http://localhost/WebGL/"; //GetCurWebUrl();
? ? ? ? // ? ?Debug.Log("UrlMsg: " + UrlMsg);
? ? ? ? // ? ?//jsapi_ticket=LIKLckvwlJT9cWIhEQTwfCAOltWVYohgnqfFqTJCnBdvECHdP06M2ysW3sQqCTMv8tgUxU9av9Dn8ghivMZ0PQ&noncestr=yCPqfcy4DRVu3QCH×tamp=1692415454&url=http://localhost/WebGL/
? ? ? ? // ? ?// 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 ?
? ? ? ? // ? ?string rawstring = "jsapi_ticket=" + JSTicketTicket + "&noncestr=" + nonceStr + "×tamp=" + timestamp + "&url=" + UrlMsg + "";
? ? ? ? // ? ?//string signature = FormsAuthentication.HashPasswordForStoringInConfigFile(rawstring, "SHA1").ToLower();
? ? ? ? // ? ?string signature = GetSha1(rawstring);
? ? ? ? // ? ?tip = "signature: " + signature;
? ? ? ? // ? ?//cf839bdf7af95e5764c3eea8459c8c6acf112081
? ? ? ? // ? ?Debug.Log(tip);
? ? ? ? // ? ?//Hashtable signPackage = new Hashtable();
? ? ? ? // ? ?//signPackage.Add("appId", "wx22db6c710f661f68");
? ? ? ? // ? ?//signPackage.Add("nonceStr", nonceStr);
? ? ? ? // ? ?//signPackage.Add("timestamp", timestamp);
? ? ? ? // ? ?//signPackage.Add("url", url);
? ? ? ? // ? ?//signPackage.Add("signature", signature);
? ? ? ? // ? ?//signPackage.Add("rawString", rawstring);
? ? ? ? // ? ?//signPackage.Add("JSTicketTicket", JSTicketTicket);
? ? ? ? // ? ?//OpenWechatShareMenu(appID, timestamp, nonceStr, signature);
? ? ? ? //}
? ? ? ? //catch(Exception ex)
? ? ? ? //{
? ? ? ? //}
? ? }
}
?
js部分代碼
? ? GetCurWebUrl: function () {
? ? ? ? // var returnStr = window.top.location.href;
? ? ? ? // var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
? ? ? ? // writeStringToMemory(returnStr, buffer);
? ? ? ? // return buffer;
? ? ? ? var returnStr = window.top.location.href;
? ? ? ? var bufferSize = lengthBytesUTF8(returnStr)+1;
? ? ? ? var buffer = _malloc(bufferSize);
? ? ? ? stringToUTF8(returnStr, buffer, bufferSize);
? ? ? ? return buffer;文章來源:http://www.zghlxwxcb.cn/news/detail-659773.html
? ? },文章來源地址http://www.zghlxwxcb.cn/news/detail-659773.html
到了這里,關(guān)于微信jssdk 簽名獲取方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!