tools.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. wx.getSystemInfo({
  26. success: res=>{
  27. let modelmes = res.model;
  28. if (modelmes.search('iPhone X') != -1) {
  29. resolve(true)
  30. }
  31. resolve(false)
  32. }
  33. })
  34. })
  35. }
  36. export function getLocation () {
  37. return new Promise ( (resolve, reject) => {
  38. wx.getLocation({
  39. type: 'wgs84',
  40. success: function (res) {
  41. var latitude = res.latitude;
  42. var longitude = res.longitude;
  43. getApp().globalData.qqmapsdk.reverseGeocoder({
  44. location: {
  45. latitude: latitude,
  46. longitude: longitude
  47. },
  48. success: function (res) {
  49. var ad_info = res.result.ad_info;
  50. let city = ad_info.city.replace('市', '');
  51. resolve(city)
  52. },
  53. fail:function (res) {
  54. wx.showModal({
  55. title: '定位失败',
  56. });
  57. }
  58. });
  59. },
  60. fail(err) {
  61. wx.showModal({
  62. title: '定位失败,请手动选择城市',
  63. });
  64. }
  65. })
  66. })
  67. }