index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * @description: 跳转小程序
  3. */
  4. export const navigateToMiniProgram = (appId) => {
  5. return new Promise((resolve, reject) => {
  6. tt.navigateToMiniProgram({
  7. appId: appId,
  8. path: "",
  9. extraData: {
  10. foo: "bar",
  11. },
  12. envVersion: "develop",
  13. success(res) {
  14. resolve(res);
  15. },
  16. fail(error) {
  17. reject(error);
  18. },
  19. });
  20. });
  21. };
  22. /**
  23. * @description: 获取手机相关配置,每次都直接取接口
  24. * @param {string} key
  25. * @return {*}
  26. */
  27. export const getSystem = (key) => {
  28. return new Promise((resolve, reject) => {
  29. tt.getSystemInfo({
  30. success: (res) => {
  31. if (key && key in res) {
  32. resolve(res[key]);
  33. } else {
  34. resolve(res);
  35. }
  36. },
  37. fail: (error) => {
  38. reject(error);
  39. },
  40. });
  41. });
  42. };
  43. /**
  44. * @description: js异步转同步优化
  45. * @param {*} promiseObj
  46. * @return {*}
  47. */
  48. export const to = (promiseObj) => {
  49. return promiseObj
  50. .then((data) => {
  51. return [null, data];
  52. })
  53. .catch((err) => [err]);
  54. };
  55. /**
  56. * @description: 对象排序
  57. * @param {object} data
  58. * @return {object}
  59. */
  60. export const sortObj = (data) => {
  61. let arr = [];
  62. for (let key in data) {
  63. arr.push(key);
  64. }
  65. arr = arr.sort();
  66. let newData = {};
  67. for (var i in arr) {
  68. newData[arr[i]] = data[arr[i]];
  69. }
  70. return newData;
  71. };
  72. /**
  73. * @description: 加密
  74. * @param {*} value
  75. * @return {string}
  76. */
  77. const compile = (value) => {
  78. value = JSON.stringify(value);
  79. let result = String.fromCharCode(value.charCodeAt(0) + value.length);
  80. for (let i = 1; i < value.length; i++) {
  81. result += String.fromCharCode(
  82. value.charCodeAt(i) + value.charCodeAt(i - 1)
  83. );
  84. }
  85. return escape(result);
  86. };
  87. /**
  88. * @description:解密
  89. * @param {string} value
  90. * @return {*}
  91. */
  92. const uncompile = (value) => {
  93. value = unescape(value);
  94. let result = String.fromCharCode(value.charCodeAt(0) - value.length);
  95. for (let i = 1; i < value.length; i++) {
  96. result += String.fromCharCode(
  97. value.charCodeAt(i) - result.charCodeAt(i - 1)
  98. );
  99. }
  100. result = JSON.parse(result);
  101. return result;
  102. };
  103. /**
  104. * @description: 二次处理key
  105. * @param {string} key
  106. * @return {string}
  107. */
  108. const getKey = (key) => {
  109. return `xinxin_${key}_@`;
  110. };
  111. /**
  112. * @description: 获取
  113. * @param {string} key
  114. * @return {*}
  115. */
  116. export const getStorage = (key) => {
  117. let value = tt.getStorageSync(getKey(key));
  118. return value ? uncompile(value) : false;
  119. };
  120. /**
  121. * @description: 存储
  122. * @param {string} key
  123. * @param {*} value
  124. * @return {*}
  125. */
  126. export const setStorage = (key, value) => {
  127. tt.setStorageSync(getKey(key), compile(value));
  128. };
  129. /**
  130. * @description: 删除
  131. * @param {string} key
  132. */
  133. export const removeStorage = (key) => {
  134. tt.removeStorageSync(getKey(key));
  135. };
  136. /**
  137. * @description: 随机字符串
  138. * @param {number}
  139. * @return {string}
  140. */
  141. export const randomString = (length) => {
  142. const chars =
  143. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  144. var result = "";
  145. for (let i = length; i > 0; --i)
  146. result += chars[Math.floor(Math.random() * chars.length)];
  147. return result;
  148. };
  149. /**
  150. * @description: 安卓虚拟支付
  151. * @param {object}
  152. * @return {*}
  153. */
  154. export const requestGamePayment = (data) => {
  155. return new Promise((resolve, reject) => {
  156. console.log("虚拟支付开始:", data);
  157. tt.requestGamePayment({
  158. ...data,
  159. success: (res) => {
  160. resolve(res);
  161. },
  162. fail: (error) => {
  163. console.log("虚拟支付失败:", error);
  164. reject(error);
  165. },
  166. });
  167. });
  168. };
  169. /**
  170. * @description: iOS 虚拟支付 发起钻石支付
  171. * @param {object}
  172. * @return {*}
  173. */
  174. export const openAwemeCustomerService = (data) => {
  175. return new Promise((resolve, reject) => {
  176. console.log("发起钻石支付:", data);
  177. tt.openAwemeCustomerService({
  178. ...data,
  179. success: (res) => {
  180. resolve(res);
  181. },
  182. fail: (error) => {
  183. console.log("钻石支付失败:", error);
  184. reject(error);
  185. },
  186. });
  187. });
  188. };