using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.SceneManagement; using System.Text.RegularExpressions; public class PayGameMode : MonoBehaviour { [SerializeField] Texture2D centericon; [SerializeField] GameObject parent; byte[] URLData; int stepLength = 0; bool bURLData = false; string localDeviceName; string qrCodeUrl; //= "https://www.weimaqi.net/index_.aspx?device_name=WXR02153" bool bShowQRCode = false; UnitySerialPort serialPort; [SerializeField] GameObject QRPlanObj; private void Awake() { serialPort = parent.GetComponent(); } // Start is called before the first frame update void Start() { //URLData = new byte[66]; // byte[] inArray = {0xAA,0x40,0xDD,0xE8,0x05,0xEC,0x5E,0xAC,0x5F,0x4E,0xCE,0xD7,0xFE,0x12,0x5B,0x5F,0xC3,0xD7,0x95,0x98,0x74,0xAE,0x9D,0xD3,0x33,0xF7,0x64,0x12,0x14,0x92,0x4A,0x22,0x56,0xBE,0xF9,0x54,0xE2,0xDD,0x90,0xD2,0xCD,0xB6,0x3A,0x64,0x44,0x1D,0x64,0x98,0xC6,0xAC,0x66,0x15,0x14,0xEA,0x39,0x35,0x79,0xEF,0x74,0x1C,0xAC,0xD0,0x9A,0x51,0x00,0x00}; // int inArrayLen=inArray.GetLength(0); // Debug.Log("inArray长度="+inArray.GetLength(0)); // DES des = parent.GetComponent(); // byte[] outArray = des.Decrypt(inArray); // Debug.Log("解密结果1="+BitConverter.ToString(outArray)); // string str = ASCIIToStr(outArray); // Debug.Log("URL="+str); //GetQRCode(); //byte[] payNum = new byte[2] { 0x00, 0x01 }; //if (BitConverter.IsLittleEndian) // Array.Reverse(payNum); //need the bytes in the reverse order //int coin = BitConverter.ToInt16(payNum, 0); //Debug.Log("投币数coin:" + coin); //StandaloneAPI.InsertMultipleCoinForAddTime(coin); ////隐藏扫码面板 //QRPlanObj.SetActive(false); } public void sendDataBtn() { Debug.Log("sendDataBtn"); GetQRCode(); } public IEnumerator QRCodeCoroutine() { yield return new WaitForSeconds(0.5f); GetQRCode(); } void GetQRCode() { //只显示一次二维码 if (bShowQRCode) return; //请求二维码 byte[] QRData = GenerateQRCodeRequireData(); //UnitySerialPort serialPort = parent.GetComponent(); serialPort.SendData(QRData); } public byte[] GenerateAnswerRequireData() { byte[] aArray = new byte[8]; aArray[0] = 0x5A; aArray[1] = 0x00; System.Random random = new System.Random(); int rundom1 = random.Next(0, 256); int rundom2 = random.Next(0, 255); aArray[2] = (byte)rundom1;// 生成1到255的随机整数 aArray[3] = (byte)rundom2; aArray[4] = 0x01; //数据域 aArray[5] = 0x00; //加0 aArray[6] = 0x00; aArray[7] = 0x00; // Debug.Log("主板应答原始数据="+BitConverter.ToString(aArray)); DES des = parent.GetComponent(); byte[] outArray = des.Encryption(aArray, 0x08); // Debug.Log("主板应答加密数据="+BitConverter.ToString(outArray)); return outArray; } //主板接收盒子的查询后应答 public void Answer() { byte[] anwserData = GenerateAnswerRequireData(); //UnitySerialPort serialPort = parent.GetComponent(); serialPort.SendData(anwserData); } //主板发起二维码请求 byte[] GenerateQRCodeRequireData() { byte[] aArray = new byte[8]; aArray[0] = 0x5A; aArray[1] = 0x02; System.Random random = new System.Random(); int rundom1 = random.Next(0, 256); int rundom2 = random.Next(0, 255); aArray[2] = (byte)rundom1;// 生成1到255的随机整数 aArray[3] = (byte)rundom2; aArray[4] = 0x01; aArray[5] = 0x00; //加0 aArray[6] = 0x00; aArray[7] = 0x00; DES des = parent.GetComponent(); //Debug.Log("请求二维码前数据=" + BitConverter.ToString(aArray)); byte[] outArray = des.Encryption(aArray, 0x08); //Debug.Log("请求二维码前数据加密=" + BitConverter.ToString(outArray)); return outArray; } // public void ShowQRCode(byte[] decryptData,int step) public void ShowQRCode(byte[] decryptData) { CreatQR qr = parent.GetComponent(); string http_str = ASCIIToStr(decryptData); Debug.Log("URL=" + http_str); qr.GenerateQRImage3(http_str, 512, 512, centericon); } public void ShowQRCode(byte[] originData, int step) { if (step == 0) { int urlLength = Convert.ToInt32(originData[1]); //Debug.Log("单片机问询--originData[1]:" + urlLength); URLData = new byte[urlLength + 2]; bURLData = true; for (int i = 0; i < URLData.Length; i++) { URLData[i] = 0x00; } stepLength = originData.Length; for (int i = 0; i < stepLength; i++) { //第一个部分 32 个byte URLData[i] = originData[i]; } //Debug.Log("stepLength1===" + stepLength); } else { if (!bURLData) return; //后续直接从stepLength下标开始 for (int i = 0; i < originData.Length; i++) { URLData[stepLength + i] = originData[i]; } stepLength += originData.Length; //Debug.Log("URLData:" + URLData.Length); //Debug.Log("stepLength2===" + stepLength); //数据添加完全后 if (stepLength == URLData.Length) { DES des = parent.GetComponent(); byte[] outArray = des.Decrypt(URLData); CreatQR qr = parent.GetComponent(); string http_str = ASCIIToStr(outArray); // Debug.Log("URL= " + http_str); string[] str = http_str.Split("device_name="); //https://www.weimaqi.net/index_.aspx?device_name=WXR02153 //去除字符串中隐藏了 unicode 字符,记录本地一个设备名字 WXR02153 localDeviceName = Regex.Replace(str[str.Length - 1], @"[^\w:/ ]", string.Empty); //Debug.Log("设备码:" + localDeviceName); // Debug.Log("设备码length:" + localDeviceName.Length); string _httpEnd = "http" + str[0].Split("http")[1] + "device_name=" + localDeviceName; //Debug.Log("二维码URL = (" + _httpEnd + "),length:"+_httpEnd.Length); qrCodeUrl = _httpEnd; //Debug.Log("二维码URL length:" + _httpEnd.Length); bShowQRCode = true; //记录显示一个QRCode的状态 bURLData = false; //qr.GenerateQRImage3(_httpEnd, 512, 512, centericon); qr.onlyQRCode(_httpEnd, 512, 512); } } } byte[] GeneratePayAnswerRequireData(byte[] payID, byte[] payNum) { byte[] aArray = new byte[16]; aArray[0] = 0x5A; aArray[1] = 0x01; System.Random random = new System.Random(); int rundom1 = random.Next(0, 256); int rundom2 = random.Next(0, 255); aArray[2] = (byte)rundom1;// 生成1到255的随机整数 aArray[3] = (byte)rundom2; aArray[4] = 0x07; //出货 aArray[5] = 0x00; //支付唯一码 aArray[6] = payID[0]; aArray[7] = payID[1]; aArray[8] = payID[2]; aArray[9] = payID[3]; //支付数值 aArray[10] = payNum[0]; aArray[11] = payNum[1]; //加0 aArray[12] = 0x00; aArray[13] = 0x00; aArray[14] = 0x00; aArray[15] = 0x00; //Debug.Log("支付应答原始数组=" + BitConverter.ToString(aArray)); // GameObject parent = this.transform.gameObject; DES des = parent.GetComponent(); byte[] outArray = des.Encryption(aArray, 0x10); //Debug.Log("支付应答原始数组加密=" + BitConverter.ToString(outArray)); return outArray; } public void PayAnswer(byte[] decryptData) { //设备名, todo 对比是否与本机记录的设备编号一致,否则丢弃不理会。 byte[] deviceName = new byte[8]; for (int i = 0; i < 8; i++) { deviceName[i] = decryptData[5 + i]; } string orderDevicename = ASCIIToStr(deviceName); Debug.Log("支付的设备码:" + orderDevicename + ",length:" + orderDevicename.Length); if ( localDeviceName != orderDevicename) { Debug.LogError("二维码的设备号和支付设备号不一致!!"); serialPort.ToastShow("二维码的设备号和支付设备号不一致!!"); return; } //支付唯一码, 主板如收到与上一次相同的唯一码,说明是同一笔订单(可能某种原因盒子重发了,此时主板按正常应答即可) byte[] payID = new byte[4]; for (int i = 0; i < 4; i++) { payID[i] = decryptData[5 + 8 + i]; } //Debug.Log("payID:" + BitConverter.ToString(payID)); //支付数值s byte[] payNum = new byte[2]; for (int i = 0; i < 2; i++) { payNum[i] = decryptData[5 + 8 + 4 + i]; } //5A-01-0C-EE-0E -57-58-52-30-32-31-35-33 -00-07-EE-0C -00-16 -00-00-00-00-00 byte[] payData = GeneratePayAnswerRequireData(payID, payNum); //UnitySerialPort serialPort = parent.GetComponent(); serialPort.SendData(payData); //Debug.Log("decryptData:"+ BitConverter.ToString(decryptData)); //Debug.Log(BitConverter.ToString(payNum)); if (BitConverter.IsLittleEndian) Array.Reverse(payNum); //need the bytes in the reverse order int coin = BitConverter.ToInt16(payNum, 0); Debug.Log("投币数coin:" + coin); //Debug.Log(BitConverter.ToString(payNum)) ; paySuccess(coin); } //支付成功后相应的操作 void paySuccess(int coin) { StandaloneAPI.InsertMultipleCoinForAddTime(coin); //隐藏扫码面板 QRPlanObj.SetActive(false); //SceneManager.LoadScene("Entry"); //投币后,如果在home页面并且连接了设备。自动进行下一级页面 if (CommonConfig.StandaloneMode && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess) { if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home" && PersistenHandler.ins.menuBackCtr.views.Count == 0 && !FindObjectOfType()) { ViewMgr.Instance.ShowView(); } } } public string getQRCodeUrl() { return qrCodeUrl; } public void setQRPlanObjActive(bool active) { QRPlanObj.SetActive(active); } string ASCIIToStr(byte[] array) { //string转byte数组 // byte[] array = {0x68,0x74,0x74,0x70,0x73,0x3a,0x2f,0x2f,0x77,0x77,0x77,0x2e,0x77,0x65,0x69,0x6d,0x61,0x71,0x69,0x2e,0x6e,0x65,0x74,0x2f,0x69,0x6e,0x64,0x65,0x78,0x5f,0x2e,0x61,0x73,0x70,0x78,0x3f,0x64,0x65,0x76,0x69,0x63,0x65,0x5f,0x6e,0x61,0x6d,0x65,0x3d,0x57,0x58,0x52,0x30,0x32,0x31,0x35,0x33,0x00,0x00,0x00}; // //byte数组转string // string s=System.Text.Encoding.ASCII.GetString(array);//这里的array就是存放ASCII的byte数组 // Debug.Log(s); return System.Text.Encoding.ASCII.GetString(array); } }