index.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* eslint-disable */
  2. interface HTML5 extends Window {
  3. plus?: any;
  4. }
  5. export default new (class {
  6. private h5 = new Promise<HTML5>((resolve) => {
  7. document.addEventListener('plusready', () => {
  8. resolve(window);
  9. })
  10. })
  11. constructor() {
  12. // 监听返回按钮事件
  13. this.onPlusReady((plus) => {
  14. let firstBack = true;
  15. const webview = plus.webview.currentWebview();
  16. plus.key.addEventListener('backbutton', () => {
  17. webview.canBack((e: any) => {
  18. // 判断能否继续返回
  19. if (e.canBack) {
  20. webview.back();
  21. } else {
  22. // 1秒内连续两次按返回键退出应用
  23. if (firstBack) {
  24. firstBack = false;
  25. plus.nativeUI.toast('再按一次退出应用');
  26. setTimeout(() => {
  27. firstBack = true;
  28. }, 1000);
  29. } else {
  30. plus.runtime.quit();
  31. }
  32. }
  33. })
  34. })
  35. })
  36. }
  37. onPlusReady(callback: (plus: any) => void) {
  38. this.h5.then((res) => {
  39. callback(res.plus)
  40. })
  41. }
  42. hasPlus() {
  43. return Object.prototype.hasOwnProperty.call(window, 'plus')
  44. }
  45. /**
  46. * 客户端的版本名称
  47. * @returns
  48. */
  49. getVersion(callback: (version: string) => void) {
  50. this.onPlusReady((plus) => {
  51. plus.runtime.getProperty(plus.runtime.appid, (info: { version: string }) => {
  52. callback(info.version)
  53. })
  54. })
  55. }
  56. /**
  57. * 获取客户端的版本号
  58. * @returns
  59. */
  60. getVersionCode(callback: (versionCode: string) => void) {
  61. this.onPlusReady((plus) => {
  62. plus.runtime.getProperty(plus.runtime.appid, (info: { versionCode: string }) => {
  63. callback(info.versionCode)
  64. })
  65. })
  66. }
  67. /**
  68. * 获取状态栏高度
  69. * @param callback
  70. */
  71. getStatusBarHeight(callback: (statusbarHeight: number) => void) {
  72. this.onPlusReady((plus) => {
  73. const height = plus.navigator.getStatusbarHeight();
  74. callback(height);
  75. })
  76. }
  77. /**
  78. * 设置状态栏文字颜色
  79. * @param color dark - 暗色,light - 亮色
  80. */
  81. setStatusBarStyle(color: 'dark' | 'light') {
  82. this.onPlusReady((plus) => {
  83. plus.navigator.setStatusBarStyle(color);
  84. })
  85. }
  86. /**
  87. * 隐藏状态栏
  88. */
  89. hideStatusBar() {
  90. this.onPlusReady((plus) => {
  91. plus.navigator.setFullscreen(true);
  92. })
  93. }
  94. /**
  95. * 显示状态栏
  96. */
  97. showStatusBar() {
  98. this.onPlusReady((plus) => {
  99. plus.navigator.setFullscreen(false);
  100. })
  101. }
  102. /**
  103. * 设置应用全屏
  104. */
  105. setFullSreen() {
  106. this.onPlusReady((plus) => {
  107. this.hideStatusBar();
  108. plus.navigator.hideSystemNavigation();
  109. })
  110. }
  111. /**
  112. * 应用退出全屏
  113. */
  114. exitFullSreen() {
  115. this.onPlusReady((plus) => {
  116. this.showStatusBar();
  117. plus.navigator.showSystemNavigation();
  118. })
  119. }
  120. /**
  121. * 更新应用
  122. * @param url
  123. */
  124. updateApp(url: string) {
  125. this.onPlusReady((plus) => {
  126. const dtask = plus.downloader.createDownload(
  127. url,
  128. {
  129. filename: ''
  130. },
  131. function (d: { filename: string }, status: number) {
  132. if (status == 200) {
  133. // 当前下载的状态
  134. installApp(d.filename) // 调用安装的方法
  135. } else {
  136. //plus.nativeUI.alert('下载失败')
  137. }
  138. }
  139. )
  140. dtask.start() // 开启下载的任务
  141. // app自动更新进度
  142. dtask.addEventListener('statechanged', function (task: { state: number }) {
  143. // 给下载任务设置一个监听 并根据状态 做操作
  144. switch (task.state) {
  145. case 1:
  146. console.log('正在下载')
  147. break
  148. case 2:
  149. console.log('已连接到服务器')
  150. break
  151. case 3:
  152. // console.log(task)
  153. // console.log(task.downloadedSize)//当前的大
  154. // console.log(task.totalSize)//安装包的大小
  155. }
  156. })
  157. // 自动更新
  158. function installApp(path: string) {
  159. plus.nativeUI.showWaiting('正在更新...')
  160. plus.runtime.install(
  161. path,
  162. {
  163. // true表示强制安装,不进行版本号的校验;false则需要版本号校验,如果将要安装应用的版本号不高于现有应用的版本号则终止安装,并返回安装失败。 仅安装wgt和wgtu时生效,默认值 false
  164. force: false
  165. },
  166. function () {
  167. plus.nativeUI.closeWaiting()
  168. console.log('更新成功!')
  169. plus.runtime.restart()
  170. },
  171. function (e: { message: string }) {
  172. plus.nativeUI.closeWaiting()
  173. plus.nativeUI.alert('更新失败:' + e.message)
  174. }
  175. )
  176. }
  177. })
  178. }
  179. /**
  180. * 保存图片到相册
  181. * @param base64Data
  182. */
  183. saveImage(base64Data: string, fileName?: string) {
  184. this.onPlusReady((plus) => {
  185. const bitmap = new plus.nativeObj.Bitmap()
  186. const filename = fileName ?? new Date().getTime()
  187. bitmap.loadBase64Data(base64Data)
  188. bitmap.save(`_doc/${filename}.jpg`, { overwrite: true, quality: 100, }, (e: Event) => {
  189. //保存到系统相册
  190. plus.gallery.save(
  191. e.target,
  192. () => {
  193. //销毁Bitmap图片
  194. bitmap.clear();
  195. plus.nativeUI.toast('已保存到相册中')
  196. },
  197. () => {
  198. //销毁Bitmap图片
  199. bitmap.clear();
  200. plus.nativeUI.toast('保存失败')
  201. }
  202. )
  203. }, (err: { message: string }) => {
  204. plus.nativeUI.toast(err.message)
  205. })
  206. })
  207. }
  208. /**
  209. * 打开本地文件(安卓正式包可能无效)
  210. * https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.openFile
  211. * @param filePath
  212. */
  213. openFile(filePath: string) {
  214. if (this.hasPlus()) {
  215. this.onPlusReady((plus) => {
  216. plus.runtime.openFile('_www/' + filePath)
  217. })
  218. } else {
  219. window.open(filePath)
  220. }
  221. }
  222. /**
  223. * https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.openURL
  224. * @param url
  225. */
  226. openURL(url: string) {
  227. if (this.hasPlus()) {
  228. this.onPlusReady((plus) => {
  229. plus.runtime.openURL(url)
  230. })
  231. } else {
  232. window.open(url)
  233. }
  234. }
  235. /**
  236. * 读取本地文件内容
  237. * @param filePath
  238. * @returns
  239. */
  240. getLocalFileContent(filePath: string) {
  241. return new Promise<any>((resolve, reject) => {
  242. this.onPlusReady((plus) => {
  243. plus.io.resolveLocalFileSystemURL('_www/' + filePath, (entry: any) => {
  244. entry.file((file: any) => {
  245. const fileReader = new plus.io.FileReader()
  246. fileReader.readAsText(file, 'utf-8')
  247. fileReader.onloadend = (evt: any) => {
  248. resolve(evt.target.result)
  249. }
  250. })
  251. }, (e: any) => {
  252. reject(e.message)
  253. })
  254. })
  255. })
  256. }
  257. })