index.uts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // @ts-ignore
  2. import type { Callback } from '../../index.uts'
  3. // @ts-ignore
  4. import { parsePage } from '../util.uts'
  5. // @ts-ignore
  6. import { send } from '../../index.uts'
  7. import {
  8. connectSocket,
  9. FirstSocketTaskEmitterParams,
  10. firstSocketTaskEmitter,
  11. // @ts-ignore
  12. } from './Socket.uts'
  13. export const getPageStack = (callback: Callback): void => {
  14. callback(
  15. {
  16. // @ts-ignore
  17. pageStack: getCurrentPages().map((page: Page): UTSJSONObject => {
  18. return parsePage(page)
  19. }),
  20. },
  21. null
  22. )
  23. }
  24. export type GetCurrentPageParams = {
  25. // @ts-ignore
  26. callback: (result: UTSJSONObject | null, error: any | null) => void
  27. }
  28. // @ts-ignore
  29. function _getCurrentPage(): Page | null {
  30. // @ts-ignore
  31. const pages = getCurrentPages()
  32. return pages.length > 0 ? pages[pages.length - 1] : null
  33. }
  34. export const getCurrentPage = (params: GetCurrentPageParams): void => {
  35. const page = _getCurrentPage()
  36. const result = page != null ? parsePage(page) : null
  37. params.callback(result, null)
  38. }
  39. export type CallUniMethodParams = {
  40. method: string
  41. args: any[]
  42. }
  43. export const callUniMethod = (
  44. params: CallUniMethodParams,
  45. callback: Callback
  46. ): void => {
  47. const method = params.method
  48. const args = params.args
  49. const success = (result: any) => {
  50. const timeout = method == 'pageScrollTo' ? 350 : 0
  51. setTimeout(() => {
  52. callback({ result }, null)
  53. }, timeout)
  54. }
  55. const onApiCallback = (data: any | null, _: any | null) => {
  56. const id = args[0] as string
  57. send({ id, result: { method, data } })
  58. }
  59. switch (method) {
  60. case 'navigateTo':
  61. // @ts-ignore
  62. const _arg = new UTSJSONObject(args[0])
  63. // @ts-ignore
  64. uni.navigateTo({
  65. url: _arg['url'] as string,
  66. animationType:
  67. _arg['animationType'] != null
  68. ? (_arg['animationType'] as string)
  69. : 'pop-in',
  70. animationDuration:
  71. _arg['animationDuration'] != null
  72. ? (_arg['animationDuration'] as number)
  73. : 300,
  74. success,
  75. fail(error) {
  76. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  77. callback(null, error)
  78. },
  79. })
  80. break
  81. case 'redirectTo':
  82. // @ts-ignore
  83. uni.redirectTo({
  84. // @ts-ignore
  85. url: new UTSJSONObject(args[0])['url'] as string,
  86. success,
  87. fail(error) {
  88. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  89. callback(null, error)
  90. },
  91. })
  92. break
  93. case 'reLaunch':
  94. // @ts-ignore
  95. uni.reLaunch({
  96. // @ts-ignore
  97. url: new UTSJSONObject(args[0])['url'] as string,
  98. success,
  99. fail(error) {
  100. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  101. callback(null, error)
  102. },
  103. })
  104. break
  105. case 'navigateBack':
  106. // @ts-ignore
  107. const _arg = new UTSJSONObject(args[0])
  108. // @ts-ignore
  109. uni.navigateBack({
  110. animationType:
  111. _arg['animationType'] != null
  112. ? (_arg['animationType'] as string)
  113. : 'pop-out',
  114. animationDuration:
  115. _arg['animationDuration'] != null
  116. ? (_arg['animationDuration'] as number)
  117. : 300,
  118. success,
  119. fail(error) {
  120. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  121. callback(null, error)
  122. },
  123. })
  124. break
  125. case 'switchTab':
  126. // @ts-ignore
  127. uni.switchTab({
  128. // @ts-ignore
  129. url: new UTSJSONObject(args[0])['url'] as string,
  130. success,
  131. fail(error) {
  132. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  133. callback(null, error)
  134. },
  135. })
  136. break
  137. case 'getStorage':
  138. // @ts-ignore
  139. uni.getStorage({
  140. // @ts-ignore
  141. key: new UTSJSONObject(args[0])['key'] as string,
  142. success,
  143. fail(error) {
  144. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  145. callback(null, error)
  146. },
  147. })
  148. break
  149. case 'setStorage':
  150. // @ts-ignore
  151. const _arg = new UTSJSONObject(args[0])
  152. // @ts-ignore
  153. uni.setStorage({
  154. key: _arg['key'] as string,
  155. data: _arg['data']!,
  156. success,
  157. fail(error) {
  158. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  159. callback(null, error)
  160. },
  161. })
  162. break
  163. case 'getStorageSync':
  164. // @ts-ignore
  165. callback({ result: uni.getStorageSync(args[0] as string) }, null)
  166. break
  167. case 'setStorageSync':
  168. // @ts-ignore
  169. callback({ result: uni.setStorageSync(args[0] as string, args[1]) }, null)
  170. break
  171. case 'getStorageInfo':
  172. // @ts-ignore
  173. uni.getStorageInfo({
  174. success,
  175. fail(error) {
  176. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  177. callback(null, error)
  178. },
  179. })
  180. break
  181. case 'getStorageInfoSync':
  182. // @ts-ignore
  183. callback({ result: uni.getStorageInfoSync() }, null)
  184. break
  185. case 'removeStorage':
  186. // @ts-ignore
  187. uni.removeStorage({
  188. // @ts-ignore
  189. key: new UTSJSONObject(args[0])['key'] as string,
  190. success,
  191. fail(error) {
  192. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  193. callback(null, error)
  194. },
  195. })
  196. break
  197. case 'removeStorageSync':
  198. // @ts-ignore
  199. callback({ result: uni.removeStorageSync(args[0] as string) }, null)
  200. break
  201. case 'clearStorage':
  202. // @ts-ignore
  203. uni.clearStorage({
  204. success,
  205. fail(error) {
  206. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  207. callback(null, error)
  208. },
  209. })
  210. break
  211. case 'clearStorageSync':
  212. // @ts-ignore
  213. callback({ result: uni.clearStorageSync() }, null)
  214. break
  215. case 'showToast':
  216. // @ts-ignore
  217. const _arg = new UTSJSONObject(args[0])
  218. // @ts-ignore
  219. uni.showToast({
  220. title: _arg['title'] as string,
  221. icon: _arg['icon'] != null ? (_arg['icon'] as string) : 'success',
  222. image:
  223. _arg['image'] != null && _arg['image'] != ''
  224. ? (_arg['image'] as string)
  225. : null,
  226. mask: _arg['mask'] != null ? (_arg['mask'] as boolean) : false,
  227. duration:
  228. _arg['duration'] != null ? (_arg['duration'] as number) : 1500,
  229. position:
  230. _arg['position'] != null ? (_arg['position'] as string) : null,
  231. success,
  232. fail(error) {
  233. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  234. callback(null, error)
  235. },
  236. })
  237. break
  238. case 'hideToast':
  239. // @ts-ignore
  240. uni.hideToast()
  241. break
  242. case 'showLoading':
  243. // @ts-ignore
  244. const _arg = new UTSJSONObject(args[0])
  245. // @ts-ignore
  246. uni.showLoading({
  247. title: _arg['title'] as string,
  248. mask: _arg['mask'] != null ? (_arg['mask'] as boolean) : false,
  249. success,
  250. fail(error) {
  251. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  252. callback(null, error)
  253. },
  254. })
  255. break
  256. case 'hideLoading':
  257. // @ts-ignore
  258. uni.hideLoading()
  259. break
  260. case 'showModal':
  261. // @ts-ignore
  262. const _arg = new UTSJSONObject(args[0])
  263. // @ts-ignore
  264. uni.showModal({
  265. title: _arg['title'] != null ? (_arg['title'] as string) : null,
  266. content: _arg['content'] != null ? (_arg['content'] as string) : null,
  267. showCancel:
  268. _arg['showCancel'] != null ? (_arg['showCancel'] as boolean) : true,
  269. cancelText:
  270. _arg['cancelText'] != null ? (_arg['cancelText'] as string) : null,
  271. cancelColor:
  272. _arg['cancelColor'] != null ? (_arg['cancelColor'] as string) : null,
  273. confirmText:
  274. _arg['confirmText'] != null ? (_arg['confirmText'] as string) : null,
  275. confirmColor:
  276. _arg['confirmColor'] != null
  277. ? (_arg['confirmColor'] as string)
  278. : null,
  279. editable:
  280. _arg['editable'] != null ? (_arg['editable'] as boolean) : false,
  281. placeholderText:
  282. _arg['placeholderText'] != null
  283. ? (_arg['placeholderText'] as string)
  284. : null,
  285. success,
  286. fail(error) {
  287. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  288. callback(null, error)
  289. },
  290. })
  291. break
  292. case 'showActionSheet':
  293. // @ts-ignore
  294. const _arg = new UTSJSONObject(args[0])
  295. // @ts-ignore
  296. uni.showActionSheet({
  297. title: _arg['title'] != null ? (_arg['title'] as string) : null,
  298. // @ts-expect-error
  299. itemList: JSON.parse<string[]>(JSON.stringify(_arg['itemList']))!,
  300. itemColor:
  301. _arg['itemColor'] != null ? (_arg['itemColor'] as string) : null,
  302. success,
  303. fail(error) {
  304. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  305. callback(null, error)
  306. },
  307. })
  308. break
  309. case 'connectSocket':
  310. // @ts-ignore
  311. const _arg = new UTSJSONObject(args[0])
  312. connectSocket(_arg['id'] as string, _arg['url'] as string, callback)
  313. break
  314. case 'onSocketOpen':
  315. firstSocketTaskEmitter(
  316. { method: 'onOpen' } as FirstSocketTaskEmitterParams,
  317. onApiCallback
  318. )
  319. break
  320. case 'onSocketMessage':
  321. firstSocketTaskEmitter(
  322. { method: 'onMessage' } as FirstSocketTaskEmitterParams,
  323. onApiCallback
  324. )
  325. break
  326. case 'onSocketError':
  327. firstSocketTaskEmitter(
  328. { method: 'onError' } as FirstSocketTaskEmitterParams,
  329. onApiCallback
  330. )
  331. break
  332. case 'onSocketClose':
  333. firstSocketTaskEmitter(
  334. { method: 'onClose' } as FirstSocketTaskEmitterParams,
  335. onApiCallback
  336. )
  337. break
  338. case 'sendSocketMessage':
  339. firstSocketTaskEmitter(
  340. {
  341. method: 'send',
  342. // @ts-ignore
  343. data: new UTSJSONObject(args[0])['data'],
  344. } as FirstSocketTaskEmitterParams,
  345. callback
  346. )
  347. break
  348. case 'closeSocket':
  349. // @ts-ignore
  350. const _arg = new UTSJSONObject(args[0])
  351. firstSocketTaskEmitter(
  352. {
  353. method: 'close',
  354. code: _arg['code'] as number,
  355. reason: _arg['reason'] as string,
  356. } as FirstSocketTaskEmitterParams,
  357. callback
  358. )
  359. break
  360. case 'getSystemInfo':
  361. // @ts-ignore
  362. uni.getSystemInfo({
  363. success,
  364. fail(error) {
  365. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  366. callback(null, error)
  367. },
  368. })
  369. break
  370. case 'getSystemInfoSync':
  371. // @ts-ignore
  372. callback({ result: uni.getSystemInfoSync() }, null)
  373. break
  374. case 'getDeviceInfo':
  375. // @ts-ignore
  376. callback({ result: uni.getDeviceInfo() }, null)
  377. break
  378. case 'getSystemSetting':
  379. // @ts-ignore
  380. callback({ result: uni.getSystemSetting() }, null)
  381. break
  382. case 'getAppBaseInfo':
  383. // @ts-ignore
  384. callback({ result: uni.getAppBaseInfo() }, null)
  385. break
  386. case 'getAppAuthorizeSetting':
  387. // @ts-ignore
  388. callback({ result: uni.getAppAuthorizeSetting() }, null)
  389. break
  390. case 'openAppAuthorizeSetting':
  391. // @ts-ignore
  392. uni.openAppAuthorizeSetting({
  393. success,
  394. fail(error) {
  395. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  396. callback(null, error)
  397. },
  398. })
  399. break
  400. case 'pageScrollTo':
  401. // @ts-ignore
  402. const _arg = new UTSJSONObject(args[0])
  403. // @ts-ignore
  404. uni.pageScrollTo({
  405. scrollTop: _arg['scrollTop'] as number,
  406. duration: _arg['duration'] as number,
  407. success,
  408. fail(error) {
  409. error.errMsg = error.errMsg.replace(`${method}: fail `, '')
  410. callback(null, error)
  411. },
  412. })
  413. break
  414. default:
  415. callback(null, { errMsg: 'uni.' + method + ' not exists.' })
  416. break
  417. }
  418. }
  419. export type CaptureScreenshotParams = {
  420. id?: string | null
  421. fullPage?: boolean | null
  422. path?: string | null
  423. offsetX?: string | null
  424. offsetY?: string | null
  425. }
  426. export const captureScreenshot = (
  427. params: CaptureScreenshotParams,
  428. callback: Callback
  429. ): void => {
  430. const currentPage = _getCurrentPage()
  431. if (currentPage != null) {
  432. currentPage.$viewToTempFilePath({
  433. id: params.id,
  434. offsetX: params.offsetX !== null ? params.offsetX : '0',
  435. offsetY: params.offsetY !== null ? params.offsetY : '0',
  436. wholeContent: params.fullPage == true,
  437. path: params.path,
  438. success: (res) => {
  439. // @ts-ignore
  440. const fileManager = uni.getFileSystemManager()
  441. fileManager.readFile({
  442. encoding: 'base64',
  443. filePath: res.tempFilePath,
  444. success(readFileRes) {
  445. callback(
  446. {
  447. errMsg: 'screenshot:ok',
  448. tempFilePath: res.tempFilePath,
  449. data: readFileRes.data,
  450. },
  451. null
  452. )
  453. },
  454. fail(error) {
  455. callback(null, error)
  456. },
  457. // @ts-ignore
  458. } as ReadFileOptions)
  459. },
  460. fail: (error) => {
  461. callback(null, error)
  462. },
  463. })
  464. } else {
  465. callback(null, { errMsg: `currentPage is not found.` })
  466. }
  467. }