足力健前端,vue版本

index.js 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762
  1. import Vue from 'vue';
  2. const _toString = Object.prototype.toString;
  3. const hasOwnProperty = Object.prototype.hasOwnProperty;
  4. function isFn (fn) {
  5. return typeof fn === 'function'
  6. }
  7. function isStr (str) {
  8. return typeof str === 'string'
  9. }
  10. function isPlainObject (obj) {
  11. return _toString.call(obj) === '[object Object]'
  12. }
  13. function hasOwn (obj, key) {
  14. return hasOwnProperty.call(obj, key)
  15. }
  16. function noop () {}
  17. /**
  18. * Create a cached version of a pure function.
  19. */
  20. function cached (fn) {
  21. const cache = Object.create(null);
  22. return function cachedFn (str) {
  23. const hit = cache[str];
  24. return hit || (cache[str] = fn(str))
  25. }
  26. }
  27. /**
  28. * Camelize a hyphen-delimited string.
  29. */
  30. const camelizeRE = /-(\w)/g;
  31. const camelize = cached((str) => {
  32. return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
  33. });
  34. const HOOKS = [
  35. 'invoke',
  36. 'success',
  37. 'fail',
  38. 'complete',
  39. 'returnValue'
  40. ];
  41. const globalInterceptors = {};
  42. const scopedInterceptors = {};
  43. function mergeHook (parentVal, childVal) {
  44. const res = childVal
  45. ? parentVal
  46. ? parentVal.concat(childVal)
  47. : Array.isArray(childVal)
  48. ? childVal : [childVal]
  49. : parentVal;
  50. return res
  51. ? dedupeHooks(res)
  52. : res
  53. }
  54. function dedupeHooks (hooks) {
  55. const res = [];
  56. for (let i = 0; i < hooks.length; i++) {
  57. if (res.indexOf(hooks[i]) === -1) {
  58. res.push(hooks[i]);
  59. }
  60. }
  61. return res
  62. }
  63. function removeHook (hooks, hook) {
  64. const index = hooks.indexOf(hook);
  65. if (index !== -1) {
  66. hooks.splice(index, 1);
  67. }
  68. }
  69. function mergeInterceptorHook (interceptor, option) {
  70. Object.keys(option).forEach(hook => {
  71. if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) {
  72. interceptor[hook] = mergeHook(interceptor[hook], option[hook]);
  73. }
  74. });
  75. }
  76. function removeInterceptorHook (interceptor, option) {
  77. if (!interceptor || !option) {
  78. return
  79. }
  80. Object.keys(option).forEach(hook => {
  81. if (HOOKS.indexOf(hook) !== -1 && isFn(option[hook])) {
  82. removeHook(interceptor[hook], option[hook]);
  83. }
  84. });
  85. }
  86. function addInterceptor (method, option) {
  87. if (typeof method === 'string' && isPlainObject(option)) {
  88. mergeInterceptorHook(scopedInterceptors[method] || (scopedInterceptors[method] = {}), option);
  89. } else if (isPlainObject(method)) {
  90. mergeInterceptorHook(globalInterceptors, method);
  91. }
  92. }
  93. function removeInterceptor (method, option) {
  94. if (typeof method === 'string') {
  95. if (isPlainObject(option)) {
  96. removeInterceptorHook(scopedInterceptors[method], option);
  97. } else {
  98. delete scopedInterceptors[method];
  99. }
  100. } else if (isPlainObject(method)) {
  101. removeInterceptorHook(globalInterceptors, method);
  102. }
  103. }
  104. function wrapperHook (hook) {
  105. return function (data) {
  106. return hook(data) || data
  107. }
  108. }
  109. function isPromise (obj) {
  110. return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
  111. }
  112. function queue (hooks, data) {
  113. let promise = false;
  114. for (let i = 0; i < hooks.length; i++) {
  115. const hook = hooks[i];
  116. if (promise) {
  117. promise = Promise.then(wrapperHook(hook));
  118. } else {
  119. const res = hook(data);
  120. if (isPromise(res)) {
  121. promise = Promise.resolve(res);
  122. }
  123. if (res === false) {
  124. return {
  125. then () {}
  126. }
  127. }
  128. }
  129. }
  130. return promise || {
  131. then (callback) {
  132. return callback(data)
  133. }
  134. }
  135. }
  136. function wrapperOptions (interceptor, options = {}) {
  137. ['success', 'fail', 'complete'].forEach(name => {
  138. if (Array.isArray(interceptor[name])) {
  139. const oldCallback = options[name];
  140. options[name] = function callbackInterceptor (res) {
  141. queue(interceptor[name], res).then((res) => {
  142. /* eslint-disable no-mixed-operators */
  143. return isFn(oldCallback) && oldCallback(res) || res
  144. });
  145. };
  146. }
  147. });
  148. return options
  149. }
  150. function wrapperReturnValue (method, returnValue) {
  151. const returnValueHooks = [];
  152. if (Array.isArray(globalInterceptors.returnValue)) {
  153. returnValueHooks.push(...globalInterceptors.returnValue);
  154. }
  155. const interceptor = scopedInterceptors[method];
  156. if (interceptor && Array.isArray(interceptor.returnValue)) {
  157. returnValueHooks.push(...interceptor.returnValue);
  158. }
  159. returnValueHooks.forEach(hook => {
  160. returnValue = hook(returnValue) || returnValue;
  161. });
  162. return returnValue
  163. }
  164. function getApiInterceptorHooks (method) {
  165. const interceptor = Object.create(null);
  166. Object.keys(globalInterceptors).forEach(hook => {
  167. if (hook !== 'returnValue') {
  168. interceptor[hook] = globalInterceptors[hook].slice();
  169. }
  170. });
  171. const scopedInterceptor = scopedInterceptors[method];
  172. if (scopedInterceptor) {
  173. Object.keys(scopedInterceptor).forEach(hook => {
  174. if (hook !== 'returnValue') {
  175. interceptor[hook] = (interceptor[hook] || []).concat(scopedInterceptor[hook]);
  176. }
  177. });
  178. }
  179. return interceptor
  180. }
  181. function invokeApi (method, api, options, ...params) {
  182. const interceptor = getApiInterceptorHooks(method);
  183. if (interceptor && Object.keys(interceptor).length) {
  184. if (Array.isArray(interceptor.invoke)) {
  185. const res = queue(interceptor.invoke, options);
  186. return res.then((options) => {
  187. return api(wrapperOptions(interceptor, options), ...params)
  188. })
  189. } else {
  190. return api(wrapperOptions(interceptor, options), ...params)
  191. }
  192. }
  193. return api(options, ...params)
  194. }
  195. const promiseInterceptor = {
  196. returnValue (res) {
  197. if (!isPromise(res)) {
  198. return res
  199. }
  200. return res.then(res => {
  201. return res[1]
  202. }).catch(res => {
  203. return res[0]
  204. })
  205. }
  206. };
  207. const SYNC_API_RE =
  208. /^\$|sendNativeEvent|restoreGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/;
  209. const CONTEXT_API_RE = /^create|Manager$/;
  210. // Context例外情况
  211. const CONTEXT_API_RE_EXC = ['createBLEConnection'];
  212. // 同步例外情况
  213. const ASYNC_API = ['createBLEConnection'];
  214. const CALLBACK_API_RE = /^on|^off/;
  215. function isContextApi (name) {
  216. return CONTEXT_API_RE.test(name) && CONTEXT_API_RE_EXC.indexOf(name) === -1
  217. }
  218. function isSyncApi (name) {
  219. return SYNC_API_RE.test(name) && ASYNC_API.indexOf(name) === -1
  220. }
  221. function isCallbackApi (name) {
  222. return CALLBACK_API_RE.test(name) && name !== 'onPush'
  223. }
  224. function handlePromise (promise) {
  225. return promise.then(data => {
  226. return [null, data]
  227. })
  228. .catch(err => [err])
  229. }
  230. function shouldPromise (name) {
  231. if (
  232. isContextApi(name) ||
  233. isSyncApi(name) ||
  234. isCallbackApi(name)
  235. ) {
  236. return false
  237. }
  238. return true
  239. }
  240. /* eslint-disable no-extend-native */
  241. if (!Promise.prototype.finally) {
  242. Promise.prototype.finally = function (callback) {
  243. const promise = this.constructor;
  244. return this.then(
  245. value => promise.resolve(callback()).then(() => value),
  246. reason => promise.resolve(callback()).then(() => {
  247. throw reason
  248. })
  249. )
  250. };
  251. }
  252. function promisify (name, api) {
  253. if (!shouldPromise(name)) {
  254. return api
  255. }
  256. return function promiseApi (options = {}, ...params) {
  257. if (isFn(options.success) || isFn(options.fail) || isFn(options.complete)) {
  258. return wrapperReturnValue(name, invokeApi(name, api, options, ...params))
  259. }
  260. return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
  261. invokeApi(name, api, Object.assign({}, options, {
  262. success: resolve,
  263. fail: reject
  264. }), ...params);
  265. })))
  266. }
  267. }
  268. const EPS = 1e-4;
  269. const BASE_DEVICE_WIDTH = 750;
  270. let isIOS = false;
  271. let deviceWidth = 0;
  272. let deviceDPR = 0;
  273. function checkDeviceWidth () {
  274. const {
  275. platform,
  276. pixelRatio,
  277. windowWidth
  278. } = swan.getSystemInfoSync(); // uni=>swan runtime 编译目标是 uni 对象,内部不允许直接使用 uni
  279. deviceWidth = windowWidth;
  280. deviceDPR = pixelRatio;
  281. isIOS = platform === 'ios';
  282. }
  283. function upx2px (number, newDeviceWidth) {
  284. if (deviceWidth === 0) {
  285. checkDeviceWidth();
  286. }
  287. number = Number(number);
  288. if (number === 0) {
  289. return 0
  290. }
  291. let result = (number / BASE_DEVICE_WIDTH) * (newDeviceWidth || deviceWidth);
  292. if (result < 0) {
  293. result = -result;
  294. }
  295. result = Math.floor(result + EPS);
  296. if (result === 0) {
  297. if (deviceDPR === 1 || !isIOS) {
  298. return 1
  299. } else {
  300. return 0.5
  301. }
  302. }
  303. return number < 0 ? -result : result
  304. }
  305. const interceptors = {
  306. promiseInterceptor
  307. };
  308. var baseApi = /*#__PURE__*/Object.freeze({
  309. __proto__: null,
  310. upx2px: upx2px,
  311. addInterceptor: addInterceptor,
  312. removeInterceptor: removeInterceptor,
  313. interceptors: interceptors
  314. });
  315. var previewImage = {
  316. args (fromArgs) {
  317. let currentIndex = parseInt(fromArgs.current);
  318. if (isNaN(currentIndex)) {
  319. return
  320. }
  321. const urls = fromArgs.urls;
  322. if (!Array.isArray(urls)) {
  323. return
  324. }
  325. const len = urls.length;
  326. if (!len) {
  327. return
  328. }
  329. if (currentIndex < 0) {
  330. currentIndex = 0;
  331. } else if (currentIndex >= len) {
  332. currentIndex = len - 1;
  333. }
  334. if (currentIndex > 0) {
  335. fromArgs.current = urls[currentIndex];
  336. fromArgs.urls = urls.filter(
  337. (item, index) => index < currentIndex ? item !== urls[currentIndex] : true
  338. );
  339. } else {
  340. fromArgs.current = urls[0];
  341. }
  342. return {
  343. indicator: false,
  344. loop: false
  345. }
  346. }
  347. };
  348. // 不支持的 API 列表
  349. const todos = [
  350. // 'hideKeyboard',
  351. // 'onGyroscopeChange',
  352. // 'startGyroscope',
  353. // 'stopGyroscope',
  354. // 'openBluetoothAdapter',
  355. // 'startBluetoothDevicesDiscovery',
  356. // 'onBluetoothDeviceFound',
  357. // 'stopBluetoothDevicesDiscovery',
  358. // 'onBluetoothAdapterStateChange',
  359. // 'getConnectedBluetoothDevices',
  360. // 'getBluetoothDevices',
  361. // 'getBluetoothAdapterState',
  362. // 'closeBluetoothAdapter',
  363. // 'writeBLECharacteristicValue',
  364. // 'readBLECharacteristicValue',
  365. // 'onBLEConnectionStateChange',
  366. // 'onBLECharacteristicValueChange',
  367. // 'notifyBLECharacteristicValueChange',
  368. // 'getBLEDeviceServices',
  369. // 'getBLEDeviceCharacteristics',
  370. // 'createBLEConnection',
  371. // 'closeBLEConnection',
  372. // 'onBeaconServiceChange',
  373. // 'onBeaconUpdate',
  374. // 'getBeacons',
  375. // 'startBeaconDiscovery',
  376. // 'stopBeaconDiscovery',
  377. // 'hideShareMenu',
  378. // 'onWindowResize',
  379. // 'offWindowResize',
  380. // 'vibrate'
  381. ];
  382. // 存在兼容性的 API 列表
  383. const canIUses = [];
  384. function createTodoMethod (contextName, methodName) {
  385. return function unsupported () {
  386. console.error(`百度小程序 ${contextName}暂不支持${methodName}`);
  387. }
  388. }
  389. function _handleEnvInfo (result) {
  390. result.miniProgram = {
  391. appId: result.appKey
  392. };
  393. result.plugin = {
  394. version: result.sdkVersion
  395. };
  396. }
  397. // 需要做转换的 API 列表
  398. const protocols = {
  399. request: {
  400. args (fromArgs) {
  401. // TODO
  402. // data 不支持 ArrayBuffer
  403. // method 不支持 TRACE, CONNECT
  404. return {
  405. method: 'method',
  406. dataType (type) {
  407. return {
  408. name: 'dataType',
  409. value: type === 'json' ? type : 'string'
  410. }
  411. }
  412. }
  413. }
  414. },
  415. connectSocket: {
  416. args: {
  417. method: false
  418. }
  419. },
  420. previewImage,
  421. getRecorderManager: {
  422. returnValue (fromRet) {
  423. fromRet.onFrameRecorded = createTodoMethod('RecorderManager', 'onFrameRecorded');
  424. }
  425. },
  426. getBackgroundAudioManager: {
  427. returnValue (fromRet) {
  428. fromRet.onPrev = createTodoMethod('BackgroundAudioManager', 'onPrev');
  429. fromRet.onNext = createTodoMethod('BackgroundAudioManager', 'onNext');
  430. }
  431. },
  432. scanCode: {
  433. args: {
  434. onlyFromCamera: false,
  435. scanType: false
  436. }
  437. },
  438. navigateToMiniProgram: {
  439. name: 'navigateToSmartProgram',
  440. args: {
  441. appId: 'appKey',
  442. envVersion: false
  443. }
  444. },
  445. navigateBackMiniProgram: {
  446. name: 'navigateBackSmartProgram'
  447. },
  448. showShareMenu: {
  449. name: 'openShare'
  450. },
  451. getAccountInfoSync: {
  452. name: 'getEnvInfoSync',
  453. returnValue: _handleEnvInfo
  454. }
  455. };
  456. const CALLBACKS = ['success', 'fail', 'cancel', 'complete'];
  457. function processCallback (methodName, method, returnValue) {
  458. return function (res) {
  459. return method(processReturnValue(methodName, res, returnValue))
  460. }
  461. }
  462. function processArgs (methodName, fromArgs, argsOption = {}, returnValue = {}, keepFromArgs = false) {
  463. if (isPlainObject(fromArgs)) { // 一般 api 的参数解析
  464. const toArgs = keepFromArgs === true ? fromArgs : {}; // returnValue 为 false 时,说明是格式化返回值,直接在返回值对象上修改赋值
  465. if (isFn(argsOption)) {
  466. argsOption = argsOption(fromArgs, toArgs) || {};
  467. }
  468. for (const key in fromArgs) {
  469. if (hasOwn(argsOption, key)) {
  470. let keyOption = argsOption[key];
  471. if (isFn(keyOption)) {
  472. keyOption = keyOption(fromArgs[key], fromArgs, toArgs);
  473. }
  474. if (!keyOption) { // 不支持的参数
  475. console.warn(`百度小程序 ${methodName}暂不支持${key}`);
  476. } else if (isStr(keyOption)) { // 重写参数 key
  477. toArgs[keyOption] = fromArgs[key];
  478. } else if (isPlainObject(keyOption)) { // {name:newName,value:value}可重新指定参数 key:value
  479. toArgs[keyOption.name ? keyOption.name : key] = keyOption.value;
  480. }
  481. } else if (CALLBACKS.indexOf(key) !== -1) {
  482. toArgs[key] = processCallback(methodName, fromArgs[key], returnValue);
  483. } else {
  484. if (!keepFromArgs) {
  485. toArgs[key] = fromArgs[key];
  486. }
  487. }
  488. }
  489. return toArgs
  490. } else if (isFn(fromArgs)) {
  491. fromArgs = processCallback(methodName, fromArgs, returnValue);
  492. }
  493. return fromArgs
  494. }
  495. function processReturnValue (methodName, res, returnValue, keepReturnValue = false) {
  496. if (isFn(protocols.returnValue)) { // 处理通用 returnValue
  497. res = protocols.returnValue(methodName, res);
  498. }
  499. return processArgs(methodName, res, returnValue, {}, keepReturnValue)
  500. }
  501. function wrapper (methodName, method) {
  502. if (hasOwn(protocols, methodName)) {
  503. const protocol = protocols[methodName];
  504. if (!protocol) { // 暂不支持的 api
  505. return function () {
  506. console.error(`百度小程序 暂不支持${methodName}`);
  507. }
  508. }
  509. return function (arg1, arg2) { // 目前 api 最多两个参数
  510. let options = protocol;
  511. if (isFn(protocol)) {
  512. options = protocol(arg1);
  513. }
  514. arg1 = processArgs(methodName, arg1, options.args, options.returnValue);
  515. const args = [arg1];
  516. if (typeof arg2 !== 'undefined') {
  517. args.push(arg2);
  518. }
  519. const returnValue = swan[options.name || methodName].apply(swan, args);
  520. if (isSyncApi(methodName)) { // 同步 api
  521. return processReturnValue(methodName, returnValue, options.returnValue, isContextApi(methodName))
  522. }
  523. return returnValue
  524. }
  525. }
  526. return method
  527. }
  528. const todoApis = Object.create(null);
  529. const TODOS = [
  530. 'onTabBarMidButtonTap',
  531. 'subscribePush',
  532. 'unsubscribePush',
  533. 'onPush',
  534. 'offPush',
  535. 'share'
  536. ];
  537. function createTodoApi (name) {
  538. return function todoApi ({
  539. fail,
  540. complete
  541. }) {
  542. const res = {
  543. errMsg: `${name}:fail:暂不支持 ${name} 方法`
  544. };
  545. isFn(fail) && fail(res);
  546. isFn(complete) && complete(res);
  547. }
  548. }
  549. TODOS.forEach(function (name) {
  550. todoApis[name] = createTodoApi(name);
  551. });
  552. var providers = {
  553. oauth: ['baidu'],
  554. share: ['baidu'],
  555. payment: ['baidu'],
  556. push: ['baidu']
  557. };
  558. function getProvider ({
  559. service,
  560. success,
  561. fail,
  562. complete
  563. }) {
  564. let res = false;
  565. if (providers[service]) {
  566. res = {
  567. errMsg: 'getProvider:ok',
  568. service,
  569. provider: providers[service]
  570. };
  571. isFn(success) && success(res);
  572. } else {
  573. res = {
  574. errMsg: 'getProvider:fail:服务[' + service + ']不存在'
  575. };
  576. isFn(fail) && fail(res);
  577. }
  578. isFn(complete) && complete(res);
  579. }
  580. var extraApi = /*#__PURE__*/Object.freeze({
  581. __proto__: null,
  582. getProvider: getProvider
  583. });
  584. const getEmitter = (function () {
  585. if (typeof getUniEmitter === 'function') {
  586. /* eslint-disable no-undef */
  587. return getUniEmitter
  588. }
  589. let Emitter;
  590. return function getUniEmitter () {
  591. if (!Emitter) {
  592. Emitter = new Vue();
  593. }
  594. return Emitter
  595. }
  596. })();
  597. function apply (ctx, method, args) {
  598. return ctx[method].apply(ctx, args)
  599. }
  600. function $on () {
  601. return apply(getEmitter(), '$on', [...arguments])
  602. }
  603. function $off () {
  604. return apply(getEmitter(), '$off', [...arguments])
  605. }
  606. function $once () {
  607. return apply(getEmitter(), '$once', [...arguments])
  608. }
  609. function $emit () {
  610. return apply(getEmitter(), '$emit', [...arguments])
  611. }
  612. var eventApi = /*#__PURE__*/Object.freeze({
  613. __proto__: null,
  614. $on: $on,
  615. $off: $off,
  616. $once: $once,
  617. $emit: $emit
  618. });
  619. function requestPayment (params) {
  620. let parseError = false;
  621. if (typeof params.orderInfo === 'string') {
  622. try {
  623. params.orderInfo = JSON.parse(params.orderInfo);
  624. } catch (e) {
  625. parseError = true;
  626. }
  627. }
  628. if (parseError) {
  629. params.fail && params.fail({
  630. errMsg: 'requestPayment:fail: 参数 orderInfo 数据结构不正确,参考:https://uniapp.dcloud.io/api/plugins/payment?id=orderinfo'
  631. });
  632. } else {
  633. swan.requestPolymerPayment(params);
  634. }
  635. }
  636. var api = /*#__PURE__*/Object.freeze({
  637. __proto__: null,
  638. requestPayment: requestPayment
  639. });
  640. const MPPage = Page;
  641. const MPComponent = Component;
  642. const customizeRE = /:/g;
  643. const customize = cached((str) => {
  644. return camelize(str.replace(customizeRE, '-'))
  645. });
  646. function initTriggerEvent (mpInstance) {
  647. const oldTriggerEvent = mpInstance.triggerEvent;
  648. mpInstance.triggerEvent = function (event, ...args) {
  649. return oldTriggerEvent.apply(mpInstance, [customize(event), ...args])
  650. };
  651. }
  652. function initHook (name, options) {
  653. const oldHook = options[name];
  654. if (!oldHook) {
  655. options[name] = function () {
  656. initTriggerEvent(this);
  657. };
  658. } else {
  659. options[name] = function (...args) {
  660. initTriggerEvent(this);
  661. return oldHook.apply(this, args)
  662. };
  663. }
  664. }
  665. Page = function (options = {}) {
  666. initHook('onLoad', options);
  667. return MPPage(options)
  668. };
  669. Component = function (options = {}) {
  670. initHook('created', options);
  671. return MPComponent(options)
  672. };
  673. const PAGE_EVENT_HOOKS = [
  674. 'onPullDownRefresh',
  675. 'onReachBottom',
  676. 'onShareAppMessage',
  677. 'onPageScroll',
  678. 'onResize',
  679. 'onTabItemTap'
  680. ];
  681. function initMocks (vm, mocks) {
  682. const mpInstance = vm.$mp[vm.mpType];
  683. mocks.forEach(mock => {
  684. if (hasOwn(mpInstance, mock)) {
  685. vm[mock] = mpInstance[mock];
  686. }
  687. });
  688. }
  689. function hasHook (hook, vueOptions) {
  690. if (!vueOptions) {
  691. return true
  692. }
  693. if (Vue.options && Array.isArray(Vue.options[hook])) {
  694. return true
  695. }
  696. vueOptions = vueOptions.default || vueOptions;
  697. if (isFn(vueOptions)) {
  698. if (isFn(vueOptions.extendOptions[hook])) {
  699. return true
  700. }
  701. if (vueOptions.super &&
  702. vueOptions.super.options &&
  703. Array.isArray(vueOptions.super.options[hook])) {
  704. return true
  705. }
  706. return false
  707. }
  708. if (isFn(vueOptions[hook])) {
  709. return true
  710. }
  711. const mixins = vueOptions.mixins;
  712. if (Array.isArray(mixins)) {
  713. return !!mixins.find(mixin => hasHook(hook, mixin))
  714. }
  715. }
  716. function initHooks (mpOptions, hooks, vueOptions) {
  717. hooks.forEach(hook => {
  718. if (hasHook(hook, vueOptions)) {
  719. mpOptions[hook] = function (args) {
  720. return this.$vm && this.$vm.__call_hook(hook, args)
  721. };
  722. }
  723. });
  724. }
  725. function initVueComponent (Vue, vueOptions) {
  726. vueOptions = vueOptions.default || vueOptions;
  727. let VueComponent;
  728. if (isFn(vueOptions)) {
  729. VueComponent = vueOptions;
  730. } else {
  731. VueComponent = Vue.extend(vueOptions);
  732. }
  733. vueOptions = VueComponent.options;
  734. return [VueComponent, vueOptions]
  735. }
  736. function initSlots (vm, vueSlots) {
  737. if (Array.isArray(vueSlots) && vueSlots.length) {
  738. const $slots = Object.create(null);
  739. vueSlots.forEach(slotName => {
  740. $slots[slotName] = true;
  741. });
  742. vm.$scopedSlots = vm.$slots = $slots;
  743. }
  744. }
  745. function initVueIds (vueIds, mpInstance) {
  746. vueIds = (vueIds || '').split(',');
  747. const len = vueIds.length;
  748. if (len === 1) {
  749. mpInstance._$vueId = vueIds[0];
  750. } else if (len === 2) {
  751. mpInstance._$vueId = vueIds[0];
  752. mpInstance._$vuePid = vueIds[1];
  753. }
  754. }
  755. function initData (vueOptions, context) {
  756. let data = vueOptions.data || {};
  757. const methods = vueOptions.methods || {};
  758. if (typeof data === 'function') {
  759. try {
  760. data = data.call(context); // 支持 Vue.prototype 上挂的数据
  761. } catch (e) {
  762. if (process.env.VUE_APP_DEBUG) {
  763. console.warn('根据 Vue 的 data 函数初始化小程序 data 失败,请尽量确保 data 函数中不访问 vm 对象,否则可能影响首次数据渲染速度。', data);
  764. }
  765. }
  766. } else {
  767. try {
  768. // 对 data 格式化
  769. data = JSON.parse(JSON.stringify(data));
  770. } catch (e) {}
  771. }
  772. if (!isPlainObject(data)) {
  773. data = {};
  774. }
  775. Object.keys(methods).forEach(methodName => {
  776. if (context.__lifecycle_hooks__.indexOf(methodName) === -1 && !hasOwn(data, methodName)) {
  777. data[methodName] = methods[methodName];
  778. }
  779. });
  780. return data
  781. }
  782. const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
  783. function createObserver (name) {
  784. return function observer (newVal, oldVal) {
  785. if (this.$vm) {
  786. this.$vm[name] = newVal; // 为了触发其他非 render watcher
  787. }
  788. }
  789. }
  790. function initBehaviors (vueOptions, initBehavior) {
  791. const vueBehaviors = vueOptions.behaviors;
  792. const vueExtends = vueOptions.extends;
  793. const vueMixins = vueOptions.mixins;
  794. let vueProps = vueOptions.props;
  795. if (!vueProps) {
  796. vueOptions.props = vueProps = [];
  797. }
  798. const behaviors = [];
  799. if (Array.isArray(vueBehaviors)) {
  800. vueBehaviors.forEach(behavior => {
  801. behaviors.push(behavior.replace('uni://', `${"swan"}://`));
  802. if (behavior === 'uni://form-field') {
  803. if (Array.isArray(vueProps)) {
  804. vueProps.push('name');
  805. vueProps.push('value');
  806. } else {
  807. vueProps.name = {
  808. type: String,
  809. default: ''
  810. };
  811. vueProps.value = {
  812. type: [String, Number, Boolean, Array, Object, Date],
  813. default: ''
  814. };
  815. }
  816. }
  817. });
  818. }
  819. if (isPlainObject(vueExtends) && vueExtends.props) {
  820. behaviors.push(
  821. initBehavior({
  822. properties: initProperties(vueExtends.props, true)
  823. })
  824. );
  825. }
  826. if (Array.isArray(vueMixins)) {
  827. vueMixins.forEach(vueMixin => {
  828. if (isPlainObject(vueMixin) && vueMixin.props) {
  829. behaviors.push(
  830. initBehavior({
  831. properties: initProperties(vueMixin.props, true)
  832. })
  833. );
  834. }
  835. });
  836. }
  837. return behaviors
  838. }
  839. function parsePropType (key, type, defaultValue, file) {
  840. // [String]=>String
  841. if (Array.isArray(type) && type.length === 1) {
  842. return type[0]
  843. }
  844. {
  845. if (
  846. defaultValue === false &&
  847. Array.isArray(type) &&
  848. type.length === 2 &&
  849. type.indexOf(String) !== -1 &&
  850. type.indexOf(Boolean) !== -1
  851. ) { // [String,Boolean]=>Boolean
  852. if (file) {
  853. console.warn(
  854. `props.${key}.type should use Boolean instead of [String,Boolean] at ${file}`
  855. );
  856. }
  857. return Boolean
  858. }
  859. }
  860. return type
  861. }
  862. function initProperties (props, isBehavior = false, file = '') {
  863. const properties = {};
  864. if (!isBehavior) {
  865. properties.vueId = {
  866. type: String,
  867. value: ''
  868. };
  869. properties.vueSlots = { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
  870. type: null,
  871. value: [],
  872. observer: function (newVal, oldVal) {
  873. const $slots = Object.create(null);
  874. newVal.forEach(slotName => {
  875. $slots[slotName] = true;
  876. });
  877. this.setData({
  878. $slots
  879. });
  880. }
  881. };
  882. }
  883. if (Array.isArray(props)) { // ['title']
  884. props.forEach(key => {
  885. properties[key] = {
  886. type: null,
  887. observer: createObserver(key)
  888. };
  889. });
  890. } else if (isPlainObject(props)) { // {title:{type:String,default:''},content:String}
  891. Object.keys(props).forEach(key => {
  892. const opts = props[key];
  893. if (isPlainObject(opts)) { // title:{type:String,default:''}
  894. let value = opts.default;
  895. if (isFn(value)) {
  896. value = value();
  897. }
  898. opts.type = parsePropType(key, opts.type, value, file);
  899. properties[key] = {
  900. type: PROP_TYPES.indexOf(opts.type) !== -1 ? opts.type : null,
  901. value,
  902. observer: createObserver(key)
  903. };
  904. } else { // content:String
  905. const type = parsePropType(key, opts, null, file);
  906. properties[key] = {
  907. type: PROP_TYPES.indexOf(type) !== -1 ? type : null,
  908. observer: createObserver(key)
  909. };
  910. }
  911. });
  912. }
  913. return properties
  914. }
  915. function wrapper$1 (event) {
  916. // TODO 又得兼容 mpvue 的 mp 对象
  917. try {
  918. event.mp = JSON.parse(JSON.stringify(event));
  919. } catch (e) {}
  920. event.stopPropagation = noop;
  921. event.preventDefault = noop;
  922. event.target = event.target || {};
  923. if (!hasOwn(event, 'detail')) {
  924. event.detail = {};
  925. }
  926. if (hasOwn(event, 'markerId')) {
  927. event.detail = typeof event.detail === 'object' ? event.detail : {};
  928. event.detail.markerId = event.markerId;
  929. }
  930. { // mp-baidu,checked=>value
  931. if (
  932. isPlainObject(event.detail) &&
  933. hasOwn(event.detail, 'checked') &&
  934. !hasOwn(event.detail, 'value')
  935. ) {
  936. event.detail.value = event.detail.checked;
  937. }
  938. }
  939. if (isPlainObject(event.detail)) {
  940. event.target = Object.assign({}, event.target, event.detail);
  941. }
  942. return event
  943. }
  944. function getExtraValue (vm, dataPathsArray) {
  945. let context = vm;
  946. dataPathsArray.forEach(dataPathArray => {
  947. const dataPath = dataPathArray[0];
  948. const value = dataPathArray[2];
  949. if (dataPath || typeof value !== 'undefined') { // ['','',index,'disable']
  950. const propPath = dataPathArray[1];
  951. const valuePath = dataPathArray[3];
  952. const vFor = dataPath ? vm.__get_value(dataPath, context) : context;
  953. if (Number.isInteger(vFor)) {
  954. context = value;
  955. } else if (!propPath) {
  956. context = vFor[value];
  957. } else {
  958. if (Array.isArray(vFor)) {
  959. context = vFor.find(vForItem => {
  960. return vm.__get_value(propPath, vForItem) === value
  961. });
  962. } else if (isPlainObject(vFor)) {
  963. context = Object.keys(vFor).find(vForKey => {
  964. return vm.__get_value(propPath, vFor[vForKey]) === value
  965. });
  966. } else {
  967. console.error('v-for 暂不支持循环数据:', vFor);
  968. }
  969. }
  970. if (valuePath) {
  971. context = vm.__get_value(valuePath, context);
  972. }
  973. }
  974. });
  975. return context
  976. }
  977. function processEventExtra (vm, extra, event) {
  978. const extraObj = {};
  979. if (Array.isArray(extra) && extra.length) {
  980. /**
  981. *[
  982. * ['data.items', 'data.id', item.data.id],
  983. * ['metas', 'id', meta.id]
  984. *],
  985. *[
  986. * ['data.items', 'data.id', item.data.id],
  987. * ['metas', 'id', meta.id]
  988. *],
  989. *'test'
  990. */
  991. extra.forEach((dataPath, index) => {
  992. if (typeof dataPath === 'string') {
  993. if (!dataPath) { // model,prop.sync
  994. extraObj['$' + index] = vm;
  995. } else {
  996. if (dataPath === '$event') { // $event
  997. extraObj['$' + index] = event;
  998. } else if (dataPath.indexOf('$event.') === 0) { // $event.target.value
  999. extraObj['$' + index] = vm.__get_value(dataPath.replace('$event.', ''), event);
  1000. } else {
  1001. extraObj['$' + index] = vm.__get_value(dataPath);
  1002. }
  1003. }
  1004. } else {
  1005. extraObj['$' + index] = getExtraValue(vm, dataPath);
  1006. }
  1007. });
  1008. }
  1009. return extraObj
  1010. }
  1011. function getObjByArray (arr) {
  1012. const obj = {};
  1013. for (let i = 1; i < arr.length; i++) {
  1014. const element = arr[i];
  1015. obj[element[0]] = element[1];
  1016. }
  1017. return obj
  1018. }
  1019. function processEventArgs (vm, event, args = [], extra = [], isCustom, methodName) {
  1020. let isCustomMPEvent = false; // wxcomponent 组件,传递原始 event 对象
  1021. if (isCustom) { // 自定义事件
  1022. isCustomMPEvent = event.currentTarget &&
  1023. event.currentTarget.dataset &&
  1024. event.currentTarget.dataset.comType === 'wx';
  1025. if (!args.length) { // 无参数,直接传入 event 或 detail 数组
  1026. if (isCustomMPEvent) {
  1027. return [event]
  1028. }
  1029. return event.detail.__args__ || event.detail
  1030. }
  1031. }
  1032. const extraObj = processEventExtra(vm, extra, event);
  1033. const ret = [];
  1034. args.forEach(arg => {
  1035. if (arg === '$event') {
  1036. if (methodName === '__set_model' && !isCustom) { // input v-model value
  1037. ret.push(event.target.value);
  1038. } else {
  1039. if (isCustom && !isCustomMPEvent) {
  1040. ret.push(event.detail.__args__[0]);
  1041. } else { // wxcomponent 组件或内置组件
  1042. ret.push(event);
  1043. }
  1044. }
  1045. } else {
  1046. if (Array.isArray(arg) && arg[0] === 'o') {
  1047. ret.push(getObjByArray(arg));
  1048. } else if (typeof arg === 'string' && hasOwn(extraObj, arg)) {
  1049. ret.push(extraObj[arg]);
  1050. } else {
  1051. ret.push(arg);
  1052. }
  1053. }
  1054. });
  1055. return ret
  1056. }
  1057. const ONCE = '~';
  1058. const CUSTOM = '^';
  1059. function isMatchEventType (eventType, optType) {
  1060. return (eventType === optType) ||
  1061. (
  1062. optType === 'regionchange' &&
  1063. (
  1064. eventType === 'begin' ||
  1065. eventType === 'end'
  1066. )
  1067. )
  1068. }
  1069. function handleEvent (event) {
  1070. event = wrapper$1(event);
  1071. // [['tap',[['handle',[1,2,a]],['handle1',[1,2,a]]]]]
  1072. const dataset = (event.currentTarget || event.target).dataset;
  1073. if (!dataset) {
  1074. return console.warn('事件信息不存在')
  1075. }
  1076. const eventOpts = dataset.eventOpts || dataset['event-opts']; // 支付宝 web-view 组件 dataset 非驼峰
  1077. if (!eventOpts) {
  1078. return console.warn('事件信息不存在')
  1079. }
  1080. // [['handle',[1,2,a]],['handle1',[1,2,a]]]
  1081. const eventType = event.type;
  1082. const ret = [];
  1083. eventOpts.forEach(eventOpt => {
  1084. let type = eventOpt[0];
  1085. const eventsArray = eventOpt[1];
  1086. const isCustom = type.charAt(0) === CUSTOM;
  1087. type = isCustom ? type.slice(1) : type;
  1088. const isOnce = type.charAt(0) === ONCE;
  1089. type = isOnce ? type.slice(1) : type;
  1090. if (eventsArray && isMatchEventType(eventType, type)) {
  1091. eventsArray.forEach(eventArray => {
  1092. const methodName = eventArray[0];
  1093. if (methodName) {
  1094. let handlerCtx = this.$vm;
  1095. if (
  1096. handlerCtx.$options.generic &&
  1097. handlerCtx.$parent &&
  1098. handlerCtx.$parent.$parent
  1099. ) { // mp-weixin,mp-toutiao 抽象节点模拟 scoped slots
  1100. handlerCtx = handlerCtx.$parent.$parent;
  1101. }
  1102. if (methodName === '$emit') {
  1103. handlerCtx.$emit.apply(handlerCtx,
  1104. processEventArgs(
  1105. this.$vm,
  1106. event,
  1107. eventArray[1],
  1108. eventArray[2],
  1109. isCustom,
  1110. methodName
  1111. ));
  1112. return
  1113. }
  1114. const handler = handlerCtx[methodName];
  1115. if (!isFn(handler)) {
  1116. throw new Error(` _vm.${methodName} is not a function`)
  1117. }
  1118. if (isOnce) {
  1119. if (handler.once) {
  1120. return
  1121. }
  1122. handler.once = true;
  1123. }
  1124. ret.push(handler.apply(handlerCtx, processEventArgs(
  1125. this.$vm,
  1126. event,
  1127. eventArray[1],
  1128. eventArray[2],
  1129. isCustom,
  1130. methodName
  1131. )));
  1132. }
  1133. });
  1134. }
  1135. });
  1136. if (
  1137. eventType === 'input' &&
  1138. ret.length === 1 &&
  1139. typeof ret[0] !== 'undefined'
  1140. ) {
  1141. return ret[0]
  1142. }
  1143. }
  1144. const hooks = [
  1145. 'onShow',
  1146. 'onHide',
  1147. 'onError',
  1148. 'onPageNotFound'
  1149. ];
  1150. function parseBaseApp (vm, {
  1151. mocks,
  1152. initRefs
  1153. }) {
  1154. if (vm.$options.store) {
  1155. Vue.prototype.$store = vm.$options.store;
  1156. }
  1157. Vue.prototype.mpHost = "mp-baidu";
  1158. Vue.mixin({
  1159. beforeCreate () {
  1160. if (!this.$options.mpType) {
  1161. return
  1162. }
  1163. this.mpType = this.$options.mpType;
  1164. this.$mp = {
  1165. data: {},
  1166. [this.mpType]: this.$options.mpInstance
  1167. };
  1168. this.$scope = this.$options.mpInstance;
  1169. delete this.$options.mpType;
  1170. delete this.$options.mpInstance;
  1171. if (this.mpType !== 'app') {
  1172. initRefs(this);
  1173. initMocks(this, mocks);
  1174. }
  1175. }
  1176. });
  1177. const appOptions = {
  1178. onLaunch (args) {
  1179. if (this.$vm) { // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
  1180. return
  1181. }
  1182. this.$vm = vm;
  1183. this.$vm.$mp = {
  1184. app: this
  1185. };
  1186. this.$vm.$scope = this;
  1187. // vm 上也挂载 globalData
  1188. this.$vm.globalData = this.globalData;
  1189. this.$vm._isMounted = true;
  1190. this.$vm.__call_hook('mounted', args);
  1191. this.$vm.__call_hook('onLaunch', args);
  1192. }
  1193. };
  1194. // 兼容旧版本 globalData
  1195. appOptions.globalData = vm.$options.globalData || {};
  1196. // 将 methods 中的方法挂在 getApp() 中
  1197. const methods = vm.$options.methods;
  1198. if (methods) {
  1199. Object.keys(methods).forEach(name => {
  1200. appOptions[name] = methods[name];
  1201. });
  1202. }
  1203. initHooks(appOptions, hooks);
  1204. return appOptions
  1205. }
  1206. function findVmByVueId (vm, vuePid) {
  1207. const $children = vm.$children;
  1208. // 优先查找直属(反向查找:https://github.com/dcloudio/uni-app/issues/1200)
  1209. for (let i = $children.length - 1; i >= 0; i--) {
  1210. const childVm = $children[i];
  1211. if (childVm.$scope._$vueId === vuePid) {
  1212. return childVm
  1213. }
  1214. }
  1215. // 反向递归查找
  1216. let parentVm;
  1217. for (let i = $children.length - 1; i >= 0; i--) {
  1218. parentVm = findVmByVueId($children[i], vuePid);
  1219. if (parentVm) {
  1220. return parentVm
  1221. }
  1222. }
  1223. }
  1224. function initBehavior (options) {
  1225. return Behavior(options)
  1226. }
  1227. function initRefs (vm) {
  1228. const mpInstance = vm.$scope;
  1229. Object.defineProperty(vm, '$refs', {
  1230. get () {
  1231. const $refs = {};
  1232. const components = mpInstance.selectAllComponents('.vue-ref');
  1233. components.forEach(component => {
  1234. const ref = component.dataset.ref;
  1235. $refs[ref] = component.$vm || component;
  1236. });
  1237. const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for');
  1238. forComponents.forEach(component => {
  1239. const ref = component.dataset.ref;
  1240. if (!$refs[ref]) {
  1241. $refs[ref] = [];
  1242. }
  1243. $refs[ref].push(component.$vm || component);
  1244. });
  1245. return $refs
  1246. }
  1247. });
  1248. }
  1249. function handleLink (event) {
  1250. const {
  1251. vuePid,
  1252. vueOptions
  1253. } = event.detail || event.value; // detail 是微信,value 是百度(dipatch)
  1254. let parentVm;
  1255. if (vuePid) {
  1256. parentVm = findVmByVueId(this.$vm, vuePid);
  1257. }
  1258. if (!parentVm) {
  1259. parentVm = this.$vm;
  1260. }
  1261. vueOptions.parent = parentVm;
  1262. }
  1263. const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix'];
  1264. function isPage () {
  1265. return !this.ownerId
  1266. }
  1267. function initRelation (detail) {
  1268. this.dispatch('__l', detail);
  1269. }
  1270. function parseApp (vm) {
  1271. // 百度 onShow 竟然会在 onLaunch 之前
  1272. const appOptions = parseBaseApp(vm, {
  1273. mocks,
  1274. initRefs
  1275. });
  1276. appOptions.onShow = function onShow (args) {
  1277. if (!this.$vm) {
  1278. this.onLaunch(args);
  1279. }
  1280. this.$vm.__call_hook('onShow', args);
  1281. };
  1282. return appOptions
  1283. }
  1284. function createApp (vm) {
  1285. App(parseApp(vm));
  1286. return vm
  1287. }
  1288. function parseBaseComponent (vueComponentOptions, {
  1289. isPage,
  1290. initRelation
  1291. } = {}) {
  1292. const [VueComponent, vueOptions] = initVueComponent(Vue, vueComponentOptions);
  1293. const options = {
  1294. multipleSlots: true,
  1295. addGlobalClass: true,
  1296. ...(vueOptions.options || {})
  1297. };
  1298. const componentOptions = {
  1299. options,
  1300. data: initData(vueOptions, Vue.prototype),
  1301. behaviors: initBehaviors(vueOptions, initBehavior),
  1302. properties: initProperties(vueOptions.props, false, vueOptions.__file),
  1303. lifetimes: {
  1304. attached () {
  1305. const properties = this.properties;
  1306. const options = {
  1307. mpType: isPage.call(this) ? 'page' : 'component',
  1308. mpInstance: this,
  1309. propsData: properties
  1310. };
  1311. initVueIds(properties.vueId, this);
  1312. // 处理父子关系
  1313. initRelation.call(this, {
  1314. vuePid: this._$vuePid,
  1315. vueOptions: options
  1316. });
  1317. // 初始化 vue 实例
  1318. this.$vm = new VueComponent(options);
  1319. // 处理$slots,$scopedSlots(暂不支持动态变化$slots)
  1320. initSlots(this.$vm, properties.vueSlots);
  1321. // 触发首次 setData
  1322. this.$vm.$mount();
  1323. },
  1324. ready () {
  1325. // 当组件 props 默认值为 true,初始化时传入 false 会导致 created,ready 触发, 但 attached 不触发
  1326. // https://developers.weixin.qq.com/community/develop/doc/00066ae2844cc0f8eb883e2a557800
  1327. if (this.$vm) {
  1328. this.$vm._isMounted = true;
  1329. this.$vm.__call_hook('mounted');
  1330. this.$vm.__call_hook('onReady');
  1331. }
  1332. },
  1333. detached () {
  1334. this.$vm && this.$vm.$destroy();
  1335. }
  1336. },
  1337. pageLifetimes: {
  1338. show (args) {
  1339. this.$vm && this.$vm.__call_hook('onPageShow', args);
  1340. },
  1341. hide () {
  1342. this.$vm && this.$vm.__call_hook('onPageHide');
  1343. },
  1344. resize (size) {
  1345. this.$vm && this.$vm.__call_hook('onPageResize', size);
  1346. }
  1347. },
  1348. methods: {
  1349. __l: handleLink,
  1350. __e: handleEvent
  1351. }
  1352. };
  1353. // externalClasses
  1354. if (vueOptions.externalClasses) {
  1355. componentOptions.externalClasses = vueOptions.externalClasses;
  1356. }
  1357. if (Array.isArray(vueOptions.wxsCallMethods)) {
  1358. vueOptions.wxsCallMethods.forEach(callMethod => {
  1359. componentOptions.methods[callMethod] = function (args) {
  1360. return this.$vm[callMethod](args)
  1361. };
  1362. });
  1363. }
  1364. if (isPage) {
  1365. return componentOptions
  1366. }
  1367. return [componentOptions, VueComponent]
  1368. }
  1369. const newLifecycle = swan.canIUse('lifecycle-2-0');
  1370. function parseComponent (vueOptions) {
  1371. const componentOptions = parseBaseComponent(vueOptions, {
  1372. isPage,
  1373. initRelation
  1374. });
  1375. // 关于百度小程序生命周期的说明(组件作为页面时):
  1376. // lifetimes:attached --> methods:onShow --> methods:onLoad --> methods:onReady
  1377. // 这里在强制将onShow挪到onLoad之后触发,另外一处修改在page-parser.js
  1378. const oldAttached = componentOptions.lifetimes.attached;
  1379. componentOptions.lifetimes.attached = function attached () {
  1380. oldAttached.call(this);
  1381. if (isPage.call(this)) { // 百度 onLoad 在 attached 之前触发
  1382. // 百度 当组件作为页面时 pageinstancce 不是原来组件的 instance
  1383. this.pageinstance.$vm = this.$vm;
  1384. if (hasOwn(this.pageinstance, '_$args')) {
  1385. this.$vm.$mp.query = this.pageinstance._$args;
  1386. this.$vm.__call_hook('onLoad', this.pageinstance._$args);
  1387. this.$vm.__call_hook('onShow');
  1388. delete this.pageinstance._$args;
  1389. }
  1390. } else {
  1391. // 百度小程序组件不触发methods内的onReady
  1392. if (this.$vm) {
  1393. this.$vm._isMounted = true;
  1394. this.$vm.__call_hook('mounted');
  1395. }
  1396. }
  1397. };
  1398. if (newLifecycle) {
  1399. delete componentOptions.lifetimes.ready;
  1400. componentOptions.methods.onReady = function () {
  1401. if (this.$vm) {
  1402. this.$vm._isMounted = true;
  1403. this.$vm.__call_hook('mounted');
  1404. this.$vm.__call_hook('onReady');
  1405. }
  1406. };
  1407. }
  1408. componentOptions.messages = {
  1409. __l: componentOptions.methods.__l
  1410. };
  1411. delete componentOptions.methods.__l;
  1412. return componentOptions
  1413. }
  1414. const hooks$1 = [
  1415. 'onShow',
  1416. 'onHide',
  1417. 'onUnload'
  1418. ];
  1419. hooks$1.push(...PAGE_EVENT_HOOKS);
  1420. function parseBasePage (vuePageOptions, {
  1421. isPage,
  1422. initRelation
  1423. }) {
  1424. const pageOptions = parseComponent(vuePageOptions);
  1425. initHooks(pageOptions.methods, hooks$1, vuePageOptions);
  1426. pageOptions.methods.onLoad = function (args) {
  1427. this.$vm.$mp.query = args; // 兼容 mpvue
  1428. this.$vm.__call_hook('onLoad', args);
  1429. };
  1430. return pageOptions
  1431. }
  1432. function detached ($vm) {
  1433. $vm.$children.forEach(childVm => {
  1434. childVm.$scope.detached();
  1435. });
  1436. $vm.$scope.detached();
  1437. }
  1438. function onPageUnload ($vm) {
  1439. $vm.$destroy();
  1440. $vm.$children.forEach(childVm => {
  1441. detached(childVm);
  1442. });
  1443. }
  1444. function parsePage (vuePageOptions) {
  1445. const pageOptions = parseBasePage(vuePageOptions, {
  1446. isPage,
  1447. initRelation
  1448. });
  1449. // 纠正百度小程序生命周期methods:onShow在methods:onLoad之前触发的问题
  1450. pageOptions.methods.onShow = function onShow () {
  1451. if (this.$vm && this.$vm.$mp.query) {
  1452. this.$vm.__call_hook('onShow');
  1453. }
  1454. };
  1455. pageOptions.methods.onLoad = function onLoad (args) {
  1456. // 百度 onLoad 在 attached 之前触发,先存储 args, 在 attached 里边触发 onLoad
  1457. if (this.$vm) {
  1458. this.$vm.$mp.query = args;
  1459. this.$vm.__call_hook('onLoad', args);
  1460. this.$vm.__call_hook('onShow');
  1461. } else {
  1462. this.pageinstance._$args = args;
  1463. }
  1464. };
  1465. pageOptions.methods.onUnload = function onUnload () {
  1466. this.$vm.__call_hook('onUnload');
  1467. onPageUnload(this.$vm);
  1468. };
  1469. return pageOptions
  1470. }
  1471. function createPage (vuePageOptions) {
  1472. {
  1473. return Component(parsePage(vuePageOptions))
  1474. }
  1475. }
  1476. function createComponent (vueOptions) {
  1477. {
  1478. return Component(parseComponent(vueOptions))
  1479. }
  1480. }
  1481. todos.forEach(todoApi => {
  1482. protocols[todoApi] = false;
  1483. });
  1484. canIUses.forEach(canIUseApi => {
  1485. const apiName = protocols[canIUseApi] && protocols[canIUseApi].name ? protocols[canIUseApi].name
  1486. : canIUseApi;
  1487. if (!swan.canIUse(apiName)) {
  1488. protocols[canIUseApi] = false;
  1489. }
  1490. });
  1491. let uni = {};
  1492. if (typeof Proxy !== 'undefined' && "mp-baidu" !== 'app-plus') {
  1493. uni = new Proxy({}, {
  1494. get (target, name) {
  1495. if (target[name]) {
  1496. return target[name]
  1497. }
  1498. if (baseApi[name]) {
  1499. return baseApi[name]
  1500. }
  1501. if (api[name]) {
  1502. return promisify(name, api[name])
  1503. }
  1504. {
  1505. if (extraApi[name]) {
  1506. return promisify(name, extraApi[name])
  1507. }
  1508. if (todoApis[name]) {
  1509. return promisify(name, todoApis[name])
  1510. }
  1511. }
  1512. if (eventApi[name]) {
  1513. return eventApi[name]
  1514. }
  1515. if (!hasOwn(swan, name) && !hasOwn(protocols, name)) {
  1516. return
  1517. }
  1518. return promisify(name, wrapper(name, swan[name]))
  1519. },
  1520. set (target, name, value) {
  1521. target[name] = value;
  1522. return true
  1523. }
  1524. });
  1525. } else {
  1526. Object.keys(baseApi).forEach(name => {
  1527. uni[name] = baseApi[name];
  1528. });
  1529. {
  1530. Object.keys(todoApis).forEach(name => {
  1531. uni[name] = promisify(name, todoApis[name]);
  1532. });
  1533. Object.keys(extraApi).forEach(name => {
  1534. uni[name] = promisify(name, todoApis[name]);
  1535. });
  1536. }
  1537. Object.keys(eventApi).forEach(name => {
  1538. uni[name] = eventApi[name];
  1539. });
  1540. Object.keys(api).forEach(name => {
  1541. uni[name] = promisify(name, api[name]);
  1542. });
  1543. Object.keys(swan).forEach(name => {
  1544. if (hasOwn(swan, name) || hasOwn(protocols, name)) {
  1545. uni[name] = promisify(name, wrapper(name, swan[name]));
  1546. }
  1547. });
  1548. }
  1549. swan.createApp = createApp;
  1550. swan.createPage = createPage;
  1551. swan.createComponent = createComponent;
  1552. var uni$1 = uni;
  1553. export default uni$1;
  1554. export { createApp, createComponent, createPage };