tools.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @param title String,提示的内容
  3. * @param duration String,提示的延迟时间,单位毫秒,默认:1500
  4. * @param mask Boolean,是否显示透明蒙层,防止触摸穿透,默认:false
  5. * @param icon String,图标:success、error、fail、exception、loading、none,默认:none
  6. **/
  7. export function createMessage(title, duration = 1500, mask = false, icon = "none") {
  8. uni.showToast({
  9. title,
  10. duration: duration,
  11. mask,
  12. icon
  13. });
  14. }
  15. /**
  16. * @param url String,请求的地址,默认:none
  17. * @param data Object,请求的参数,默认:{}
  18. * @param method String,请求的方式,默认:GET
  19. * @param loading Boolean,是否需要loading ,默认:false
  20. * @param header Object,headers,默认:{}
  21. * @returns promise
  22. **/
  23. export function createRequest(url, data = {}, loading = false, method = 'GET', header = {}) {
  24. if (loading) {
  25. uni.showLoading({
  26. title: '请稍后',
  27. mask: true
  28. })
  29. }
  30. return new Promise((resolve, reject) => {
  31. uni.request({
  32. url: url,
  33. method: method,
  34. data: data,
  35. header: header,
  36. success: res => {
  37. if (res.statusCode === 200) {
  38. resolve(res.data)
  39. } else {
  40. if (res.data.msg) {
  41. const str = typeof res.data.resolve === 'string' ? ',' + res.data.resolve :
  42. ''
  43. createMessage(res.data.msg + str)
  44. }
  45. throw new Error('请求错误' + url)
  46. reject()
  47. }
  48. },
  49. fail: (err) => {
  50. reject(err)
  51. },
  52. complete: () => {
  53. uni.hideLoading();
  54. }
  55. });
  56. })
  57. }
  58. /**
  59. * 数据格式化
  60. * @param obj Object,响应的数据
  61. * @param type Number 0 | 1,处理类型
  62. * @returns Object {address = string, name = string, location = {lon, lat }, infomation = {}}
  63. */
  64. export function formatterAdressLocation(obj, type) {
  65. switch (type) {
  66. case 1:
  67. return {
  68. address: obj.formatted_address,
  69. name: '',
  70. location: obj.location,
  71. infomation: obj.addressComponent
  72. }
  73. break;
  74. case 2:
  75. const [lon, lat] = obj.lonlat.split(',')
  76. return {
  77. address: obj.address,
  78. name: obj.name,
  79. location: {
  80. lon,
  81. lat
  82. },
  83. infomation: obj
  84. }
  85. break
  86. case 3:
  87. return {
  88. address: obj.location.keyWord,
  89. name: '',
  90. location: {
  91. lon: obj.location.lon,
  92. lat: obj.location.lat,
  93. },
  94. infomation: obj.location
  95. }
  96. default:
  97. break;
  98. }
  99. }
  100. export default {
  101. createMessage,
  102. createRequest,
  103. formatterAdressLocation
  104. }