|
- import * as API from './API';
-
- let APP = getApp();
- let FUNCTION_TEXT = 'function';
- /*判断是否iphonex*/
- function isIPhoneX() {
- let screenHeight = wx.getSystemInfoSync().screenHeight
- let bottom = wx.getSystemInfoSync().safeArea.bottom
- return screenHeight !== bottom
- }
-
- /*获取当前页url*/
- function getCurrentPageUrl() {
- var pages = getCurrentPages()
- var currentPage = pages[pages.length - 1]
- var url = currentPage.route
- return url
- }
-
- /*获取当前页带参数的url*/
- function getCurrentPageUrlWithArgs() {
- var pages = getCurrentPages()
- var currentPage = pages[pages.length - 1]
- var url = currentPage.route
- var options = currentPage.options
- var urlWithArgs = url + '?'
- for (var key in options) {
- var value = options[key]
- urlWithArgs += key + '=' + value + '&'
- }
- urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
- return urlWithArgs
- }
-
- /**
- * 无图标,纯文本Toast提示
- */
- function showToastNoneIcon(title) {
- if (title == undefined || title == '') {
- title = '';
- }
- return wx.showToast({
- title: title,
- icon: 'none',
- });
- }
- /**
- * 显示Modal弹框(无取消按钮,只显示)
- * @param {标题} title
- * @param {内容} content
- * @param {按钮文字,默认确定} confirmText
- */
- function showModalNoneCancel(title, content, confirmText) {
- wx.showModal({
- title: title,
- showCancel: false,
- content: content,
- confirmText: (typeof confirmText == 'undefined' || confirmText == '') ? '确定' : confirmText,
- success: function (e) {
-
- }
- })
- }
- /**
- * 显示完整的Modal弹框提示
- * @param {标题} title
- * @param {内容} content
- * @param {确认} confirmText
- * @param {取消} cancelText
- * @param {回调} param4
- */
- function showModalOnClick(title, content, confirmText, cancelText, { confirm, cancel }) {
- wx.showModal({
- title: title,
- content: content,
- confirmText: (typeof confirmText == 'undefined' || confirmText == '') ? '确定' : confirmText,
- cancelText: (typeof cancelText == 'undefined' || cancelText == '') ? '取消' : cancelText,
- success: function (e) {
- if (e.confirm) {
- confirm();
- } else if (e.cancel) {
- cancel();
- }
-
- }
- })
- }
- /**
- * 有背景图层的loading
- * * @param {内容} content
- */
- function showLoadingHaveMask(content) {
- wx.showLoading({
- title: (typeof content == "undefined" || content == '') ? '加载中...' : content,
- mask: true
- })
- }
- /**
- * 隐藏loading
- */
- function hideLoadingHaveMask() {
- wx.hideLoading();
- }
-
- /**
- * 全局通用网络请求方法,默认post传输
- */
- function httpRequest(url, data, {
- success,
- fail,
- complete
- }) {
- wx.showNavigationBarLoading();
- let finalData = {};
- Object.assign(finalData, data);
- // finalData.token = getApp().globalData.userInfo.token;
- wx.request({
- url,
- data: finalData,
- method: data.method || 'POST',
- timeout: 60000,
- header: {
- 'Authorization':'Bearer '+getApp().globalData.userInfo.token
- },
- success: function (response) {
- if (response.data && response.data._code == API.INVALID_USER_TOKEN_CODE) {
- //微信登陆失效
- showToastNoneIcon(API.MSG_INVALID_USER_TOKEN);
- wx.navigateTo({
- url: API.USER_LOGIN_PAGE_PATH,
- })
-
- } else if (typeof success === FUNCTION_TEXT) {
- success(response.data);
- }
- },
- fail: function (response) {
- if (typeof fail === FUNCTION_TEXT) {
- fail(handleFail(response));
- } else {
- showToastNoneIcon(API.MSG_FAIL_HTTP);
- }
- },
- complete: function (response) {
- if (typeof complete === FUNCTION_TEXT) {
- if (response.data && response.data._code == API.SUCCESS_CODE) {
- complete(response.data);
- } else {
- complete(handleFail(response.data));
- }
- }
- wx.hideNavigationBarLoading();
- }
- })
- }
- /**
- * 网络访问(无其他处理)
- * @param {地址} url
- * @param {参数} data
- * @param {方法 get or post} method
- * @param {回调} param3
- */
- function httpRequestNoneDetal(url, data, method, {
- success,
- fail,
- complete
- }) {
- wx.showNavigationBarLoading();
- wx.request({
- url,
- data: data,
- method: method,
- success: function (response) {
-
- if (typeof success === FUNCTION_TEXT) {
- success(response.data);
- }
- },
- fail: function (response) {
- if (typeof fail === FUNCTION_TEXT) {
- fail(handleFail(response));
- } else {
- showToastNoneIcon(API.MSG_FAIL_HTTP);
- }
- },
- complete: function (response) {
- if (typeof complete === FUNCTION_TEXT) {
- if (response.data && response.data._code == API.SUCCESS_CODE) {
- complete(response.data);
- } else {
- complete(handleFail(response.data));
- }
- }
- wx.hideNavigationBarLoading();
- }
- })
-
- }
- /**
- * 调用失败
- */
- function handleFail(data = '') {
- let { _msg = API.MSG_FAIL_HTTP, _code = 10001, _data = '', } = data;
- return {
- _code,
- _data,
- _msg
- }
- }
- /**
- * 微信授权 获取信息
- */
- // function initSQFromWX() {
- // let that = this;
- // wx.getSetting({
- // success(res) {
-
- // // if (res.authSetting['scope.userInfo']) {
- // // console.log('个人信息:authSetting 已授权');
- // // } else {
- // // console.log('个人信息:authSetting 未授权');
- // // }
- // if (res.authSetting['scope.userLocation']) {
- // //定位已开启,暂不做功能处理
- // console.log('定位:authSetting 已授权');
- // getApp().globalData.setInfo.locationOpenIdWX = true;
- // } else {
- // //定位未开启,申请授权
- // wx.authorize({
- // scope: 'scope.userLocation',
- // success() {
- // //定位已开启,暂不做功能处理
- // console.log('定位:wx.authorize success');
- // getApp().globalData.setInfo.locationOpenIdWX = true;
- // }
- // ,
- // fail() {
- // //如果之前已经拒绝过,直接返回fail 不弹窗
- // console.log('定位:wx.authorize fail');
- // getApp().globalData.setInfo.locationOpenIdWX = false;
- // wx.navigateTo({
- // url: '/pages/wxAuth/wxAuth',
- // })
-
- // }
-
- // })
-
- // }
- // },
- // fail(e) {
- // },
- // complete() {
- // }
- // });
- // }
-
- /**
- * 获取微信Code
- */
- function getCOdeFromWX({ complate }) {
- showLoadingHaveMask('正在加载数据..');
- wx.login({
- success: function (data) {
- console.log(data)
- complate(data.code);
- hideLoadingHaveMask()
- },
- fail: function (err) {
- hideLoadingHaveMask();
- showModalNoneCancel("温馨提示", "登陆失败,建议请重新打开小程序")
- }
- })
- }
- /**
- * ,获取到的微信用户信息(昵称、头像、省市 赋值给globalData.wxUserInfo)
- */
- function getUserInfoFomWX({ success }) {
- wx.getUserProfile({
- desc: '用于完善会员资料',
- success: res => {
- getApp().globalData.wxUserInfo.nickName = res.userInfo.nickName;
- getApp().globalData.wxUserInfo.avatarUrl = res.userInfo.avatarUrl;
- //不再返回 强制返回“”
- // getApp().globalData.wxUserInfo.province = res.userInfo.province;
- // getApp().globalData.wxUserInfo.city = res.userInfo.city;
- console.log("获取到个人信息:" + res.userInfo.nickName);
- success(res);
-
- },
- complete: res => {
- }
-
- })
- }
- /**
- * 获取地理位置
- * @param {回调} param0
- */
- function getLocationFromWX({ success, fail }) {
- wx.getLocation({
- type: 'wgs84',
- success(res) {
- getApp().globalData.setInfo.latitude = res.latitude;
- getApp().globalData.setInfo.longitude = res.longitude;
- success();
- }
- , fail(res) {
- showToastNoneIcon('获取地理信息失败');
- fail(res);
- }
- })
-
- }
-
-
- function convert_length(length) {
- return Math.round(wx.getSystemInfoSync().windowWidth * length / 750);
- }
- /**
- * 比较版本号(参数'1.11.0', '1.9.9',返回1)
- * @param {*} v1
- * @param {*} v2
- */
- function compareVersion(v1, v2) {
- v1 = v1.split('.')
- v2 = v2.split('.')
- const len = Math.max(v1.length, v2.length)
-
- while (v1.length < len) {
- v1.push('0')
- }
- while (v2.length < len) {
- v2.push('0')
- }
-
- for (let i = 0; i < len; i++) {
- const num1 = parseInt(v1[i])
- const num2 = parseInt(v2[i])
-
- if (num1 > num2) {
- return 1
- } else if (num1 < num2) {
- return -1
- }
- }
- return 0
- }
- export {
- getCurrentPageUrl,
- getCurrentPageUrlWithArgs,
- showToastNoneIcon,
- showModalNoneCancel,
- showModalOnClick,
- showLoadingHaveMask,
- hideLoadingHaveMask,
- httpRequest,
- httpRequestNoneDetal,
- getCOdeFromWX,
- getLocationFromWX,
- getUserInfoFomWX,
- convert_length,
- isIPhoneX
- }
|