123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- import store from '@/store'
- import config from '@/config'
- import { getToken } from '@/utils/auth'
- import errorCode from '@/utils/errorCode'
- let timeout = 10000
- const baseUrl = config.baseUrl
- /**
- * 显示消息提示框
- * @param content 提示的标题
- */
- export function toast(content) {
- uni.showToast({
- icon: 'none',
- title: content
- })
- }
- /**
- * 显示模态弹窗
- * @param content 提示的标题
- */
- export function showConfirm(content) {
- return new Promise((resolve, reject) => {
- uni.showModal({
- title: '提示',
- content: content,
- cancelText: '取消',
- confirmText: '确定',
- success: function(res) {
- resolve(res)
- }
- })
- })
- }
- // 字典值匹配
- export function selectDictValue(datas, value) {
- var actions = [];
- Object.keys(datas).some((key) => {
- if (datas[key].dictValue == ('' + value)) {
- actions.push(datas[key].dictLabel);
- return true;
- }
- })
- return actions.join('');
- }
- export function selectDictLabel(datas, value) {
- var actions = [];
- Object.keys(datas).some((key) => {
- if (datas[key].dictLabel == ('' + value)) {
- actions.push(datas[key].dictValue);
- return true;
- }
- })
- return actions.join('');
- }
- export function selectValueKey(datas, value) {
- var actions = [];
- var idx=0;
- Object.keys(datas).some((key) => {
- if (datas[key].value == ('' + value)) {
- idx=key;
- actions.push(datas[key].label);
- return true;
- }
- })
- var newObj={
- actions:actions.join(''),
- key:idx
- }
- return newObj
- }
- export function selectValue(datas, value) {
- var actions = [];
- var idx=0;
- Object.keys(datas).some((key) => {
- if (datas[key].value == ('' + value)) {
- actions.push(datas[key].label);
- return true;
- }
- })
- return actions.join('')
- }
- export function selectValuetext(datas, value) {
- var actions = [];
- var idx=0;
- Object.keys(datas).some((key) => {
- if (datas[key].value == ('' + value)) {
- actions.push(datas[key].text);
- return true;
- }
- })
- return actions.join('')
- }
- /**
- * 参数处理
- * @param params 参数
- */
- export function tansParams(params) {
- let result = ''
- for (const propName of Object.keys(params)) {
- const value = params[propName]
- var part = encodeURIComponent(propName) + "="
- if (value !== null && value !== "" && typeof (value) !== "undefined") {
- if (typeof value === 'object') {
- for (const key of Object.keys(value)) {
- if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
- let params = propName + '[' + key + ']'
- var subPart = encodeURIComponent(params) + "="
- result += subPart + encodeURIComponent(value[key]) + "&"
- }
- }
- } else {
- result += part + encodeURIComponent(value) + "&"
- }
- }
- }
- return result
- }
- // 上传
- //上传图片(本地地址识别一张)
- export function uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback) {
- const isToken = (config.headers || {}).isToken === false
- config.header = config.header || {}
- if (getToken() && !isToken) {
- config.header['Authorization'] = 'Bearer ' + getToken()
- }
- // get请求映射params参数
- if (config.params) {
- let url = config.url + '?' + tansParams(config.params)
- url = url.slice(0, -1)
- config.url = url
- }
-
- uni.showLoading({
- title: '上传中'
- })
- var failfile = [];
- uni.uploadFile({
- timeout: config.timeout || timeout,
- url: baseUrl + api, //仅为示例,非真实的接口地址
- filePath: filePaths[i],
- name: 'file',
- header: config.header,
- formData: config.formData,
- success: function(resp) {
- uni.hideLoading();
- let result = JSON.parse(resp.data)
- const code = result.code || 200
- const msg = errorCode[code] || result.msg || errorCode['default']
- // console.log(result.fileName,8)
- if (result.code == 200) {
- successUp++;
- files[i] = result;
- } else if(result.code==401) {
- showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
- if (res.confirm) {
- store.dispatch('LogOut').then(res => {
- uni.reLaunch({ url: '/pages/login/login' })
- })
- }
- })
- callback('无效的会话,或者会话已过期,请重新登录。');
- }else{
- failfile = failfile.concat(filePaths[i])
- failUp++;
- }
- },
- fail: function(res) {
- uni.hideLoading();
- failfile = failfile.concat(filePaths[i])
- failUp++;
- },
- complete: function(rsp) {
- // console.log(rsp, filePaths[i])
- uni.hideLoading();
- i++;
- if (i == length) {
- uni.showToast({
- title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败!',
- icon: 'none',
- duration: 2000
- });
- callback(files);
- } else { //递归调用upload函数
- uploadIdentify(api, filePaths, successUp, failUp, i, length, files, callback);
- }
- }
- });
- }
- //上传图片
- export function uploadmore(api, filePaths, successUp, failUp, i, length, files, callback) {
- const isToken = (config.headers || {}).isToken === false
- config.header = config.header || {}
- if (getToken() && !isToken) {
- config.header['Authorization'] = 'Bearer ' + getToken()
- }
- // get请求映射params参数
- if (config.params) {
- let url = config.url + '?' + tansParams(config.params)
- url = url.slice(0, -1)
- config.url = url
- }
-
- uni.showLoading({
- title: '上传中'
- })
- var failfile = [];
- uni.uploadFile({
- timeout: config.timeout || timeout,
- url: baseUrl + api, //仅为示例,非真实的接口地址
- filePath: filePaths[i],
- name: 'file',
- header: config.header,
- formData: config.formData,
- success: function(resp) {
- uni.hideLoading();
- let result = JSON.parse(resp.data)
- const code = result.code || 200
- const msg = errorCode[code] || result.msg || errorCode['default']
- // console.log(result.fileName,8)
- if (result.code == 200) {
- successUp++;
- files[i] = result.fileName;
- } else if(result.code==401) {
- showConfirm("登录状态已过期,您可以继续留在该页面,或者重新登录?").then(res => {
- if (res.confirm) {
- store.dispatch('LogOut').then(res => {
- uni.reLaunch({ url: '/pages/login/login' })
- })
- }
- })
- callback('无效的会话,或者会话已过期,请重新登录。');
- }else{
- failfile = failfile.concat(filePaths[i])
- failUp++;
- }
- },
- fail: function(res) {
- uni.hideLoading();
- failfile = failfile.concat(filePaths[i])
- failUp++;
- },
- complete: function(rsp) {
- // console.log(rsp, filePaths[i])
- uni.hideLoading();
- i++;
- if (i == length) {
- uni.showToast({
- title: '总共' + successUp + '张上传成功,' + failUp + '张上传失败!',
- icon: 'none',
- duration: 2000
- });
- callback(files);
- } else { //递归调用upload函数
- uploadmore(api, filePaths, successUp, failUp, i, length, files, callback);
- }
- }
- });
- }
- // 判断权限
- export function setPermissions(callback) {
- // #ifdef APP-PLUS
- if (plus.os.name == 'Android') { // 判断是Android
- var main = plus.android.runtimeMainActivity();
- var pkName = main.getPackageName();
- var uid = main.getApplicationInfo().plusGetAttribute("uid");
- var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
- //android.support.v4升级为androidx
- if (NotificationManagerCompat == null) {
- NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
- }
- var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
- console.log(areNotificationsEnabled,223)
- // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
- if (!areNotificationsEnabled) {
- uni.showModal({
- title: '通知权限开启提醒',
- content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
- // showCancel: false,
- confirmText: '去设置',
- success: function(res) {
- if (res.confirm) {
- var Intent = plus.android.importClass('android.content.Intent');
- var Build = plus.android.importClass("android.os.Build");
- //android 8.0引导
- if (Build.VERSION.SDK_INT >= 26) {
- var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
- intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
- } else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
- var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
- intent.putExtra("app_package", pkName);
- intent.putExtra("app_uid", uid);
- } else { //(<21)其他--跳转到该应用管理的详情页
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
- intent.setData(uri);
- }
- // 跳转到该应用的系统通知设置页
- main.startActivity(intent);
- }
- }
- });
- }else{
- callback(1);
- }
- } else if (plus.os.name == 'iOS') { // 判断是ISO
- var isOn = undefined;
- var types = 0;
- var app = plus.ios.invoke('UIApplication', 'sharedApplication');
- var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
- if (settings) {
- types = settings.plusGetAttribute('types');
- plus.ios.deleteObject(settings);
- } else {
- types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
- }
- plus.ios.deleteObject(app);
- isOn = (0 != types);
- if (isOn == false) {
- uni.showModal({
- title: '通知权限开启提醒',
- content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
- showCancel: false,
- confirmText: '去设置',
- success: function(res) {
- if (res.confirm) {
- var app = plus.ios.invoke('UIApplication', 'sharedApplication');
- var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
- plus.ios.invoke(app, 'openURL:', setting);
- plus.ios.deleteObject(setting);
- plus.ios.deleteObject(app);
- }
- }
- });
- }
- }
- // #endif
- }
|