UNPKG

icx-meum-vue-common-module

Version:

icx-meum-vue-common-module

184 lines (178 loc) 5.28 kB
/** * Created by wangbagang on 2017/5/25. */ import * as CONST from '../icx/const' import wechat from '../config/wechat' import { isMeumApp, getBaseUrl, isNullObj } from '../icx/utils' import { CHANGE_LOADING_STATE, GET_PERSON_INFO, CHANGE_CURRENT_PERSON_ID } from '../store/mutation-types' import { mapMutations, mapActions, mapGetters } from 'vuex' export default { data () { return { WebViewJavascriptBridge: null } }, created: function () { wechat.setWechatShareDefault() // 如果是觅我app,需要获取当前app中的当前person信息 if (isMeumApp()) { const _self = this document.addEventListener('WebViewJavascriptBridgeReady', function () { _self.WebViewJavascriptBridge = window.WebViewJavascriptBridge }, false) if (window.WebViewJavascriptBridge) { this.getAppPerson() } } this.startInit(!isMeumApp()) }, watch: { /** * 一旦获取到个人信息,开始执行页面的入口方法 * @param value */ currentPersonInfo: function (value) { if (value && value.personId && this.startLoadingPage) { this.startLoadingPage() } }, WebViewJavascriptBridge () { this.getAppPerson() } }, activated () { if (this.currentPersonId === '') { this.startInit(true) } }, computed: { ...mapGetters([ 'personInfo', 'currentPersonInfo', 'currentPersonId' ]) }, methods: { /** * 包含loading的页面必须以startPage页面为开始方法,否则loading就得手动调用CHANGE_LOADING_STATE事件 */ async startInit (isLoadInfo) { if (typeof this.startLoadingPage === 'function') { const _self = this this.CHANGE_LOADING_STATE({ state: CONST.INT_LOADING, buttonClick: () => _self.startInit(isLoadInfo), loadingText: null, buttonText: null }) if (isLoadInfo) { const response = await this.getPerson(this.currentPersonId) if (this.isApiResponseOk(response) && isNullObj(this.personInfo) && this.currentPersonId !== CONST.INT_MEUM_PERSON_ID) { await this.getPerson('') } } } }, /** * 加载成功,给用户一个加载的过程(timer) */ loadingSuccess () { const _self = this setTimeout(function () { _self.CHANGE_LOADING_STATE({ state: CONST.INT_LOADING_SUCCESS }) }) }, /** * 加载失败 */ loadingFail () { this.CHANGE_LOADING_STATE({ state: CONST.INT_LOADING_FAIL }) }, /** * 获取app的personId */ getAppPerson () { if (isMeumApp()) { const _self = this window.WebViewJavascriptBridge.callHandler(CONST.APP_METHOD_GET_CURRENT_PERSON_ID, {}, function (response) { if (typeof response != 'undefined' && response != null) { _self.CHANGE_CURRENT_PERSON_ID(response == 0 ? '' : response) _self.startInit(true) } }) } }, /** * 加载完成,可以改变按钮文字等等 * @param loadingParam */ loadingFinish (loadingParam) { this.CHANGE_LOADING_STATE(loadingParam) }, /** * 设置页面标题 * @param title */ setPageTitle (title = CONST.STRING_APP_NAME) { let $body = document.body document.title = title let $iframe = document.createElement('iframe') $iframe.src = 'static/favicon.ico' $iframe.addEventListener('load', function () { setTimeout(function () { $iframe.removeEventListener('load', null) $body.removeChild($iframe) }, 0) }) $body.appendChild($iframe) }, /** * 请求是否需要登录 * @param response * @returns {boolean} */ isApiToLogin (response) { return response.errorCode === CONST.INT_LOGIN_STRONG || response.errorCode === CONST.INT_LOGIN_WEAK }, /** * 请求是否成功 * @param response * @returns {*|boolean} */ isApiResponseOk (response) { return response && response.errorCode === CONST.INT_REQUEST_OK }, /** * 如果当前页面可能会在app中使用的话,请使用此方法打开新页面或者超链接 * @param location */ openWebViewWithBack (location) { if (typeof location === 'string' && location.indexOf('http') !== -1) { if (isMeumApp()) { window.WebViewJavascriptBridge.callHandler(CONST.APP_METHOD_START_WEBVIEW_ACTIVITY_WITH_BACK, location, function (response) {}) } else { window.location.href = location } } else if (typeof location === 'object') { if (isMeumApp()) { var url = this.$router.resolve(location) window.WebViewJavascriptBridge.callHandler(CONST.APP_METHOD_START_WEBVIEW_ACTIVITY_WITH_BACK, getBaseUrl(`${window.document.location.host}${url.href}`), function (response) {}) } else { this.$router.push(location) } } }, ...mapActions([ GET_PERSON_INFO ]), ...mapMutations({ CHANGE_LOADING_STATE, CHANGE_CURRENT_PERSON_ID }) } }