H5齐鲁医疗
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
sims-scaninvoice-qlyl/js/common/base.js

470 lines
16 KiB

// var uid = localStorage.getItem('uid');
// var token = localStorage.getItem('token');
const openId = localStorage.getItem('openId');
/*线上全局配置变量*/
const config = {
baseUrl: '/wxkf',
//本项目域名
projectUrl: "/order-api",
// projectUrl: "/ysy1",
wxscanUrl: "/wxscaninvoice",
//销项基础服务
simsBaseUrl: "/base-service",
//接口域名以及项目名称
interfaceUrl: "/ele-myinvoice",
//发票列表票夹
myinvoiceUrl: "/my-invoice",
//微信服务地址
wxservicePath: "http://wdfp.5ifapiao.com/wxservice",
wxserviceTestPath: '/wxservice',
//发票名片接口地址
invoiceCardUrl: "/myinvoice-invoice-card",
//账号相关接口地址
accountUrl: "/myinvoice-account",
//企业logo获取地址
logoUrl: "/myinvoice-data"
};
// /*测试全局配置变量*/
// var config = {
// baseUrl: 'http://sims.ele-cloud.com.com',
// //本项目域名
// projectUrl: "http://test.5ifapiao.com:8888/order-api",
// // projectUrl:"https://sims.ele-cloud.com.com/wxscaninvoice",
//
//
// wxscanUrl: "http://test.5ifapiao.com:8888/wxscaninvoice",
//
// //接口域名以及项目名称
// // interfaceUrl: "https://sims.ele-cloud.com.com/ele-myinvoice",
// interfaceUrl: "http://test.5ifapiao.com:8888/ele-myinvoice",
//
// //发票列表票夹
// // myinvoiceUrl:"http://sims.ele-cloud.com.com/my-invoice-lyang",
// myinvoiceUrl: "http://sims.ele-cloud.com.com/my-invoice",
//
// //微信服务地址
// // wxservicePath: "https://wdfp.5ifapiao.com/wxservice",
// wxservicePath: "http://wdfp.5ifapiao.com/wxservice",
//
// //发票名片接口地址
// // invoiceCardUrl: "https://sims.ele-cloud.com.com/myinvoice-invoice-card",
// invoiceCardUrl: "http://test.5ifapiao.com:8888/myinvoice-invoice-card",
//
// //账号相关接口地址
// // accountUrl: "https://sims.ele-cloud.com.com/myinvoice-account",
// accountUrl: "http://test.5ifapiao.com:8888/myinvoice-account",
//
// //企业logo获取地址
// // logoUrl: "https://sims.ele-cloud.com.com/myinvoice-data"
// logoUrl: "http://test.5ifapiao.com:8888/myinvoice-data"
//
//
// };
//请求头类型
function getProtocol() {
return window.location.protocol + "//";
}
//域名:端口
function getHost() {
return window.location.host; //localhost:8080
}
//跳转页面
function toPage(page) {
window.location.href = getPreUrl() + page;
}
function getPreUrl() {
return getProtocol() + getHost() + '/' + getProjectName();
}
function getProjectName() {
const relativePath = location.pathname;
const tmp = relativePath.split("/");
if (tmp[1].length === 0) {
return tmp[2];
} else {
return tmp[1];
}
}
function updateUrlParam(num) {
const params = new URLSearchParams(location.search.slice(1));
params.set('type', '');
window.history.replaceState({}, '', `${location.pathname}?${params + location.hash}`);
}
//获取url参数的value getUrlArgumentValue(参数名称)
function getUrlArgumentValue(name) {
const hash = window.location.hash;
if (hash !== "" && name === 'code') { //路径中有锚点 不能直接获取到 参数
if (hash.indexOf('&') > 0) {
return hash.split('&')[1].split('=')[1];
} else {
return hash.split('?')[1].split('=')[1];
}
}
if (hash !== "" && name === 'path') {
return window.location.search.substr(6).split("?")[0] + hash.split('&')[0];
}
const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
const r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]) + hash;
} else {
return null;
}
}
//获取url参数列表 数组
function getUrlArgumentArr() {
let name, value;
let str = location.href; //取得整个地址栏
let num = str.indexOf("?");
str = str.substr(num + 1); //取得所有参数 stringvar.substr(start [, length ]
const arr = str.split("&"); //各个参数放到数组里
for (let i = 0; i < arr.length; i++) {
num = arr[i].indexOf("=");
if (num > 0) {
name = arr[i].substring(0, num);
value = arr[i].substr(num + 1);
this[name] = value;
}
}
return arr;
}
//获取微信code 重定向返回当前页面 重定向回来后 有code 通过code获取openid 和 token
function getOpenidOrToken() {
const pathCode = encodeURIComponent(window.location.href);
if (pathCode.indexOf("%26code%3D") < 0 || pathCode.indexOf("%3fcode%3D") < 0) {
window.location.href = config.wxservicePath + '/api/toGetUserInfo?type=1&path=' + pathCode;
}
//basejs中获取地址参数值
const code = getUrlArgumentValue("code");
//掉后台接口 通过code获取oppentId-->token
if (code != null && code !== '') {
$.ajax({
type: 'POST',
data: {
"code": code
},
dataType: 'JSON',
url: config.accountUrl + '/account/wx/getOpenId',
async: false,
header: {
'content-type': 'application/x-www-form-urlencoded'
}, // 设置请求的 header
success: function (data) {
info = data.data;
localStorage.setItem('openId', info.user);
localStorage.setItem("token", info.token);
}
})
}
}
function getUnionIdAndOpenId() {
const pathCode = encodeURIComponent(window.location.href);
if (pathCode.indexOf("%26code%3D") < 0 || pathCode.indexOf("%3fcode%3D") < 0) {
window.location.href = config.wxservicePath + '/api/toGetUserInfo?type=1&path=' + pathCode;
}
//basejs中获取地址参数值
const code = getUrlArgumentValue("code");
if (code != null && code !== '') {
$.ajax({
type: "get",
url: config.accountUrl + '/account/wx/getOpenId?code=' + code,
async: false,
success: function (data) {
return data;
}
});
}
}
//获取code并获取用户信息
function getUserInfoCode(appid) {
let code = "";
const pathCode = encodeURIComponent(window.location.href);
if (pathCode.indexOf("%26code%3D") >= 0 || pathCode.indexOf("%3fcode%3D") >= 0) {
code = getUrlArgumentValue("code");
} else {
if (appid == null) {
window.location.href = config.wxservicePath + '/api/toGetUserInfo?type=1&path=' + pathCode;
} else {
window.location.href = config.wxservicePath + '/api/toGetUserInfo?type=1&path=' + pathCode + "&appid=" + appid;
}
}
//获取用户信息
if (code != null && code !== '') {
$.ajax({
type: "post",
url: config.wxserviceTestPath + "/api/getUserInfo",
data: JSON.stringify({
"code": code,
"type": "1",
"gzh_appid": appid
}),
contentType: "application/json;charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
},
complete: function (xml) {
const responseText = JSON.parse(xml.responseText);
if (xml.status === 200) {
const subscribe = responseText.subscribe;
const openId = responseText.openid;
const unionId = responseText.unionid;
window.localStorage.setItem('openId', openId);
window.localStorage.setItem('subscribe', subscribe);
window.localStorage.setItem('unionId', unionId);
//登陆成功调jsp列表
// pathCode1 = decodeURIComponent(pathCode);
// pathCode = getUrlArgumentValue("path");
// if (pathCode != null) {
// window.location.replace(pathCode);
// } else {
// window.location.replace(pathCode1);
// }
} else {
alert_timing_open(getCodeInfo(responseText.code));
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert_loading_close();
alert_timing_open('登录失败请重新登录');
},
})
}
}
//弹框 title:标题, description:描述, type:1显示确认 2显示确认取消
function alert_prompt_open(title, description, type) {
const body = document.getElementsByTagName('body')[0];
const sub = (type === "1" ? '<div class="alert_prompt_bottom_two" style="display: flex;height:0.7rem;line-height:0.7rem;margin-top:0.2rem;">' +
'<span class="alert_prompt_bottom_two_affirm" style="color: #FFAA00;flex: 1;font-size:0.34rem;" onclick="alert_prompt_close()">确定</span>' +
'</div>' :
'<div class="alert_prompt_bottom_one" style="display: flex;height:0.7rem;line-height:0.7rem;margin-top:0.2rem;">' +
'<span class="alert_prompt_bottom_one_cancel" style="border-right: 1px solid #DEDEDE; color: #ABABAB;font-size: 0.34rem; flex: 1;" onclick="alert_prompt_close()">取消</span>' +
'<span class="alert_prompt_bottom_one_affirm" style="color: #FFAA00;border-right: 1px solid #DEDEDE; border:none;font-size: 0.34rem; flex: 1;" onclick="alert_prompt_close()">确定</span>' +
'</div>');
const str = '<div class="alert_prompt_open" style="position: fixed; top: 0; left: 0; bottom: 0; right: 0; background: rgba(0,0,0,0.7); z-index: 1001;">' +
'<div class="alert_prompt_count" style="width: 5.5rem; height:3.52rem; background-color: #FFFFFF; border-radius: 0.08rem; margin-left: 50%; margin-top: 3rem; position: absolute; left: -2.75rem;">' +
'<div class="alert_prompt_info" style="width: 100%; margin-top: 0.6rem; text-align: center;">' +
'<div class="alert_prompt_top" style="border-bottom: 1px solid #DEDEDE;">' +
'<div style="font-size: 0.28rem; color: #666666;height: 0.4rem;line-height: 0.4rem;font-weight:700;">' + title + '</div>' +
'<span style="font-size: 0.28rem;color: #666666; margin-top: 0.4rem; height: 0.4rem;line-height: 0.4rem;margin-bottom: 0.6rem; display: block;">' + description + '</span>' +
'</div>' +
sub +
'</div>' +
'</div>' +
'</div>';
body.insertAdjacentHTML('beforeend', str);
}
//关闭弹框
function alert_prompt_close() {
const body = document.getElementsByTagName('body')[0];
const open = document.getElementsByClassName('alert_prompt_open')[0];
body.removeChild(open);
}
//打开load弹框
function alert_loading_open(info) {
const body = document.getElementsByTagName('body')[0];
if (body !== undefined) {
var info = info != null ? info : '请等待...';
const str = '<div class="alert_loading_open" style="position: fixed; top: 0; bottom: 0; left: 0; right: 0; z-index: 9999999;">' +
'<div class="alert_content" style="height: 1.3rem; width: 3rem; background:rgba(17, 17, 17, 0.7); position: fixed; left:-1.5rem; margin-top: 50%; margin-left: 50%; border-radius: 8px;">' +
'<div class="alert_info" style="padding: 10px 0;text-align: center;">' +
'<img src="../../image/common/timg.gif" style="height: 0.5rem;width: 0.5rem;"/>' +
'<span style="display: block; color: #FFFFFF; white-space:nowrap; font-size:0.3rem">' + info + '</span>' +
'</div>' +
'</div>' +
'</div>';
body.insertAdjacentHTML('beforeend', str);
}
}
//关闭load弹框
function alert_loading_close() {
const body = document.getElementsByTagName('body')[0];
const open = document.getElementsByClassName('alert_loading_open')[0];
if (open !== undefined) {
body.removeChild(open);
}
}
//定时自动关闭弹框
function alert_timing_open(info) {
const body = document.getElementsByTagName('body')[0];
const str =
'<div class="toast_box" style="position: fixed;top: 0;right: 0;bottom: 0;left: 0;z-index: 9999999;">' +
'<div class="toast_content" style="width:auto !important;max-width: 50%;height:auto;line-height: 0.4rem;border-radius:0.08rem;text-align:center;font-size: 0.28rem;color: #fff;background: rgba(0,0,0,0.7);padding: 0.3rem 0.45rem;margin: 3.6rem auto;">' +
info +
'</div>' +
'</div>';
body.insertAdjacentHTML('beforeend', str);
setTimeout(function () {
const body = document.getElementsByTagName('body')[0];
const open = document.getElementsByClassName('toast_box')[0];
body.removeChild(open);
}, 2000);
}
//处理键盘弹出 收起影响的元素
function monitorKeyboard(element) {
const h = document.body.clientHeight;
window.onresize = function () {
if (document.documentElement.clientHeight < h) {
$(element).hide();
} else {
$(element).show();
}
};
}
//处理键盘弹出 收起影响的元素
function monitorKeyboardScroll(element) {
const h = document.body.scrollHeight; //屏幕高度
window.onresize = function () {
if (document.body.scrollHeight < h) {
$(element).hide();
} else {
$(element).show();
}
};
}
//设置token 和 uid
function setTokenOrUid(token, uid) {
window.localStorage.setItem('token', token);
window.localStorage.setItem('uid', uid);
}
/**
* 功能:通过 .class #id div 获取元素 class 与 div 返回arr
* @param ele : .class
* @param ele : #id
* @param ele : div
*/
function getEle(ele) {
const indexStusD = ele.indexOf('.');
const indexStusJ = ele.indexOf('#');
if (indexStusD !== -1) {
var ele = ele.split('.')[1];
return document.getElementsByClassName(ele);
} else if (indexStusJ !== -1) {
var ele = ele.split('#')[1];
return document.getElementById(ele);
} else if (indexStusD === -1 && indexStusJ === -1) {
return document.getElementsByTagName(ele);
}
}
//设置token 和 uid
function setTokenOrUid(token, uid) {
window.localStorage.setItem('token', token);
window.localStorage.setItem('uid', uid);
this.token = token;
this.uid = uid;
}
/*input框日历控件软键盘同时出现*/
function cancelRili(cancelId) {
$("#" + cancelId).blur();
}
/*时间戳转换成日期格式*/
function fmtDate(obj) {
const date = new Date(obj);
const y = 1900 + date.getYear();
const m = "0" + (date.getMonth() + 1);
const d = "0" + date.getDate();
return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
}
/*时间戳转换成日期加时间*/
function getdate(obj) {
const now = new Date(obj),
y = now.getFullYear(),
m = ("0" + (now.getMonth() + 1)).slice(-2),
d = ("0" + now.getDate()).slice(-2);
return y + "-" + m + "-" + d + " " + now.toTimeString().substr(0, 8);
}
/*保留两位小数*/
function changeTwoDecimal_f(x) {
var f_x = parseFloat(x);
if (isNaN(f_x)) {
return 0;
}
var f_x = Math.round(x * 100) / 100;
let s_x = f_x.toString();
let pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
function isWeiXin() {
const ua = window.navigator.userAgent.toLowerCase();
return ua.indexOf('micromessenger') !== -1;
}
// AES加密
function encrypt(plaintText,key) {
let aesKey = CryptoJS.enc.Utf8.parse(key);
let encryptedData = CryptoJS.AES.encrypt(plaintText, aesKey, {
iv: CryptoJS.enc.Utf8.parse("6543210987654321"),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
encryptedData = encryptedData.ciphertext.toString();
let encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
return CryptoJS.enc.Base64.stringify(encryptedHexStr);
}
// AES解密
function decrypt(encryptedData,key) {
let aesKey = CryptoJS.enc.Utf8.parse(key);
let encryptedHexStr = CryptoJS.enc.Hex.parse(encryptedData);
let encryptedBase64Str = CryptoJS.enc.Base64.stringify(encryptedHexStr)
const decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, aesKey, {
iv: CryptoJS.enc.Utf8.parse("6543210987654321"),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return decryptedData.toString(CryptoJS.enc.Utf8);
}