From 020faa4ea954b6b66e7de1da559a96fb58e84841 Mon Sep 17 00:00:00 2001 From: liuxiaohang <283700113@qq.com> Date: Thu, 26 Aug 2021 21:28:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(mp-toutiao):=20=E4=BF=AE=E5=A4=8D=20?= =?UTF-8?q?=E5=AD=97=E8=8A=82=E5=B0=8F=E7=A8=8B=E5=BA=8F$emit=E4=B8=8D?= =?UTF-8?q?=E8=A7=A6=E5=8F=91=20fixed=20#2774?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Conflicts: # packages/uni-mp-toutiao/dist/index.js --- packages/uni-mp-toutiao/dist/index.js | 390 ++++++++++++----------- src/platforms/mp-weixin/runtime/index.js | 8 +- 2 files changed, 203 insertions(+), 195 deletions(-) diff --git a/packages/uni-mp-toutiao/dist/index.js b/packages/uni-mp-toutiao/dist/index.js index 489ac3aac..54dcb784d 100644 --- a/packages/uni-mp-toutiao/dist/index.js +++ b/packages/uni-mp-toutiao/dist/index.js @@ -1,53 +1,53 @@ import Vue from 'vue'; -function b64DecodeUnicode (str) { - return decodeURIComponent(atob(str).split('').map(function (c) { - return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) - }).join('')) -} - -function getCurrentUserInfo () { - const token = ( tt).getStorageSync('uni_id_token') || ''; - const tokenArr = token.split('.'); - if (!token || tokenArr.length !== 3) { - return { - uid: null, - role: [], - permission: [], - tokenExpired: 0 - } - } - let userInfo; - try { - userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1])); - } catch (error) { - throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message) - } - userInfo.tokenExpired = userInfo.exp * 1000; - delete userInfo.exp; - delete userInfo.iat; - return userInfo -} - -function uniIdMixin (Vue) { - Vue.prototype.uniIDHasRole = function (roleId) { - const { - role - } = getCurrentUserInfo(); - return role.indexOf(roleId) > -1 - }; - Vue.prototype.uniIDHasPermission = function (permissionId) { - const { - permission - } = getCurrentUserInfo(); - return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1 - }; - Vue.prototype.uniIDTokenValid = function () { - const { - tokenExpired - } = getCurrentUserInfo(); - return tokenExpired > Date.now() - }; +function b64DecodeUnicode (str) { + return decodeURIComponent(atob(str).split('').map(function (c) { + return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) + }).join('')) +} + +function getCurrentUserInfo () { + const token = ( tt).getStorageSync('uni_id_token') || ''; + const tokenArr = token.split('.'); + if (!token || tokenArr.length !== 3) { + return { + uid: null, + role: [], + permission: [], + tokenExpired: 0 + } + } + let userInfo; + try { + userInfo = JSON.parse(b64DecodeUnicode(tokenArr[1])); + } catch (error) { + throw new Error('获取当前用户信息出错,详细错误信息为:' + error.message) + } + userInfo.tokenExpired = userInfo.exp * 1000; + delete userInfo.exp; + delete userInfo.iat; + return userInfo +} + +function uniIdMixin (Vue) { + Vue.prototype.uniIDHasRole = function (roleId) { + const { + role + } = getCurrentUserInfo(); + return role.indexOf(roleId) > -1 + }; + Vue.prototype.uniIDHasPermission = function (permissionId) { + const { + permission + } = getCurrentUserInfo(); + return this.uniIDHasRole('admin') || permission.indexOf(permissionId) > -1 + }; + Vue.prototype.uniIDTokenValid = function () { + const { + tokenExpired + } = getCurrentUserInfo(); + return tokenExpired > Date.now() + }; } const _toString = Object.prototype.toString; @@ -581,44 +581,44 @@ var previewImage = { } }; -const UUID_KEY = '__DC_STAT_UUID'; -let deviceId; -function addUuid (result) { - deviceId = deviceId || tt.getStorageSync(UUID_KEY); - if (!deviceId) { - deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7); - tt.setStorage({ - key: UUID_KEY, - data: deviceId - }); - } - result.deviceId = deviceId; -} - -function addSafeAreaInsets (result) { - if (result.safeArea) { - const safeArea = result.safeArea; - result.safeAreaInsets = { - top: safeArea.top, - left: safeArea.left, - right: result.windowWidth - safeArea.right, - bottom: result.windowHeight - safeArea.bottom - }; - } -} - -var getSystemInfo = { - returnValue: function (result) { - addUuid(result); - addSafeAreaInsets(result); - } +const UUID_KEY = '__DC_STAT_UUID'; +let deviceId; +function addUuid (result) { + deviceId = deviceId || tt.getStorageSync(UUID_KEY); + if (!deviceId) { + deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7); + tt.setStorage({ + key: UUID_KEY, + data: deviceId + }); + } + result.deviceId = deviceId; +} + +function addSafeAreaInsets (result) { + if (result.safeArea) { + const safeArea = result.safeArea; + result.safeAreaInsets = { + top: safeArea.top, + left: safeArea.left, + right: result.windowWidth - safeArea.right, + bottom: result.windowHeight - safeArea.bottom + }; + } +} + +var getSystemInfo = { + returnValue: function (result) { + addUuid(result); + addSafeAreaInsets(result); + } }; -const oName = 'getUserInfo'; -const nName = 'getUserProfile'; - -var getUserProfile = { - name: tt.canIUse(nName) ? nName : oName +const oName = 'getUserInfo'; +const nName = 'getUserProfile'; + +var getUserProfile = { + name: tt.canIUse(nName) ? nName : oName }; // 不支持的 API 列表 @@ -978,96 +978,96 @@ var eventApi = /*#__PURE__*/Object.freeze({ $emit: $emit }); -function createMediaQueryObserver () { - const mediaQueryObserver = {}; - const { - windowWidth, - windowHeight - } = tt.getSystemInfoSync(); - - const orientation = windowWidth < windowHeight ? 'portrait' : 'landscape'; - - mediaQueryObserver.observe = (options, callback) => { - let matches = true; - for (const item in options) { - const itemValue = item === 'orientation' ? options[item] : Number(options[item]); - if (options[item] !== '') { - if (item === 'width') { - if (itemValue === windowWidth) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - if (item === 'minWidth') { - if (windowWidth >= itemValue) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - if (item === 'maxWidth') { - if (windowWidth <= itemValue) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - - if (item === 'height') { - if (itemValue === windowHeight) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - if (item === 'minHeight') { - if (windowHeight >= itemValue) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - if (item === 'maxHeight') { - if (windowHeight <= itemValue) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - - if (item === 'orientation') { - if (options[item] === orientation) { - matches = true; - } else { - matches = false; - callback(matches); - return matches - } - } - } - } - callback(matches); - - return matches - }; - - mediaQueryObserver.disconnect = () => { - }; - - return mediaQueryObserver +function createMediaQueryObserver () { + const mediaQueryObserver = {}; + const { + windowWidth, + windowHeight + } = tt.getSystemInfoSync(); + + const orientation = windowWidth < windowHeight ? 'portrait' : 'landscape'; + + mediaQueryObserver.observe = (options, callback) => { + let matches = true; + for (const item in options) { + const itemValue = item === 'orientation' ? options[item] : Number(options[item]); + if (options[item] !== '') { + if (item === 'width') { + if (itemValue === windowWidth) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + if (item === 'minWidth') { + if (windowWidth >= itemValue) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + if (item === 'maxWidth') { + if (windowWidth <= itemValue) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + + if (item === 'height') { + if (itemValue === windowHeight) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + if (item === 'minHeight') { + if (windowHeight >= itemValue) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + if (item === 'maxHeight') { + if (windowHeight <= itemValue) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + + if (item === 'orientation') { + if (options[item] === orientation) { + matches = true; + } else { + matches = false; + callback(matches); + return matches + } + } + } + } + callback(matches); + + return matches + }; + + mediaQueryObserver.disconnect = () => { + }; + + return mediaQueryObserver } var api = /*#__PURE__*/Object.freeze({ @@ -1091,7 +1091,11 @@ function initTriggerEvent (mpInstance) { }; } -function initHook (name, options) { +function initHook (name, options, isComponent) { + { + // fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created + isComponent && (options = options.lifetimes); + } const oldHook = options[name]; if (!oldHook) { options[name] = function () { @@ -1113,7 +1117,7 @@ if (!MPPage.__$wrappered) { Page.after = MPPage.after; Component = function (options = {}) { - initHook('created', options); + initHook('created', options, true); return MPComponent(options) }; } @@ -2297,23 +2301,23 @@ function createSubpackageApp (vm) { return vm } -function createPlugin (vm) { - const appOptions = parseApp(vm); - if (isFn(appOptions.onShow) && tt.onAppShow) { - tt.onAppShow((...args) => { - appOptions.onShow.apply(vm, args); - }); - } - if (isFn(appOptions.onHide) && tt.onAppHide) { - tt.onAppHide((...args) => { - appOptions.onHide.apply(vm, args); - }); - } - if (isFn(appOptions.onLaunch)) { - const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync(); - appOptions.onLaunch.call(vm, args); - } - return vm +function createPlugin (vm) { + const appOptions = parseApp(vm); + if (isFn(appOptions.onShow) && tt.onAppShow) { + tt.onAppShow((...args) => { + appOptions.onShow.apply(vm, args); + }); + } + if (isFn(appOptions.onHide) && tt.onAppHide) { + tt.onAppHide((...args) => { + appOptions.onHide.apply(vm, args); + }); + } + if (isFn(appOptions.onLaunch)) { + const args = tt.getLaunchOptionsSync && tt.getLaunchOptionsSync(); + appOptions.onLaunch.call(vm, args); + } + return vm } todos.forEach(todoApi => { diff --git a/src/platforms/mp-weixin/runtime/index.js b/src/platforms/mp-weixin/runtime/index.js index 2e3a1fc71..d35d55cf1 100644 --- a/src/platforms/mp-weixin/runtime/index.js +++ b/src/platforms/mp-weixin/runtime/index.js @@ -24,7 +24,11 @@ function initTriggerEvent (mpInstance) { } } -function initHook (name, options) { +function initHook (name, options, isComponent) { + if (__PLATFORM__ === 'mp-toutiao') { + // fix by Lxh 字节自定义组件Component构造器文档上写有created,但是实测只触发了lifetimes上的created + isComponent && (options = options.lifetimes) + } const oldHook = options[name] if (!oldHook) { options[name] = function () { @@ -46,7 +50,7 @@ if (!MPPage.__$wrappered) { Page.after = MPPage.after Component = function (options = {}) { - initHook('created', options) + initHook('created', options, true) return MPComponent(options) } } -- GitLab