my.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. const utils = require("./utils");
  2. const propsPolyfill = require("./propsPolyfill");
  3. const descObj = require("./desc.js");
  4. const apiObj = {startBeaconDiscovery:{fn: function fn(obj = {}) {
  5. const startBeaconDiscoveryParams = descObj.startBeaconDiscovery.body.params.props
  6. const params = utils.defineGetter(obj, startBeaconDiscoveryParams, (_obj, prop) => {
  7. utils.warn(
  8. `startBeaconDiscovery的参数不支持 ${prop} 属性!`,
  9. {
  10. apiName: `startBeaconDiscovery/${prop}`,
  11. errorType: startBeaconDiscoveryParams[prop].type,
  12. type: 'api',
  13. },
  14. )
  15. })
  16. return my.startBeaconDiscovery(params)
  17. },},stopBeaconDiscovery:{fn: function fn(obj = {}) {
  18. my.stopBeaconDiscovery(obj)
  19. },},onBeaconUpdate:{fn: function fn(cb) {
  20. return my.onBeaconUpdate({
  21. success: cb,
  22. fail: cb,
  23. })
  24. },},onBeaconServiceChange:{fn: function fn(cb) {
  25. return my.onBeaconServiceChange({
  26. success: cb,
  27. fail: cb,
  28. complete: cb,
  29. })
  30. },},getBeacons:{fn: function fn(obj = {}) {
  31. return my.getBeacons(obj)
  32. },},writeBLECharacteristicValue:{fn: function fn(obj = {}) {
  33. if (obj.value) {
  34. obj.value = utils.ab2hex(obj.value)
  35. }
  36. my.writeBLECharacteristicValue(obj)
  37. },},createBLEConnection:{fn: function fn(obj = {}) {
  38. return my.connectBLEDevice(obj)
  39. },},closeBLEConnection:{fn: function fn(obj = {}) {
  40. return my.disconnectBLEDevice(obj)
  41. },},onBLEConnectionStateChange:{fn: function fn(obj = {}) {
  42. return my.onBLEConnectionStateChanged(obj)
  43. },},onBLECharacteristicValueChange:{fn: function fn(cb) {
  44. my.onBLECharacteristicValueChange((res) => {
  45. res.value = utils.changeType(res.value)
  46. cb && cb(res)
  47. })
  48. },},getBLEDeviceServices:{fn: function fn(obj = {}) {
  49. my.getBLEDeviceServices({
  50. ...obj,
  51. success: (res) => {
  52. if (res.services) {
  53. res.services.forEach((item) => {
  54. item.uuid = item.serviceId
  55. delete item.serviceId
  56. })
  57. }
  58. obj.success && obj.success(res)
  59. },
  60. })
  61. },},getBLEDeviceCharacteristics:{fn: function fn(obj = {}) {
  62. my.getBLEDeviceCharacteristics({
  63. ...obj,
  64. success: (res) => {
  65. if (res.characteristics) {
  66. res.characteristics.forEach((item) => {
  67. item.uuid = item.characteristicId
  68. delete item.characteristicId
  69. })
  70. }
  71. obj.success && obj.success(res)
  72. },
  73. })
  74. },},addPhoneContact:{fn: function fn(obj = {}) {
  75. if (obj.weChatNumber) {
  76. obj.alipayAccount = obj.weChatNumber
  77. }
  78. my.addPhoneContact(obj)
  79. },},startBluetoothDevicesDiscovery:{fn: function fn(obj = {}) {
  80. if (obj.interval) {
  81. obj.interval = Math.round(obj.interval)
  82. }
  83. my.startBluetoothDevicesDiscovery(obj)
  84. },},onBluetoothDeviceFound:{fn: function fn(cb) {
  85. const onBluetoothDeviceFoundProps = descObj.onBluetoothDeviceFound.body.returnValue.props
  86. my.onBluetoothDeviceFound((res) => {
  87. const arr = res.devices.map((item) => {
  88. item.advertisData = utils.changeType(item.advertisData)
  89. return utils.defineGetter(item, onBluetoothDeviceFoundProps, (obj, prop) => {
  90. utils.warn(
  91. `onBluetoothDeviceFound的返回值不支持 ${prop} 属性!`,
  92. {
  93. apiName: `onBluetoothDeviceFound/${prop}`,
  94. errorType: onBluetoothDeviceFoundProps[prop].type,
  95. type: 'api',
  96. },
  97. )
  98. })
  99. })
  100. res.devices = arr
  101. cb && cb(res)
  102. })
  103. },},getBluetoothDevices:{fn: function fn(obj = {}) {
  104. const getBluetoothDevicesProps = descObj.getBluetoothDevices.body.returnValue.props.devices.props
  105. my.getBluetoothDevices({
  106. ...obj,
  107. success: (res) => {
  108. const arr = res.devices.map((item) => {
  109. item.advertisData = utils.changeType(item.advertisData)
  110. return utils.defineGetter(item, getBluetoothDevicesProps, (_obj, prop) => {
  111. utils.warn(
  112. `getBluetoothDevices的返回值不支持 ${prop} 属性!`,
  113. {
  114. apiName: `getBluetoothDevices/${prop}`,
  115. errorType: getBluetoothDevicesProps[prop].type,
  116. type: 'api',
  117. },
  118. )
  119. })
  120. })
  121. res.devices = arr
  122. obj.success && obj.success(res)
  123. },
  124. })
  125. },},setClipboardData:{fn: function fn(obj = {}) {
  126. if (obj.data) {
  127. obj.text = obj.data
  128. delete obj.data
  129. }
  130. my.setClipboard(obj)
  131. },},getClipboardData:{fn: function fn(obj = {}) {
  132. my.getClipboard({
  133. ...obj,
  134. success: (res) => {
  135. res.data = res.text
  136. delete res.text
  137. obj.success && obj.success(res)
  138. },
  139. })
  140. },},onNetworkStatusChange:{fn: function fn(cb) {
  141. my.onNetworkStatusChange((res) => {
  142. res.networkType = res.networkType.toLowerCase()
  143. const typeObjMap = {
  144. unknown: 'unknown',
  145. wifi: 'wifi',
  146. '2g': '2g',
  147. '3g': '3g',
  148. '4g': '4g',
  149. }
  150. if (res && !res.isConnected) {
  151. res.networkType = 'none'
  152. } else {
  153. res.networkType = typeObjMap[res.networkType] || res.networkType
  154. }
  155. cb && cb(res)
  156. })
  157. },},setScreenBrightness:{fn: function fn(obj = {}) {
  158. if (obj.value) {
  159. obj.brightness = obj.value
  160. delete obj.value
  161. }
  162. my.setScreenBrightness(obj)
  163. },},getScreenBrightness:{fn: function fn(obj = {}) {
  164. my.getScreenBrightness({
  165. success: (res) => {
  166. res.value = res.brightness
  167. delete res.brightness
  168. obj.success && obj.success(res)
  169. },
  170. fail: (res) => {
  171. obj.fail && obj.fail(res)
  172. },
  173. })
  174. },},scanCode:{fn: function fn(obj = {}) {
  175. obj.hideAlbum = obj.onlyFromCamera || false
  176. my.scan(obj)
  177. },},stopGyroscope:{fn: function fn(obj = {}) {
  178. const stopGyroscopeParams = descObj.stopGyroscope.body.params.props
  179. const params = utils.defineGetter(obj, stopGyroscopeParams, (_obj, prop) => {
  180. utils.warn(
  181. `stopGyroscope的参数不支持 ${prop} 属性!`,
  182. {
  183. apiName: `stopGyroscope${prop}`,
  184. errorType: stopGyroscopeParams[prop].type,
  185. type: 'api',
  186. },
  187. )
  188. })
  189. return my.offGyroscopeChange(params)
  190. },},onCompassChange:{fn: function fn(cb) {
  191. const onCompassChangeReturnValue = descObj.onCompassChange.body.returnValue.props
  192. my.onCompassChange((res) => {
  193. const _res = utils.defineGetter(res, onCompassChangeReturnValue, (obj, prop) => {
  194. utils.warn(
  195. `onCompassChange的返回值不支持 ${prop} 属性!`,
  196. {
  197. apiName: `onCompassChange/${prop}`,
  198. errorType: onCompassChangeReturnValue[prop].type,
  199. type: 'api',
  200. },
  201. )
  202. })
  203. cb && cb(_res)
  204. })
  205. },},stopCompass:{fn: function fn(obj = {}) {
  206. const stopCompassParams = descObj.stopCompass.body.params.props
  207. const params = utils.defineGetter(obj, stopCompassParams, (_obj, prop) => {
  208. utils.warn(
  209. `stopCompass的参数不支持 ${prop} 属性!`,
  210. {
  211. apiName: `stopCompass/${prop}`,
  212. errorType: stopCompassParams[prop].type,
  213. type: 'api',
  214. },
  215. )
  216. })
  217. return my.offCompassChange(params)
  218. },},stopAccelerometer:{fn: function fn(obj = {}) {
  219. const stopAccelerometerParams = descObj.stopAccelerometer.body.params.props
  220. const params = utils.defineGetter(obj, stopAccelerometerParams, (_obj, prop) => {
  221. utils.warn(
  222. `stopAccelerometer的参数不支持 ${prop} 属性!`,
  223. {
  224. apiName: `stopCompass/${prop}`,
  225. errorType: stopAccelerometerParams[prop].type,
  226. type: 'api',
  227. },
  228. )
  229. })
  230. return my.offAccelerometerChange(params)
  231. },},makePhoneCall:{fn: function fn(obj = {}) {
  232. const makePhoneCallParams = descObj.makePhoneCall.body.params.props
  233. if (obj.phoneNumber) {
  234. obj.number = obj.phoneNumber
  235. delete obj.phoneNumber
  236. }
  237. const params = utils.defineGetter(obj, makePhoneCallParams, (_obj, prop) => {
  238. utils.warn(
  239. `makePhoneCall的参数不支持 ${prop} 属性!`,
  240. {
  241. apiName: `makePhoneCall/${prop}`,
  242. errorType: makePhoneCallParams[prop].type,
  243. type: 'api',
  244. },
  245. )
  246. })
  247. return my.makePhoneCall(params)
  248. },},canIUse:{fn: function fn(params) {
  249. const paramsList = params.split('.')
  250. if (paramsList[1] && paramsList[1] === 'success') {
  251. paramsList[1] = 'return'
  252. }
  253. const str = paramsList.join('.')
  254. return my.canIUse(str)
  255. },},getSystemInfoSync:{fn: function fn() {
  256. let ret = my.getSystemInfoSync()
  257. const getSystemInfoSyncProps
  258. = descObj.getSystemInfoSync.body.returnValue.props
  259. ret = utils.defineGetter(ret, getSystemInfoSyncProps, (obj, prop) => {
  260. utils.warn(`getSystemInfoSync的返回值不支持 ${prop} 属性!`, {
  261. apiName: `getSystemInfoSync/${prop}`,
  262. errorType: getSystemInfoSyncProps[prop].type,
  263. type: 'api',
  264. })
  265. })
  266. /**
  267. * 处理Android屏幕宽度返回值
  268. */
  269. if (ret.platform.toLowerCase() === 'android') {
  270. ret.platform = 'android'
  271. ret.screenWidth /= ret.pixelRatio
  272. ret.screenHeight /= ret.pixelRatio
  273. } else if (ret.platform.toLowerCase() === 'ios') {
  274. ret.platform = 'ios'
  275. // mock的安全区位置信息(模拟器iphonex screenHeight:812)
  276. if (ret.screenHeight >= propsPolyfill.screenHeight) {
  277. ret.safeArea = propsPolyfill.safeArea
  278. ret.safeArea.bottom = ret.screenHeight - propsPolyfill.safeHeight
  279. } else {
  280. ret.safeArea = {
  281. bottom: ret.screenHeight,
  282. height: ret.screenHeight,
  283. left: 0,
  284. right: ret.screenWidth,
  285. top: ret.statusBarHeight,
  286. width: ret.screenWidth,
  287. }
  288. }
  289. }
  290. // mock的版本,确保微信小程序源码里读取后和目标版本匹配都能通过
  291. ret.SDKVersion = '10.0.0'
  292. return ret
  293. },},getSystemInfo:{fn: function fn(obj = {}) {
  294. const getSystemInfoProps = descObj.getSystemInfo.body.returnValue.props
  295. my.getSystemInfo({
  296. ...obj,
  297. success: (res) => {
  298. res = utils.defineGetter(res, getSystemInfoProps, (_obj, prop) => {
  299. utils.warn(`getSystemInfo的返回值不支持 ${prop} 属性!`, {
  300. apiName: `getSystemInfo/${prop}`,
  301. errorType: getSystemInfoProps[prop].type,
  302. type: 'api',
  303. })
  304. })
  305. /**
  306. * 处理Android屏幕宽度返回值
  307. */
  308. if (res.platform.toLowerCase() === 'android') {
  309. res.platform = 'android'
  310. res.screenWidth /= res.pixelRatio
  311. res.screenHeight /= res.pixelRatio
  312. } else if (res.platform.toLowerCase() === 'ios') {
  313. res.platform = 'ios'
  314. if (res.screenHeight >= propsPolyfill.screenHeight) {
  315. res.safeArea = propsPolyfill.safeArea
  316. res.safeArea.bottom = res.screenHeight - propsPolyfill.safeHeight
  317. } else {
  318. res.safeArea = {
  319. bottom: res.screenHeight,
  320. height: res.screenHeight,
  321. left: 0,
  322. right: res.screenWidth,
  323. top: res.statusBarHeight,
  324. width: res.screenWidth,
  325. }
  326. }
  327. }
  328. // mock的版本,确保微信小程序源码里读取后和目标版本匹配都能通过
  329. res.SDKVersion = '10.0.0'
  330. obj.success && obj.success(res)
  331. },
  332. })
  333. },},showToast:{fn: function fn(obj = {}) {
  334. const showToastProps = descObj.showToast.body.params.props
  335. if (obj.title) {
  336. obj.content = obj.title
  337. delete obj.title
  338. }
  339. if (!obj.duration) {
  340. obj.duration = 2000
  341. }
  342. if (obj.icon) {
  343. if (obj.icon === 'success') {
  344. obj.type = 'success'
  345. } else if (obj.icon === 'loading') {
  346. obj.type = 'none'
  347. utils.warn('showToast暂不支持loading', {
  348. apiName: 'showToast/loading',
  349. errorType: 0,
  350. type: 'api',
  351. })
  352. } else {
  353. obj.type = 'none'
  354. }
  355. delete obj.icon
  356. } else {
  357. obj.type = 'success'
  358. }
  359. const params = utils.defineGetter(obj, showToastProps, (_obj, prop) => {
  360. utils.warn(`showToast的参数不支持 ${prop} 属性!`, {
  361. apiName: `showToast/${prop}`,
  362. errorType: showToastProps[prop].type,
  363. type: 'api',
  364. })
  365. })
  366. my.showToast(params)
  367. },},showModal:{fn: function fn(obj = {}) {
  368. const showModalProps = descObj.showModal.body.params.props
  369. if (obj.cancelText !== undefined) {
  370. obj.cancelButtonText = obj.cancelText
  371. delete obj.cancelText
  372. }
  373. if (obj.confirmText !== undefined) {
  374. obj.confirmButtonText = obj.confirmText
  375. delete obj.confirmText
  376. }
  377. const params = utils.defineGetter(obj, showModalProps, (_obj, prop) => {
  378. utils.warn(`showModal的参数不支持 ${prop} 属性!`, {
  379. apiName: `showModal/${prop}`,
  380. errorType: showModalProps[prop].type,
  381. type: 'api',
  382. })
  383. })
  384. my.confirm({
  385. ...params,
  386. success(res) {
  387. if (res.confirm) {
  388. res.cancel = false
  389. } else {
  390. res.cancel = true
  391. }
  392. obj.success && obj.success(res)
  393. },
  394. })
  395. },},showLoading:{fn: function fn(obj = {}) {
  396. const showLoadingProps = descObj.showLoading.body.params.props
  397. if (obj.title) {
  398. obj.content = obj.title
  399. delete obj.title
  400. }
  401. const params = utils.defineGetter(obj, showLoadingProps, (_obj, prop) => {
  402. utils.warn(`showLoading的参数不支持 ${prop} 属性!`, {
  403. apiName: `showLoading/${prop}`,
  404. errorType: showLoadingProps[prop].type,
  405. type: 'api',
  406. })
  407. })
  408. my.showLoading(params)
  409. },},showActionSheet:{fn: function fn(obj = {}) {
  410. const showActionSheetProps = descObj.showActionSheet.body.params.props
  411. if (obj.itemList) {
  412. obj.items = obj.itemList
  413. delete obj.itemList
  414. }
  415. const params = utils.defineGetter(
  416. obj,
  417. showActionSheetProps,
  418. (_obj, prop) => {
  419. utils.warn(`showActionSheet的参数不支持 ${prop} 属性!`, {
  420. apiName: `showActionSheet/${prop}`,
  421. errorType: showActionSheetProps[prop].type,
  422. type: 'api',
  423. })
  424. },
  425. )
  426. my.showActionSheet({
  427. ...params,
  428. success: (res) => {
  429. res.tapIndex = res.index
  430. delete res.index
  431. obj.success && obj.success(res)
  432. },
  433. })
  434. },},hideToast:{fn: function fn(obj) {
  435. try {
  436. my.hideToast()
  437. obj.success
  438. && obj.success({
  439. errMsg: 'hideToast: ok',
  440. })
  441. } catch (err) {
  442. obj.fail && obj.fail(err)
  443. } finally {
  444. obj.complete
  445. && obj.complete({
  446. errMsg: 'hideToast: ok',
  447. })
  448. }
  449. },},hideLoading:{fn: function fn(obj) {
  450. try {
  451. my.hideLoading()
  452. obj.success
  453. && obj.success({
  454. errMsg: 'hideLoading: ok',
  455. })
  456. } catch (err) {
  457. obj.fail && obj.fail(err)
  458. } finally {
  459. obj.complete
  460. && obj.complete({
  461. errMsg: 'hideLoading: ok',
  462. })
  463. }
  464. },},showNavigationBarLoading:{fn: function fn(obj = {}) {
  465. try {
  466. my.showNavigationBarLoading()
  467. obj.success
  468. && obj.success({
  469. errMsg: 'showNavigationBarLoading: ok',
  470. })
  471. } catch (err) {
  472. obj.fail && obj.fail(err)
  473. } finally {
  474. obj.complete
  475. && obj.complete({
  476. errMsg: 'showNavigationBarLoading: ok',
  477. })
  478. }
  479. },},setNavigationBarTitle:{fn: function fn(obj = {}) {
  480. return my.setNavigationBar(obj)
  481. },},setNavigationBarColor:{fn: function fn(obj = {}) {
  482. return my.setNavigationBar(obj)
  483. },},hideNavigationBarLoading:{fn: function fn(obj = {}) {
  484. try {
  485. my.hideNavigationBarLoading()
  486. obj.success
  487. && obj.success({
  488. errMsg: 'hideNavigationBarLoading: ok',
  489. })
  490. } catch (err) {
  491. obj.fail && obj.fail(err)
  492. } finally {
  493. obj.complete
  494. && obj.complete({
  495. errMsg: 'hideNavigationBarLoading: ok',
  496. })
  497. }
  498. },},setTabBarStyle:{fn: function fn(obj = {}) {
  499. if (obj.color && obj.color.length === 4) {
  500. const color = obj.color.slice(1)
  501. obj.color = `#${color}${color}`
  502. }
  503. my.setTabBarStyle(obj)
  504. },},setTabBarItem:{fn: function fn(obj = {}) {
  505. if (!obj.iconPath || !obj.selectedIconPath) {
  506. utils.warn('setTabBarItem的iconPath和selectedIconPath是必传的!', {
  507. apiName: 'setTabBarItem/iconPath和selectedIconPath',
  508. errorType: 0,
  509. type: 'api',
  510. })
  511. }
  512. my.setTabBarItem(obj)
  513. },},stopPullDownRefresh:{fn: function fn(obj = {}) {
  514. try {
  515. my.stopPullDownRefresh()
  516. obj.success
  517. && obj.success({
  518. errMsg: 'stopPullDownRefresh: ok',
  519. })
  520. } catch (err) {
  521. obj.fail && obj.fail(err)
  522. } finally {
  523. obj.complete
  524. && obj.complete({
  525. errMsg: 'stopPullDownRefresh: ok',
  526. })
  527. }
  528. },},pageScrollTo:{fn: function fn(obj = {}) {
  529. const pageScrollToParams = descObj.pageScrollTo.body.params.props
  530. const params = utils.defineGetter(
  531. obj,
  532. pageScrollToParams,
  533. (_obj, prop) => {
  534. utils.warn(`pageScrollTo的参数不支持 ${prop} 属性!`, {
  535. apiName: `pageScrollTo/${prop}`,
  536. errorType: pageScrollToParams[prop].type,
  537. type: 'api',
  538. })
  539. },
  540. )
  541. my.pageScrollTo(params)
  542. try {
  543. my.pageScrollTo()
  544. obj.success
  545. && obj.success({
  546. errMsg: 'pageScrollTo: ok',
  547. })
  548. } catch (err) {
  549. obj.fail && obj.fail(err)
  550. } finally {
  551. obj.complete
  552. && obj.complete({
  553. errMsg: 'pageScrollTo: ok',
  554. })
  555. }
  556. },},request:{fn: function fn(obj = {}) {
  557. if (obj.header) {
  558. obj.headers = obj.header
  559. delete obj.header
  560. }
  561. obj.method = obj.method || 'GET'
  562. obj.method = obj.method.toUpperCase()
  563. if (obj.method !== 'GET' && obj.method !== 'POST') {
  564. utils.warn(`request暂不支持${obj.method}请求方式`, {
  565. apiName: `request/${obj.method}`,
  566. errorType: 0,
  567. type: 'api',
  568. })
  569. obj.method = 'GET'
  570. }
  571. if (obj.responseType) {
  572. utils.warn('支付宝暂不支持responseType', {
  573. apiName: 'request/responseType',
  574. errorType: 0,
  575. type: 'api',
  576. })
  577. }
  578. let task = my.request({
  579. ...obj,
  580. success(res) {
  581. res.header = res.headers
  582. res.statusCode = res.status
  583. delete res.headers
  584. delete res.status
  585. obj.success && obj.success(res)
  586. },
  587. fail(err) {
  588. const errMsg = 'request:fail abort'
  589. if (err.errorMessage === errMsg) {
  590. err = {
  591. errMsg,
  592. }
  593. }
  594. obj.fail && obj.fail(err)
  595. },
  596. complete(res) {
  597. obj.complete && obj.complete(res)
  598. },
  599. })
  600. task = task || {}
  601. task.onHeadersReceived = function() {}
  602. task.offHeadersReceived = function() {}
  603. return task
  604. },},createMapContext:{fn: function fn(obj = {}) {
  605. const createMapContextProps
  606. = descObj.createMapContext.body.returnValue.props
  607. const data = my.createMapContext(obj)
  608. for (const key in createMapContextProps) {
  609. if (createMapContextProps[key].type === 0) {
  610. data[key] = () => {}
  611. }
  612. }
  613. return utils.defineGetter(data, createMapContextProps, (_obj, prop) => {
  614. utils.warn(`createMapContext的返回值不支持 ${prop} 属性!`, {
  615. apiName: `createMapContext/${prop}`,
  616. errorType: createMapContextProps[prop].type,
  617. type: 'api',
  618. })
  619. })
  620. },},createVideoContext:{fn: function fn(id, that) {
  621. if (that) {
  622. utils.warn('createVideoContext暂不支持传递实例this', {
  623. apiName: 'createVideoContext',
  624. errorType: 0,
  625. type: 'api',
  626. })
  627. }
  628. return my.createVideoContext(id)
  629. },},createCameraContext:{fn: function fn(id = '') {
  630. const res = my.createCameraContext(id)
  631. res.setZoom = () => {
  632. utils.warn('支付宝暂不支持setZoom', {
  633. apiName: 'createCameraContext/setZoom',
  634. errorType: 0,
  635. type: 'api',
  636. })
  637. }
  638. return res
  639. },},previewImage:{fn: function fn(obj = {}) {
  640. const imgList = obj.urls || []
  641. const index = imgList.indexOf(obj.current)
  642. obj.current = index
  643. return my.previewImage(obj)
  644. },},compressImage:{fn: function fn(obj = {}) {
  645. if (obj.src) {
  646. obj.apFilePaths = [obj.src]
  647. delete obj.src
  648. }
  649. my.compressImage({
  650. ...obj,
  651. success(res) {
  652. res.tempFilePath = res.apFilePaths[0]
  653. delete res.apFilePath
  654. obj.success && obj.success(res)
  655. },
  656. })
  657. },},chooseImage:{fn: function fn(obj = {}) {
  658. if (!obj.count) {
  659. obj.count = 9
  660. }
  661. my.chooseImage({
  662. ...obj,
  663. // success(res) {
  664. // res.tempFilePaths = res.apFilePaths
  665. // delete res.apFilePath
  666. // utils.warn('暂不支持tempFiles', {
  667. // apiName: 'chooseImage/tempFiles',
  668. // errorType: 0,
  669. // type: 'api',
  670. // })
  671. // obj.success && obj.success(res)
  672. // },
  673. fail(err) {
  674. err.errMsg = err.errorMessage || ''
  675. },
  676. })
  677. },},saveImageToPhotosAlbum:{fn: function fn(obj = {}) {
  678. if (obj.filePath) {
  679. obj.url = obj.filePath
  680. }
  681. return my.saveImage(obj)
  682. },},openLocation:{fn: function fn(obj = {}) {
  683. if (obj.scale) {
  684. utils.warn('支付宝scale的取值为3-19,默认15', {
  685. apiName: 'openLocation/scale',
  686. errorType: 4,
  687. type: 'api',
  688. })
  689. if (obj.scale > 19) {
  690. obj.scale = 19
  691. } else if (obj.scale < 3) {
  692. obj.scale = 3
  693. }
  694. }
  695. return my.openLocation(obj)
  696. },},getLocation:{fn: function fn(obj = {}) {
  697. const type = obj.type || 'wgs84'
  698. const getLocationProps = descObj.getLocation.body.returnValue.props
  699. my.getLocation({
  700. ...obj,
  701. type: 0,
  702. success(res) {
  703. let data = res
  704. if (type === 'wgs84') {
  705. const lnglat = utils.gcj02towgs84(res.longitude, res.latitude)
  706. data = Object.assign(res, {
  707. longitude: lnglat[0],
  708. latitude: lnglat[1],
  709. })
  710. }
  711. data = utils.defineGetter(data, getLocationProps, (_obj, prop) => {
  712. utils.warn(`getLocation的返回值不支持 ${prop} 属性!`, {
  713. apiName: `getLocation/${prop}`,
  714. errorType: getLocationProps[prop].type,
  715. type: 'api',
  716. })
  717. })
  718. obj.success && obj.success(data)
  719. },
  720. })
  721. },},openCard:{fn: function fn(obj) {
  722. const openCardParams = descObj.openCard.body.params.props
  723. const params = utils.defineGetter(obj, openCardParams, (_obj, prop) => {
  724. utils.warn(
  725. `openCard的参数不支持 ${prop} 属性!`,
  726. {
  727. apiName: `openCard/${prop}`,
  728. errorType: openCardParams[prop].type,
  729. type: 'api',
  730. },
  731. )
  732. })
  733. return my.openCardList(params)
  734. },},login:{fn: function fn(obj = {}) {
  735. my.getAuthCode({
  736. scopes: 'auth_base',
  737. success: (res) => {
  738. const resObj = {
  739. code: res.authCode,
  740. }
  741. if (res.authCode) {
  742. resObj.errMsg = 'login:ok'
  743. if (obj.success) {
  744. obj.success(resObj)
  745. }
  746. } else {
  747. resObj.errMsg = 'login:fail'
  748. if (obj.success) {
  749. obj.success(resObj)
  750. }
  751. }
  752. },
  753. fail: (err) => {
  754. if (obj.fail) {
  755. obj.fail(err)
  756. }
  757. },
  758. complete: (res) => {
  759. if (res.authCode) {
  760. const resObj = {
  761. code: res.authCode,
  762. errMsg: 'login:ok',
  763. }
  764. if (obj.complete) {
  765. obj.complete(resObj)
  766. }
  767. } else if (obj.complete) {
  768. obj.complete(res)
  769. }
  770. },
  771. })
  772. },},hideKeyboard:{fn: function fn(obj = {}) {
  773. my.hideKeyboard(obj)
  774. if (typeof obj.success === 'function') {
  775. obj.success()
  776. }
  777. if (typeof obj.complete === 'function') {
  778. obj.complete()
  779. }
  780. },},getNetworkType:{fn: function fn(obj = {}) {
  781. my.getNetworkType({
  782. ...obj,
  783. success(res) {
  784. res.networkType = res.networkType.toLowerCase()
  785. const typeObjMap = {
  786. unknown: 'unknown',
  787. wifi: 'wifi',
  788. '2g': '2g',
  789. '3g': '3g',
  790. '4g': '4g',
  791. }
  792. if (res && !res.networkAvailable) {
  793. res.networkType = 'none'
  794. } else {
  795. res.networkType = typeObjMap[res.networkType] || res.networkType
  796. }
  797. obj.success && obj.success(res)
  798. },
  799. })
  800. },},canvasToTempFilePath:{fn: function fn(obj = {}) {
  801. const ctx = my.createCanvasContext(obj.canvasId)
  802. ctx.toTempFilePath({
  803. ...obj,
  804. success(res) {
  805. res.tempFilePath = res.apFilePath
  806. delete res.apFilePath
  807. obj.success && obj.success(res)
  808. },
  809. })
  810. },},canvasPutImageData:{fn: function fn(obj = {}) {
  811. const ctx = my.createCanvasContext(obj.canvasId)
  812. ctx.putImageData({
  813. ...obj,
  814. success(res) {
  815. obj.success && obj.success(res)
  816. },
  817. })
  818. },},canvasGetImageData:{fn: function fn(obj = {}) {
  819. const ctx = my.createCanvasContext(obj.canvasId)
  820. ctx.getImageData({
  821. ...obj,
  822. success(res) {
  823. obj.success && obj.success(res)
  824. },
  825. })
  826. },},saveFile:{fn: function fn(obj = {}) {
  827. if (obj.tempFilePath) {
  828. obj.apFilePath = obj.tempFilePath
  829. delete obj.tempFilePath
  830. }
  831. my.saveFile({
  832. ...obj,
  833. success(res) {
  834. res.savedFilePath = res.apFilePath
  835. delete res.apFilePath
  836. obj.success && obj.success(res)
  837. },
  838. })
  839. },},removeSavedFile:{fn: function fn(obj = {}) {
  840. if (obj.filePath) {
  841. obj.apFilePath = obj.filePath
  842. delete obj.filePath
  843. }
  844. return my.removeSavedFile(obj)
  845. },},getSavedFileList:{fn: function fn(obj = {}) {
  846. my.getSavedFileList({
  847. success(res) {
  848. if (res.fileList.length) {
  849. const ret = res.fileList.map((item) => {
  850. item.filePath = item.apFilePath
  851. delete item.apFilePath
  852. return item
  853. })
  854. res.fileList = ret
  855. obj.success && obj.success(res)
  856. } else {
  857. obj.success && obj.success(res)
  858. }
  859. },
  860. })
  861. },},getSavedFileInfo:{fn: function fn(obj = {}) {
  862. if (obj.filePath) {
  863. obj.apFilePath = obj.filePath
  864. delete obj.filePath
  865. }
  866. return my.getSavedFileInfo(obj)
  867. },},getFileInfo:{fn: function fn(obj = {}) {
  868. if (obj.filePath) {
  869. obj.apFilePath = obj.filePath
  870. delete obj.filePath
  871. }
  872. return my.getFileInfo(obj)
  873. },},downloadFile:{fn: function fn(obj = {}) {
  874. const downloadFileReturnValue
  875. = descObj.downloadFile.body.returnValue.props
  876. if (obj.filePath !== undefined) {
  877. utils.warn('支付宝暂不支持 filePath', {
  878. apiName: 'downloadFile/filePath',
  879. errorType: 0,
  880. type: 'api',
  881. })
  882. }
  883. my.downloadFile({
  884. ...obj,
  885. success(res) {
  886. res.tempFilePath = res.apFilePath
  887. if (res.apFilePath) {
  888. res.statusCode = 200
  889. }
  890. delete res.apFilePath
  891. if (!res.statusCode) {
  892. utils.warn('支付宝暂不支持statusCode', {
  893. apiName: 'downloadFile/statusCode',
  894. errorType: 0,
  895. type: 'api',
  896. })
  897. }
  898. obj.success && obj.success(res)
  899. },
  900. })
  901. const task = {
  902. abort() {},
  903. offHeadersReceived() {},
  904. offProgressUpdate() {},
  905. onHeadersReceived() {},
  906. onProgressUpdate() {},
  907. }
  908. return utils.defineGetter(task, downloadFileReturnValue, (_obj, prop) => {
  909. utils.warn(`downloadFile的返回值不支持 ${prop} 属性!`, {
  910. apiName: `downloadFile/${prop}`,
  911. errorType: downloadFileReturnValue[prop].type,
  912. type: 'api',
  913. })
  914. })
  915. },},uploadFile:{fn: function fn(obj = {}) {
  916. const uploadFileValue = descObj.uploadFile.body.returnValue.props
  917. if (obj.name) {
  918. obj.fileName = obj.name
  919. delete obj.name
  920. }
  921. const pathArr = obj.filePath.split('.')
  922. obj.fileType = 'image'
  923. const fileType = {
  924. video: ['ogg', 'avi', 'wma', 'rmvb', 'rm', 'flash', 'mp4', '3gp'],
  925. audio: ['wav', 'mp3'],
  926. }
  927. const typeName = pathArr[pathArr.length - 1]
  928. Object.keys(fileType).forEach((key) => {
  929. fileType[key].forEach((item) => {
  930. if (typeName.toLowerCase() === item) {
  931. obj.fileType = key
  932. }
  933. })
  934. })
  935. my.uploadFile(obj)
  936. const task = {
  937. abort() {},
  938. offHeadersReceived() {},
  939. offProgressUpdate() {},
  940. onHeadersReceived() {},
  941. onProgressUpdate() {},
  942. }
  943. return utils.defineGetter(task, uploadFileValue, (_obj, prop) => {
  944. utils.warn(`uploadFile的返回值不支持 ${prop} 属性!`, {
  945. apiName: `uploadFile/${prop}`,
  946. errorType: uploadFileValue[prop].type,
  947. type: 'api',
  948. })
  949. })
  950. },},connectSocket:{fn: function fn(obj = {}) {
  951. const connectSocketParams = descObj.connectSocket.body.params.props
  952. const params = utils.defineGetter(
  953. obj,
  954. connectSocketParams,
  955. (_obj, prop) => {
  956. utils.warn(`connectSocket的参数不支持 ${prop} 属性!`, {
  957. apiName: `connectSocket/${prop}`,
  958. errorType: connectSocketParams[prop].type,
  959. type: 'api',
  960. })
  961. },
  962. )
  963. my.connectSocket(params)
  964. const task = {
  965. close(_obj = {}) {
  966. my.closeSocket(_obj)
  967. },
  968. onClose(fn) {
  969. my.onSocketClose(fn)
  970. },
  971. onError(fn) {
  972. my.offSocketOpen(fn)
  973. },
  974. onMessage(fn) {
  975. my.onSocketMessage(fn)
  976. },
  977. onOpen(fn) {
  978. my.onSocketOpen((res) => {
  979. fn(res)
  980. })
  981. },
  982. send(_obj = {}) {
  983. my.sendSocketMessage(_obj)
  984. },
  985. }
  986. return task
  987. },},onSocketOpen:{fn: function fn(obj) {
  988. my.onSocketOpen((res) => {
  989. utils.warn('onSocketOpen 成功回调缺少header', {
  990. apiName: 'onSocketOpen/header',
  991. errorType: 0,
  992. type: 'api',
  993. })
  994. obj(res)
  995. })
  996. },},closeSocket:{fn: function fn(obj = {}) {
  997. const closeSocketParams = descObj.closeSocket.body.params.props
  998. const params = utils.defineGetter(
  999. obj,
  1000. closeSocketParams,
  1001. (_obj, prop) => {
  1002. utils.warn(`closeSocket的参数不支持 ${prop} 属性!`, {
  1003. apiName: `closeSocket/${prop}`,
  1004. errorType: closeSocketParams[prop].type,
  1005. type: 'api',
  1006. })
  1007. },
  1008. )
  1009. my.closeSocket(params)
  1010. },},getRecorderManager:{fn: function fn() {
  1011. const getRecorderManagerProps
  1012. = descObj.getRecorderManager.body.returnValue.props
  1013. const RecorderManager = my.getRecorderManager()
  1014. for (const key in getRecorderManagerProps) {
  1015. if (getRecorderManagerProps[key].type === 0) {
  1016. RecorderManager[key] = () => {}
  1017. }
  1018. }
  1019. return utils.defineGetter(
  1020. RecorderManager,
  1021. getRecorderManagerProps,
  1022. (obj, prop) => {
  1023. utils.warn(`getRecorderManager的返回值不支持 ${prop} 属性!`, {
  1024. apiName: `getRecorderManager/${prop}`,
  1025. errorType: getRecorderManagerProps[prop].type,
  1026. type: 'api',
  1027. })
  1028. },
  1029. )
  1030. },},setStorageSync:{fn:function fn(key = '', data = '') {
  1031. return my.setStorageSync({
  1032. key,
  1033. data,
  1034. })
  1035. },},getStorage:{fn:function fn(obj) {
  1036. return my.getStorage({
  1037. key: obj.key,
  1038. success: (res) => {
  1039. if (
  1040. res.message
  1041. && res.message === '查无此key'
  1042. && typeof obj.fail === 'function'
  1043. ) {
  1044. const Msg = {
  1045. errMsg: 'getStorage:fail data not found',
  1046. }
  1047. obj.fail(Msg)
  1048. } else if (typeof obj.success === 'function') {
  1049. obj.success(res)
  1050. }
  1051. },
  1052. complete: (res) => {
  1053. if (typeof obj.complete === 'function') {
  1054. if (res.message && res.message === '查无此key') {
  1055. const Msg = {
  1056. errMsg: 'getStorage:fail data not found',
  1057. }
  1058. obj.complete(Msg)
  1059. } else {
  1060. obj.complete(res)
  1061. }
  1062. }
  1063. },
  1064. })
  1065. },},getStorageSync:{fn: function fn(key = '') {
  1066. const storeData = my.getStorageSync({
  1067. key,
  1068. })
  1069. return storeData.data || ''
  1070. },},removeStorageSync:{fn: function fn(key = '') {
  1071. return my.removeStorageSync({
  1072. key,
  1073. })
  1074. },},removeStorage:{fn:function fn(obj) {
  1075. const Msg = {
  1076. errMsg: 'removeStorage:ok',
  1077. }
  1078. return my.removeStorage({
  1079. ...obj,
  1080. success: () => {
  1081. if (typeof obj.success === 'function') {
  1082. obj.success(Msg)
  1083. }
  1084. },
  1085. complete: () => {
  1086. if (typeof obj.complete === 'function') {
  1087. obj.complete(Msg)
  1088. }
  1089. },
  1090. })
  1091. },},createSelectorQuery:{fn: function fn() {
  1092. const SQ = my.createSelectorQuery()
  1093. function Query() {
  1094. this.query = SQ
  1095. this._selectType = 0 // 0: array, 1: object
  1096. this.in = function(p) {
  1097. if (typeof this.query.in === 'function') {
  1098. this.query.in(p)
  1099. return this
  1100. } else {
  1101. return this
  1102. }
  1103. }
  1104. this.select = function(p) {
  1105. const s = utils.parseSelector(p)
  1106. this.query.select(s)
  1107. this._selectType = 1
  1108. return this
  1109. }
  1110. this.node = () => {
  1111. utils.warn('支付宝暂不支持使用SelectorQuery NodesRef.node', {
  1112. apiName: 'SelectorQuery NodesRef.node',
  1113. errorType: 0,
  1114. type: 'api',
  1115. })
  1116. return this
  1117. }
  1118. this.selectAll = function(p) {
  1119. const s = utils.parseSelector(p)
  1120. this.query.selectAll(s)
  1121. return this
  1122. }
  1123. this.selectViewport = function(p) {
  1124. this.query.selectViewport(p)
  1125. return this
  1126. }
  1127. this.boundingClientRect = function(p) {
  1128. this.query.boundingClientRect()
  1129. this.exec(p)
  1130. return this
  1131. }
  1132. this.scrollOffset = function(p) {
  1133. const self = this
  1134. this.query.scrollOffset().exec((ret) => {
  1135. if (self._selectType) {
  1136. self._selectType = 0
  1137. if (Array.isArray(ret) && ret.length === 1) {
  1138. ret = ret[0]
  1139. }
  1140. }
  1141. p && p(ret)
  1142. })
  1143. return this
  1144. }
  1145. this.exec = function(p) {
  1146. this.query.exec((ret) => {
  1147. if (Array.isArray(ret)) {
  1148. ret = ret.map((obj) => {
  1149. return Array.isArray(obj)
  1150. ? obj.map((item) => ({
  1151. ...item,
  1152. id: utils.nextUid(),
  1153. }))
  1154. : {
  1155. ...obj,
  1156. id: utils.nextUid(),
  1157. }
  1158. })
  1159. }
  1160. if (this._selectType) {
  1161. this._selectType = 0
  1162. if (Array.isArray(ret)) {
  1163. if (ret.length === 1) {
  1164. ret = ret[0]
  1165. } else if (ret.length > 1) {
  1166. utils.warn(
  1167. '支付宝SelectorQuery.exec查询结果按请求次序构成数组,数组中每项为一次查询的结果',
  1168. {
  1169. apiName: 'boundingClientRect',
  1170. errorType: 1,
  1171. type: 'api',
  1172. },
  1173. )
  1174. }
  1175. }
  1176. }
  1177. p && p(ret)
  1178. })
  1179. return this
  1180. }
  1181. }
  1182. Query.prototype = SQ
  1183. const res = new Query()
  1184. return res
  1185. },},createIntersectionObserver:{fn: function fn(...p) {
  1186. const OB = my.createIntersectionObserver(...p)
  1187. function Observer() {
  1188. this.observer = OB
  1189. this.relativeTo = function(...args) {
  1190. const t = utils.parseSelector(args[0])
  1191. const o = args[1]
  1192. this.observer.relativeTo(t, o)
  1193. return this
  1194. }
  1195. this.relativeToViewport = function(...args) {
  1196. this.observer.relativeToViewport(...args)
  1197. return this
  1198. }
  1199. this.observe = function(...args) {
  1200. const t = utils.parseSelector(args[0])
  1201. const cb = args[1]
  1202. this.observer.observe(t, (ret) => {
  1203. cb && cb(ret)
  1204. })
  1205. }
  1206. }
  1207. Observer.prototype = OB
  1208. const res = new Observer()
  1209. return res
  1210. },},createAnimation:{fn: function fn(obj = {}) {
  1211. if (obj.timingFunction) {
  1212. obj.timeFunction = obj.timingFunction
  1213. delete obj.timingFunction
  1214. }
  1215. const animation = my.createAnimation(obj)
  1216. animation.config.delay = animation.config.delay || 0
  1217. animation.option = {
  1218. transition: animation.config,
  1219. transformOrigin: animation.config.transformOrigin,
  1220. }
  1221. return animation
  1222. },},showShareMenu:{fn: function fn() {
  1223. return my.showSharePanel()
  1224. },},saveVideoToPhotosAlbum:{fn: function fn(obj = {}) {
  1225. const params = {
  1226. ...obj,
  1227. src: obj.filePath,
  1228. }
  1229. return my.saveVideoToPhotosAlbum(params)
  1230. },},chooseAddress:{fn: function fn(obj = {}) {
  1231. my.getAddress({
  1232. success(_res) {
  1233. const result = {}
  1234. const res = _res.result
  1235. result.cityName = res.city
  1236. result.countyName = res.area
  1237. result.detailInfo = res.street
  1238. result.errMsg = 'chooseAddress:ok'
  1239. result.nationalCode = ''
  1240. result.postalCode = ''
  1241. result.provinceName = res.prov
  1242. result.telNumber = res.mobilePhone
  1243. result.userName = res.fullname
  1244. obj.success && obj.success(result)
  1245. },
  1246. })
  1247. },},chooseVideo:{fn: function fn(obj = {}) {
  1248. my.chooseVideo({
  1249. success(result) {
  1250. result.tempFilePath = result.apFilePath
  1251. delete result.apFilePath
  1252. obj.success && obj.success(result)
  1253. },
  1254. })
  1255. },},getUpdateManager:{fn: function fn() {
  1256. return my.getUpdateManager()
  1257. },},getUserInfo:{fn: function fn(obj) {
  1258. const getUserInfoSuccessRes = descObj.getUserInfo.body.params.props
  1259. if (obj.withCredentials || obj.lang) {
  1260. utils.warn(
  1261. 'GetAuthUserInfo不支持 withCredentials 或 lang 参数.',
  1262. {
  1263. apiName: 'getUserInfo/withCredentials 或 getUserInfo/lang',
  1264. errorType: 0,
  1265. type: 'api',
  1266. },
  1267. )
  1268. }
  1269. my.getAuthCode({
  1270. scopes: 'auth_user',
  1271. success: () => {
  1272. my.getAuthUserInfo({
  1273. ...obj,
  1274. success(res) {
  1275. utils.defineGetter(res, getUserInfoSuccessRes, (_obj, prop) => {
  1276. utils.warn(
  1277. `getUserInfo的参数不支持 ${prop} 属性!`,
  1278. {
  1279. apiName: `getUserInfo/${prop}`,
  1280. errorType: getUserInfoSuccessRes[prop].type,
  1281. type: 'api',
  1282. },
  1283. )
  1284. })
  1285. const _res = {}
  1286. _res.userInfo = res
  1287. _res.userInfo.avatarUrl = res.avatar
  1288. obj.success && obj.success(_res)
  1289. },
  1290. })
  1291. },
  1292. })
  1293. },},reportAnalytics:{fn: function fn(key, value) {
  1294. if (typeof value !== 'object') {
  1295. if (!(value instanceof Object)) {
  1296. value = {
  1297. data: value,
  1298. }
  1299. }
  1300. }
  1301. return my.reportAnalytics(key, value)
  1302. },},requestPayment:{fn: function fn(obj = {}) {
  1303. const requestPaymentParams = descObj.requestPayment.body.params.props
  1304. const params = utils.defineGetter(obj, requestPaymentParams, (_obj, prop) => {
  1305. utils.warn(
  1306. `requestPayment的参数不支持 ${prop} 属性!`,
  1307. {
  1308. apiName: `requestPayment/${prop}`,
  1309. errorType: requestPaymentParams[prop].type,
  1310. type: 'api',
  1311. },
  1312. )
  1313. })
  1314. return my.tradePay(params)
  1315. },},authorize:{fn: function fn(obj = {}) {
  1316. const authorizeParams = descObj.authorize.body.params.props
  1317. if (obj.scope) {
  1318. delete obj.scope
  1319. obj.scopes = 'auth_user'
  1320. }
  1321. const params = utils.defineGetter(obj, authorizeParams, (_obj, prop) => {
  1322. utils.warn(
  1323. `authorize的参数不支持 ${prop} 属性!`,
  1324. {
  1325. apiName: `authorize/${prop}`,
  1326. errorType: authorizeParams[prop].type,
  1327. type: 'api',
  1328. },
  1329. )
  1330. })
  1331. return my.getAuthCode(params)
  1332. },},addCard:{fn: function fn(obj) {
  1333. const addCardParams = descObj.addCard.body.params.props
  1334. const params = utils.defineGetter(obj, addCardParams, (_obj, prop) => {
  1335. utils.warn(
  1336. `addCard的参数不支持 ${prop} 属性!`,
  1337. {
  1338. apiName: `startSoterAuthentication/${prop}`,
  1339. errorType: addCardParams[prop].type,
  1340. type: 'api',
  1341. },
  1342. )
  1343. })
  1344. return my.addCardAuth(params)
  1345. },},startSoterAuthentication:{fn: function fn(obj) {
  1346. const startSoterAuthenticationParams = descObj.startSoterAuthentication.body.params.props
  1347. const params = utils.defineGetter(obj, startSoterAuthenticationParams, (_obj, prop) => {
  1348. utils.warn(
  1349. `startSoterAuthentication的参数不支持 ${prop} 属性!`,
  1350. {
  1351. apiName: prop,
  1352. errorType: startSoterAuthenticationParams[prop].type,
  1353. type: 'api',
  1354. },
  1355. )
  1356. })
  1357. return my.ap.faceVerify(params)
  1358. },},getSetting:{fn: function fn(options = {}) {
  1359. function setLocation(cb) {
  1360. my.getLocation({
  1361. success(res) {
  1362. res.authSetting['scope.userLocation'] = true
  1363. cb && cb()
  1364. },
  1365. })
  1366. }
  1367. if (my.getSetting) {
  1368. my.getSetting({
  1369. ...options,
  1370. success(res) {
  1371. const { success } = options
  1372. if (!success) { return }
  1373. success({
  1374. authSetting: utils.mapAuthSetting(res.authSetting),
  1375. })
  1376. },
  1377. })
  1378. } else {
  1379. const res = {}
  1380. res.authSetting = {}
  1381. /**
  1382. * scope=[userInfo, location, album, camera, audioRecord]
  1383. */
  1384. if (options && options.success) {
  1385. setLocation(() => {
  1386. options.success(res)
  1387. })
  1388. }
  1389. }
  1390. },},openSetting:{fn: function fn(options = {}) {
  1391. my.openSetting({
  1392. ...options,
  1393. success: (res) => {
  1394. const { success } = options
  1395. if (!success) {
  1396. return
  1397. }
  1398. success({
  1399. authSetting: utils.mapAuthSetting(res.authSetting),
  1400. })
  1401. },
  1402. })
  1403. },},hideHomeButton:{fn: function fn(obj = {}) {
  1404. const hideHomeButtonProps = descObj.hideHomeButton.body.params.props
  1405. const param = utils.defineGetter(obj, hideHomeButtonProps, (_obj, prop) => {
  1406. utils.warn(
  1407. `hideHomeButton的返回值不支持 ${prop} 属性!`,
  1408. {
  1409. apiName: `hideHomeButton/${prop}`,
  1410. errorType: hideHomeButtonProps[prop].type,
  1411. type: 'api',
  1412. },
  1413. )
  1414. })
  1415. return my.hideBackHome(param)
  1416. },},cloud:{init: function init() {
  1417. utils.warn(
  1418. '支付宝暂不支持init',
  1419. {
  1420. apiName: 'cloud/init',
  1421. errorType: 0,
  1422. type: 'api',
  1423. },
  1424. )
  1425. },},nextTick:function nextTick(fn, delay = 0) {
  1426. if (typeof fn === 'function') {
  1427. if (!delay) {
  1428. Promise.resolve().then(fn)
  1429. } else {
  1430. setTimeout(fn, delay)
  1431. }
  1432. }
  1433. },}
  1434. module.exports = apiObj;