ttsAudio.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // 这个东西我都没执行yarn add crypto竟然能用,可能另一个项目安装了全局共享了,如报错找不到,执行一下yarn add crypto
  2. import crypto from 'crypto'
  3. export class AccessToken {
  4. static encodeText(text) {
  5. let encodedText = encodeURIComponent(text);
  6. return encodedText.replace('+', '%20').replace('*', '%2A').replace('~', '%7E');
  7. }
  8. static encodeDict(dict) {
  9. let keys = Object.keys(dict).sort();
  10. return keys.map(key => `${this.encodeText(key)}=${this.encodeText(dict[key])}`).join('&');
  11. }
  12. static async createToken(accessKeyId, accessKeySecret) {
  13. const parameters = {
  14. AccessKeyId: accessKeyId,
  15. Action: 'CreateToken',
  16. Format: 'JSON',
  17. RegionId: 'cn-shanghai',
  18. SignatureMethod: 'HMAC-SHA1',
  19. SignatureNonce: uuidv4(),
  20. SignatureVersion: '1.0',
  21. Timestamp: new Date().toISOString(),
  22. Version: '2019-02-28'
  23. };
  24. const queryString = this.encodeDict(parameters);
  25. console.log('Normalized request string:', queryString);
  26. const stringToSign = `GET&${this.encodeText('/')}&${this.encodeText(queryString)}`;
  27. console.log('String to sign:', stringToSign);
  28. const hmac = crypto.createHmac('sha1', `${accessKeySecret}&`);
  29. hmac.update(stringToSign);
  30. const signature = hmac.digest('base64');
  31. console.log('Signature:', signature);
  32. const encodedSignature = this.encodeText(signature);
  33. console.log('URL-encoded signature:', encodedSignature);
  34. const fullUrl = `https://nls-meta.cn-shanghai.aliyuncs.com/?Signature=${encodedSignature}&${queryString}`;
  35. console.log('URL:', fullUrl);
  36. let resData = await new Promise((resolve, reject) => {
  37. uni.request({
  38. url: fullUrl,
  39. method: 'GET',
  40. success: res => {
  41. const data = res.data
  42. resolve({
  43. token: data.Token.Id,
  44. expireTime: data.Token.ExpireTime
  45. })
  46. },
  47. fail: error => {
  48. console.log(error)
  49. reject(error)
  50. }
  51. })
  52. })
  53. console.log('res',resData)
  54. if(resData){
  55. return resData
  56. }
  57. // Using fetch for HTTP request
  58. // const response = await fetch(fullUrl);
  59. // if (response.ok) {
  60. // const jsonResponse = await response.json();
  61. // if (jsonResponse.Token) {
  62. // return {
  63. // token: jsonResponse.Token.Id,
  64. // expireTime: jsonResponse.Token.ExpireTime
  65. // };
  66. // }
  67. // }
  68. // console.error(await response.text());
  69. return {
  70. token: null,
  71. expireTime: null
  72. };
  73. }
  74. }
  75. // Sample UUIDv4 function, or you could use a library like `uuid`
  76. function uuidv4() {
  77. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  78. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  79. return v.toString(16);
  80. });
  81. }