123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- const wxApi = {
- /**
- * @description 统一接口调用
- * @param {String} api 微信接口名称
- * @param {Object} options 微信接口传参
- */
- _api(api, options) {
- options = options || {};
- for (let i in options) {
- options[i] === null && delete options[i];
- }
- if (!wx[api]) return this._error('call', 'wxApi', api, 'not a wxapi');
- const success = options.success;
- options.success = res => {
- res.data && (res = res.data);
- success && success(res);
- };
- !options.fail && (options.fail = res => {
- this._error('api', api, res);
- });
- wx[api](options);
- },
- /**
- * @description 统一错误处理
- * @param {String} type 错误类型
- * @param {String} info 错误描述
- * @param {Object} res 具体错误数据
- * @param {Int} level 错误等级
- */
- _error(type, info, res, level) {
- console.log({
- type: type,
- info: info,
- res: res,
- level: level
- });
- },
- showToast(params, type) {
- params = {
- title: params.title || '',
- mask: params.mask || true,
- icon: params.icon || 'none'
- }
- if (type && type === 'loading') {
- this._api('showLoading', params);
- } else {
- params.duration = 2000;
- this._api('showToast', params);
- }
- },
- hideToast(type) {
- if (type && type === 'loading') {
- this._api('hideLoading');
- } else {
- this._api('hideToast');
- }
- },
- getLocation(cb) {
- const _this = this;
- this.getSetting({
- success(res) {
- if (!res.authSetting['scope.userLocation']) {
- _this.authorize({
- scope: 'scope.userLocation',
- success() {
- wx.getLocation({
- type: 'wgs84',
- success(res) {
- cb && cb(res);
- }
- });
- },
- fail(err) {
- global.router.goto({
- path: '/pages/auth/index',
- type: global.ROUTER_TYPE.CLOSE_ALL,
- query: {
- type: 'getLocation'
- }
- });
- }
- });
- } else {
- wx.getLocation({
- type: 'wgs84',
- success(res) {
- cb && cb(res);
- }
- });
- }
- },
- fail(err) {
- _this.showToast({
- title: '获取用户设置失败,请稍后重试'
- });
- }
- })
- },
- getSetting(params) {
- this._api('getSetting', params);
- },
- authorize(params) {
- this._api('authorize', params);
- },
- showConfirm(title, content, success, confirmText, cancelText = '取消', confirmColor = '', cancelColor = '') {
- this._api('showModal', {
- title: title,
- content: content,
- cancelText: cancelText,
- cancelColor: cancelColor,
- confirmText: confirmText,
- confirmColor: confirmColor,
- success: success
- });
- },
- openSetting(success) {
- this._api('openSetting', {
- success: success
- });
- },
- scanCode(params) {
- this._api('scanCode', params);
- },
- openLocation(params) {
- this._api('openLocation', params);
- },
- requestPayment(params) {
- this._api('requestPayment', params);
- }
- };
-
- export default wxApi;
|