http.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var siteinfo = require('../siteinfo.js'); //require这个js模块
  2. const http = {}
  3. const headers = {
  4. 'content-type': 'application/x-www-form-urlencoded'
  5. }
  6. http.request = (url, data, method='POST', hideErr = true, hideLoading = true) => {
  7. if(!hideLoading){
  8. uni.showLoading({
  9. title: '加载中...'
  10. });
  11. }
  12. let controller = data.controller =='admin' ? 'admin/':'api/';
  13. return new Promise((resolve, reject) => {
  14. uni.request({
  15. url: siteinfo.siteroot + '/index.php?s=/'+controller + url,
  16. method:'POST',
  17. data: data,
  18. dataType: 'json',
  19. header: headers,
  20. success: res => {
  21. // console.log(res,url,"ddddddddddd")
  22. if (res.statusCode == 200) {
  23. if (res.data.code == 200) {
  24. resolve(res.data); //返回成功提示信息
  25. } else if(res.data.code =="10000"||res.data.code ==10000){
  26. // 会员卡
  27. resolve(res.data);
  28. }else {
  29. reject(res.data); //返回错误提示信息
  30. if (!hideErr) {
  31. setTimeout(() => {
  32. uni.showToast({
  33. title: res.data.message,
  34. icon: 'none'
  35. })
  36. }, 10)
  37. }
  38. }
  39. } else {
  40. // setTimeout(() => {
  41. // uni.showToast({
  42. // title: '网络出小差了~',
  43. // icon: 'none'
  44. // })
  45. // }, 10)
  46. }
  47. },
  48. fail: res => {
  49. console.log('uni.request:fail', res)
  50. // setTimeout(() => {
  51. // uni.showToast({
  52. // title: '网络连接错误',
  53. // icon: 'none'
  54. // })
  55. // }, 10)
  56. // let err = {
  57. // code: '-1',
  58. // msg: "网络连接错误"
  59. // }
  60. // reject(err);
  61. //返回错误提示信息
  62. },
  63. complete: function(res) {
  64. if (!hideLoading) {
  65. uni.hideLoading()
  66. }
  67. }
  68. })
  69. });
  70. }
  71. http.uploadImg = (filePath) => {
  72. // console.log(filePath)
  73. uni.showLoading({
  74. title: '上传中...'
  75. });
  76. return new Promise((resolve, reject) => {
  77. uni.uploadFile({
  78. url: 'http://business.coffunity.cn/admin.php/api/common/uploadingImg', //仅为示例,非真实的接口地址
  79. filePath: filePath,
  80. name: 'file',
  81. success: res => {
  82. if (res.statusCode == 200) {
  83. let res_data = JSON.parse(res.data)
  84. if (res_data.code == 200) {
  85. resolve(res_data); //返回成功提示信息
  86. } else {
  87. reject(res_data); //返回错误提示信息
  88. setTimeout(() => {
  89. uni.showToast({
  90. title: res_data.msg,
  91. icon: 'none'
  92. })
  93. }, 10)
  94. }
  95. } else {
  96. setTimeout(() => {
  97. uni.showToast({
  98. title: '服务器异常',
  99. icon: 'none'
  100. })
  101. }, 10)
  102. }
  103. },
  104. fail: res => {
  105. setTimeout(() => {
  106. uni.showToast({
  107. title: '网络连接错误',
  108. icon: 'none'
  109. })
  110. }, 10)
  111. let err = {
  112. code: '-1',
  113. msg: "网络连接错误"
  114. }
  115. reject(err); //返回错误提示信息
  116. },
  117. complete: function(res) {
  118. uni.hideLoading()
  119. }
  120. });
  121. })
  122. }
  123. export default http