Uniapp.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import type { RequestCacheStorage, RequestOptions } from "../core/RequestCore";
  2. import type { RequestImplementer } from "../core/RequestImplementer";
  3. import { isNullOrEmpty } from "../utils/Utils";
  4. export class FormData implements Record<string, any> {
  5. public constructor() {}
  6. [index: string]: any;
  7. // 添加字段
  8. append(name: string, value: any) {
  9. if (!this[name]) {
  10. this[name] = [];
  11. }
  12. this[name].push(value);
  13. }
  14. // 获取字段值
  15. get(name: string) {
  16. if (this[name]) {
  17. return this[name][0];
  18. }
  19. return null;
  20. }
  21. // 获取所有字段值
  22. getAll(name: string) {
  23. return this[name] || [];
  24. }
  25. // 删除字段
  26. delete(name: string) {
  27. delete this[name];
  28. }
  29. // 检查是否存在字段
  30. has(name: string) {
  31. return this[name] !== undefined;
  32. }
  33. // 设置字段值(覆盖原有值)
  34. set(name: string, value: any) {
  35. this[name] = value;
  36. }
  37. // 获取所有字段的键名
  38. getKeys() {
  39. return Object.keys(this);
  40. }
  41. }
  42. export class Response {
  43. public constructor(url: string, data: unknown, options: {
  44. headers: Record<string, unknown>,
  45. status: number,
  46. }, errMsg: string) {
  47. this.errMsg = errMsg;
  48. this.data = data;
  49. this.url = url;
  50. this.status = options.status;
  51. this.headers = options.headers;
  52. this.ok = options.status >= 200 && options.status <= 399;
  53. }
  54. get statusText() {
  55. return this.ok ? 'OK' : 'ERROR:' + this.status;
  56. }
  57. headers: Record<string, unknown>;
  58. ok: boolean;
  59. status: number;
  60. errMsg: string;
  61. url: string;
  62. data: unknown;
  63. json() : Promise<any> {
  64. return new Promise<any>((resolve, reject) => {
  65. if (typeof this.data === 'undefined' || isNullOrEmpty(this.data)) {
  66. resolve({});
  67. return;
  68. }
  69. if (typeof this.data === 'object') {
  70. resolve(this.data);
  71. return;
  72. }
  73. let data = null;
  74. if (typeof this.data === 'string') {
  75. try {
  76. data = JSON.parse(this.data);
  77. } catch(e) {
  78. console.log('json error: ' + e, this.data);
  79. reject(e);
  80. }
  81. } else {
  82. data = this.data;
  83. }
  84. resolve(data);
  85. })
  86. }
  87. }
  88. const uniappImplementer : RequestImplementer = {
  89. getCache: function (key: string) {
  90. return new Promise<RequestCacheStorage|null>((resolve, reject) => {
  91. uni.getStorage({
  92. key: key,
  93. success: (res) => {
  94. resolve(res.data ? JSON.parse(res.data) as RequestCacheStorage : null);
  95. },
  96. fail: (res) => {
  97. resolve(null);
  98. }
  99. });
  100. });
  101. },
  102. setCache: async function (key: string, value: RequestCacheStorage|null) {
  103. return new Promise<void>((resolve, reject) => {
  104. uni.setStorage({
  105. key: key,
  106. data: JSON.stringify(value),
  107. success: (res) => {
  108. resolve();
  109. },
  110. fail: (res) => {
  111. resolve();
  112. }
  113. });
  114. });
  115. },
  116. doRequest: function (url: string, init?: RequestOptions, timeout?: number): Promise<Response> {
  117. return new Promise<Response>((resolve, reject) => {
  118. uni.request({
  119. url: url,
  120. timeout: timeout,
  121. ...init,
  122. success(res) {
  123. const response = new Response(url, res.data, {
  124. headers: res.header,
  125. status: res.statusCode,
  126. }, 'success');
  127. resolve(response);
  128. },
  129. fail(res) {
  130. reject(res);
  131. },
  132. })
  133. });
  134. }
  135. };
  136. export default uniappImplementer;