| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- import DnSdk from "../lib/DnSdk";
- /**
- * @description: 跳转小程序
- */
- export const navigateToMiniProgram = (appId) => {
- return new Promise((resolve, reject) => {
- wx.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) => {
- wx.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}
- */
- export 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 {*}
- */
- export 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 = wx.getStorageSync(getKey(key));
- return value ? uncompile(value) : false;
- };
- /**
- * @description: 存储
- * @param {string} key
- * @param {*} value
- * @return {*}
- */
- export const setStorage = (key, value) => {
- wx.setStorageSync(getKey(key), compile(value));
- };
- /**
- * @description: 删除
- * @param {string} key
- */
- export const removeStorage = (key) => {
- console.log('remove', key)
- wx.removeStorageSync(getKey(key));
- };
- /**
- * @description: 虚拟支付
- * @param {object}
- * @return {*}
- */
- export const requestMidasPayment = (data) => {
- return new Promise((resolve, reject) => {
- wx.requestMidasPayment({
- ...data,
- success: (res) => {
- console.log('zhifu success', res)
- // 腾讯广告小游戏SDK上报支付
-
- DnSdk.getInstance().onPurchase(data.amt * 100);
- resolve(res);
- },
- fail: (error) => {
- console.log('zhifu fail', error)
- reject(error);
- },
- });
- });
- };
- /**
- * @description: 发起道具直购支付
- * @param {object} data
- * @return {*}
- */
- export const requestMidasPaymentGameItem = (data) => {
- return new Promise((resolve, reject) => {
- wx.requestMidasPaymentGameItem({
- ...data,
- success: (res) => {
- console.log('zhifu', res)
- // 腾讯广告小游戏SDK上报支付
-
- DnSdk.getInstance().onPurchase(data.amt * 100);
- resolve(res);
- },
- fail: (error) => {
- console.log('zhifu fail', error)
- reject(error);
- },
- });
- });
- }
- /**
- * @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: 获取unix时间戳(秒)
- * @return {number}
- */
- export const unixTimestamp = () => {
- return Math.floor(Date.now() / 1000);
- };
- /**
- * 版本判断
- * @param {string} v1
- * @param {string} v2
- * @return {number}
- */
- export const compareVersion = (v1, v2) => {
- v1 = v1.split(".");
- v2 = v2.split(".");
- const len = Math.max(v1.length, v2.length);
- while (v1.length < len) {
- v1.push("0");
- }
- while (v2.length < len) {
- v2.push("0");
- }
- for (let i = 0; i < len; i++) {
- const num1 = parseInt(v1[i]);
- const num2 = parseInt(v2[i]);
- if (num1 > num2) {
- return 1;
- } else if (num1 < num2) {
- return -1;
- }
- }
- return 0;
- };
- export const assign = (obj, defaultInfo) => {
- let info = {};
- //合并,后覆盖前,所以必定有默认key
- Object.assign(info, defaultInfo, obj);
- let result = {};
- for (let key in defaultInfo) {
- if (typeof info[key] !== "undefined") {
- obj[key] = info[key];
- result[key] = info[key];
- }
- }
- return result;
- };
- /**
- * @description: 节流函数
- * @param {Function} func 要节流的函数
- * @param {number} wait 等待时间(毫秒)
- * @return {Function} 节流后的函数
- */
- export const throttle = (func, wait = 2000) => {
- let lastCall = 0;
- let pendingPromise = null;
- return function executedFunction(...args) {
- const now = Date.now();
- if (now - lastCall >= wait) {
- // 可以执行,重置时间
- lastCall = now;
- pendingPromise = null;
- return func.apply(this, args);
- } else {
- // 被节流,返回一个被拒绝的 Promise
- if (!pendingPromise) {
- pendingPromise = Promise.reject(new Error("请求过于频繁,请稍后再试"));
- }
- return pendingPromise;
- }
- };
- };
|