You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.8KB

  1. // -------------------------- 通用方法 ---------------------------
  2. //扩展js string endwith,startwith方法
  3. String.prototype.endWith = function (str) {
  4. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  5. return false;
  6. if (this.substring(this.length - str.length) == str)
  7. return true;
  8. else
  9. return false;
  10. }
  11. String.prototype.startWith = function (str) {
  12. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  13. return false;
  14. if (this.substr(0, str.length) == str)
  15. return true;
  16. else
  17. return false;
  18. }
  19. //UTF-16转UTF-8
  20. function utf16ToUtf8(s) {
  21. if (!s) {
  22. return;
  23. }
  24. var i, code, ret = [],
  25. len = s.length;
  26. for (i = 0; i < len; i++) {
  27. code = s.charCodeAt(i);
  28. if (code > 0x0 && code <= 0x7f) {
  29. //单字节
  30. //UTF-16 0000 - 007F
  31. //UTF-8 0xxxxxxx
  32. ret.push(s.charAt(i));
  33. } else if (code >= 0x80 && code <= 0x7ff) {
  34. //双字节
  35. //UTF-16 0080 - 07FF
  36. //UTF-8 110xxxxx 10xxxxxx
  37. ret.push(
  38. //110xxxxx
  39. String.fromCharCode(0xc0 | ((code >> 6) & 0x1f)),
  40. //10xxxxxx
  41. String.fromCharCode(0x80 | (code & 0x3f))
  42. );
  43. } else if (code >= 0x800 && code <= 0xffff) {
  44. //三字节
  45. //UTF-16 0800 - FFFF
  46. //UTF-8 1110xxxx 10xxxxxx 10xxxxxx
  47. ret.push(
  48. //1110xxxx
  49. String.fromCharCode(0xe0 | ((code >> 12) & 0xf)),
  50. //10xxxxxx
  51. String.fromCharCode(0x80 | ((code >> 6) & 0x3f)),
  52. //10xxxxxx
  53. String.fromCharCode(0x80 | (code & 0x3f))
  54. );
  55. }
  56. }
  57. return ret.join('');
  58. }
  59. //若要显示:当前日期加时间(如:200906121200)
  60. function currentTime() {
  61. var now = new Date();
  62. var year = now.getFullYear(); //年
  63. var month = now.getMonth() + 1; //月
  64. var day = now.getDate(); //日
  65. var hh = now.getHours(); //时
  66. var mm = now.getMinutes(); //分
  67. var clock = year + "";
  68. if (month < 10)
  69. clock += "0";
  70. clock += month + "";
  71. if (day < 10)
  72. clock += "0";
  73. clock += day + "";
  74. if (hh < 10)
  75. clock += "0";
  76. clock += hh + "";
  77. if (mm < 10) clock += '0';
  78. clock += mm;
  79. return (clock);
  80. }
  81. /**
  82. * 判断文件个数是否为0,若为0则关闭
  83. * @param {*} name
  84. */
  85. function closeEtIfNoDocument() {
  86. var etApp = wps.EtApplication();
  87. var docs = etApp.Workbooks;
  88. if (docs && docs.Count == 0) {
  89. etApp.Quit();
  90. }
  91. }
  92. function activeTab() {
  93. wps.ribbonUI.ActivateTab('WPSWorkExtTab');
  94. }
  95. function showOATab() {
  96. wps.PluginStorage.setItem("ShowOATabDocActive", pCheckIfOADoc()); //根据文件是否为OA文件来显示OA菜单
  97. wps.ribbonUI.Invalidate(); // 刷新Ribbon自定义按钮的状态
  98. }
  99. function pGetParamName(data, attr) {
  100. var start = data.indexOf(attr);
  101. data = data.substring(start + attr.length);
  102. return data;
  103. }
  104. /**
  105. * 从requst中获取文件名(确保请求中有filename这个参数)
  106. * @param {*} request
  107. * @param {*} url
  108. */
  109. function pGetFileName(request, url) {
  110. var disposition = request.getResponseHeader("Content-Disposition");
  111. var filename = "";
  112. if (disposition) {
  113. var matchs = pGetParamName(disposition, "filename=");
  114. if (matchs) {
  115. filename = decodeURIComponent(matchs);
  116. } else {
  117. filename = "petro" + Date.getTime();
  118. }
  119. } else {
  120. filename = url.substring(url.lastIndexOf("/") + 1);
  121. filename=filename.split("?")[0]
  122. }
  123. return filename;
  124. }
  125. function StringToUint8Array(string) {
  126. var binLen, buffer, chars, i, _i;
  127. binLen = string.length;
  128. buffer = new ArrayBuffer(binLen);
  129. chars = new Uint8Array(buffer);
  130. for (var i = 0; i < binLen; ++i) {
  131. chars[i] = String.prototype.charCodeAt.call(string, i);
  132. }
  133. return buffer;
  134. }
  135. function DownloadFile(url, callback) {
  136. // 需要根据业务实现一套
  137. var xhr = new XMLHttpRequest();
  138. xhr.onreadystatechange = function () {
  139. if (this.readyState == 4 && this.status == 200) {
  140. var path = wps.Env.GetTempPath() + "/" + pGetFileName(xhr, url);
  141. var reader = new FileReader();
  142. reader.onload = function () {
  143. wps.FileSystem.writeAsBinaryString(path, reader.result);
  144. callback(path);
  145. };
  146. reader.readAsBinaryString(xhr.response);
  147. }
  148. }
  149. xhr.open('GET', url);
  150. xhr.responseType = 'blob';
  151. xhr.send();
  152. }
  153. function UploadFile(strFileName, strPath, uploadPath, strFieldName, OnSuccess, OnFail) {
  154. var xhr = new XMLHttpRequest();
  155. xhr.open('POST', uploadPath);
  156. function KFormData() {
  157. this.fake = true;
  158. this.boundary = "--------FormData" + Math.random();
  159. this._fields = [];
  160. }
  161. KFormData.prototype.append = function (key, value) {
  162. this._fields.push([key, value]);
  163. }
  164. KFormData.prototype.toString = function () {
  165. var boundary = this.boundary;
  166. var body = "";
  167. this._fields.forEach(function (field) {
  168. body += "--" + boundary + "\r\n";
  169. if (field[1].name) {
  170. var file = field[1];
  171. body += "Content-Disposition: form-data; name=\"" + field[0] + "\"; filename=\"" + file.name + "\"\r\n";
  172. body += "Content-Type: " + file.type + "\r\n\r\n";
  173. body += file.getAsBinary() + "\r\n";
  174. } else {
  175. body += "Content-Disposition: form-data; name=\"" + field[0] + "\";\r\n\r\n";
  176. body += field[1] + "\r\n";
  177. }
  178. });
  179. body += "--" + boundary + "--";
  180. return body;
  181. }
  182. var fileData = wps.FileSystem.readAsBinaryString(strPath);
  183. var data = new KFormData();
  184. data.append('file', {
  185. name: strFileName,
  186. type: "application/octet-stream",
  187. getAsBinary: function () {
  188. return fileData;
  189. }
  190. });
  191. xhr.onreadystatechange = function () {
  192. if (xhr.readyState == 4) {
  193. if (xhr.status == 200)
  194. OnSuccess(xhr.response)
  195. else
  196. OnFail(xhr.response);
  197. }
  198. };
  199. xhr.setRequestHeader("Cache-Control", "no-cache");
  200. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  201. if (data.fake) {
  202. xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + data.boundary);
  203. var arr = StringToUint8Array(data.toString());
  204. xhr.send(arr);
  205. } else {
  206. xhr.send(data);
  207. }
  208. }