| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- /**
- * @description: 跳转小程序
- */
- export const navigateToMiniProgram = (appId) => {
- return new Promise((resolve, reject) => {
- tt.navigateToMiniProgram({
- appId: appId,
- path: "",
- extraData: {
- foo: "bar",
- },
- envVersion: "develop",
- success(res) {
- resolve(res);
- },
- fail(error) {
- reject(error);
- },
- });
- });
- };
- /**
- * @description: 获取手机相关配置,每次都直接取接口
- * @param {string} key
- * @return {*}
- */
- export const getSystem = (key) => {
- return new Promise((resolve, reject) => {
- tt.getSystemInfo({
- success: (res) => {
- if (key && key in res) {
- resolve(res[key]);
- } else {
- resolve(res);
- }
- },
- fail: (error) => {
- reject(error);
- },
- });
- });
- };
- /**
- * @description: js异步转同步优化
- * @param {*} promiseObj
- * @return {*}
- */
- export const to = (promiseObj) => {
- return promiseObj
- .then((data) => {
- return [null, data];
- })
- .catch((err) => [err]);
- };
- /**
- * @description: 对象排序
- * @param {object} data
- * @return {object}
- */
- export const sortObj = (data) => {
- let arr = [];
- for (let key in data) {
- arr.push(key);
- }
- arr = arr.sort();
- let newData = {};
- for (var i in arr) {
- newData[arr[i]] = data[arr[i]];
- }
- return newData;
- };
- /**
- * @description: 加密
- * @param {*} value
- * @return {string}
- */
- const compile = (value) => {
- value = JSON.stringify(value);
- let result = String.fromCharCode(value.charCodeAt(0) + value.length);
- for (let i = 1; i < value.length; i++) {
- result += String.fromCharCode(
- value.charCodeAt(i) + value.charCodeAt(i - 1)
- );
- }
- return escape(result);
- };
- /**
- * @description:解密
- * @param {string} value
- * @return {*}
- */
- const uncompile = (value) => {
- value = unescape(value);
- let result = String.fromCharCode(value.charCodeAt(0) - value.length);
- for (let i = 1; i < value.length; i++) {
- result += String.fromCharCode(
- value.charCodeAt(i) - result.charCodeAt(i - 1)
- );
- }
- result = JSON.parse(result);
- return result;
- };
- /**
- * @description: 二次处理key
- * @param {string} key
- * @return {string}
- */
- const getKey = (key) => {
- return `xinxin_${key}_@`;
- };
- /**
- * @description: 获取
- * @param {string} key
- * @return {*}
- */
- export const getStorage = (key) => {
- let value = tt.getStorageSync(getKey(key));
- return value ? uncompile(value) : false;
- };
- /**
- * @description: 存储
- * @param {string} key
- * @param {*} value
- * @return {*}
- */
- export const setStorage = (key, value) => {
- tt.setStorageSync(getKey(key), compile(value));
- };
- /**
- * @description: 删除
- * @param {string} key
- */
- export const removeStorage = (key) => {
- tt.removeStorageSync(getKey(key));
- };
- /**
- * @description: 随机字符串
- * @param {number}
- * @return {string}
- */
- export const randomString = (length) => {
- const chars =
- "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- var result = "";
- for (let i = length; i > 0; --i)
- result += chars[Math.floor(Math.random() * chars.length)];
- return result;
- };
- /**
- * @description: 安卓虚拟支付
- * @param {object}
- * @return {*}
- */
- export const requestGamePayment = (data) => {
- return new Promise((resolve, reject) => {
- console.log("虚拟支付开始:", data);
- tt.requestGamePayment({
- ...data,
- success: (res) => {
- resolve(res);
- },
- fail: (error) => {
- console.log("虚拟支付失败:", error);
- reject(error);
- },
- });
- });
- };
- /**
- * @description: iOS 虚拟支付 发起钻石支付
- * @param {object}
- * @return {*}
- */
- export const openAwemeCustomerService = (data) => {
- return new Promise((resolve, reject) => {
- console.log("发起钻石支付:", data);
- tt.openAwemeCustomerService({
- ...data,
- success: (res) => {
- resolve(res);
- },
- fail: (error) => {
- console.log("钻石支付失败:", error);
- reject(error);
- },
- });
- });
- };
|