25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

common.js 8.6KB

5 ay önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. //扩展js string endwith,startwith方法
  60. String.prototype.endWith = function (str) {
  61. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  62. return false;
  63. if (this.substring(this.length - str.length) == str)
  64. return true;
  65. else
  66. return false;
  67. }
  68. String.prototype.startWith = function (str) {
  69. if (str == null || str == "" || this.length == 0 || str.length > this.length)
  70. return false;
  71. if (this.substr(0, str.length) == str)
  72. return true;
  73. else
  74. return false;
  75. }
  76. //UTF-16转UTF-8
  77. function utf16ToUtf8(s) {
  78. if (!s) {
  79. return;
  80. }
  81. var i, code, ret = [],
  82. len = s.length;
  83. for (i = 0; i < len; i++) {
  84. code = s.charCodeAt(i);
  85. if (code > 0x0 && code <= 0x7f) {
  86. //单字节
  87. //UTF-16 0000 - 007F
  88. //UTF-8 0xxxxxxx
  89. ret.push(s.charAt(i));
  90. } else if (code >= 0x80 && code <= 0x7ff) {
  91. //双字节
  92. //UTF-16 0080 - 07FF
  93. //UTF-8 110xxxxx 10xxxxxx
  94. ret.push(
  95. //110xxxxx
  96. String.fromCharCode(0xc0 | ((code >> 6) & 0x1f)),
  97. //10xxxxxx
  98. String.fromCharCode(0x80 | (code & 0x3f))
  99. );
  100. } else if (code >= 0x800 && code <= 0xffff) {
  101. //三字节
  102. //UTF-16 0800 - FFFF
  103. //UTF-8 1110xxxx 10xxxxxx 10xxxxxx
  104. ret.push(
  105. //1110xxxx
  106. String.fromCharCode(0xe0 | ((code >> 12) & 0xf)),
  107. //10xxxxxx
  108. String.fromCharCode(0x80 | ((code >> 6) & 0x3f)),
  109. //10xxxxxx
  110. String.fromCharCode(0x80 | (code & 0x3f))
  111. );
  112. }
  113. }
  114. return ret.join('');
  115. }
  116. //若要显示:当前日期加时间(如:200906121200)
  117. function currentTime() {
  118. var now = new Date();
  119. var year = now.getFullYear(); //年
  120. var month = now.getMonth() + 1; //月
  121. var day = now.getDate(); //日
  122. var hh = now.getHours(); //时
  123. var mm = now.getMinutes(); //分
  124. var clock = year + "";
  125. if (month < 10)
  126. clock += "0";
  127. clock += month + "";
  128. if (day < 10)
  129. clock += "0";
  130. clock += day + "";
  131. if (hh < 10)
  132. clock += "0";
  133. clock += hh + "";
  134. if (mm < 10) clock += '0';
  135. clock += mm;
  136. return (clock);
  137. }
  138. /**
  139. * 判断WPP中的文件个数是否为0,若为0则关闭WPP函数
  140. * @param {*} name
  141. */
  142. function closeWppIfNoDocument() {
  143. var wppApp = wps.WppApplication();
  144. var docs = wppApp.Presentations;
  145. if (docs && docs.Count == 0) {
  146. wppApp.Quit();
  147. }
  148. }
  149. function activeTab() {
  150. wps.ribbonUI.ActivateTab('WPSWorkExtTab');
  151. }
  152. function showOATab() {
  153. wps.PluginStorage.setItem("ShowOATabDocActive", pCheckIfOADoc()); //根据文件是否为OA文件来显示OA菜单
  154. wps.ribbonUI.Invalidate(); // 刷新Ribbon自定义按钮的状态
  155. }
  156. function pGetParamName(data, attr) {
  157. var start = data.indexOf(attr);
  158. data = data.substring(start + attr.length);
  159. return data;
  160. }
  161. /**
  162. * 从requst中获取文件名(确保请求中有filename这个参数)
  163. * @param {*} request
  164. * @param {*} url
  165. */
  166. function pGetFileName(request, url) {
  167. var disposition = request.getResponseHeader("Content-Disposition");
  168. var filename = "";
  169. if (disposition) {
  170. var matchs = pGetParamName(disposition, "filename=");
  171. if (matchs) {
  172. filename = decodeURIComponent(matchs);
  173. } else {
  174. filename = "petro" + Date.getTime();
  175. }
  176. } else {
  177. filename = url.substring(url.lastIndexOf("/") + 1);
  178. filename=filename.split("?")[0]
  179. }
  180. return filename;
  181. }
  182. function StringToUint8Array(string) {
  183. var binLen, buffer, chars, i, _i;
  184. binLen = string.length;
  185. buffer = new ArrayBuffer(binLen);
  186. chars = new Uint8Array(buffer);
  187. for (var i = 0; i < binLen; ++i) {
  188. chars[i] = String.prototype.charCodeAt.call(string, i);
  189. }
  190. return buffer;
  191. }
  192. function DownloadFile(url, callback) {
  193. // 需要根据业务实现一套
  194. var xhr = new XMLHttpRequest();
  195. xhr.onreadystatechange = function () {
  196. if (this.readyState == 4 && this.status == 200) {
  197. var path = wps.Env.GetTempPath() + "/" + pGetFileName(xhr, url);
  198. var reader = new FileReader();
  199. reader.onload = function () {
  200. wps.FileSystem.writeAsBinaryString(path, reader.result);
  201. callback(path);
  202. };
  203. reader.readAsBinaryString(xhr.response);
  204. }
  205. }
  206. xhr.open('GET', url);
  207. xhr.responseType = 'blob';
  208. xhr.send();
  209. }
  210. function UploadFile(strFileName, strPath, uploadPath, strFieldName, OnSuccess, OnFail) {
  211. var xhr = new XMLHttpRequest();
  212. xhr.open('POST', uploadPath);
  213. function KFormData() {
  214. this.fake = true;
  215. this.boundary = "--------FormData" + Math.random();
  216. this._fields = [];
  217. }
  218. KFormData.prototype.append = function (key, value) {
  219. this._fields.push([key, value]);
  220. }
  221. KFormData.prototype.toString = function () {
  222. var boundary = this.boundary;
  223. var body = "";
  224. this._fields.forEach(function (field) {
  225. body += "--" + boundary + "\r\n";
  226. if (field[1].name) {
  227. var file = field[1];
  228. body += "Content-Disposition: form-data; name=\"" + field[0] + "\"; filename=\"" + file.name + "\"\r\n";
  229. body += "Content-Type: " + file.type + "\r\n\r\n";
  230. body += file.getAsBinary() + "\r\n";
  231. } else {
  232. body += "Content-Disposition: form-data; name=\"" + field[0] + "\";\r\n\r\n";
  233. body += field[1] + "\r\n";
  234. }
  235. });
  236. body += "--" + boundary + "--";
  237. return body;
  238. }
  239. var fileData = wps.FileSystem.readAsBinaryString(strPath);
  240. var data = new KFormData();
  241. data.append('file', {
  242. name: strFileName,
  243. type: "application/octet-stream",
  244. getAsBinary: function () {
  245. return fileData;
  246. }
  247. });
  248. xhr.onreadystatechange = function () {
  249. if (xhr.readyState == 4) {
  250. if (xhr.status == 200)
  251. OnSuccess(xhr.response)
  252. else
  253. OnFail(xhr.response);
  254. }
  255. };
  256. xhr.setRequestHeader("Cache-Control", "no-cache");
  257. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  258. if (data.fake) {
  259. xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + data.boundary);
  260. var arr = StringToUint8Array(data.toString());
  261. xhr.send(arr);
  262. } else {
  263. xhr.send(data);
  264. }
  265. }