protocols.js 6.8 KB
Newer Older
X
xiaoyucoding 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
const TODOS = [ // 不支持的 API 列表
  'hideTabBar',
  'hideTabBarRedDot',
  'removeTabBarBadge',
  'setTabBarBadge',
  'setTabBarItem',
  'setTabBarStyle',
  'showTabBar',
  'showTabBarRedDot',
  'startPullDownRefresh',
  'saveImageToPhotosAlbum',
  'getRecorderManager',
  'getBackgroundAudioManager',
  'createInnerAudioContext',
  'chooseVideo',
  'saveVideoToPhotosAlbum',
  'createVideoContext',
  'openDocument',
  'startAccelerometer',
  'startCompass',
  'addPhoneContact',
  'createIntersectionObserver'
fxy060608's avatar
fxy060608 已提交
23 24
]

X
xiaoyucoding 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
function _handleNetworkInfo (result) {
  switch (result.networkType) {
    case 'NOTREACHABLE':
      result.networkType = 'none'
      break
    case 'WWAN':
      // TODO ?
      result.networkType = '3g'
      break
    default:
      result.networkType = result.networkType.toLowerCase()
      break
  }
X
xiaoyucoding 已提交
38
  return {}
X
xiaoyucoding 已提交
39 40
}

X
xiaoyucoding 已提交
41 42 43 44 45 46 47 48
function _handleSystemInfo (result) {
  let platform = result.platform ? result.platform.toLowerCase() : 'devtools'
  if (!~['android', 'ios'].indexOf(platform)) {
    platform = 'devtools'
  }
  result.platform = platform
}

X
xiaoyucoding 已提交
49 50 51 52 53 54
const protocols = { // 需要做转换的 API 列表
  returnValue (methodName, res) { // 通用 returnValue 解析
    if (res.error || res.errorMessage) {
      res.errMsg = `${methodName}:fail ${res.errorMessage || res.error}`
      delete res.error
      delete res.errorMessage
X
xiaoyucoding 已提交
55 56
    } else {
      res.errMsg = `${methodName}:ok`
X
xiaoyucoding 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    }
    return res
  },
  request: {
    name: 'httpRequest',
    args (fromArgs) {
      if (!fromArgs.header) { // 默认增加 header 参数,方便格式化 content-type
        fromArgs.header = {}
      }
      return {
        header (header = {}, toArgs) {
          const headers = {
            'content-type': 'application/json'
          }
          Object.keys(header).forEach(key => {
            headers[key.toLocaleLowerCase()] = header[key]
          })
          return {
            name: 'headers',
            value: headers
          }
        },
        method: 'method', // TODO 支付宝小程序仅支持 get,post
        responseType: false
      }
    },
    returnValue: {
      status: 'statusCode',
      headers: 'header'
    }
  },
  setNavigationBarColor: {
    name: 'setNavigationBar',
    args: {
      frontColor: false,
      animation: false
    }
  },
  setNavigationBarTitle: {
    name: 'setNavigationBar'
  },
  showModal ({
    showCancel = true
  } = {}) {
    if (showCancel) {
      return {
        name: 'confirm',
        args: {
          cancelColor: false,
          confirmColor: false,
          cancelText: 'cancelButtonText',
          confirmText: 'confirmButtonText'
        },
        returnValue (fromRes, toRes) {
          toRes.confirm = fromRes.confirm
          toRes.cancel = !fromRes.confirm
        }
      }
    }
    return {
      name: 'alert',
      args: {
        confirmColor: false,
        confirmText: 'buttonText'
      },
      returnValue (fromRes, toRes) {
        toRes.confirm = true
        toRes.cancel = false
      }
    }
  },
  showToast ({
    icon = 'success'
  } = {}) {
    const args = {
      title: 'content',
      icon: 'type',
      duration: false,
      image: false,
      mask: false
    }
    if (icon === 'loading') {
      return {
        name: 'showLoading',
        args
      }
    }
    return {
      name: 'showToast',
      args
    }
  },
  showActionSheet: {
    name: 'showActionSheet',
    args: {
      itemList: 'items',
      itemColor: false
    },
    returnValue: {
      index: 'tapIndex'
    }
  },
X
xiaoyucoding 已提交
159 160 161 162 163 164
  showLoading: {
    args: {
      title: 'content',
      mask: false
    }
  },
X
xiaoyucoding 已提交
165 166 167 168 169 170 171 172
  uploadFile: {
    args: {
      name: 'fileName'
    }
    // 从测试结果看,是有返回对象的,文档上没有说明。
  },
  downloadFile: {
    returnValue: {
X
xiaoyucoding 已提交
173
      apFilePath: 'tempFilePath'
X
xiaoyucoding 已提交
174 175 176 177 178 179 180 181 182 183 184
    }
  },
  connectSocket: {
    args: {
      method: false,
      protocols: false
    }
    // TODO 有没有返回值还需要测试下
  },
  chooseImage: {
    returnValue: {
X
xiaoyucoding 已提交
185
      apFilePaths: 'tempFilePaths'
X
xiaoyucoding 已提交
186 187 188 189
    }
  },
  previewImage: {
    args (fromArgs) {
X
xiaoyucoding 已提交
190 191
      // 支付宝小程序的 current 是索引值,而非图片地址。
      if (fromArgs.current && Array.isArray(fromArgs.urls)) {
X
xiaoyucoding 已提交
192
        const index = fromArgs.urls.indexOf(fromArgs.current)
X
xiaoyucoding 已提交
193
        fromArgs.current = ~index ? index : 0
X
xiaoyucoding 已提交
194
      }
X
xiaoyucoding 已提交
195
      return {
X
xiaoyucoding 已提交
196 197
        indicator: false,
        loop: false
X
xiaoyucoding 已提交
198
      }
X
xiaoyucoding 已提交
199 200 201 202 203 204 205
    }
  },
  saveFile: {
    args: {
      tempFilePath: 'apFilePath'
    },
    returnValue: {
X
xiaoyucoding 已提交
206
      apFilePath: 'savedFilePath'
X
xiaoyucoding 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219
    }
  },
  getSavedFileInfo: {
    args: {
      filePath: 'apFilePath'
    },
    returnValue (result) {
      if (result.fileList && result.fileList.length) {
        result.fileList.forEach(file => {
          file.filePath = file.apFilePath
          delete file.apFilePath
        })
      }
X
xiaoyucoding 已提交
220
      return {}
X
xiaoyucoding 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    }
  },
  removeSavedFile: {
    args: {
      filePath: 'apFilePath'
    }
  },
  getLocation: {
    args: {
      type: false,
      altitude: false
    }
  },
  openLocation: {
    args: {
      // TODO address 参数在阿里上是必传的
    }
  },
  getNetworkType: {
    returnValue: _handleNetworkInfo
  },
  onNetworkStatusChange: {
    returnValue: _handleNetworkInfo
  },
  stopAccelerometer: {
    name: 'offAccelerometerChange'
  },
  stopCompass: {
    name: 'offCompassChange'
  },
  scanCode: {
    name: 'scan',
    args: {
      onlyFromCamera: 'hideAlbum',
      scanType: false
X
xiaoyucoding 已提交
256 257 258
    },
    returnValue: {
      code: 'result'
X
xiaoyucoding 已提交
259 260 261 262 263 264 265 266 267 268 269
    }
  },
  setClipboardData: {
    name: 'setClipboard',
    args: {
      data: 'text'
    }
  },
  getClipboardData: {
    name: 'getClipboard',
    returnValue: {
X
xiaoyucoding 已提交
270
      text: 'data'
X
xiaoyucoding 已提交
271 272 273 274 275 276
    }
  },
  pageScrollTo: {
    args: {
      duration: false
    }
X
xiaoyucoding 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
  },
  login: {
    name: 'getAuthCode',
    returnValue (result) {
      result.code = result.authCode
    }
  },
  getUserInfo: {
    name: 'getAuthUserInfo',
    returnValue (result) {
      result.userInfo = {
        nickName: result.nickName,
        avatarUrl: result.avatar
      }
    }
  },
  requestPayment: {
    name: 'tradePay',
    args: {
      orderInfo: 'orderStr'
    }
X
xiaoyucoding 已提交
298 299 300 301 302 303 304
  },
  getBLEDeviceServices: {
    returnValue (result) {
      result.services.forEach((item) => {
        item.uuid = item.serviceId
      })
    }
X
xiaoyucoding 已提交
305 306 307 308 309
  },
  makePhoneCall: {
    args: {
      phoneNumber: 'number'
    }
X
xiaoyucoding 已提交
310 311 312
  },
  stopGyroscope: {
    name: 'offGyroscopeChange'
X
xiaoyucoding 已提交
313 314 315 316 317 318
  },
  getSystemInfo: {
    returnValue: _handleSystemInfo
  },
  getSystemInfoSync: {
    returnValue: _handleSystemInfo
X
xiaoyucoding 已提交
319
  }
fxy060608's avatar
fxy060608 已提交
320 321 322
}

TODOS.forEach(todoApi => {
X
xiaoyucoding 已提交
323
  protocols[todoApi] = false
fxy060608's avatar
fxy060608 已提交
324 325 326
})

export default protocols