magicsound/modules/api/AppServer.js
2024-09-20 15:22:38 +08:00

225 lines
4.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* AppServer API调用封装
* @constructor
*/
// import { sj } from "./Sj.js"
import {
md5
} from "./md5.js"
function objectToQueryParams(obj) {
return Object.keys(obj).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])).join('&');
}
var AppServer = function() {
}
var serverConfig = {}
//dev-localhost
var baseUrl = 'https://music.shhuanmeng.com'
// var basePayCenterUrl = 'https://api-pay.hissai.com'
serverConfig.apiUrl_Music_GetMusicGenresList = baseUrl + '/api/Music/GetMusicGenresList'
serverConfig.apiUrl_Music_GetMusicGenresInfo = baseUrl + '/api/Music/GetMusicGenresInfo'
/**
* 获取完整的application/x-www-form-urlencoded请求参数
* 1填充必要的请求参数
* 2填充签名
* @param postData
*/
AppServer.prototype.getPostFormBody = function(postData) {
var fullPostData = Object.assign({}, postData)
//补全参数
// if (!postData.appId) {
// fullPostData.appId = 1002;
// //#ifdef MP-TOUTIAO
// fullPostData.appId = 1004;
// // #endif
// // #ifndef
// fullPostData.appId = 1002;
// // #endif
// // fullPostData.appId = 1002;
// }
// if(!postData.bossId){
// fullPostData.bossId = 0;
// }
// if (!postData.userId) {
// let userId = uni.getStorageSync("userId");
// if (userId) {
// fullPostData.userId = userId
// }
// }
// console.log("参数", fullPostData);
//签名
// fullPostData.sign = this.getSign(fullPostData)
//url编码
// var formBody = [];
// for (var key in fullPostData) {
// var encodedKey = encodeURIComponent(key);
// var encodedValue = encodeURIComponent(fullPostData[key]);
// formBody.push(encodedKey + "=" + encodedValue);
// }
// formBody = formBody.join("&");
// return formBody
}
/**
* @param {Object} postData
* 获取参数签名
*/
AppServer.prototype.getSign = function(postData) {
let arr = [];
for (let key in postData) {
if (key == 'sign')
continue;
arr.push(key)
}
arr.sort();
let str = '';
for (let i in arr) {
let value = postData[arr[i]]
if (value || value == 0) {
str += value
}
}
//添加saltKey
let saltKey = ""
try {
let token = uni.getStorageSync("token")
if (token) {
saltKey = token.split('.')[2];
}
} catch (e) {
saltKey = ""
}
str += saltKey
let sign = md5(str)
// console.log("要签名的字符串:" + str + ",sign=" + sign)
return sign
}
/**
* 接口请求
*/
AppServer.prototype.postData = async function(url, postData) {
// var formBody = this.getPostFormBody(postData)
// console.log(formBody);
let authToken = uni.getStorageSync("token");
return uni.request({
url: url,
method: 'POST',
header: {
'Content-Type': "application/json",
'Authorization': authToken
},
data: JSON.stringify(postData)
})
.then((res) => {
// 此处的 res 参数,与使用默认方式调用时 success 回调中的 res 参数一致
console.log(`post,url=${url},form=${formBody},response=${res.data}`);
//在这里处理验签失败等问题
return res.data;
})
.catch((err) => {
// 此处的 err 参数,与使用默认方式调用时 fail 回调中的 err 参数一致
console.error(err);
return err;
});
}
AppServer.prototype.getData = async function(url, postData) {
let authToken = uni.getStorageSync("token");
if (postData != null) {
var parmat = objectToQueryParams(postData);
if (url.indexOf("?") > -1) {
url += "&" + parmat;
} else {
url += "?" + parmat;
}
}
return uni.request({
url: url,
method: 'GET',
header: {
'Authorization': authToken
},
// data: JSON.stringify(postData)
})
.then((res) => {
// 此处的 res 参数,与使用默认方式调用时 success 回调中的 res 参数一致
console.log(`post,url=${url},response=${res.data}`);
//在这里处理验签失败等问题
return res.data;
})
.catch((err) => {
// 此处的 err 参数,与使用默认方式调用时 fail 回调中的 err 参数一致
console.error(err);
return err;
});
}
/**
*
* @param taskId
* @param prompt
* @param steps
*/
AppServer.prototype.GetMusicGenresList = async function() {
// 添加基本参数
// 签名
var url = serverConfig.apiUrl_Music_GetMusicGenresList
return this.getData(url).then((data) => {
return data;
})
}
AppServer.prototype.GetServerList = async function() {
return this.postData(serverConfig.apiUrl_AppConfig_GetServerList).then(data => {
console.log((data));
})
}
/**
* 运行平台
*/
AppServer.prototype.GetPlatformIsAndroid = function() {
let port = uni.getSystemInfoSync().platform
switch (port) {
case 'android':
console.log('运行Android上', port);
return true;
break; //android
case 'ios':
console.log('运行iOS上', port);
return false;
break;
default:
console.log('运行在开发者工具上');
return false;
break;
//devtools
}
}
export {
AppServer
}