PayGameMode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using UnityEngine.SceneManagement;
  6. using System.Text.RegularExpressions;
  7. public class PayGameMode : MonoBehaviour
  8. {
  9. [SerializeField]
  10. Texture2D centericon;
  11. [SerializeField]
  12. GameObject parent;
  13. byte[] URLData;
  14. int stepLength = 0;
  15. bool bURLData = false;
  16. string localDeviceName;
  17. string qrCodeUrl; //= "https://www.weimaqi.net/index_.aspx?device_name=WXR02153"
  18. bool bShowQRCode = false;
  19. UnitySerialPort serialPort;
  20. [SerializeField]
  21. GameObject QRPlanObj;
  22. private void Awake()
  23. {
  24. serialPort = parent.GetComponent<UnitySerialPort>();
  25. }
  26. // Start is called before the first frame update
  27. void Start()
  28. {
  29. //URLData = new byte[66];
  30. // 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};
  31. // int inArrayLen=inArray.GetLength(0);
  32. // Debug.Log("inArray长度="+inArray.GetLength(0));
  33. // DES des = parent.GetComponent<DES>();
  34. // byte[] outArray = des.Decrypt(inArray);
  35. // Debug.Log("解密结果1="+BitConverter.ToString(outArray));
  36. // string str = ASCIIToStr(outArray);
  37. // Debug.Log("URL="+str);
  38. //GetQRCode();
  39. //byte[] payNum = new byte[2] { 0x00, 0x01 };
  40. //if (BitConverter.IsLittleEndian)
  41. // Array.Reverse(payNum); //need the bytes in the reverse order
  42. //int coin = BitConverter.ToInt16(payNum, 0);
  43. //Debug.Log("投币数coin:" + coin);
  44. //StandaloneAPI.InsertMultipleCoinForAddTime(coin);
  45. ////隐藏扫码面板
  46. //QRPlanObj.SetActive(false);
  47. }
  48. public void sendDataBtn()
  49. {
  50. Debug.Log("sendDataBtn");
  51. GetQRCode();
  52. }
  53. public IEnumerator QRCodeCoroutine()
  54. {
  55. yield return new WaitForSeconds(0.5f);
  56. GetQRCode();
  57. }
  58. void GetQRCode()
  59. {
  60. //只显示一次二维码
  61. if (bShowQRCode) return;
  62. //请求二维码
  63. byte[] QRData = GenerateQRCodeRequireData();
  64. //UnitySerialPort serialPort = parent.GetComponent<UnitySerialPort>();
  65. serialPort.SendData(QRData);
  66. }
  67. public byte[] GenerateAnswerRequireData()
  68. {
  69. byte[] aArray = new byte[8];
  70. aArray[0] = 0x5A;
  71. aArray[1] = 0x00;
  72. System.Random random = new System.Random();
  73. int rundom1 = random.Next(0, 256);
  74. int rundom2 = random.Next(0, 255);
  75. aArray[2] = (byte)rundom1;// 生成1到255的随机整数
  76. aArray[3] = (byte)rundom2;
  77. aArray[4] = 0x01;
  78. //数据域
  79. aArray[5] = 0x00;
  80. //加0
  81. aArray[6] = 0x00;
  82. aArray[7] = 0x00;
  83. // Debug.Log("主板应答原始数据="+BitConverter.ToString(aArray));
  84. DES des = parent.GetComponent<DES>();
  85. byte[] outArray = des.Encryption(aArray, 0x08);
  86. // Debug.Log("主板应答加密数据="+BitConverter.ToString(outArray));
  87. return outArray;
  88. }
  89. //主板接收盒子的查询后应答
  90. public void Answer()
  91. {
  92. byte[] anwserData = GenerateAnswerRequireData();
  93. //UnitySerialPort serialPort = parent.GetComponent<UnitySerialPort>();
  94. serialPort.SendData(anwserData);
  95. }
  96. //主板发起二维码请求
  97. byte[] GenerateQRCodeRequireData()
  98. {
  99. byte[] aArray = new byte[8];
  100. aArray[0] = 0x5A;
  101. aArray[1] = 0x02;
  102. System.Random random = new System.Random();
  103. int rundom1 = random.Next(0, 256);
  104. int rundom2 = random.Next(0, 255);
  105. aArray[2] = (byte)rundom1;// 生成1到255的随机整数
  106. aArray[3] = (byte)rundom2;
  107. aArray[4] = 0x01;
  108. aArray[5] = 0x00;
  109. //加0
  110. aArray[6] = 0x00;
  111. aArray[7] = 0x00;
  112. DES des = parent.GetComponent<DES>();
  113. //Debug.Log("请求二维码前数据=" + BitConverter.ToString(aArray));
  114. byte[] outArray = des.Encryption(aArray, 0x08);
  115. //Debug.Log("请求二维码前数据加密=" + BitConverter.ToString(outArray));
  116. return outArray;
  117. }
  118. // public void ShowQRCode(byte[] decryptData,int step)
  119. public void ShowQRCode(byte[] decryptData)
  120. {
  121. CreatQR qr = parent.GetComponent<CreatQR>();
  122. string http_str = ASCIIToStr(decryptData);
  123. Debug.Log("URL=" + http_str);
  124. qr.GenerateQRImage3(http_str, 512, 512, centericon);
  125. }
  126. public void ShowQRCode(byte[] originData, int step)
  127. {
  128. if (step == 0)
  129. {
  130. int urlLength = Convert.ToInt32(originData[1]);
  131. //Debug.Log("单片机问询--originData[1]:" + urlLength);
  132. URLData = new byte[urlLength + 2];
  133. bURLData = true;
  134. for (int i = 0; i < URLData.Length; i++)
  135. {
  136. URLData[i] = 0x00;
  137. }
  138. stepLength = originData.Length;
  139. for (int i = 0; i < stepLength; i++)
  140. {
  141. //第一个部分 32 个byte
  142. URLData[i] = originData[i];
  143. }
  144. //Debug.Log("stepLength1===" + stepLength);
  145. }
  146. else
  147. {
  148. if (!bURLData) return;
  149. //后续直接从stepLength下标开始
  150. for (int i = 0; i < originData.Length; i++)
  151. {
  152. URLData[stepLength + i] = originData[i];
  153. }
  154. stepLength += originData.Length;
  155. //Debug.Log("URLData:" + URLData.Length);
  156. //Debug.Log("stepLength2===" + stepLength);
  157. //数据添加完全后
  158. if (stepLength == URLData.Length)
  159. {
  160. DES des = parent.GetComponent<DES>();
  161. byte[] outArray = des.Decrypt(URLData);
  162. CreatQR qr = parent.GetComponent<CreatQR>();
  163. string http_str = ASCIIToStr(outArray);
  164. // Debug.Log("URL= " + http_str);
  165. string[] str = http_str.Split("device_name=");
  166. //https://www.weimaqi.net/index_.aspx?device_name=WXR02153
  167. //去除字符串中隐藏了 unicode 字符,记录本地一个设备名字 WXR02153
  168. localDeviceName = Regex.Replace(str[str.Length - 1], @"[^\w:/ ]", string.Empty);
  169. //Debug.Log("设备码:" + localDeviceName);
  170. // Debug.Log("设备码length:" + localDeviceName.Length);
  171. string _httpEnd = "http" + str[0].Split("http")[1] + "device_name=" + localDeviceName;
  172. //Debug.Log("二维码URL = (" + _httpEnd + "),length:"+_httpEnd.Length);
  173. qrCodeUrl = _httpEnd;
  174. //Debug.Log("二维码URL length:" + _httpEnd.Length);
  175. bShowQRCode = true; //记录显示一个QRCode的状态
  176. bURLData = false;
  177. //qr.GenerateQRImage3(_httpEnd, 512, 512, centericon);
  178. qr.onlyQRCode(_httpEnd, 512, 512);
  179. }
  180. }
  181. }
  182. byte[] GeneratePayAnswerRequireData(byte[] payID, byte[] payNum)
  183. {
  184. byte[] aArray = new byte[16];
  185. aArray[0] = 0x5A;
  186. aArray[1] = 0x01;
  187. System.Random random = new System.Random();
  188. int rundom1 = random.Next(0, 256);
  189. int rundom2 = random.Next(0, 255);
  190. aArray[2] = (byte)rundom1;// 生成1到255的随机整数
  191. aArray[3] = (byte)rundom2;
  192. aArray[4] = 0x07;
  193. //出货
  194. aArray[5] = 0x00;
  195. //支付唯一码
  196. aArray[6] = payID[0];
  197. aArray[7] = payID[1];
  198. aArray[8] = payID[2];
  199. aArray[9] = payID[3];
  200. //支付数值
  201. aArray[10] = payNum[0];
  202. aArray[11] = payNum[1];
  203. //加0
  204. aArray[12] = 0x00;
  205. aArray[13] = 0x00;
  206. aArray[14] = 0x00;
  207. aArray[15] = 0x00;
  208. //Debug.Log("支付应答原始数组=" + BitConverter.ToString(aArray));
  209. // GameObject parent = this.transform.gameObject;
  210. DES des = parent.GetComponent<DES>();
  211. byte[] outArray = des.Encryption(aArray, 0x10);
  212. //Debug.Log("支付应答原始数组加密=" + BitConverter.ToString(outArray));
  213. return outArray;
  214. }
  215. public void PayAnswer(byte[] decryptData)
  216. {
  217. //设备名, todo 对比是否与本机记录的设备编号一致,否则丢弃不理会。
  218. byte[] deviceName = new byte[8];
  219. for (int i = 0; i < 8; i++)
  220. {
  221. deviceName[i] = decryptData[5 + i];
  222. }
  223. string orderDevicename = ASCIIToStr(deviceName);
  224. Debug.Log("支付的设备码:" + orderDevicename + ",length:" + orderDevicename.Length);
  225. if ( localDeviceName != orderDevicename)
  226. {
  227. Debug.LogError("二维码的设备号和支付设备号不一致!!");
  228. serialPort.ToastShow("二维码的设备号和支付设备号不一致!!");
  229. return;
  230. }
  231. //支付唯一码, 主板如收到与上一次相同的唯一码,说明是同一笔订单(可能某种原因盒子重发了,此时主板按正常应答即可)
  232. byte[] payID = new byte[4];
  233. for (int i = 0; i < 4; i++)
  234. {
  235. payID[i] = decryptData[5 + 8 + i];
  236. }
  237. //Debug.Log("payID:" + BitConverter.ToString(payID));
  238. //支付数值s
  239. byte[] payNum = new byte[2];
  240. for (int i = 0; i < 2; i++)
  241. {
  242. payNum[i] = decryptData[5 + 8 + 4 + i];
  243. }
  244. //5A-01-0C-EE-0E -57-58-52-30-32-31-35-33 -00-07-EE-0C -00-16 -00-00-00-00-00
  245. byte[] payData = GeneratePayAnswerRequireData(payID, payNum);
  246. //UnitySerialPort serialPort = parent.GetComponent<UnitySerialPort>();
  247. serialPort.SendData(payData);
  248. //Debug.Log("decryptData:"+ BitConverter.ToString(decryptData));
  249. //Debug.Log(BitConverter.ToString(payNum));
  250. if (BitConverter.IsLittleEndian)
  251. Array.Reverse(payNum); //need the bytes in the reverse order
  252. int coin = BitConverter.ToInt16(payNum, 0);
  253. Debug.Log("投币数coin:" + coin);
  254. //Debug.Log(BitConverter.ToString(payNum)) ;
  255. paySuccess(coin);
  256. }
  257. //支付成功后相应的操作
  258. void paySuccess(int coin) {
  259. StandaloneAPI.InsertMultipleCoinForAddTime(coin);
  260. //隐藏扫码面板
  261. QRPlanObj.SetActive(false);
  262. //SceneManager.LoadScene("Entry");
  263. //投币后,如果在home页面并且连接了设备。自动进行下一级页面
  264. if (CommonConfig.StandaloneMode && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  265. {
  266. if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Home"
  267. && PersistenHandler.ins.menuBackCtr.views.Count == 0
  268. && !FindObjectOfType<NewUserGuider>())
  269. {
  270. ViewMgr.Instance.ShowView<GameStartView>();
  271. }
  272. }
  273. }
  274. public string getQRCodeUrl() {
  275. return qrCodeUrl;
  276. }
  277. public void setQRPlanObjActive(bool active) {
  278. QRPlanObj.SetActive(active);
  279. }
  280. string ASCIIToStr(byte[] array)
  281. {
  282. //string转byte数组
  283. // 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};
  284. // //byte数组转string
  285. // string s=System.Text.Encoding.ASCII.GetString(array);//这里的array就是存放ASCII的byte数组
  286. // Debug.Log(s);
  287. return System.Text.Encoding.ASCII.GetString(array);
  288. }
  289. }