index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import DnSdk from "../lib/DnSdk";
  2. /**
  3. * @description: 跳转小程序
  4. */
  5. export const navigateToMiniProgram = (appId) => {
  6. return new Promise((resolve, reject) => {
  7. wx.navigateToMiniProgram({
  8. appId: appId,
  9. path: "",
  10. extraData: {
  11. foo: "bar",
  12. },
  13. envVersion: "develop",
  14. success(res) {
  15. resolve(res);
  16. },
  17. fail(error) {
  18. reject(error);
  19. },
  20. });
  21. });
  22. };
  23. /**
  24. * @description: 获取手机相关配置,每次都直接取接口
  25. * @param {string} key
  26. * @return {*}
  27. */
  28. export const getSystem = (key) => {
  29. return new Promise((resolve, reject) => {
  30. wx.getSystemInfo({
  31. success: (res) => {
  32. if (key && key in res) {
  33. resolve(res[key]);
  34. } else {
  35. resolve(res);
  36. }
  37. },
  38. fail: (error) => {
  39. reject(error);
  40. },
  41. });
  42. });
  43. };
  44. /**
  45. * @description: js异步转同步优化
  46. * @param {*} promiseObj
  47. * @return {*}
  48. */
  49. export const to = (promiseObj) => {
  50. return promiseObj
  51. .then((data) => {
  52. return [null, data];
  53. })
  54. .catch((err) => [err]);
  55. };
  56. /**
  57. * @description: 对象排序
  58. * @param {object} data
  59. * @return {object}
  60. */
  61. export const sortObj = (data) => {
  62. let arr = [];
  63. for (let key in data) {
  64. arr.push(key);
  65. }
  66. arr = arr.sort();
  67. let newData = {};
  68. for (var i in arr) {
  69. newData[arr[i]] = data[arr[i]];
  70. }
  71. return newData;
  72. };
  73. /**
  74. * @description: 加密
  75. * @param {*} value
  76. * @return {string}
  77. */
  78. export const compile = (value) => {
  79. value = JSON.stringify(value);
  80. let result = String.fromCharCode(value.charCodeAt(0) + value.length);
  81. for (let i = 1; i < value.length; i++) {
  82. result += String.fromCharCode(
  83. value.charCodeAt(i) + value.charCodeAt(i - 1)
  84. );
  85. }
  86. return escape(result);
  87. };
  88. /**
  89. * @description:解密
  90. * @param {string} value
  91. * @return {*}
  92. */
  93. export const uncompile = (value) => {
  94. value = unescape(value);
  95. let result = String.fromCharCode(value.charCodeAt(0) - value.length);
  96. for (let i = 1; i < value.length; i++) {
  97. result += String.fromCharCode(
  98. value.charCodeAt(i) - result.charCodeAt(i - 1)
  99. );
  100. }
  101. result = JSON.parse(result);
  102. return result;
  103. };
  104. /**
  105. * @description: 二次处理key
  106. * @param {string} key
  107. * @return {string}
  108. */
  109. const getKey = (key) => {
  110. return `xinxin_${key}_@`;
  111. };
  112. /**
  113. * @description: 获取
  114. * @param {string} key
  115. * @return {*}
  116. */
  117. export const getStorage = (key) => {
  118. let value = wx.getStorageSync(getKey(key));
  119. return value ? uncompile(value) : false;
  120. };
  121. /**
  122. * @description: 存储
  123. * @param {string} key
  124. * @param {*} value
  125. * @return {*}
  126. */
  127. export const setStorage = (key, value) => {
  128. wx.setStorageSync(getKey(key), compile(value));
  129. };
  130. /**
  131. * @description: 删除
  132. * @param {string} key
  133. */
  134. export const removeStorage = (key) => {
  135. console.log('remove', key)
  136. wx.removeStorageSync(getKey(key));
  137. };
  138. /**
  139. * @description: 虚拟支付
  140. * @param {object}
  141. * @return {*}
  142. */
  143. export const requestMidasPayment = (data) => {
  144. return new Promise((resolve, reject) => {
  145. wx.requestMidasPayment({
  146. ...data,
  147. success: (res) => {
  148. console.log('zhifu success', res)
  149. // 腾讯广告小游戏SDK上报支付
  150. DnSdk.getInstance().onPurchase(data.amt * 100);
  151. resolve(res);
  152. },
  153. fail: (error) => {
  154. console.log('zhifu fail', error)
  155. reject(error);
  156. },
  157. });
  158. });
  159. };
  160. /**
  161. * @description: 发起道具直购支付
  162. * @param {object} data
  163. * @return {*}
  164. */
  165. export const requestMidasPaymentGameItem = (data) => {
  166. return new Promise((resolve, reject) => {
  167. wx.requestMidasPaymentGameItem({
  168. ...data,
  169. success: (res) => {
  170. console.log('zhifu', res)
  171. // 腾讯广告小游戏SDK上报支付
  172. DnSdk.getInstance().onPurchase(data.amt * 100);
  173. resolve(res);
  174. },
  175. fail: (error) => {
  176. console.log('zhifu fail', error)
  177. reject(error);
  178. },
  179. });
  180. });
  181. }
  182. /**
  183. * @description: 随机字符串
  184. * @param {number}
  185. * @return {string}
  186. */
  187. export const randomString = (length) => {
  188. const chars =
  189. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  190. var result = "";
  191. for (let i = length; i > 0; --i)
  192. result += chars[Math.floor(Math.random() * chars.length)];
  193. return result;
  194. };
  195. /**
  196. * @description: 获取unix时间戳(秒)
  197. * @return {number}
  198. */
  199. export const unixTimestamp = () => {
  200. return Math.floor(Date.now() / 1000);
  201. };
  202. /**
  203. * 版本判断
  204. * @param {string} v1
  205. * @param {string} v2
  206. * @return {number}
  207. */
  208. export const compareVersion = (v1, v2) => {
  209. v1 = v1.split(".");
  210. v2 = v2.split(".");
  211. const len = Math.max(v1.length, v2.length);
  212. while (v1.length < len) {
  213. v1.push("0");
  214. }
  215. while (v2.length < len) {
  216. v2.push("0");
  217. }
  218. for (let i = 0; i < len; i++) {
  219. const num1 = parseInt(v1[i]);
  220. const num2 = parseInt(v2[i]);
  221. if (num1 > num2) {
  222. return 1;
  223. } else if (num1 < num2) {
  224. return -1;
  225. }
  226. }
  227. return 0;
  228. };
  229. export const assign = (obj, defaultInfo) => {
  230. let info = {};
  231. //合并,后覆盖前,所以必定有默认key
  232. Object.assign(info, defaultInfo, obj);
  233. let result = {};
  234. for (let key in defaultInfo) {
  235. if (typeof info[key] !== "undefined") {
  236. obj[key] = info[key];
  237. result[key] = info[key];
  238. }
  239. }
  240. return result;
  241. };
  242. /**
  243. * @description: 节流函数
  244. * @param {Function} func 要节流的函数
  245. * @param {number} wait 等待时间(毫秒)
  246. * @return {Function} 节流后的函数
  247. */
  248. export const throttle = (func, wait = 2000) => {
  249. let lastCall = 0;
  250. let pendingPromise = null;
  251. return function executedFunction(...args) {
  252. const now = Date.now();
  253. if (now - lastCall >= wait) {
  254. // 可以执行,重置时间
  255. lastCall = now;
  256. pendingPromise = null;
  257. return func.apply(this, args);
  258. } else {
  259. // 被节流,返回一个被拒绝的 Promise
  260. if (!pendingPromise) {
  261. pendingPromise = Promise.reject(new Error("请求过于频繁,请稍后再试"));
  262. }
  263. return pendingPromise;
  264. }
  265. };
  266. };