UNPKG

nimble-ui

Version:
166 lines (158 loc) 5.22 kB
import { getBackData, callFn, extend } from 'nimble-lib'; import Service from '../../services'; import { DEFAULT_CONFIG, EVENT_TYPE, PAGE_TYPE, ADDRESS_DATA_TYPE } from '../constants'; export default class AddressBase extends Service { /** * 默认可选参数 */ defaultOption = DEFAULT_CONFIG; /** * 默认地址信息 */ _addressInfo = null; constructor (options) { super(options); const _that = this; _that.setDefaultOptions(options); } /** * 获取用户信息 * @param {*} args 请求参数 * @returns {Promise} */ _getUserInfo (args) { const _that = this; const _options = _that.defaultOption; let _res = _that.getUserPromise || new Promise((resolve, reject) => { if (_options.getUserInfo) { getBackData(_options.getUserInfo, [args]) .then((userInfo) => { resolve(userInfo || {}); }, reject); } else { resolve({ isLogin: false }); } }); _res.then(() => { _that.getUserPromise = null; }, () => { _that.getUserPromise = null; }); _that.getUserPromise = _res; return _res; } /** * 获取接口数据 * @param {String} type 类型 * @param {Object} params 参数 * @param {Object|undefined} headers 请求头 * @returns {Object|Array} 返回接口数据 */ _getData (type, params, headers) { const _that = this; const _options = _that.defaultOption; return getBackData(_options.getAddressData, [type, params, headers]); } /** * 唤起地址库列表 * @param {Object} options 可选参数 * @param {string} options.type 页面类型 * @returns {Promise} */ _toAddressPage (options) { const _that = this; let _res = _that.addressPromise || new Promise((resolve, reject) => { _pageWatch(); const _options = _that.getOptions(options); _that.$emit(EVENT_TYPE.REDIRECT, { type: _options.type || PAGE_TYPE.ADDRESS_LIST, options: _options }); /** * 监听用户页面改变 */ function _pageWatch () { /** * 用户设置了默认值 * @param {Object} data 数据 */ function _handler (data) { _reset(); resolve(data); } /** * 用户选择了地址 * @param {Object} data 数据 */ function select(data) { _that.successfilter(data || null).then(_handler, reject); } /** * 用户取消了选择 * @param {Object} data 数据 */ function cancel(data) { _handler(Object.assign({}, data, { _handleType: 'cancel' })); } /** * 重置 */ function _reset () { _that.addressPromise = null; _that.$off(EVENT_TYPE.CHECKED_ADDRESS, select); _that.$off(EVENT_TYPE.CANCEL_ADDRESS, cancel); } _that.$on(EVENT_TYPE.CHECKED_ADDRESS, select); _that.$on(EVENT_TYPE.CANCEL_ADDRESS, cancel); } }); _that.addressPromise = _res; return _res; } /** * 获取表单配置 * @param {*} addressInfo 地址数据 * @return {Promise} */ getFormConfig(addressInfo) { return this._getData(ADDRESS_DATA_TYPE.GET_FORM_CONFIG, addressInfo); } /** * 选择数据完成 * @param {*} addressInfo 地址数据 * @param {*} isNotEmit 是否不派发事件 * @returns {Promise} * @memberof CarCroe */ successfilter(addressInfo, isNotEmit) { const _that = this; let _options = _that.getOptions(); return new Promise((resolve, reject) => { getBackData(_options.successfilter || addressInfo, [addressInfo]) .then((res) => { resolve(res); if (!isNotEmit && !callFn(_options.diffData || false, [res, _that._nowCarData])) { setTimeout(() => { // 防止页面vuex行取到数据的问题更新接口时有问题 _that.$emit(EVENT_TYPE.SUCCESS, extend(res)); }, 0); } _that._addressInfo = res; }); }); } /** * 提示消息 * * @param {*} msgCont 消息内容 * @param {*} param 类型|参数 * @returns {Promise} * @memberof CarBase */ message(msgCont, param) { let _that = this; let _options = _that.defaultOption; return getBackData(_options.message, [msgCont, param]); } }