index.uts 6.7 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4
import {
  callUniMethod,
  CallUniMethodParams,
  captureScreenshot,
5
  CaptureScreenshotParams,
DCloud-WZF's avatar
DCloud-WZF 已提交
6 7
  getPageStack,
  getCurrentPage,
8
  GetCurrentPageParams
DCloud-WZF's avatar
DCloud-WZF 已提交
9
  // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
10
} from './apis/App/index.uts'
DCloud-WZF's avatar
DCloud-WZF 已提交
11
import {
DCloud-WZF's avatar
DCloud-WZF 已提交
12 13 14 15
  GetDataParams as PageGetDataParams,
  getData as pageGetData,
  SetDataParams as PageSetDataParams,
  setData as pageSetData,
DCloud-WZF's avatar
DCloud-WZF 已提交
16 17
  CallMethodParams,
  callMethod as pageCallMethod,
DCloud-WZF's avatar
DCloud-WZF 已提交
18 19 20
  GetElementParams as PageGetElementParams,
  getElement as pageGetElement,
  getElements as pageGetElements,
DCloud-WZF's avatar
DCloud-WZF 已提交
21 22
  GetWindowPropertiesParams,
  getWindowProperties,
DCloud-WZF's avatar
DCloud-WZF 已提交
23
  // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
24
} from './apis/Page.uts'
DCloud-WZF's avatar
DCloud-WZF 已提交
25
// @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
26
import { socketEmitter, SocketEmitterParams } from './apis/App/Socket.uts'
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40
import {
  GetElementParams as ElementGetElementParams,
  getElement as elementGetElement,
  getElements as elementGetElements,
  GetDOMPropertiesParams,
  getDOMProperties,
  GetPropertiesParams,
  getProperties,
  CallFunctionParams as ElementCallFunctionParams,
  callFunction as elementCallFunction,
  TapParams,
  tap,
  CallMethodParams as ElementCallMethodParams,
  callMethod as elementCallMethod,
DCloud-WZF's avatar
DCloud-WZF 已提交
41 42 43 44
  GetDataParams as ElementGetDataParams,
  getData as elementGetData,
  SetDataParams as ElementSetDataParams,
  setData as elementSetData,
DCloud-WZF's avatar
DCloud-WZF 已提交
45 46
  GetOffsetParams,
  getOffset,
47 48
  LongpressParams,
  longpress,
49 50
  HandleTouchEventParams,
  handleTouchEvent,
51 52
  GetAttributesParams,
  getAttributes,
DCloud-WZF's avatar
DCloud-WZF 已提交
53 54
  GetStylesParams,
  getStyles,
DCloud-WZF's avatar
DCloud-WZF 已提交
55
  // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
56
} from './apis/Element.uts'
DCloud-WZF's avatar
DCloud-WZF 已提交
57

DCloud-WZF's avatar
DCloud-WZF 已提交
58
// @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
59
let socketTask: SocketTask | null = null
fxy060608's avatar
fxy060608 已提交
60
const wsEndpoint = process.env.UNI_AUTOMATOR_WS_ENDPOINT
DCloud-WZF's avatar
DCloud-WZF 已提交
61

DCloud-WZF's avatar
DCloud-WZF 已提交
62
export function send(data: any) {
DCloud-WZF's avatar
DCloud-WZF 已提交
63
  // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
64 65 66
  socketTask?.send({ data: JSON.stringify(data) } as SendSocketMessageOptions)
}

DCloud-WZF's avatar
DCloud-WZF 已提交
67 68 69 70 71 72 73 74
export type Callback = (result: any | null, error: any | null) => void

type Msg = {
  id: string,
  method: string,
  params: any
}

DCloud-WZF's avatar
DCloud-WZF 已提交
75
export function onMessage(msg: string) {
DCloud-WZF's avatar
DCloud-WZF 已提交
76
  // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
77
  const json = JSON.parse<Msg>(msg)!
DCloud-WZF's avatar
DCloud-WZF 已提交
78
  const method = json.method
79 80 81 82
  if ((method == 'ping')) {
    send('pong')
    return
  }
DCloud-WZF's avatar
DCloud-WZF 已提交
83 84
  const params = JSON.stringify(json.params)
  const res = new Map<string, any | null>([['id', json.id]])
DCloud-WZF's avatar
DCloud-WZF 已提交
85
  try {
DCloud-WZF's avatar
DCloud-WZF 已提交
86 87 88
    const callback = (result?: any | null, error?: any | null) => {
      res.set('result', result)
      res.set('error', error)
DCloud-WZF's avatar
DCloud-WZF 已提交
89 90 91 92 93
      send(res)
    }
    if (method.startsWith('App.')) {
      switch (method) {
        case 'App.callUniMethod':
DCloud-WZF's avatar
DCloud-WZF 已提交
94
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
95
          callUniMethod(JSON.parse<CallUniMethodParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
96
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
97
        case 'App.captureScreenshot':
DCloud-WZF's avatar
DCloud-WZF 已提交
98
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
99
          captureScreenshot(JSON.parse<CaptureScreenshotParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
100 101
          break
        case 'App.getPageStack':
DCloud-WZF's avatar
DCloud-WZF 已提交
102
          getPageStack(callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
103 104
          break
        case 'App.getCurrentPage':
105
          getCurrentPage({ callback } as GetCurrentPageParams)
DCloud-WZF's avatar
DCloud-WZF 已提交
106
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
107
        case 'App.socketEmitter':
DCloud-WZF's avatar
DCloud-WZF 已提交
108
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
109
          socketEmitter(JSON.parse<SocketEmitterParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
110
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
111 112 113 114
      }
    } else if (method.startsWith('Page.')) {
      switch (method) {
        case 'Page.getData':
DCloud-WZF's avatar
DCloud-WZF 已提交
115
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
116
          pageGetData(JSON.parse<PageGetDataParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
117 118
          break
        case 'Page.setData':
DCloud-WZF's avatar
DCloud-WZF 已提交
119
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
120
          pageSetData(JSON.parse<PageSetDataParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
121
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
122
        case 'Page.callMethod':
DCloud-WZF's avatar
DCloud-WZF 已提交
123
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
124
          pageCallMethod(JSON.parse<CallMethodParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
125
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
126
        case 'Page.getElement':
DCloud-WZF's avatar
DCloud-WZF 已提交
127
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
128
          pageGetElement(JSON.parse<PageGetElementParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
129 130
          break
        case 'Page.getElements':
DCloud-WZF's avatar
DCloud-WZF 已提交
131
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
132
          pageGetElements(JSON.parse<PageGetElementParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
133
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
134
        case 'Page.getWindowProperties':
DCloud-WZF's avatar
DCloud-WZF 已提交
135
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
136
          getWindowProperties(JSON.parse<GetWindowPropertiesParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
137
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
138 139 140 141
      }
    } else if (method.startsWith('Element.')) {
      switch (method) {
        case 'Element.getElement':
DCloud-WZF's avatar
DCloud-WZF 已提交
142
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
143
          elementGetElement(JSON.parse<ElementGetElementParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
144 145
          break
        case 'Element.getElements':
DCloud-WZF's avatar
DCloud-WZF 已提交
146
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
147
          elementGetElements(JSON.parse<ElementGetElementParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
148 149
          break
        case 'Element.getDOMProperties':
DCloud-WZF's avatar
DCloud-WZF 已提交
150
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
151
          getDOMProperties(JSON.parse<GetDOMPropertiesParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
152 153
          break
        case 'Element.getProperties':
DCloud-WZF's avatar
DCloud-WZF 已提交
154
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
155
          getProperties(JSON.parse<GetPropertiesParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
156 157
          break
        case 'Element.callFunction':
DCloud-WZF's avatar
DCloud-WZF 已提交
158
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
159
          elementCallFunction(JSON.parse<ElementCallFunctionParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
160 161
          break
        case 'Element.tap':
DCloud-WZF's avatar
DCloud-WZF 已提交
162
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
163
          tap(JSON.parse<TapParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
164 165
          break
        case 'Element.callMethod':
DCloud-WZF's avatar
DCloud-WZF 已提交
166
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
167
          elementCallMethod(JSON.parse<ElementCallMethodParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
168
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
169
        case 'Element.getData':
DCloud-WZF's avatar
DCloud-WZF 已提交
170
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
171
          elementGetData(JSON.parse<ElementGetDataParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
172 173
          break
        case 'Element.setData':
DCloud-WZF's avatar
DCloud-WZF 已提交
174
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
175
          elementSetData(JSON.parse<ElementSetDataParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
176
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
177
        case 'Element.getOffset':
DCloud-WZF's avatar
DCloud-WZF 已提交
178
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
179
          getOffset(JSON.parse<GetOffsetParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
180
          break
181
        case 'Element.longpress':
DCloud-WZF's avatar
DCloud-WZF 已提交
182
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
183
          longpress(JSON.parse<LongpressParams>(params)!, callback)
184
          break
185 186 187
        case 'Element.touchstart':
        case 'Element.touchmove':
        case 'Element.touchend':
DCloud-WZF's avatar
DCloud-WZF 已提交
188
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
189
          handleTouchEvent(JSON.parse<HandleTouchEventParams>(params)!, method.split('.')[1], callback)
190
          break
191
        case 'Element.getAttributes':
DCloud-WZF's avatar
DCloud-WZF 已提交
192
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
193
          getAttributes(JSON.parse<GetAttributesParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
194 195
          break
        case 'Element.getStyles':
DCloud-WZF's avatar
DCloud-WZF 已提交
196
          // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
197
          getStyles(JSON.parse<GetStylesParams>(params)!, callback)
DCloud-WZF's avatar
DCloud-WZF 已提交
198
          break
DCloud-WZF's avatar
DCloud-WZF 已提交
199 200 201
      }
    }
  } catch (error) {
DCloud-WZF's avatar
DCloud-WZF 已提交
202
    res.set('error', { message: error.stackTraceToString() })
DCloud-WZF's avatar
DCloud-WZF 已提交
203 204 205 206
    send(res)
  }
}

fxy060608's avatar
fxy060608 已提交
207
export function initAutomator() {
DCloud-WZF's avatar
DCloud-WZF 已提交
208
  // @ts-ignore
DCloud-WZF's avatar
DCloud-WZF 已提交
209 210 211
  socketTask = uni.connectSocket({
    url: wsEndpoint
  });
212 213 214
  socketTask!.onMessage((res) => {
    onMessage(res.data as string)
  })
DCloud-WZF's avatar
DCloud-WZF 已提交
215
  socketTask!.onOpen((_) => {
DCloud-WZF's avatar
DCloud-WZF 已提交
216
    console.warn("automator.onOpen")
DCloud-WZF's avatar
DCloud-WZF 已提交
217 218 219 220 221 222 223
  })
  socketTask!.onError((err) => {
    console.warn(`automator.onError: ${JSON.stringify(err)}`);
  })
  socketTask!.onClose((_) => {
    console.warn("automator.onClose");
  })
fxy060608's avatar
fxy060608 已提交
224
}