utils.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // 封装获取授权结果, scopeName权限名称
  2. export const getAuthorize = (scopeName:any) => {
  3. return new Promise((resove) => {
  4. wx.getSetting({
  5. success: res => {
  6. res.authSetting[scopeName] ? resove(true) : resove(false);
  7. },
  8. fail: () => resove(false)
  9. })
  10. })
  11. };
  12. // 封装设置权限, scopeName权限名称
  13. export const setAuthorize = (scopeName:string) => {
  14. return new Promise((resove) => {
  15. wx.authorize({
  16. scope: scopeName,
  17. success: () => resove(true),
  18. fail: () => resove(false)
  19. })
  20. })
  21. };
  22. // 节流函数
  23. export const throttle = (fn:any, delay = 200) => {
  24. let timer:any = undefined;
  25. const _this:any = this;
  26. return function (args:any[]) {
  27. if (timer) return;
  28. timer = setTimeout(() => {
  29. fn.call(_this, args);
  30. clearTimeout(timer);
  31. timer = null;
  32. }, delay);
  33. };
  34. };
  35. // 比较版本号
  36. export const compareVersion = (v1:string, v2:string) => {
  37. const listV1 = v1.split('.')
  38. const listV2 = v2.split('.')
  39. const len = Math.max(v1.length, v2.length)
  40. while (listV1.length < len) {
  41. listV1.push('0')
  42. }
  43. while (listV2.length < len) {
  44. listV2.push('0')
  45. }
  46. for (let i = 0; i < len; i++) {
  47. const num1 = parseInt(listV1[i])
  48. const num2 = parseInt(listV2[i])
  49. if (num1 > num2) return 1
  50. else if (num1 < num2) return -1
  51. }
  52. return 0
  53. }
  54. // 检测版本号后的处理,version: 比较的版本号, callback: 版本号低于的处理回调
  55. export const checkVersion = (version:string, callback:any) => {
  56. const v = wx.getSystemInfoSync().SDKVersion
  57. console.log('比较版本号系统:', v, '比较:', version);
  58. if (compareVersion(v, version) >= 0) {
  59. return true;
  60. } else {
  61. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  62. wx.showModal({
  63. title: '提示',
  64. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
  65. showCancel: false,
  66. success: () => {
  67. callback()
  68. }
  69. })
  70. return false;
  71. }
  72. }