ydool-jssdk.4.0.0.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. !(function (win, doc) {
  2. var iosWebviewHasReady = false
  3. win._iosWebviewReadyCallback = function () {
  4. iosWebviewHasReady = true
  5. if (myBridge.fnList.length) {
  6. myBridge.fnList.forEach(function (fn) {
  7. myBridge.invoke(fn)
  8. })
  9. }
  10. }
  11. //判断访问终端
  12. var browser = {
  13. versions: function () {
  14. var u = navigator.userAgent, app = navigator.appVersion;
  15. return {
  16. trident: u.indexOf('Trident') > -1, //IE内核
  17. presto: u.indexOf('Presto') > -1, //opera内核
  18. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  19. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
  20. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  21. ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  22. android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
  23. iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
  24. iPad: u.indexOf('iPad') > -1, //是否iPad
  25. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  26. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  27. qq: u.match(/\sQQ/i) == " qq" //是否QQ
  28. };
  29. }(),
  30. language: (navigator.browserLanguage || navigator.language).toLowerCase()
  31. }
  32. var myBridge = {
  33. fnList: [],
  34. invoke: function (command) {
  35. console.log('invoke');
  36. console.log(browser.versions)
  37. console.log(navigator.userAgent)
  38. console.log(navigator.appVersion)
  39. try {
  40. if (browser.versions.android) {
  41. console.log('android');
  42. if (window.ytBridge) {
  43. window.ytBridge.invoke(command)
  44. } else {
  45. throw new Error(false)
  46. }
  47. } else if (browser.versions.ios) {
  48. console.log('ios');
  49. // WKWebview 内核的ios app 交互方式
  50. if (win.webkit && win.webkit.messageHandlers) {
  51. console.log('ios1')
  52. window.webkit.messageHandlers.invoke.postMessage(command);
  53. } else if (win.ytBridge) {
  54. console.log('ios2')
  55. // UIWebview 内核的ios app交互方式
  56. // window.ytBridge.invoke(command)
  57. if (window.ytBridge) {
  58. window.ytBridge.invoke(command)
  59. } else {
  60. throw new Error(false)
  61. }
  62. } else {
  63. console.log('ios3')
  64. this.fnList.push(command)
  65. throw new Error(false)
  66. }
  67. } else {
  68. console.log('error1111');
  69. // window.ytBridge.invoke(command)
  70. // console.log(window.ytBridge)
  71. if (win.webkit && win.webkit.messageHandlers) {
  72. console.log('ios000')
  73. window.webkit.messageHandlers.invoke.postMessage(command);
  74. } else if (win.ytBridge) {
  75. console.log('ios11');
  76. if (window.ytBridge) {
  77. console.log('ios111');
  78. window.ytBridge.invoke(command)
  79. } else {
  80. console.log('ios222');
  81. throw new Error(false)
  82. }
  83. } else {
  84. console.log('ios22');
  85. throw new Error(false)
  86. }
  87. }
  88. } catch (e) {
  89. throw new Error(false)
  90. }
  91. }
  92. }
  93. // 供native调用的回调函数队列
  94. var _js2nativeCallbackList = win._js2nativeCallbackList = []
  95. // 分享单例对象
  96. var shareInfo = {}
  97. var userInfo = {};
  98. var areaInfo = {};
  99. var locationInfo = {};
  100. var fontInfo = {};
  101. var keyboardInfo = {};
  102. var scanInfo = {};
  103. var payInfo = {};
  104. var payWayInfo = {};
  105. var recordInfo = {};
  106. var backInfo = {};
  107. var downInfo = {};
  108. var fliesInfo = {};
  109. var collectionInfo = {};
  110. var unCollectionInfo = {};
  111. var grayPlacement = {};
  112. var smUserInfo = {};
  113. var isLoginInfo = {}
  114. var ArticleActionInfo = {};
  115. var phoneMsgInfo = {};
  116. var jsSDK = {
  117. version: '2.0.0',
  118. /**
  119. * 设置分享内容
  120. * @param {Object} options 分享的参数信息
  121. */
  122. onShare: function (options) {
  123. /**
  124. * 加入一个时间戳,后续回调发生的时候可以通过时间戳判断回调是否过期,比如用户多次调用onShare
  125. * @type {[type]}
  126. */
  127. var timestamp = Date.now()
  128. options.timestamp = timestamp
  129. /**
  130. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  131. * @param {[object]} res [native传递的参数]
  132. * @return {Function} [回调函数]
  133. */
  134. var callback = function (res) {
  135. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  136. if (res) {
  137. if (callback.timestamp !== shareInfo.timestamp) {
  138. console.warn('callback expired')
  139. return
  140. }
  141. /**
  142. * 如果传入的几个回调不是函数,将会导致错误
  143. */
  144. try {
  145. switch (res.code) {
  146. case 100:
  147. shareInfo.cancel && shareInfo.cancel()
  148. break
  149. case 200:
  150. shareInfo.success && shareInfo.success()
  151. break
  152. case 400:
  153. shareInfo.error && shareInfo.error()
  154. break
  155. }
  156. } catch (error) {
  157. console.error(error)
  158. }
  159. }
  160. }
  161. callback.timestamp = timestamp
  162. shareInfo = options
  163. var num = _js2nativeCallbackList.push(callback)
  164. shareInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  165. shareInfo.methodName = 'setShareConfig'
  166. var command = JSON.stringify(shareInfo)
  167. myBridge.invoke(command)
  168. },
  169. /**
  170. * 发起分享
  171. */
  172. share: function () {
  173. var command = JSON.stringify({
  174. methodName: 'share'
  175. })
  176. myBridge.invoke(command)
  177. },
  178. /**
  179. * 唤起app端登录
  180. */
  181. login: function () {
  182. var command = JSON.stringify({
  183. methodName: 'login'
  184. })
  185. myBridge.invoke(command)
  186. },
  187. /**
  188. * 唤起app端支付,更具type区分支付类型
  189. * @param {Number} type 资源类型(以下列出部分,具体请找接口或app端获取)
  190. * 3:专辑 4:单曲 5:活动-5:活动外链6:图文资讯-6:图文资讯外链8:直播栏目9:直播节目单 10:视频直播11:视频点播
  191. * 12:主持人数据,前五名打赏排行榜14:微信矩阵19:话题20:网页(链接)24:投票 25:福利26:购物 27:回听
  192. * 29:内容 30:红包 34:短音频33:视频合集41:问答详情38:问答专家61:UGC直播间62:专辑合集
  193. * @param {Number} id 资源id
  194. */
  195. onPay: function (options) {
  196. var timestamp = Date.now()
  197. options.timestamp = timestamp
  198. /**
  199. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  200. * @param {[object]} res [native传递的参数]
  201. * @return {Function} [回调函数]
  202. */
  203. var callback = function (res) {
  204. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  205. if (res) {
  206. if (callback.timestamp !== shareInfo.timestamp) {
  207. console.warn('callback expired')
  208. return
  209. }
  210. /**
  211. * 如果传入的几个回调不是函数,将会导致错误
  212. */
  213. try {
  214. switch (res.code) {
  215. case 100:
  216. shareInfo.cancel && shareInfo.cancel()
  217. break
  218. case 200:
  219. shareInfo.success && shareInfo.success()
  220. break
  221. case 400:
  222. shareInfo.error && shareInfo.error()
  223. break
  224. }
  225. } catch (error) {
  226. console.error(error)
  227. }
  228. }
  229. }
  230. callback.timestamp = timestamp
  231. shareInfo = options
  232. var num = _js2nativeCallbackList.push(callback)
  233. shareInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  234. shareInfo.methodName = 'pay'
  235. var command = JSON.stringify(shareInfo)
  236. myBridge.invoke(command)
  237. },
  238. /**
  239. * 唤起app内跳转,根据type(值同上)
  240. * @param {Number} type 资源类型 目前支持:3专辑 6图文资讯 -6:图文资讯外链 87:打开新webview页面
  241. * @param {Number} id 资源id
  242. * @param {String} title 标题 (用于分享) type:87 为导航栏标题
  243. * @param {String} imgUrl 图片(用于分享)
  244. * @param {String} color 颜色值,支持#FFFFFF格式 type: 87为修改导航栏颜色
  245. * @param {String} url 链接地址
  246. */
  247. onJump: function (options) {
  248. options.methodName = 'jump'
  249. var command = JSON.stringify(options)
  250. myBridge.invoke(command)
  251. },
  252. onJumpCode: function (options) {
  253. var command = JSON.stringify({
  254. methodName: 'jumpCode',
  255. code: options.code,
  256. id: options.id
  257. })
  258. myBridge.invoke(command)
  259. },
  260. getFlies: function (options) {
  261. console.log('getFlies---------------------')
  262. var timestamp = Date.now()
  263. options.timestamp = timestamp
  264. /**
  265. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  266. * @param {[object]} res [native传递的参数]
  267. * @return {Function} [回调函数]
  268. */
  269. var callback = function (res) {
  270. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  271. if (res) {
  272. if (callback.timestamp !== fliesInfo.timestamp) {
  273. console.warn('callback expired')
  274. return
  275. }
  276. /**
  277. * 如果传入的几个回调不是函数,将会导致错误
  278. */
  279. try {
  280. switch (res.code) {
  281. case 100:
  282. fliesInfo.cancel && fliesInfo.cancel(res)
  283. break
  284. case 200:
  285. fliesInfo.success && fliesInfo.success(res)
  286. break
  287. case 400:
  288. fliesInfo.error && fliesInfo.error(res)
  289. break
  290. }
  291. } catch (error) {
  292. console.error(error)
  293. }
  294. }
  295. }
  296. callback.timestamp = timestamp
  297. fliesInfo = options
  298. var num = _js2nativeCallbackList.push(callback)
  299. fliesInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  300. fliesInfo.methodName = 'getFlies'
  301. var command = JSON.stringify(fliesInfo)
  302. myBridge.invoke(command)
  303. },
  304. collection: function (options) {
  305. console.log('collection---------------------')
  306. var timestamp = Date.now()
  307. options.timestamp = timestamp
  308. /**
  309. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  310. * @param {[object]} res [native传递的参数]
  311. * @return {Function} [回调函数]
  312. */
  313. var callback = function (res) {
  314. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  315. if (res) {
  316. if (callback.timestamp !== collectionInfo.timestamp) {
  317. console.warn('callback expired')
  318. return
  319. }
  320. /**
  321. * 如果传入的几个回调不是函数,将会导致错误
  322. */
  323. try {
  324. switch (res.code) {
  325. case 100:
  326. collectionInfo.cancel && collectionInfo.cancel(res)
  327. break
  328. case 200:
  329. collectionInfo.success && collectionInfo.success(res)
  330. break
  331. case 400:
  332. collectionInfo.error && collectionInfo.error(res)
  333. break
  334. }
  335. } catch (error) {
  336. console.error(error)
  337. }
  338. }
  339. }
  340. callback.timestamp = timestamp
  341. collectionInfo = options
  342. var num = _js2nativeCallbackList.push(callback)
  343. collectionInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  344. collectionInfo.methodName = 'collection'
  345. var command = JSON.stringify(collectionInfo)
  346. myBridge.invoke(command)
  347. },
  348. unCollection: function (options) {
  349. console.log('collection---------------------')
  350. var timestamp = Date.now()
  351. options.timestamp = timestamp
  352. /**
  353. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  354. * @param {[object]} res [native传递的参数]
  355. * @return {Function} [回调函数]
  356. */
  357. var callback = function (res) {
  358. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  359. if (res) {
  360. if (callback.timestamp !== unCollectionInfo.timestamp) {
  361. console.warn('callback expired')
  362. return
  363. }
  364. /**
  365. * 如果传入的几个回调不是函数,将会导致错误
  366. */
  367. try {
  368. switch (res.code) {
  369. case 100:
  370. unCollectionInfo.cancel && unCollectionInfo.cancel(res)
  371. break
  372. case 200:
  373. unCollectionInfo.success && unCollectionInfo.success(res)
  374. break
  375. case 400:
  376. unCollectionInfo.error && unCollectionInfo.error(res)
  377. break
  378. }
  379. } catch (error) {
  380. console.error(error)
  381. }
  382. }
  383. }
  384. callback.timestamp = timestamp
  385. unCollectionInfo = options
  386. var num = _js2nativeCallbackList.push(callback)
  387. unCollectionInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  388. unCollectionInfo.methodName = 'unCollection'
  389. var command = JSON.stringify(unCollectionInfo)
  390. myBridge.invoke(command)
  391. },
  392. getUserInfo: function (options) {
  393. var timestamp = Date.now()
  394. options.timestamp = timestamp
  395. /**
  396. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  397. * @param {[object]} res [native传递的参数]
  398. * @return {Function} [回调函数]
  399. */
  400. var callback = function (res) {
  401. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  402. if (res) {
  403. if (callback.timestamp !== userInfo.timestamp) {
  404. console.warn('callback expired')
  405. return
  406. }
  407. /**
  408. * 如果传入的几个回调不是函数,将会导致错误
  409. */
  410. try {
  411. switch (res.code) {
  412. case 100:
  413. userInfo.cancel && userInfo.cancel(res)
  414. break
  415. case 200:
  416. userInfo.success && userInfo.success(res)
  417. break
  418. case 400:
  419. userInfo.error && userInfo.error(res)
  420. break
  421. }
  422. } catch (error) {
  423. console.error(error)
  424. }
  425. }
  426. }
  427. callback.timestamp = timestamp
  428. userInfo = options
  429. var num = _js2nativeCallbackList.push(callback)
  430. userInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  431. userInfo.methodName = 'getUserInfo'
  432. var command = JSON.stringify(userInfo)
  433. myBridge.invoke(command)
  434. },
  435. getSmUserInfo: function (options) {
  436. var timestamp = Date.now()
  437. options.timestamp = timestamp
  438. /**
  439. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  440. * @param {[object]} res [native传递的参数]
  441. * @return {Function} [回调函数]
  442. */
  443. var callback = function (res) {
  444. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  445. if (res) {
  446. if (callback.timestamp !== smUserInfo.timestamp) {
  447. console.warn('callback expired')
  448. return
  449. }
  450. /**
  451. * 如果传入的几个回调不是函数,将会导致错误
  452. */
  453. try {
  454. switch (res.code) {
  455. case 100:
  456. smUserInfo.cancel && smUserInfo.cancel(res)
  457. break
  458. case 200:
  459. smUserInfo.success && smUserInfo.success(res)
  460. break
  461. case 400:
  462. smUserInfo.error && smUserInfo.error(res)
  463. break
  464. }
  465. } catch (error) {
  466. console.error(error)
  467. }
  468. }
  469. }
  470. callback.timestamp = timestamp
  471. smUserInfo = options
  472. var num = _js2nativeCallbackList.push(callback)
  473. smUserInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  474. smUserInfo.methodName = 'getSmUserInfo'
  475. var command = JSON.stringify(smUserInfo)
  476. myBridge.invoke(command)
  477. },
  478. getGrayPlacement: function (options) {
  479. var timestamp = Date.now()
  480. options.timestamp = timestamp
  481. /**
  482. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  483. * @param {[object]} res [native传递的参数]
  484. * @return {Function} [回调函数]
  485. */
  486. var callback = function (res) {
  487. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  488. if (res) {
  489. if (callback.timestamp !== grayPlacement.timestamp) {
  490. console.warn('callback expired')
  491. return
  492. }
  493. /**
  494. * 如果传入的几个回调不是函数,将会导致错误
  495. */
  496. try {
  497. console.log('res+++++++++++++++++', res)
  498. switch (res.code) {
  499. case 100:
  500. grayPlacement.cancel && grayPlacement.cancel(res)
  501. break
  502. case 200:
  503. grayPlacement.success && grayPlacement.success(res)
  504. break
  505. case 400:
  506. grayPlacement.error && grayPlacement.error(res)
  507. break
  508. }
  509. } catch (error) {
  510. console.error(error)
  511. }
  512. }
  513. }
  514. callback.timestamp = timestamp
  515. grayPlacement = options
  516. var num = _js2nativeCallbackList.push(callback)
  517. grayPlacement.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  518. grayPlacement.methodName = 'getGrayPlacement'
  519. var command = JSON.stringify(grayPlacement)
  520. myBridge.invoke(command)
  521. },
  522. getAreaInfo: function (options) {
  523. var timestamp = Date.now()
  524. options.timestamp = timestamp
  525. /**
  526. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  527. * @param {[object]} res [native传递的参数]
  528. * @return {Function} [回调函数]
  529. */
  530. var callback = function (res) {
  531. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  532. if (res) {
  533. if (callback.timestamp !== areaInfo.timestamp) {
  534. console.warn('callback expired')
  535. return
  536. }
  537. /**
  538. * 如果传入的几个回调不是函数,将会导致错误
  539. */
  540. try {
  541. switch (res.code) {
  542. case 100:
  543. areaInfo.cancel && areaInfo.cancel(res)
  544. break
  545. case 200:
  546. areaInfo.success && areaInfo.success(res)
  547. break
  548. case 400:
  549. areaInfo.error && areaInfo.error(res)
  550. break
  551. }
  552. } catch (error) {
  553. console.error(error)
  554. }
  555. }
  556. }
  557. callback.timestamp = timestamp
  558. areaInfo = options
  559. var num = _js2nativeCallbackList.push(callback)
  560. areaInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  561. areaInfo.methodName = 'getAreaInfo'
  562. var command = JSON.stringify(areaInfo)
  563. myBridge.invoke(command)
  564. },
  565. /**
  566. * 设置导航栏标题
  567. * @param {String} title 标题内容 setTitle({title: xxx})
  568. */
  569. setTitle: function (options) {
  570. options.methodName = 'setTitle'
  571. var command = JSON.stringify(options)
  572. myBridge.invoke(command)
  573. },
  574. /**
  575. * 获取app当前gps定位坐标
  576. * geolocation({success: res => {}})
  577. */
  578. geolocation: function (options) {
  579. console.log('geolocation')
  580. var timestamp = Date.now()
  581. options.timestamp = timestamp
  582. /**
  583. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  584. * @param {[object]} res [native传递的参数]
  585. * @return {Function} [回调函数]
  586. */
  587. var callback = function (res) {
  588. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  589. if (res) {
  590. if (callback.timestamp !== locationInfo.timestamp) {
  591. console.warn('callback expired')
  592. return
  593. }
  594. /**
  595. * 如果传入的几个回调不是函数,将会导致错误
  596. */
  597. try {
  598. switch (res.code) {
  599. case 100:
  600. locationInfo.cancel && locationInfo.cancel(res)
  601. break
  602. case 200:
  603. locationInfo.success && locationInfo.success(res)
  604. break
  605. case 400:
  606. locationInfo.error && locationInfo.error(res)
  607. break
  608. }
  609. } catch (error) {
  610. console.error(error)
  611. }
  612. }
  613. }
  614. callback.timestamp = timestamp
  615. locationInfo = options
  616. var num = _js2nativeCallbackList.push(callback)
  617. locationInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  618. locationInfo.methodName = 'geolocation'
  619. var command = JSON.stringify(locationInfo)
  620. myBridge.invoke(command)
  621. },
  622. /**
  623. * 获取字体大小范围
  624. * getFontSize({success: res => {}})
  625. * 1:大 2:中 3: 正常 4:大 5:特大
  626. */
  627. getFontSize: function (options) {
  628. var timestamp = Date.now()
  629. options.timestamp = timestamp
  630. /**
  631. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  632. * @param {[object]} res [native传递的参数]
  633. * @return {Function} [回调函数]
  634. */
  635. var callback = function (res) {
  636. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  637. if (res) {
  638. if (callback.timestamp !== fontInfo.timestamp) {
  639. console.warn('callback expired')
  640. return
  641. }
  642. /**
  643. * 如果传入的几个回调不是函数,将会导致错误
  644. */
  645. try {
  646. switch (res.code) {
  647. case 100:
  648. fontInfo.cancel && fontInfo.cancel(res)
  649. break
  650. case 200:
  651. fontInfo.success && fontInfo.success(res)
  652. break
  653. case 400:
  654. fontInfo.error && fontInfo.error(res)
  655. break
  656. }
  657. } catch (error) {
  658. console.error(error)
  659. }
  660. }
  661. }
  662. callback.timestamp = timestamp
  663. fontInfo = options
  664. var num = _js2nativeCallbackList.push(callback)
  665. fontInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  666. fontInfo.methodName = 'getFontSize'
  667. var command = JSON.stringify(fontInfo)
  668. myBridge.invoke(command)
  669. },
  670. /**
  671. * 获取键盘高度
  672. * getKeyboardHeight({success: res => {}})
  673. *
  674. */
  675. getKeyboardHeight: function (options) {
  676. var timestamp = Date.now()
  677. options.timestamp = timestamp
  678. /**
  679. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  680. * @param {[object]} res [native传递的参数]
  681. * @return {Function} [回调函数]
  682. */
  683. var callback = function (res) {
  684. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  685. if (res) {
  686. if (callback.timestamp !== keyboardInfo.timestamp) {
  687. console.warn('callback expired')
  688. return
  689. }
  690. /**
  691. * 如果传入的几个回调不是函数,将会导致错误
  692. */
  693. try {
  694. switch (res.code) {
  695. case 100:
  696. keyboardInfo.cancel && keyboardInfo.cancel(res)
  697. break
  698. case 200:
  699. keyboardInfo.success && keyboardInfo.success(res)
  700. break
  701. case 400:
  702. keyboardInfo.error && keyboardInfo.error(res)
  703. break
  704. }
  705. } catch (error) {
  706. console.error(error)
  707. }
  708. }
  709. }
  710. callback.timestamp = timestamp
  711. keyboardInfo = options
  712. var num = _js2nativeCallbackList.push(callback)
  713. keyboardInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  714. keyboardInfo.methodName = 'getKeyboardHeight'
  715. var command = JSON.stringify(keyboardInfo)
  716. myBridge.invoke(command)
  717. },
  718. /**
  719. * 唤起app端认证
  720. */
  721. auth: function () {
  722. var command = JSON.stringify({
  723. methodName: 'auth'
  724. })
  725. myBridge.invoke(command)
  726. },
  727. /**
  728. * 设置导航栏颜色
  729. * @param {String} color 颜色值 setTitle({color: '#FFF'})
  730. */
  731. setNavColor: function (options) {
  732. options.methodName = 'navColor'
  733. var command = JSON.stringify(options)
  734. myBridge.invoke(command)
  735. },
  736. /**
  737. * 唤起app扫码
  738. * scan({success: res => {res.text}})
  739. */
  740. scan: function (options) {
  741. var timestamp = Date.now()
  742. options.timestamp = timestamp
  743. /**
  744. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  745. * @param {[object]} res [native传递的参数]
  746. * @return {Function} [回调函数]
  747. */
  748. var callback = function (res) {
  749. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  750. if (res) {
  751. if (callback.timestamp !== scanInfo.timestamp) {
  752. console.warn('callback expired')
  753. return
  754. }
  755. /**
  756. * 如果传入的几个回调不是函数,将会导致错误
  757. */
  758. try {
  759. switch (res.code) {
  760. case 100:
  761. scanInfo.cancel && scanInfo.cancel(res)
  762. break
  763. case 200:
  764. scanInfo.success && scanInfo.success(res)
  765. break
  766. case 400:
  767. scanInfo.error && scanInfo.error(res)
  768. break
  769. }
  770. } catch (error) {
  771. console.error(error)
  772. }
  773. }
  774. }
  775. callback.timestamp = timestamp
  776. scanInfo = options
  777. var num = _js2nativeCallbackList.push(callback)
  778. scanInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  779. scanInfo.methodName = 'scan'
  780. var command = JSON.stringify(scanInfo)
  781. myBridge.invoke(command)
  782. },
  783. /**
  784. * 唤起app端支付方式选择,返回已选的支付方式1:支付宝 2:微信
  785. * @param {String} content 支付信息内容[物业缴费]
  786. * @param {Number} price 支付金额,单位 元
  787. * yt.payWay({content: '物业缴费', price: 10, success (res) { // res.type} })
  788. */
  789. payWay: function (options) {
  790. var timestamp = Date.now()
  791. options.timestamp = timestamp
  792. /**
  793. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  794. * @param {[object]} res [native传递的参数]
  795. * @return {Function} [回调函数]
  796. */
  797. var callback = function (res) {
  798. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  799. if (res) {
  800. if (callback.timestamp !== payWayInfo.timestamp) {
  801. console.warn('callback expired')
  802. return
  803. }
  804. /**
  805. * 如果传入的几个回调不是函数,将会导致错误
  806. */
  807. try {
  808. switch (res.code) {
  809. case 100:
  810. payWayInfo.cancel && payWayInfo.cancel(res)
  811. break
  812. case 200:
  813. payWayInfo.success && payWayInfo.success(res)
  814. break
  815. case 400:
  816. payWayInfo.error && payWayInfo.error(res)
  817. break
  818. }
  819. } catch (error) {
  820. console.error(error)
  821. }
  822. }
  823. }
  824. callback.timestamp = timestamp
  825. payWayInfo = options
  826. var num = _js2nativeCallbackList.push(callback)
  827. payWayInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  828. payWayInfo.methodName = 'payWay'
  829. var command = JSON.stringify(payWayInfo)
  830. myBridge.invoke(command)
  831. },
  832. /**
  833. * 唤起app端支付,此为通用支付,需要将支付信息传给app端
  834. * @param {type} type 支付类型 1:支付宝 2:微信
  835. * @param {String} alipayOrderNo 支付宝所需参数
  836. * @param {Object} wetChat 微信支付所需信息 yt.generalPay({type: 2, wetChat: {appId: 'xxx', nonceStr: '', partnerid: '', paySign: '', prepayId: '', signType: 'MD5', timeStamp: ''}, success () => {}})
  837. * yt.generalPay({type: 1, alipayOrderNo: 'xxxx', success () => {}})
  838. */
  839. generalPay: function (options) {
  840. var timestamp = Date.now()
  841. options.timestamp = timestamp
  842. /**
  843. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  844. * @param {[object]} res [native传递的参数]
  845. * @return {Function} [回调函数]
  846. */
  847. var callback = function (res) {
  848. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  849. if (res) {
  850. if (callback.timestamp !== payInfo.timestamp) {
  851. console.warn('callback expired')
  852. return
  853. }
  854. /**
  855. * 如果传入的几个回调不是函数,将会导致错误
  856. */
  857. try {
  858. switch (res.code) {
  859. case 100:
  860. payInfo.cancel && payInfo.cancel(res)
  861. break
  862. case 200:
  863. payInfo.success && payInfo.success(res)
  864. break
  865. case 400:
  866. payInfo.error && payInfo.error(res)
  867. break
  868. }
  869. } catch (error) {
  870. console.error(error)
  871. }
  872. }
  873. }
  874. callback.timestamp = timestamp
  875. payInfo = options
  876. var num = _js2nativeCallbackList.push(callback)
  877. payInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  878. payInfo.methodName = 'generalPay'
  879. var command = JSON.stringify(payInfo)
  880. myBridge.invoke(command)
  881. },
  882. /**
  883. * 调用app的录音功能
  884. * @param {type} type 录音执行的方法 start:开始录音 end:停止录音 onEnd:监听录音结束(主动结束录音||设置到达最大录音时长时,会自动结束||也监听一些报错信息)
  885. * play:播放(播放的是app中录音的缓存文件) pause:暂停 stop: 停止播放 onPlayEnd: 监听播放结束 save:保存(app会上传文件,然后返回已上传的地址)
  886. * @param {Number} maxDuration 最大录音时间,倒计时结束则触发停止录音
  887. * @param {String} url 录音缓存文件路径,用于播放
  888. * @param {String} src 上传保存后的绝对路径 https://xxx.mp3
  889. * @return {Object} duration:录音的时长 url:录音缓存文件路径,用于播放
  890. * yt.record({type: 'start', maxDuration: 300, success () => {}})
  891. * yt.record({type: 'end', success (res) => {code: 200, duration: 48, url: 'xxxx'}})
  892. * yt.record({type: 'onEnd', success (res) => {code: 200, duration: 48, url: 'xxxx'}})
  893. * yt.record({type: 'play', url: 'xxxx', success (res) => {})
  894. * yt.record({type: 'pause', url: 'xxxx', success (res) => {})
  895. * yt.record({type: 'stop', url: 'xxxx', success (res) => {})
  896. * yt.record({type: 'onPlayEnd', url: 'xxxx', success (res) => {})
  897. * yt.record({type: 'save', url: 'xxxx', success (res) => {code: 200, src: 'https://xxx.mp3'})
  898. */
  899. record: function (options) {
  900. var timestamp = Date.now()
  901. options.timestamp = timestamp
  902. if (options.type === 'start' && !options.maxDuration) {
  903. options.error && options.error({ msg: '请填写最大录音时长' })
  904. return
  905. }
  906. /**
  907. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  908. * @param {[object]} res [native传递的参数]
  909. * @return {Function} [回调函数]
  910. */
  911. var callback = function (res) {
  912. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  913. if (res) {
  914. if (callback.timestamp !== recordInfo.timestamp) {
  915. console.warn('callback expired')
  916. return
  917. }
  918. /**
  919. * 如果传入的几个回调不是函数,将会导致错误
  920. */
  921. try {
  922. switch (res.code) {
  923. case 100:
  924. recordInfo.cancel && recordInfo.cancel(res)
  925. break
  926. case 200:
  927. recordInfo.success && recordInfo.success(res)
  928. break
  929. case 400:
  930. recordInfo.error && recordInfo.error(res)
  931. break
  932. }
  933. } catch (error) {
  934. console.error(error)
  935. }
  936. }
  937. }
  938. callback.timestamp = timestamp
  939. recordInfo = options
  940. var num = _js2nativeCallbackList.push(callback)
  941. recordInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  942. recordInfo.methodName = 'record'
  943. var command = JSON.stringify(recordInfo)
  944. myBridge.invoke(command)
  945. },
  946. /**
  947. * 关闭app中当前webview页面,返回到上一页, 龙游通app1.9.0版本开始支持,云听版本对应4.8.0
  948. * window.yt.back({ success: res => {}, error: res => {}, cancel: res => {} })
  949. */
  950. back: function (options = {}) {
  951. var timestamp = Date.now()
  952. options.timestamp = timestamp
  953. /**
  954. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  955. * @param {[object]} res [native传递的参数]
  956. * @return {Function} [回调函数]
  957. */
  958. var callback = function (res) {
  959. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  960. if (res) {
  961. if (callback.timestamp !== backInfo.timestamp) {
  962. console.warn('callback expired')
  963. return
  964. }
  965. /**
  966. * 如果传入的几个回调不是函数,将会导致错误
  967. */
  968. try {
  969. switch (res.code) {
  970. case 100:
  971. backInfo.cancel && backInfo.cancel(res)
  972. break
  973. case 200:
  974. backInfo.success && backInfo.success(res)
  975. break
  976. case 400:
  977. backInfo.error && backInfo.error(res)
  978. break
  979. }
  980. } catch (error) {
  981. console.error(error)
  982. }
  983. }
  984. }
  985. callback.timestamp = timestamp
  986. backInfo = options
  987. var num = _js2nativeCallbackList.push(callback)
  988. backInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  989. backInfo.methodName = 'back'
  990. var command = JSON.stringify(backInfo)
  991. myBridge.invoke(command)
  992. },
  993. /**
  994. * 下载附件, 龙游通app2.2.0版本开始支持,云听版本对应4.11.0
  995. * @param {String} url 下载的附件(word,excel,pdf,图片)地址
  996. * @param {String} name 下载的附件名称,需要有对应的文件类型后缀
  997. * window.yt.downloadFile({ url: 'xxx.pdf', name: 'xxx.doc', success: res => {}, error: res => {}, cancel: res => {} })
  998. */
  999. downloadFile: function (options) {
  1000. var timestamp = Date.now()
  1001. options.timestamp = timestamp
  1002. /**
  1003. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  1004. * @param {[object]} res [native传递的参数]
  1005. * @return {Function} [回调函数]
  1006. */
  1007. var callback = function (res) {
  1008. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  1009. if (res) {
  1010. if (callback.timestamp !== downInfo.timestamp) {
  1011. console.warn('callback expired')
  1012. return
  1013. }
  1014. /**
  1015. * 如果传入的几个回调不是函数,将会导致错误
  1016. */
  1017. try {
  1018. switch (res.code) {
  1019. case 100:
  1020. downInfo.cancel && downInfo.cancel(res)
  1021. break
  1022. case 200:
  1023. downInfo.success && downInfo.success(res)
  1024. break
  1025. case 400:
  1026. downInfo.error && downInfo.error(res)
  1027. break
  1028. }
  1029. } catch (error) {
  1030. console.error(error)
  1031. }
  1032. }
  1033. }
  1034. callback.timestamp = timestamp
  1035. downInfo = options
  1036. var num = _js2nativeCallbackList.push(callback)
  1037. downInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  1038. downInfo.methodName = 'downloadFile'
  1039. var command = JSON.stringify(downInfo)
  1040. myBridge.invoke(command)
  1041. },
  1042. /**
  1043. * 跳转打开小程序页面,目前只有app中支持,5.3.0开始支持
  1044. * @param { String } originId 小程序原始id(可在小程序后台查看)
  1045. * @param { String } path 小程序页面具体路径 /pages/index/index.html?xxx
  1046. * window.yt.jumpWxMiniProgram({ originId: 'gh_4bfdd5c85f15', path: '/pages/index/index.html?xxx' })
  1047. */
  1048. jumpWxMiniProgram: function (options = {}) {
  1049. options.methodName = 'jumpWxMiniProgram'
  1050. var command = JSON.stringify(options)
  1051. myBridge.invoke(command)
  1052. },
  1053. isLogin: function (options) {
  1054. var timestamp = Date.now()
  1055. options.timestamp = timestamp
  1056. /**
  1057. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  1058. * @param {[object]} res [native传递的参数]
  1059. * @return {Function} [回调函数]
  1060. */
  1061. var callback = function (res) {
  1062. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  1063. if (res) {
  1064. if (callback.timestamp !== isLoginInfo.timestamp) {
  1065. console.warn('callback expired')
  1066. return
  1067. }
  1068. /**
  1069. * 如果传入的几个回调不是函数,将会导致错误
  1070. */
  1071. try {
  1072. switch (res.code) {
  1073. case 100:
  1074. isLoginInfo.cancel && isLoginInfo.cancel(res)
  1075. break
  1076. case 200:
  1077. isLoginInfo.success && isLoginInfo.success(res)
  1078. break
  1079. case 400:
  1080. isLoginInfo.error && isLoginInfo.error(res)
  1081. break
  1082. }
  1083. } catch (error) {
  1084. console.error(error)
  1085. }
  1086. }
  1087. }
  1088. callback.timestamp = timestamp
  1089. isLoginInfo = options
  1090. var num = _js2nativeCallbackList.push(callback)
  1091. isLoginInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  1092. isLoginInfo.methodName = 'isLogin'
  1093. var command = JSON.stringify(isLoginInfo)
  1094. myBridge.invoke(command)
  1095. },
  1096. getUserToken: function (options) {
  1097. var timestamp = Date.now()
  1098. options.timestamp = timestamp
  1099. /**
  1100. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  1101. * @param {[object]} res [native传递的参数]
  1102. * @return {Function} [回调函数]
  1103. */
  1104. var callback = function (res) {
  1105. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  1106. if (res) {
  1107. if (callback.timestamp !== isLoginInfo.timestamp) {
  1108. console.warn('callback expired')
  1109. return
  1110. }
  1111. /**
  1112. * 如果传入的几个回调不是函数,将会导致错误
  1113. */
  1114. try {
  1115. switch (res.code) {
  1116. case 100:
  1117. isLoginInfo.cancel && isLoginInfo.cancel(res)
  1118. break
  1119. case 200:
  1120. isLoginInfo.success && isLoginInfo.success(res)
  1121. break
  1122. case 400:
  1123. isLoginInfo.error && isLoginInfo.error(res)
  1124. break
  1125. }
  1126. } catch (error) {
  1127. console.error(error)
  1128. }
  1129. }
  1130. }
  1131. callback.timestamp = timestamp
  1132. isLoginInfo = options
  1133. var num = _js2nativeCallbackList.push(callback)
  1134. isLoginInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  1135. isLoginInfo.methodName = 'getUserToken'
  1136. var command = JSON.stringify(isLoginInfo)
  1137. myBridge.invoke(command)
  1138. },
  1139. getArticleAction: function (options) {
  1140. var timestamp = Date.now()
  1141. options.timestamp = timestamp
  1142. /**
  1143. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  1144. * @param {[object]} res [native传递的参数]
  1145. * @return {Function} [回调函数]
  1146. */
  1147. var callback = function (res) {
  1148. console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  1149. if (res) {
  1150. if (callback.timestamp !== ArticleActionInfo.timestamp) {
  1151. console.warn('callback expired')
  1152. return
  1153. }
  1154. /**
  1155. * 如果传入的几个回调不是函数,将会导致错误
  1156. */
  1157. try {
  1158. switch (res.code) {
  1159. case 100:
  1160. ArticleActionInfo.cancel && ArticleActionInfo.cancel(res)
  1161. break
  1162. case 200:
  1163. ArticleActionInfo.success && ArticleActionInfo.success(res)
  1164. break
  1165. case 400:
  1166. ArticleActionInfo.error && ArticleActionInfo.error(res)
  1167. break
  1168. }
  1169. } catch (error) {
  1170. console.error(error)
  1171. }
  1172. }
  1173. }
  1174. callback.timestamp = timestamp
  1175. ArticleActionInfo = options
  1176. var num = _js2nativeCallbackList.push(callback)
  1177. ArticleActionInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  1178. ArticleActionInfo.methodName = 'getArticleAction'
  1179. var command = JSON.stringify(ArticleActionInfo)
  1180. myBridge.invoke(command)
  1181. },
  1182. getPhoneMsg: function (options) {
  1183. var timestamp = Date.now()
  1184. options.timestamp = timestamp
  1185. /**
  1186. * 定义一个通用的回调函数,接收来自native端传递的参数,根据参数再来回调这边的成功、失败等回调
  1187. * @param {[object]} res [native传递的参数]
  1188. * @return {Function} [回调函数]
  1189. */
  1190. var callback = function (res) {
  1191. // console.log("native response :" + (typeof res === 'string' ? res : JSON.stringify(res)))
  1192. if (res) {
  1193. if (callback.timestamp !== phoneMsgInfo.timestamp) {
  1194. console.warn('callback expired')
  1195. return
  1196. }
  1197. /**
  1198. * 如果传入的几个回调不是函数,将会导致错误
  1199. */
  1200. try {
  1201. switch (res.code) {
  1202. case 100:
  1203. phoneMsgInfo.cancel && phoneMsgInfo.cancel(res)
  1204. break
  1205. case 200:
  1206. phoneMsgInfo.success && phoneMsgInfo.success(res)
  1207. break
  1208. case 400:
  1209. phoneMsgInfo.error && phoneMsgInfo.error(res)
  1210. break
  1211. }
  1212. } catch (error) {
  1213. console.error(error)
  1214. }
  1215. }
  1216. }
  1217. callback.timestamp = timestamp
  1218. phoneMsgInfo = options
  1219. var num = _js2nativeCallbackList.push(callback)
  1220. phoneMsgInfo.callback = '_js2nativeCallbackList[' + (num - 1) + ']'
  1221. phoneMsgInfo.methodName = 'getPhoneMsg'
  1222. var command = JSON.stringify(phoneMsgInfo)
  1223. myBridge.invoke(command)
  1224. },
  1225. }
  1226. win.yt = jsSDK
  1227. })(window, document);