tools.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. export function downloadFile({url}) {
  2. return new Promise((resolve, reject) => {
  3. wx.downloadFile({
  4. url,
  5. success: resolve,
  6. fail: reject
  7. });
  8. })
  9. }
  10. export function getQueryString(url, name) {
  11. let reg = new RegExp('(^|/?|&)' + name + '=([^&]*)(&|$)', 'i');
  12. let r = url.substr(1).match(reg);
  13. if (r != null) {
  14. return unescape(r[2]);
  15. }
  16. return null;
  17. }
  18. export function randomString(e) {
  19. for (var t = "", i = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", n = 0; n < e; n++)
  20. t += i.charAt(Math.floor(Math.random() * i.length));
  21. return t
  22. }
  23. export function isPhoneX () {
  24. return new Promise((resolve) => {
  25. let screenHeight = wx.getSystemInfoSync().screenHeight
  26. let bottom = wx.getSystemInfoSync().safeArea.bottom
  27. resolve(screenHeight != bottom)
  28. })
  29. }
  30. export function getLocation () {
  31. return new Promise ( (resolve, reject) => {
  32. wx.getLocation({
  33. type: 'wgs84',
  34. success: function (res) {
  35. var latitude = res.latitude;
  36. var longitude = res.longitude;
  37. getApp().globalData.qqmapsdk.reverseGeocoder({
  38. location: {
  39. latitude: latitude,
  40. longitude: longitude
  41. },
  42. success: function (res) {
  43. var ad_info = res.result.ad_info;
  44. let city = ad_info.city.replace('市', '');
  45. resolve(city)
  46. },
  47. fail:function (res) {
  48. wx.showModal({
  49. title: '定位失败',
  50. });
  51. }
  52. });
  53. },
  54. fail(err) {
  55. wx.showModal({
  56. title: '定位失败,请手动选择城市',
  57. });
  58. }
  59. })
  60. })
  61. }