http.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import config from './config'
  2. class Http {
  3. request(method, api, data, loading = false, toast = true) {
  4. let info = {
  5. url: config.domain + '/api/service/' + api,
  6. data,
  7. header: {
  8. 'content-type': 'application/json',
  9. 'token': wx.getStorageSync('token') || ''
  10. },
  11. method: method,
  12. timeout: 0
  13. }
  14. return new Promise((resolve, reject) => {
  15. if (loading) {
  16. wx.showLoading({
  17. title: '加载中',
  18. mask: true,
  19. })
  20. }
  21. wx.request({
  22. ...info,
  23. success: (result) => {
  24. if (result.statusCode === 401) {
  25. wx.navigateTo({
  26. url: '/service/login/login',
  27. })
  28. reject(result)
  29. }
  30. if (result.data.code !== 1) {
  31. if (toast) {
  32. wx.showToast({
  33. title: (result && result.data && result.data.msg) ? result.data.msg : '请稍后重试',
  34. icon: 'none',
  35. duration: 2000
  36. })
  37. } else {
  38. reject(result)
  39. }
  40. } else {
  41. resolve(result.data)
  42. }
  43. },
  44. fail: (res) => {
  45. reject(res)
  46. },
  47. complete: (res) => {
  48. wx.hideLoading({
  49. noConflict: true
  50. })
  51. },
  52. })
  53. })
  54. }
  55. post(url, data, loading, toast) {
  56. return this.request('POST', url, data, loading, toast)
  57. }
  58. get(url, data, loading, toast) {
  59. return this.request('GET', url, data, loading, toast)
  60. }
  61. upload(file, data, loading = false, toast = true) {
  62. return new Promise((resolve, reject) => {
  63. if (loading) {
  64. wx.showLoading({
  65. title: '加载中',
  66. mask: true,
  67. })
  68. }
  69. wx.uploadFile({
  70. url: config.domain + '/api/common/upload', //仅为示例,非真实的接口地址
  71. filePath: file,
  72. name: 'file',
  73. formData: {
  74. token: wx.getStorageSync('token'),
  75. ...data
  76. },
  77. success: (result) => {
  78. result.data = JSON.parse(result.data)
  79. if (result.statusCode === 401) {
  80. wx.navigateTo({
  81. url: '/service/login/login',
  82. })
  83. reject(result)
  84. }
  85. if (result.data.code !== 1) {
  86. if (toast) {
  87. wx.showToast({
  88. title: (result && result.data && result.data.msg) ? result.data.msg : '请稍后重试',
  89. icon: 'none',
  90. duration: 2000
  91. })
  92. }
  93. reject(result)
  94. }
  95. resolve(result.data)
  96. },
  97. fail: (res) => {
  98. reject(res)
  99. },
  100. complete: (res) => {
  101. wx.hideLoading({
  102. noConflict: true
  103. })
  104. },
  105. })
  106. })
  107. }
  108. chooseImg(source, loading, toast, sizeType) {
  109. return new Promise((resolve, reject) => {
  110. wx.chooseMedia({
  111. count: 1,
  112. mediaType: ['image'],
  113. sourceType: source,
  114. sizeType: sizeType ? sizeType : ['compressed'],
  115. camera: 'back',
  116. success: (res) => {
  117. this.upload(res.tempFiles[0].tempFilePath, {
  118. type: 'image'
  119. }, loading, toast).then(res => {
  120. resolve(res)
  121. }).catch(err => {
  122. reject(err)
  123. })
  124. },
  125. fail: (err) => {
  126. reject(err)
  127. }
  128. })
  129. })
  130. }
  131. chooseLocation(lat = '', lng = '') {
  132. return new Promise((resolve, reject) => {
  133. let params = {}
  134. if (lat) {
  135. params = {
  136. latitude: lat,
  137. longitude: lng,
  138. }
  139. }
  140. wx.chooseLocation({
  141. ...params,
  142. success: (res) => {
  143. let lat = res.latitude
  144. let lng = res.longitude
  145. let name = res.name
  146. this.post('user/getlocation', {
  147. lat: res.latitude,
  148. lng: res.longitude
  149. }).then(res => {
  150. if (res.data == false) {
  151. wx.showModal({
  152. title: '提示',
  153. content: '获取位置失败,请联系管理员',
  154. showCancel: false, //没有取消按钮的弹框
  155. buttonText: '确定',
  156. success: function (res) {
  157. if (res.confirm) {
  158. console.log('用户点击确定');
  159. } else if (res.cancel) {
  160. console.log('用户点击取消');
  161. }
  162. }
  163. });
  164. return
  165. }
  166. let city = ''
  167. let province = res.data.regeocode.addressComponent.province
  168. if (res.data.regeocode.addressComponent.city === '[]' || res.data.regeocode.addressComponent.city.length === 0) {
  169. city = province
  170. } else {
  171. city = res.data.regeocode.addressComponent.city
  172. }
  173. let district = res.data.regeocode.addressComponent.district
  174. name = name || (res.data.regeocode.aois[0] && res.data.regeocode.aois[0].name) || res.data.regeocode.formatted_address
  175. let data = {
  176. city,
  177. province,
  178. district,
  179. name,
  180. lat,
  181. lng
  182. }
  183. resolve(data)
  184. }).catch(err => {
  185. reject(err)
  186. })
  187. },
  188. fail: (e) => {
  189. let data = {
  190. province: '河南省',
  191. city: '郑州市',
  192. district: '中原区',
  193. name: '郑州市',
  194. lat: '34.74725',
  195. lng: '113.62493'
  196. }
  197. reject(data)
  198. // wx.getSetting({
  199. // withSubscriptions: true,
  200. // success:(res)=>{
  201. // console.log(res)
  202. // let authSetting = res.authSetting
  203. // if(!authSetting['scope.userLocation']){
  204. // wx.showModal({
  205. // title: '您未开启地理位置授权',
  206. // content: '请在设置中打开位置授权,以便我们能够更好的提供服务',
  207. // success (res) {
  208. // if (res.confirm) {
  209. // wx.openSetting()
  210. // } else if (res.cancel) {
  211. // let data = {
  212. // province: '河南省',
  213. // city: '郑州市',
  214. // district: '中原区',
  215. // name: '郑州市',
  216. // lat: '34.74725',
  217. // lng: '113.62493'
  218. // }
  219. // reject(data)
  220. // }
  221. // }
  222. // })
  223. // }
  224. // }
  225. // })
  226. }
  227. })
  228. })
  229. }
  230. getLocation(lat = '', lng = '') {
  231. return new Promise((resolve, reject) => {
  232. wx.getLocation({
  233. type: 'wgs84',
  234. success: (res) => {
  235. let lat = res.latitude
  236. let lng = res.longitude
  237. this.post('user/getlocation', {
  238. lat: res.latitude,
  239. lng: res.longitude
  240. }).then(res => {
  241. if (res.data == false) {
  242. wx.showModal({
  243. title: '提示',
  244. content: '获取位置失败,请联系管理员',
  245. showCancel: false, //没有取消按钮的弹框
  246. buttonText: '确定',
  247. success: function (res) {
  248. if (res.confirm) {
  249. console.log('用户点击确定');
  250. } else if (res.cancel) {
  251. console.log('用户点击取消');
  252. }
  253. }
  254. });
  255. return
  256. }
  257. let city = ''
  258. let province = res.data.regeocode.addressComponent.province
  259. if (res.data.regeocode.addressComponent.city === '[]' || res.data.regeocode.addressComponent.city.length === 0) {
  260. city = province
  261. } else {
  262. city = res.data.regeocode.addressComponent.city
  263. }
  264. let district = res.data.regeocode.addressComponent.district
  265. let name = (res.data.regeocode.aois[0] && res.data.regeocode.aois[0].name) || res.data.regeocode.formatted_address
  266. let data = {
  267. city,
  268. province,
  269. district,
  270. name,
  271. lat,
  272. lng
  273. }
  274. resolve(data)
  275. }).catch(err => {
  276. reject(err)
  277. })
  278. },
  279. fail: (e) => {
  280. wx.getSetting({
  281. withSubscriptions: true,
  282. success: (res) => {
  283. let authSetting = res.authSetting
  284. if (!authSetting['scope.userLocation']) {
  285. wx.showModal({
  286. title: '您未开启地理位置授权',
  287. content: '请在设置中打开位置授权,以便我们能够更好的提供服务',
  288. success(res) {
  289. if (res.confirm) {
  290. wx.openSetting()
  291. } else if (res.cancel) {
  292. let data = {
  293. province: '河南省',
  294. city: '郑州市',
  295. district: '中原区',
  296. name: '郑州市',
  297. lat: '34.74725',
  298. lng: '113.62493'
  299. }
  300. reject(data)
  301. }
  302. }
  303. })
  304. }
  305. }
  306. })
  307. }
  308. })
  309. })
  310. }
  311. }
  312. const http = new Http()
  313. export default http