import { pageJumpFn, arrayBufferToBase64, layerPopup } from './commonFun.js'; import ConfigsJs from '../class/config.js'; import { storageObj } from './localstorage.js'; const Configs = new ConfigsJs(); console.log(Configs.get('API_DOMAIN')) /** * 获取api */ const requestJs = { strApiDoMain: Configs.get('API_DOMAIN'), //点击统计-base saveStaTis:"/miao/statis/save", // 新统计api //用户 regist: '/miao/user/regist', //注册- registbyphone: '/miao/user/registbyphone', //手机注册- registbyemail: '/miao/user/registbyemail', //邮箱注册- emaillogin: '/miao/user/emaillogin', //邮箱登录- login: '/miao/user/login', //登录 phonelogin: '/miao/user/phonelogin', //手机登录- retrievePasswordByPhone: '/miao/user/retrievePasswordByPhone', //找回密码- info: '/miao/user/info',//用户详情- smscode: '/miao/user/smscode',//发送验证吗- bindphone: '/miao/user/bindphone',//绑定手机- setpasswd: '/miao/user/setpasswd',//修改密码- logout: '/miao/user/logout',//推出- channel: '/miao/pay/channel',//支付渠道 create: '/miao/pay/create',//创建支付订单 channelV2: '/miao/pay/v2/channel',//获取支付通道列表V2- createV2: '/miao/pay/v2/create',//创建支付订单V2- notify: '/miao/pay/notify',//支付异步回调 orderlist: '/miao/pay/orderlist',//支付订单列表- packagevip: '/miao/pay/package',//获取套餐 //视频 videoinfo: '/miao/video/info',//视频地址 //问题反馈 feedbacklist: '/miao/feedback/index',//反馈列表 feedbackcreate: '/miao/feedback/create',//反馈列表--添加- fileupdate: '/miao/file/upload',//反馈列表-上传- getVipTime: '/miao/pay/getVipTime',//涮新wip时间 getFundsInfo: '/miao/funds/info',//资金账户详情.md ok- getFundsTransactionsList: '/miao/funds/transactions/list',//交易记录.md ok - getFundsWithdrawalAccountList: '/miao/funds/withdrawal/account/list',//提现账户列表.md- getFundsWithdrawalOrderList: '/miao/funds/withdrawal/order/list',//提现订单列表.md ok- getFundsWithdrawalOrderCreate: '/miao/funds/withdrawal/order/create',//申请提现.md- getFundsWithdrawalOrderPut: '/miao/funds/withdrawal/account/put',//设置提现账户.md getReferralList: '/miao/referral/list', //推广统计.md- getReferralShare: '/miao/referral/share', //推广链接- checkregist: '/miao/user/regist/check', //注册前置接口- getcaptcha: '/miao/user/captcha',//获取验证码- resetPasswordByKey:'/miao/user/resetPasswordByKey', //根据key设置密码 /** * 默认参数 */ source: { url: "", type: 'POST', dataType: 'json', //预期的服务器响应的数据类型。 processData: true, //布尔值,规定通过请求发送的数据是否转换为查询字符串。默认是 true。 contentType: "application/x-www-form-urlencoded", //发送数据到服务器时所使用的内容类型。默认是:"application/x-www-form-urlencoded"。 cache: true, //是否缓存被请求页面。默认是 true。 async: true, //是否异步处理。默认是 true。 data: { }, }, //fetch fetchFun(url, data, callback, errFun) { fetch(url) .then((response) => response.arrayBuffer()) .then((buffer) => { if (buffer) { buffer = buffer.slice(17, buffer.length); const imageStr = arrayBufferToBase64(buffer); callback(imageStr, data); } }) .catch(error => { console.log(error) errFun(data) }); }, asyncFunction(url) { return new Promise((resolve, reject) => { fetch(url) .then((response) => response.arrayBuffer()) .then((buffer) => { if (buffer) { buffer = buffer.slice(17, buffer.length); var imageStr = arrayBufferToBase64(buffer); resolve(imageStr); } }) .catch(error => { reject('异步操作失败'); }); }); }, asyncFunction2(url) { return new Promise((resolve, reject) => { fetch(url) .then((response) => response.arrayBuffer()) .then((buffer) => { if (buffer) { var imageStr = arrayBufferToBase64(buffer); resolve(imageStr); } }) .catch(error => { reject('异步操作失败'); }); }); }, //原生请求-post ajaxPost(url, data, callback) { var xhr = new XMLHttpRequest(); xhr.open('POST', url, true); xhr.setRequestHeader('accept', 'application/x.hubserver.admin+json'); xhr.send(data); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { callback(xhr.responseText); } } }, //原生请求-get ajaxGet(url, data, callback, errFun) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.setRequestHeader('accept', 'application/x.hubserver.admin+json'); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { callback(xhr.responseText, data); } if (xhr.readyState == 4 && xhr.status == 0) { this.errFun(data) } } }, //获取本地token getStorageToken(key) { var token = window.localStorage.getItem(key) if (!token) { return false } return token }, /** * * post 请求 * @param source 数据 * @param successHandle 访问成功的回调函数 * @param errorHandle 访问失败回调函数 * 如果不要loding,可以设置为false,默认是“正在加载。。” * 如果需要表单上传,请设置from 为true ,默认为false, * 如果没有token,headers也可以注释 * postFun(source, successfn, errorfn,loding='正在加载。。') * **/ postFun: function (source, successfn, errorfn, loading = '正在加载。。', from = false) { let layerIndex = null; // 设置Ajax请求参数 if (from) { source['processData'] = false; source['contentType'] = false; source['cache'] = false; } // 封装显示加载层的逻辑 function showLoading() { if (loading !== false) { layerIndex = layer.open({ type: 2, content: loading, }); } } // 封装关闭加载层的逻辑 function closeLoading() { if (layerIndex !== null) { layer.close(layerIndex); } } // 封装处理成功响应的逻辑 function handleSuccessResponse(res) { // 解密处理 if (Configs.get('MIAO_API_ENCRYPT_STATUS') == '1') { let returnENC = EncAndDec.decryptData(res, Configs.get('MIAO_API_ENCRYPT_KEY')); res = JSON.parse(returnENC); } if (res.code != '000') { if(source['url'] == '/miao/pay/create'){ res.msg = '当前支付通道拥堵,请更换其他支付通道支付。' } if(res.code != '014'){ layerPopup(res.msg,3); } // 重新登录 if (res.code === '2000' || res.code === '995' || res.code === '996') { localStorage.setItem(storageObj.loginStatus, false) location.href = "/member/login.html" } } successfn(res); } // 发起Ajax POST请求 $.ajax({ url: this.strApiDoMain + source['url'], type: 'POST', data: source['data'], dataType: source['dataType'], async: source['async'], headers: { 'Accept': "application/x.hubserver.admin+json", 'token': localStorage.getItem(storageObj.tokenKey) }, beforeSend: showLoading, complete: closeLoading, success: handleSuccessResponse, error: function (e) { if (errorfn) errorfn(e); closeLoading(); } }); }, /** * * get 请求 * @param source 数据 * @param successHandle 访问成功的回调函数 * @param errorHandle 访问失败回调函数 * 如果不要loding,可以设置为false,默认是“正在加载。。” * 如果没有token,headers也可以注释 * getFun(source, successfn, errorfn,loding='正在加载。。') * **/ getFun: function (source, successFun, errorFun, loadingMessage = '正在加载...') { const layerIndex = loadingMessage !== false ? layer.open({ type: 2, content: loadingMessage }) : null; $.ajax({ url: this.strApiDoMain + source['url'], type: 'GET', dataType: source['dataType'], async: source['async'], headers: { 'Accept': "application/x.hubserver.admin+json", 'token': localStorage.getItem(storageObj.tokenKey) }, beforeSend: function () { // 加载中的逻辑已经在外面处理 }, complete: function () { if (layerIndex !== null) { layer.close(layerIndex); } }, success: function (res) { handleResponse(res); // 处理响应的逻辑可以抽取出来 successFun(res); // 调用成功的回调函数 }, error: function (e) { if (errorFun) errorFun(e); // 只有在提供了错误回调函数的情况下才调用 } }); // 处理响应逻辑 function handleResponse(res) { // 解密处理 if (Configs.get('MIAO_API_ENCRYPT_STATUS') == '1') { let returnENC = EncAndDec.decryptData(res, Configs.get('MIAO_API_ENCRYPT_KEY')); res = JSON.parse(returnENC); } if (['2000', '2002', '2003','995','996'].includes(res.code)) { localStorage.setItem(storageObj.loginStatus, false); localStorage.setItem(storageObj.isMember, false); localStorage.setItem(storageObj.tokenKey, ''); localStorage.setItem(storageObj.userInfo, ''); const redirectMap = { '995': '/member/login.html', '996': '/member/login.html', '2000': '/member/login.html', '2002': '/member/buy.html', '2003': '/home.html' // 假设这是视频不存在时的页面 }; pageJumpFn({ 'url': redirectMap[res.code], 'url_type': 1 }); } else if (res.code != '000') { layerPopup(res.msg, 3); } else { //layerPopup(res.message, 3); // 处理其他code的逻辑,如果有必要 } } }, /** * * 访问失败的回调函数 * */ errorfn(err) { console.log(err); } } export default requestJs;