uni.mp.esm.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. import { SLOT_DEFAULT_NAME, EventChannel, invokeArrayFns, MINI_PROGRAM_PAGE_RUNTIME_HOOKS, ON_LOAD, ON_SHOW, ON_HIDE, ON_UNLOAD, ON_RESIZE, ON_TAB_ITEM_TAP, ON_REACH_BOTTOM, ON_PULL_DOWN_REFRESH, ON_ADD_TO_FAVORITES, isUniLifecycleHook, ON_READY, once, ON_LAUNCH, ON_ERROR, ON_THEME_CHANGE, ON_PAGE_NOT_FOUND, ON_UNHANDLE_REJECTION, addLeadingSlash, stringifyQuery, customizeEvent } from '@dcloudio/uni-shared';
  2. import { isArray, isFunction, hasOwn, extend, isPlainObject, isObject } from '@vue/shared';
  3. import { ref, nextTick, findComponentPropsData, toRaw, updateProps, hasQueueJob, invalidateJob, devtoolsComponentAdded, getExposeProxy, isRef, pruneComponentPropsCache } from 'vue';
  4. import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n';
  5. const MP_METHODS = [
  6. 'createSelectorQuery',
  7. 'createIntersectionObserver',
  8. 'selectAllComponents',
  9. 'selectComponent',
  10. ];
  11. function createEmitFn(oldEmit, ctx) {
  12. return function emit(event, ...args) {
  13. const scope = ctx.$scope;
  14. if (scope && event) {
  15. const detail = { __args__: args };
  16. {
  17. scope.triggerEvent(event, detail);
  18. }
  19. }
  20. return oldEmit.apply(this, [event, ...args]);
  21. };
  22. }
  23. function initBaseInstance(instance, options) {
  24. const ctx = instance.ctx;
  25. // mp
  26. ctx.mpType = options.mpType; // @deprecated
  27. ctx.$mpType = options.mpType;
  28. ctx.$mpPlatform = "quickapp-webview";
  29. ctx.$scope = options.mpInstance;
  30. // TODO @deprecated
  31. ctx.$mp = {};
  32. if (__VUE_OPTIONS_API__) {
  33. ctx._self = {};
  34. }
  35. // slots
  36. instance.slots = {};
  37. if (isArray(options.slots) && options.slots.length) {
  38. options.slots.forEach((name) => {
  39. instance.slots[name] = true;
  40. });
  41. if (instance.slots[SLOT_DEFAULT_NAME]) {
  42. instance.slots.default = true;
  43. }
  44. }
  45. ctx.getOpenerEventChannel = function () {
  46. if (!this.__eventChannel__) {
  47. this.__eventChannel__ = new EventChannel();
  48. }
  49. return this.__eventChannel__;
  50. };
  51. ctx.$hasHook = hasHook;
  52. ctx.$callHook = callHook;
  53. // $emit
  54. instance.emit = createEmitFn(instance.emit, ctx);
  55. }
  56. function initComponentInstance(instance, options) {
  57. initBaseInstance(instance, options);
  58. const ctx = instance.ctx;
  59. MP_METHODS.forEach((method) => {
  60. ctx[method] = function (...args) {
  61. const mpInstance = ctx.$scope;
  62. if (mpInstance && mpInstance[method]) {
  63. return mpInstance[method].apply(mpInstance, args);
  64. }
  65. };
  66. });
  67. }
  68. function initMocks(instance, mpInstance, mocks) {
  69. const ctx = instance.ctx;
  70. mocks.forEach((mock) => {
  71. if (hasOwn(mpInstance, mock)) {
  72. instance[mock] = ctx[mock] = mpInstance[mock];
  73. }
  74. });
  75. }
  76. function hasHook(name) {
  77. const hooks = this.$[name];
  78. if (hooks && hooks.length) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. function callHook(name, args) {
  84. if (name === 'mounted') {
  85. callHook.call(this, 'bm'); // beforeMount
  86. this.$.isMounted = true;
  87. name = 'm';
  88. }
  89. {
  90. if (name === 'onLoad' &&
  91. args &&
  92. args.__id__ &&
  93. isFunction(qa.getEventChannel)) {
  94. this.__eventChannel__ = qa.getEventChannel(args.__id__);
  95. delete args.__id__;
  96. }
  97. }
  98. const hooks = this.$[name];
  99. return hooks && invokeArrayFns(hooks, args);
  100. }
  101. const PAGE_INIT_HOOKS = [
  102. ON_LOAD,
  103. ON_SHOW,
  104. ON_HIDE,
  105. ON_UNLOAD,
  106. ON_RESIZE,
  107. ON_TAB_ITEM_TAP,
  108. ON_REACH_BOTTOM,
  109. ON_PULL_DOWN_REFRESH,
  110. ON_ADD_TO_FAVORITES,
  111. // 'onReady', // lifetimes.ready
  112. // 'onPageScroll', // 影响性能,开发者手动注册
  113. // 'onShareTimeline', // 右上角菜单,开发者手动注册
  114. // 'onShareAppMessage' // 右上角菜单,开发者手动注册
  115. ];
  116. function findHooks(vueOptions, hooks = new Set()) {
  117. if (vueOptions) {
  118. Object.keys(vueOptions).forEach((name) => {
  119. if (isUniLifecycleHook(name, vueOptions[name])) {
  120. hooks.add(name);
  121. }
  122. });
  123. if (__VUE_OPTIONS_API__) {
  124. const { extends: extendsOptions, mixins } = vueOptions;
  125. if (mixins) {
  126. mixins.forEach((mixin) => findHooks(mixin, hooks));
  127. }
  128. if (extendsOptions) {
  129. findHooks(extendsOptions, hooks);
  130. }
  131. }
  132. }
  133. return hooks;
  134. }
  135. function initHook(mpOptions, hook, excludes) {
  136. if (excludes.indexOf(hook) === -1 && !hasOwn(mpOptions, hook)) {
  137. mpOptions[hook] = function (args) {
  138. return this.$vm && this.$vm.$callHook(hook, args);
  139. };
  140. }
  141. }
  142. const EXCLUDE_HOOKS = [ON_READY];
  143. function initHooks(mpOptions, hooks, excludes = EXCLUDE_HOOKS) {
  144. hooks.forEach((hook) => initHook(mpOptions, hook, excludes));
  145. }
  146. function initUnknownHooks(mpOptions, vueOptions, excludes = EXCLUDE_HOOKS) {
  147. findHooks(vueOptions).forEach((hook) => initHook(mpOptions, hook, excludes));
  148. }
  149. function initRuntimeHooks(mpOptions, runtimeHooks) {
  150. if (!runtimeHooks) {
  151. return;
  152. }
  153. const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS);
  154. hooks.forEach((hook) => {
  155. if (runtimeHooks & MINI_PROGRAM_PAGE_RUNTIME_HOOKS[hook]) {
  156. initHook(mpOptions, hook, []);
  157. }
  158. });
  159. }
  160. const findMixinRuntimeHooks = /*#__PURE__*/ once(() => {
  161. const runtimeHooks = [];
  162. const app = isFunction(getApp) && getApp({ allowDefault: true });
  163. if (app && app.$vm && app.$vm.$) {
  164. const mixins = app.$vm.$.appContext.mixins;
  165. if (isArray(mixins)) {
  166. const hooks = Object.keys(MINI_PROGRAM_PAGE_RUNTIME_HOOKS);
  167. mixins.forEach((mixin) => {
  168. hooks.forEach((hook) => {
  169. if (hasOwn(mixin, hook) && !runtimeHooks.includes(hook)) {
  170. runtimeHooks.push(hook);
  171. }
  172. });
  173. });
  174. }
  175. }
  176. return runtimeHooks;
  177. });
  178. function initMixinRuntimeHooks(mpOptions) {
  179. initHooks(mpOptions, findMixinRuntimeHooks());
  180. }
  181. const HOOKS = [
  182. ON_SHOW,
  183. ON_HIDE,
  184. ON_ERROR,
  185. ON_THEME_CHANGE,
  186. ON_PAGE_NOT_FOUND,
  187. ON_UNHANDLE_REJECTION,
  188. ];
  189. function parseApp(instance, parseAppOptions) {
  190. const internalInstance = instance.$;
  191. if (__VUE_PROD_DEVTOOLS__) {
  192. // 定制 App 的 $children
  193. Object.defineProperty(internalInstance.ctx, '$children', {
  194. get() {
  195. return getCurrentPages().map((page) => page.$vm);
  196. },
  197. });
  198. }
  199. const appOptions = {
  200. globalData: (instance.$options && instance.$options.globalData) || {},
  201. $vm: instance,
  202. onLaunch(options) {
  203. this.$vm = instance; // 飞书小程序可能会把 AppOptions 序列化,导致 $vm 对象部分属性丢失
  204. const ctx = internalInstance.ctx;
  205. if (this.$vm && ctx.$scope) {
  206. // 已经初始化过了,主要是为了百度,百度 onShow 在 onLaunch 之前
  207. return;
  208. }
  209. initBaseInstance(internalInstance, {
  210. mpType: 'app',
  211. mpInstance: this,
  212. slots: [],
  213. });
  214. ctx.globalData = this.globalData;
  215. instance.$callHook(ON_LAUNCH, options);
  216. },
  217. };
  218. const { onError } = internalInstance;
  219. if (onError) {
  220. internalInstance.appContext.config.errorHandler = (err) => {
  221. instance.$callHook(ON_ERROR, err);
  222. };
  223. }
  224. initLocale(instance);
  225. const vueOptions = instance.$.type;
  226. initHooks(appOptions, HOOKS);
  227. initUnknownHooks(appOptions, vueOptions);
  228. if (__VUE_OPTIONS_API__) {
  229. const methods = vueOptions.methods;
  230. methods && extend(appOptions, methods);
  231. }
  232. if (parseAppOptions) {
  233. parseAppOptions.parse(appOptions);
  234. }
  235. return appOptions;
  236. }
  237. function initCreateApp(parseAppOptions) {
  238. return function createApp(vm) {
  239. return App(parseApp(vm, parseAppOptions));
  240. };
  241. }
  242. function initCreateSubpackageApp(parseAppOptions) {
  243. return function createApp(vm) {
  244. const appOptions = parseApp(vm, parseAppOptions);
  245. const app = isFunction(getApp) &&
  246. getApp({
  247. allowDefault: true,
  248. });
  249. if (!app)
  250. return;
  251. vm.$.ctx.$scope = app;
  252. const globalData = app.globalData;
  253. if (globalData) {
  254. Object.keys(appOptions.globalData).forEach((name) => {
  255. if (!hasOwn(globalData, name)) {
  256. globalData[name] = appOptions.globalData[name];
  257. }
  258. });
  259. }
  260. Object.keys(appOptions).forEach((name) => {
  261. if (!hasOwn(app, name)) {
  262. app[name] = appOptions[name];
  263. }
  264. });
  265. initAppLifecycle(appOptions, vm);
  266. if (process.env.UNI_SUBPACKAGE) {
  267. (qa.$subpackages || (qa.$subpackages = {}))[process.env.UNI_SUBPACKAGE] = {
  268. $vm: vm,
  269. };
  270. }
  271. };
  272. }
  273. function initAppLifecycle(appOptions, vm) {
  274. if (isFunction(appOptions.onLaunch)) {
  275. const args = qa.getLaunchOptionsSync && qa.getLaunchOptionsSync();
  276. appOptions.onLaunch(args);
  277. }
  278. if (isFunction(appOptions.onShow) && qa.onAppShow) {
  279. qa.onAppShow((args) => {
  280. vm.$callHook('onShow', args);
  281. });
  282. }
  283. if (isFunction(appOptions.onHide) && qa.onAppHide) {
  284. qa.onAppHide((args) => {
  285. vm.$callHook('onHide', args);
  286. });
  287. }
  288. }
  289. function initLocale(appVm) {
  290. const locale = ref(normalizeLocale(qa.getSystemInfoSync().language) || LOCALE_EN);
  291. Object.defineProperty(appVm, '$locale', {
  292. get() {
  293. return locale.value;
  294. },
  295. set(v) {
  296. locale.value = v;
  297. },
  298. });
  299. }
  300. function initVueIds(vueIds, mpInstance) {
  301. if (!vueIds) {
  302. return;
  303. }
  304. const ids = vueIds.split(',');
  305. const len = ids.length;
  306. if (len === 1) {
  307. mpInstance._$vueId = ids[0];
  308. }
  309. else if (len === 2) {
  310. mpInstance._$vueId = ids[0];
  311. mpInstance._$vuePid = ids[1];
  312. }
  313. }
  314. const EXTRAS = ['externalClasses'];
  315. function initExtraOptions(miniProgramComponentOptions, vueOptions) {
  316. EXTRAS.forEach((name) => {
  317. if (hasOwn(vueOptions, name)) {
  318. miniProgramComponentOptions[name] = vueOptions[name];
  319. }
  320. });
  321. }
  322. function initWxsCallMethods(methods, wxsCallMethods) {
  323. if (!isArray(wxsCallMethods)) {
  324. return;
  325. }
  326. wxsCallMethods.forEach((callMethod) => {
  327. methods[callMethod] = function (args) {
  328. return this.$vm[callMethod](args);
  329. };
  330. });
  331. }
  332. function selectAllComponents(mpInstance, selector, $refs) {
  333. const components = mpInstance.selectAllComponents(selector);
  334. components.forEach((component) => {
  335. const ref = component.properties.uR;
  336. $refs[ref] = component.$vm || component;
  337. });
  338. }
  339. function initRefs(instance, mpInstance) {
  340. Object.defineProperty(instance, 'refs', {
  341. get() {
  342. const $refs = {};
  343. selectAllComponents(mpInstance, '.r', $refs);
  344. const forComponents = mpInstance.selectAllComponents('.r-i-f');
  345. forComponents.forEach((component) => {
  346. const ref = component.properties.uR;
  347. if (!ref) {
  348. return;
  349. }
  350. if (!$refs[ref]) {
  351. $refs[ref] = [];
  352. }
  353. $refs[ref].push(component.$vm || component);
  354. });
  355. return $refs;
  356. },
  357. });
  358. }
  359. function nextSetDataTick(mpInstance, fn) {
  360. // 随便设置一个字段来触发回调(部分平台必须有字段才可以,比如头条)
  361. mpInstance.setData({ r1: 1 }, () => fn());
  362. }
  363. function initSetRef(mpInstance) {
  364. if (!mpInstance._$setRef) {
  365. mpInstance._$setRef = (fn) => {
  366. nextTick(() => nextSetDataTick(mpInstance, fn));
  367. };
  368. }
  369. }
  370. const builtInProps = [
  371. // 百度小程序,快手小程序自定义组件不支持绑定动态事件,动态dataset,故通过props传递事件信息
  372. // event-opts
  373. 'eO',
  374. // 组件 ref
  375. 'uR',
  376. // 组件 ref-in-for
  377. 'uRIF',
  378. // 组件 id
  379. 'uI',
  380. // 组件类型 m: 小程序组件
  381. 'uT',
  382. // 组件 props
  383. 'uP',
  384. // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
  385. 'uS',
  386. ];
  387. function initDefaultProps(options, isBehavior = false) {
  388. const properties = {};
  389. if (!isBehavior) {
  390. // 均不指定类型,避免微信小程序 property received type-uncompatible value 警告
  391. builtInProps.forEach((name) => {
  392. properties[name] = {
  393. type: null,
  394. value: '',
  395. };
  396. });
  397. // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
  398. properties.uS = {
  399. type: null,
  400. value: [],
  401. observer: function (newVal) {
  402. const $slots = Object.create(null);
  403. newVal &&
  404. newVal.forEach((slotName) => {
  405. $slots[slotName] = true;
  406. });
  407. this.setData({
  408. $slots,
  409. });
  410. },
  411. };
  412. }
  413. if (options.behaviors) {
  414. // wx://form-field
  415. if (options.behaviors.includes('qa://form-field')) {
  416. if (!options.properties || !options.properties.name) {
  417. properties.name = {
  418. type: null,
  419. value: '',
  420. };
  421. }
  422. if (!options.properties || !options.properties.value) {
  423. properties.value = {
  424. type: null,
  425. value: '',
  426. };
  427. }
  428. }
  429. }
  430. return properties;
  431. }
  432. function initVirtualHostProps(options) {
  433. const properties = {};
  434. return properties;
  435. }
  436. /**
  437. *
  438. * @param mpComponentOptions
  439. * @param isBehavior
  440. */
  441. function initProps(mpComponentOptions) {
  442. if (!mpComponentOptions.properties) {
  443. mpComponentOptions.properties = {};
  444. }
  445. extend(mpComponentOptions.properties, initDefaultProps(mpComponentOptions), initVirtualHostProps(mpComponentOptions.options));
  446. }
  447. const PROP_TYPES = [String, Number, Boolean, Object, Array, null];
  448. function parsePropType(type, defaultValue) {
  449. // [String]=>String
  450. if (isArray(type) && type.length === 1) {
  451. return type[0];
  452. }
  453. return type;
  454. }
  455. function normalizePropType(type, defaultValue) {
  456. const res = parsePropType(type);
  457. return PROP_TYPES.indexOf(res) !== -1 ? res : null;
  458. }
  459. /**
  460. * 初始化页面 props,方便接收页面参数,类型均为String,默认值均为''
  461. * @param param
  462. * @param rawProps
  463. */
  464. function initPageProps({ properties }, rawProps) {
  465. if (isArray(rawProps)) {
  466. rawProps.forEach((key) => {
  467. properties[key] = {
  468. type: String,
  469. value: '',
  470. };
  471. });
  472. }
  473. else if (isPlainObject(rawProps)) {
  474. Object.keys(rawProps).forEach((key) => {
  475. const opts = rawProps[key];
  476. if (isPlainObject(opts)) {
  477. // title:{type:String,default:''}
  478. let value = opts.default;
  479. if (isFunction(value)) {
  480. value = value();
  481. }
  482. const type = opts.type;
  483. opts.type = normalizePropType(type);
  484. properties[key] = {
  485. type: opts.type,
  486. value,
  487. };
  488. }
  489. else {
  490. // content:String
  491. properties[key] = {
  492. type: normalizePropType(opts),
  493. };
  494. }
  495. });
  496. }
  497. }
  498. function findPropsData(properties, isPage) {
  499. return ((isPage
  500. ? findPagePropsData(properties)
  501. : findComponentPropsData(properties.uP)) || {});
  502. }
  503. function findPagePropsData(properties) {
  504. const propsData = {};
  505. if (isPlainObject(properties)) {
  506. Object.keys(properties).forEach((name) => {
  507. if (builtInProps.indexOf(name) === -1) {
  508. propsData[name] = properties[name];
  509. }
  510. });
  511. }
  512. return propsData;
  513. }
  514. function initFormField(vm) {
  515. // 同步 form-field 的 name,value 值
  516. const vueOptions = vm.$options;
  517. if (isArray(vueOptions.behaviors) &&
  518. vueOptions.behaviors.includes('uni://form-field')) {
  519. vm.$watch('modelValue', () => {
  520. vm.$scope &&
  521. vm.$scope.setData({
  522. name: vm.name,
  523. value: vm.modelValue,
  524. });
  525. }, {
  526. immediate: true,
  527. });
  528. }
  529. }
  530. function initData(_) {
  531. return {};
  532. }
  533. function initPropsObserver(componentOptions) {
  534. const observe = function observe() {
  535. const up = this.properties.uP;
  536. if (!up) {
  537. return;
  538. }
  539. if (this.$vm) {
  540. updateComponentProps(up, this.$vm.$);
  541. }
  542. else if (this.properties.uT === 'm') {
  543. // 小程序组件
  544. updateMiniProgramComponentProperties(up, this);
  545. }
  546. };
  547. {
  548. componentOptions.properties.uP.observer = observe;
  549. }
  550. }
  551. function updateMiniProgramComponentProperties(up, mpInstance) {
  552. const prevProps = mpInstance.properties;
  553. const nextProps = findComponentPropsData(up) || {};
  554. if (hasPropsChanged(prevProps, nextProps, false)) {
  555. mpInstance.setData(nextProps);
  556. }
  557. }
  558. function updateComponentProps(up, instance) {
  559. const prevProps = toRaw(instance.props);
  560. const nextProps = findComponentPropsData(up) || {};
  561. if (hasPropsChanged(prevProps, nextProps)) {
  562. updateProps(instance, nextProps, prevProps, false);
  563. if (hasQueueJob(instance.update)) {
  564. invalidateJob(instance.update);
  565. }
  566. {
  567. instance.update();
  568. }
  569. }
  570. }
  571. function hasPropsChanged(prevProps, nextProps, checkLen = true) {
  572. const nextKeys = Object.keys(nextProps);
  573. if (checkLen && nextKeys.length !== Object.keys(prevProps).length) {
  574. return true;
  575. }
  576. for (let i = 0; i < nextKeys.length; i++) {
  577. const key = nextKeys[i];
  578. if (nextProps[key] !== prevProps[key]) {
  579. return true;
  580. }
  581. }
  582. return false;
  583. }
  584. function initBehaviors(vueOptions) {
  585. const vueBehaviors = vueOptions.behaviors;
  586. let vueProps = vueOptions.props;
  587. if (!vueProps) {
  588. vueOptions.props = vueProps = [];
  589. }
  590. const behaviors = [];
  591. if (isArray(vueBehaviors)) {
  592. vueBehaviors.forEach((behavior) => {
  593. behaviors.push(behavior.replace('uni://', 'qa://'));
  594. if (behavior === 'uni://form-field') {
  595. if (isArray(vueProps)) {
  596. vueProps.push('name');
  597. vueProps.push('modelValue');
  598. }
  599. else {
  600. vueProps.name = {
  601. type: String,
  602. default: '',
  603. };
  604. vueProps.modelValue = {
  605. type: [String, Number, Boolean, Array, Object, Date],
  606. default: '',
  607. };
  608. }
  609. }
  610. });
  611. }
  612. return behaviors;
  613. }
  614. function applyOptions(componentOptions, vueOptions) {
  615. componentOptions.data = initData();
  616. componentOptions.behaviors = initBehaviors(vueOptions);
  617. }
  618. function parseComponent(vueOptions, { parse, mocks, isPage, initRelation, handleLink, initLifetimes, }) {
  619. vueOptions = vueOptions.default || vueOptions;
  620. const options = {
  621. multipleSlots: true,
  622. // styleIsolation: 'apply-shared',
  623. addGlobalClass: true,
  624. pureDataPattern: /^uP$/,
  625. };
  626. if (isArray(vueOptions.mixins)) {
  627. vueOptions.mixins.forEach((item) => {
  628. if (isObject(item.options)) {
  629. extend(options, item.options);
  630. }
  631. });
  632. }
  633. if (vueOptions.options) {
  634. extend(options, vueOptions.options);
  635. }
  636. const mpComponentOptions = {
  637. options,
  638. lifetimes: initLifetimes({ mocks, isPage, initRelation, vueOptions }),
  639. pageLifetimes: {
  640. show() {
  641. if (__VUE_PROD_DEVTOOLS__) {
  642. devtoolsComponentAdded(this.$vm.$);
  643. }
  644. this.$vm && this.$vm.$callHook('onPageShow');
  645. },
  646. hide() {
  647. this.$vm && this.$vm.$callHook('onPageHide');
  648. },
  649. resize(size) {
  650. this.$vm && this.$vm.$callHook('onPageResize', size);
  651. },
  652. },
  653. methods: {
  654. __l: handleLink,
  655. },
  656. };
  657. if (__VUE_OPTIONS_API__) {
  658. applyOptions(mpComponentOptions, vueOptions);
  659. }
  660. initProps(mpComponentOptions);
  661. initPropsObserver(mpComponentOptions);
  662. initExtraOptions(mpComponentOptions, vueOptions);
  663. initWxsCallMethods(mpComponentOptions.methods, vueOptions.wxsCallMethods);
  664. if (parse) {
  665. parse(mpComponentOptions, { handleLink });
  666. }
  667. return mpComponentOptions;
  668. }
  669. function initCreateComponent(parseOptions) {
  670. return function createComponent(vueComponentOptions) {
  671. return Component(parseComponent(vueComponentOptions, parseOptions));
  672. };
  673. }
  674. let $createComponentFn;
  675. let $destroyComponentFn;
  676. function getAppVm() {
  677. if (process.env.UNI_MP_PLUGIN) {
  678. return qa.$vm;
  679. }
  680. if (process.env.UNI_SUBPACKAGE) {
  681. return qa.$subpackages[process.env.UNI_SUBPACKAGE].$vm;
  682. }
  683. return getApp().$vm;
  684. }
  685. function $createComponent(initialVNode, options) {
  686. if (!$createComponentFn) {
  687. $createComponentFn = getAppVm().$createComponent;
  688. }
  689. const proxy = $createComponentFn(initialVNode, options);
  690. return getExposeProxy(proxy.$) || proxy;
  691. }
  692. function $destroyComponent(instance) {
  693. if (!$destroyComponentFn) {
  694. $destroyComponentFn = getAppVm().$destroyComponent;
  695. }
  696. return $destroyComponentFn(instance);
  697. }
  698. function parsePage(vueOptions, parseOptions) {
  699. const { parse, mocks, isPage, initRelation, handleLink, initLifetimes } = parseOptions;
  700. const miniProgramPageOptions = parseComponent(vueOptions, {
  701. mocks,
  702. isPage,
  703. initRelation,
  704. handleLink,
  705. initLifetimes,
  706. });
  707. initPageProps(miniProgramPageOptions, (vueOptions.default || vueOptions).props);
  708. const methods = miniProgramPageOptions.methods;
  709. methods.onLoad = function (query) {
  710. this.options = query;
  711. this.$page = {
  712. fullPath: addLeadingSlash(this.route + stringifyQuery(query)),
  713. };
  714. return this.$vm && this.$vm.$callHook(ON_LOAD, query);
  715. };
  716. initHooks(methods, PAGE_INIT_HOOKS);
  717. {
  718. initUnknownHooks(methods, vueOptions);
  719. }
  720. initRuntimeHooks(methods, vueOptions.__runtimeHooks);
  721. initMixinRuntimeHooks(methods);
  722. parse && parse(miniProgramPageOptions, { handleLink });
  723. return miniProgramPageOptions;
  724. }
  725. function initCreatePage(parseOptions) {
  726. return function createPage(vuePageOptions) {
  727. return Component(parsePage(vuePageOptions, parseOptions));
  728. };
  729. }
  730. const MPPage = Page;
  731. const MPComponent = Component;
  732. function initTriggerEvent(mpInstance) {
  733. const oldTriggerEvent = mpInstance.triggerEvent;
  734. const newTriggerEvent = function (event, ...args) {
  735. return oldTriggerEvent.apply(mpInstance, [customizeEvent(event), ...args]);
  736. };
  737. // 京东小程序triggerEvent为只读属性
  738. try {
  739. mpInstance.triggerEvent = newTriggerEvent;
  740. }
  741. catch (error) {
  742. mpInstance._triggerEvent = newTriggerEvent;
  743. }
  744. }
  745. function initMiniProgramHook(name, options, isComponent) {
  746. const oldHook = options[name];
  747. if (!oldHook) {
  748. options[name] = function () {
  749. initTriggerEvent(this);
  750. };
  751. }
  752. else {
  753. options[name] = function (...args) {
  754. initTriggerEvent(this);
  755. return oldHook.apply(this, args);
  756. };
  757. }
  758. }
  759. Page = function (options) {
  760. initMiniProgramHook(ON_LOAD, options);
  761. return MPPage(options);
  762. };
  763. Component = function (options) {
  764. initMiniProgramHook('created', options);
  765. // 小程序组件
  766. const isVueComponent = options.properties && options.properties.uP;
  767. if (!isVueComponent) {
  768. initProps(options);
  769. initPropsObserver(options);
  770. }
  771. return MPComponent(options);
  772. };
  773. function provide(instance, key, value) {
  774. if (!instance) {
  775. if ((process.env.NODE_ENV !== 'production')) {
  776. console.warn(`provide() can only be used inside setup().`);
  777. }
  778. }
  779. else {
  780. let provides = instance.provides;
  781. // by default an instance inherits its parent's provides object
  782. // but when it needs to provide values of its own, it creates its
  783. // own provides object using parent provides object as prototype.
  784. // this way in `inject` we can simply look up injections from direct
  785. // parent and let the prototype chain do the work.
  786. const parentProvides = instance.parent && instance.parent.provides;
  787. if (parentProvides === provides) {
  788. provides = instance.provides = Object.create(parentProvides);
  789. }
  790. // TS doesn't allow symbol as index type
  791. provides[key] = value;
  792. }
  793. }
  794. function initProvide(instance) {
  795. const provideOptions = instance.$options.provide;
  796. if (!provideOptions) {
  797. return;
  798. }
  799. const provides = isFunction(provideOptions)
  800. ? provideOptions.call(instance)
  801. : provideOptions;
  802. const internalInstance = instance.$;
  803. for (const key in provides) {
  804. provide(internalInstance, key, provides[key]);
  805. }
  806. }
  807. function inject(instance, key, defaultValue, treatDefaultAsFactory = false) {
  808. if (instance) {
  809. // #2400
  810. // to support `app.use` plugins,
  811. // fallback to appContext's `provides` if the intance is at root
  812. const provides = instance.parent == null
  813. ? instance.vnode.appContext && instance.vnode.appContext.provides
  814. : instance.parent.provides;
  815. if (provides && key in provides) {
  816. // TS doesn't allow symbol as index type
  817. return provides[key];
  818. }
  819. else if (arguments.length > 1) {
  820. return treatDefaultAsFactory && isFunction(defaultValue)
  821. ? defaultValue()
  822. : defaultValue;
  823. }
  824. else if ((process.env.NODE_ENV !== 'production')) {
  825. console.warn(`injection "${String(key)}" not found.`);
  826. }
  827. }
  828. else if ((process.env.NODE_ENV !== 'production')) {
  829. console.warn(`inject() can only be used inside setup() or functional components.`);
  830. }
  831. }
  832. function initInjections(instance) {
  833. const injectOptions = instance.$options.inject;
  834. if (!injectOptions) {
  835. return;
  836. }
  837. const internalInstance = instance.$;
  838. const ctx = internalInstance.ctx;
  839. if (isArray(injectOptions)) {
  840. for (let i = 0; i < injectOptions.length; i++) {
  841. const key = injectOptions[i];
  842. ctx[key] = inject(internalInstance, key);
  843. }
  844. }
  845. else {
  846. for (const key in injectOptions) {
  847. const opt = injectOptions[key];
  848. let injected;
  849. if (isObject(opt)) {
  850. if ('default' in opt) {
  851. injected = inject(internalInstance, opt.from || key, opt.default, true /* treat default function as factory */);
  852. }
  853. else {
  854. injected = inject(internalInstance, opt.from || key);
  855. }
  856. }
  857. else {
  858. injected = inject(internalInstance, opt);
  859. }
  860. if (isRef(injected)) {
  861. Object.defineProperty(ctx, key, {
  862. enumerable: true,
  863. configurable: true,
  864. get: () => injected.value,
  865. set: (v) => (injected.value = v),
  866. });
  867. }
  868. else {
  869. ctx[key] = injected;
  870. }
  871. }
  872. }
  873. }
  874. // @ts-ignore
  875. function initLifetimes$1({ mocks, isPage, initRelation, vueOptions, }) {
  876. function attached() {
  877. initSetRef(this);
  878. const properties = this.properties;
  879. initVueIds(properties.uI, this);
  880. const relationOptions = {
  881. vuePid: this._$vuePid,
  882. };
  883. // 初始化 vue 实例
  884. const mpInstance = this;
  885. const mpType = isPage(mpInstance) ? 'page' : 'component';
  886. if (mpType === 'page' && !mpInstance.route && mpInstance.__route__) {
  887. mpInstance.route = mpInstance.__route__;
  888. }
  889. const props = findPropsData(properties, mpType === 'page');
  890. this.$vm = $createComponent({
  891. type: vueOptions,
  892. props,
  893. }, {
  894. mpType,
  895. mpInstance,
  896. slots: properties.uS || {},
  897. parentComponent: relationOptions.parent && relationOptions.parent.$,
  898. onBeforeSetup(instance, options) {
  899. initRefs(instance, mpInstance);
  900. initMocks(instance, mpInstance, mocks);
  901. initComponentInstance(instance, options);
  902. },
  903. });
  904. if (mpType === 'component') {
  905. initFormField(this.$vm);
  906. }
  907. if (mpType === 'page') {
  908. if (__VUE_OPTIONS_API__) {
  909. initInjections(this.$vm);
  910. initProvide(this.$vm);
  911. }
  912. }
  913. // 处理父子关系
  914. initRelation(this, relationOptions);
  915. }
  916. function detached() {
  917. if (this.$vm) {
  918. pruneComponentPropsCache(this.$vm.$.uid);
  919. $destroyComponent(this.$vm);
  920. }
  921. }
  922. {
  923. return { attached, detached };
  924. }
  925. }
  926. const instances = Object.create(null);
  927. function parse(componentOptions, { handleLink }) {
  928. componentOptions.methods.__l = handleLink;
  929. }
  930. function initLifetimes(lifetimesOptions) {
  931. return extend(initLifetimes$1(lifetimesOptions), {
  932. ready() {
  933. if (this.$vm && lifetimesOptions.isPage(this)) {
  934. if (this.pageinstance) {
  935. this.__webviewId__ = this.pageinstance.__pageId__;
  936. }
  937. this.$vm.$callCreatedHook();
  938. nextSetDataTick(this, () => {
  939. this.$vm.$callHook('mounted');
  940. this.$vm.$callHook(ON_READY);
  941. });
  942. }
  943. else {
  944. this.is && console.warn(this.is + ' is not ready');
  945. }
  946. },
  947. detached() {
  948. this.$vm && $destroyComponent(this.$vm);
  949. // 清理
  950. const webviewId = this.__webviewId__;
  951. webviewId &&
  952. Object.keys(instances).forEach((key) => {
  953. if (key.indexOf(webviewId + '_') === 0) {
  954. delete instances[key];
  955. }
  956. });
  957. },
  958. });
  959. }
  960. const mocks = ['nodeId', 'componentName', '_componentId', 'uniquePrefix'];
  961. function isPage(mpInstance) {
  962. return !!mpInstance.methods.onLoad;
  963. }
  964. function initRelation(mpInstance) {
  965. // triggerEvent 后,接收事件时机特别晚,已经到了 ready 之后
  966. const nodeId = mpInstance.nodeId + '';
  967. const webviewId = mpInstance.pageinstance.__pageId__ + '';
  968. instances[webviewId + '_' + nodeId] = mpInstance.$vm;
  969. mpInstance.triggerEvent('__l', {
  970. nodeId,
  971. webviewId,
  972. });
  973. }
  974. function handleLink({ detail: { nodeId, webviewId }, }) {
  975. const vm = instances[webviewId + '_' + nodeId];
  976. if (!vm) {
  977. return;
  978. }
  979. let parentVm = instances[webviewId + '_' + vm.$scope.ownerId];
  980. if (!parentVm) {
  981. parentVm = this.$vm;
  982. }
  983. vm.$.parent = parentVm.$;
  984. const createdVm = function () {
  985. if (__VUE_OPTIONS_API__) {
  986. parentVm.$children.push(vm);
  987. const parent = parentVm.$;
  988. vm.$.provides = parent
  989. ? parent.provides
  990. : Object.create(parent.appContext.provides);
  991. initInjections(vm);
  992. initProvide(vm);
  993. }
  994. vm.$callCreatedHook();
  995. };
  996. const mountedVm = function () {
  997. // 处理当前 vm 子
  998. if (vm._$childVues) {
  999. vm._$childVues.forEach(([createdVm]) => createdVm());
  1000. vm._$childVues.forEach(([, mountedVm]) => mountedVm());
  1001. delete vm._$childVues;
  1002. }
  1003. vm.$callHook('mounted');
  1004. vm.$callHook(ON_READY);
  1005. };
  1006. // 当 parentVm 已经 mounted 时,直接触发,否则延迟
  1007. if (!parentVm || parentVm.$.isMounted) {
  1008. createdVm();
  1009. mountedVm();
  1010. }
  1011. else {
  1012. (parentVm._$childVues || (parentVm._$childVues = [])).push([
  1013. createdVm,
  1014. mountedVm,
  1015. ]);
  1016. }
  1017. }
  1018. var parseComponentOptions = /*#__PURE__*/Object.freeze({
  1019. __proto__: null,
  1020. handleLink: handleLink,
  1021. initLifetimes: initLifetimes$1,
  1022. initRelation: initRelation,
  1023. isPage: isPage,
  1024. mocks: mocks,
  1025. parse: parse
  1026. });
  1027. var parsePageOptions = /*#__PURE__*/Object.freeze({
  1028. __proto__: null,
  1029. handleLink: handleLink,
  1030. initLifetimes: initLifetimes,
  1031. initRelation: initRelation,
  1032. isPage: isPage,
  1033. mocks: mocks,
  1034. parse: parse
  1035. });
  1036. const createApp = initCreateApp();
  1037. const createPage = initCreatePage(parsePageOptions);
  1038. const createComponent = initCreateComponent(parseComponentOptions);
  1039. const createSubpackageApp = initCreateSubpackageApp();
  1040. qa.EventChannel = EventChannel;
  1041. qa.createApp = global.createApp = createApp;
  1042. qa.createPage = createPage;
  1043. qa.createComponent = createComponent;
  1044. qa.createSubpackageApp = global.createSubpackageApp =
  1045. createSubpackageApp;
  1046. export { createApp, createComponent, createPage, createSubpackageApp };