httputil.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // let BASE_Server = "http://127.0.0.1:8329"
  2. // let BASE_Server = "https://192.168.1.109:8080"
  3. let BASE_Server = "https://sookajs.top:18080"
  4. // let BASE_Server = "http://192.168.2.9:18080"
  5. let app = getApp()
  6. //-----------------------------登录,获取token-----------------------------------
  7. function init_userInfo() {
  8. if (app.globalToken == null) {
  9. wx.getUserProfile({
  10. desc: 'desc',
  11. success: (res) => {
  12. getSysUserInfo(res.userInfo)
  13. },
  14. fail: res => {
  15. }
  16. })
  17. }
  18. }
  19. function getSysUserInfo(info) {
  20. let that = this
  21. wx.login({
  22. success(res) {
  23. var code = res.code
  24. var data = {
  25. wxCode: code,
  26. wxNickName: info.nickName,
  27. wxAvatarUrl: info.avatarUrl
  28. }
  29. // http.send_post_login("/minapp/AppLoginController/appLogin",data)
  30. send_post("/auth/applogin", data, loginSuccess)
  31. },
  32. fail(res) {
  33. }
  34. })
  35. }
  36. function loginSuccess(res) {
  37. if (res.data.code != 200) {
  38. wx.showToast({
  39. title: res.data.msg != null ? res.data.msg : '',
  40. icon: "none"
  41. })
  42. } else {
  43. app.globalToken = res.data.access_token
  44. //登录成功,获取首页数据
  45. this.initIndexPage()
  46. }
  47. }
  48. //------------------------------------------------------------------
  49. /**
  50. * 携带token json数据 POST请求
  51. * @param {*} url
  52. * @param {*} data
  53. * @param {*} successfun
  54. */
  55. function send_post(url, data, successfun) {
  56. if (app.globalToken == null) {
  57. wx.showToast({
  58. title: '尚未登录,登录后即可使用',
  59. icon: 'none'
  60. })
  61. return
  62. }
  63. wx.request({
  64. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  65. method: "POST",
  66. header: {
  67. 'content-type': 'application/json',
  68. 'Authorization': 'Bearer ' + app.globalToken
  69. },
  70. data: data,
  71. success(res) {
  72. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  73. // var json = JSON.parse(d)
  74. authToken(res.data)
  75. successfun(res.data)
  76. },
  77. fail(res){
  78. // console.log("TAG","返回数据:"+res.errno+'_'+res.errMsg)
  79. wx.showToast({
  80. title: "网络请求失败",
  81. })
  82. }
  83. })
  84. }
  85. /**
  86. * 携带token表单数据POST请求
  87. * @param {*} url
  88. * @param {*} data
  89. * @param {*} successfun
  90. */
  91. function post(url, data, successfun) {
  92. if (app.globalToken == null) {
  93. wx.showToast({
  94. title: '尚未登录,登录后即可使用',
  95. icon: 'none'
  96. })
  97. return
  98. }
  99. wx.request({
  100. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  101. method: "POST",
  102. header: {
  103. 'content-type': 'application/x-www-form-urlencoded',
  104. 'Authorization': 'Bearer ' + app.globalToken
  105. },
  106. data: data,
  107. success(res) {
  108. successfun(res.data)
  109. },
  110. fail(res){
  111. wx.showToast({
  112. title: "网络请求失败",
  113. })
  114. }
  115. })
  116. }
  117. /**
  118. * 无token json数据使用的POST请求
  119. * @param {*} url
  120. * @param {*} data
  121. * @param {*} successfun
  122. */
  123. function post_token(url, data, successfun) {
  124. wx.request({
  125. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  126. method: "POST",
  127. header: {
  128. 'content-type': 'application/json',
  129. 'Authorization': 'Bearer ' + app.globalToken
  130. },
  131. data: data,
  132. success(res) {
  133. authToken(res.data)
  134. successfun(res.data)
  135. },
  136. fail(res){
  137. wx.showToast({
  138. title: "网络请求失败",
  139. icon:'none',
  140. })
  141. }
  142. })
  143. }
  144. /**
  145. * 备用的GET请求
  146. * @param {*} url
  147. * @param {*} data
  148. * @param {*} successfun
  149. */
  150. function get(url, data, successfun) {
  151. if (app.globalToken == null) {
  152. wx.showToast({
  153. title: '尚未登录,登录后即可使用',
  154. icon: 'none'
  155. })
  156. return
  157. }
  158. wx.request({
  159. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  160. method: "GET",
  161. header: {
  162. 'content-type': 'application/x-www-form-urlencoded',
  163. 'Authorization': 'Bearer ' + app.globalToken
  164. },
  165. params: data,
  166. success(res) {
  167. successfun(res.data)
  168. },
  169. fail(res){
  170. wx.showToast({
  171. title: "网络请求失败",
  172. })
  173. }
  174. })
  175. }
  176. /**
  177. * 携带token使用的GET请求
  178. * @param {*} url
  179. * @param {*} data
  180. * @param {*} successfun
  181. */
  182. function send_get(url, data, successfun) {
  183. if (app.globalToken == null) {
  184. wx.showToast({
  185. title: '尚未登录,登录后即可使用',
  186. icon: 'none'
  187. })
  188. return
  189. }
  190. wx.request({
  191. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  192. method: "GET",
  193. header: {
  194. 'content-type': 'application/json',
  195. 'Authorization': 'Bearer ' + app.globalToken
  196. },
  197. data: data,
  198. success(res) {
  199. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  200. // var json = JSON.parse(d)
  201. authToken(res.data)
  202. successfun(res.data)
  203. },
  204. fail(res){
  205. wx.showToast({
  206. title: "网络请求失败",
  207. icon:'none',
  208. })
  209. }
  210. })
  211. }
  212. /**
  213. * 无token 可使用的 GET 请求
  214. * @param {*} url
  215. * @param {*} data
  216. * @param {*} successfun
  217. */
  218. function get_token(url, data, successfun) {
  219. wx.request({
  220. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  221. method: "GET",
  222. header: {
  223. 'content-type': 'application/json',
  224. 'Authorization': 'Bearer ' + app.globalToken
  225. },
  226. data: data,
  227. success(res) {
  228. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  229. // var json = JSON.parse(d)
  230. authToken(res.data)
  231. successfun(res.data)
  232. },
  233. fail(res){
  234. wx.showToast({
  235. title: "网络请求失败",
  236. icon:'none',
  237. })
  238. }
  239. })
  240. }
  241. function send_postdecode(url, data, successfun) {
  242. wx.request({
  243. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  244. method: "POST",
  245. header: {
  246. 'content-type': 'application/x-www-form-urlencoded'
  247. },
  248. data: data,
  249. success(res) {
  250. var json = JSON.parse(res.data)
  251. successfun(json)
  252. }
  253. })
  254. }
  255. function send_photo(data, successfun) {
  256. var that = this
  257. for (let i = 0; i < data.length; i++) {
  258. wx.uploadFile({
  259. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  260. filePath: data[i] + "",
  261. name: 'file',
  262. success: function (res) {
  263. var json = JSON.parse(res.data)
  264. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  265. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  266. successfun(sss)
  267. }
  268. })
  269. }
  270. }
  271. function send(data, successfun) {
  272. var that = this
  273. wx.uploadFile({
  274. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  275. filePath: data[0] + "",
  276. name: 'file',
  277. success: function (res) {
  278. var json = JSON.parse(res.data)
  279. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  280. // wx.setStorageSync("carnumber", sss.data.carNumber)
  281. successfun(sss)
  282. },
  283. fail: function (res) {}
  284. })
  285. }
  286. module.exports = {
  287. BASE_Server: BASE_Server,
  288. send_photo: send_photo,
  289. send: send,
  290. post: post,
  291. send_post: send_post,
  292. post_token: post_token,
  293. // wxpost: wxpost,
  294. get: get,
  295. send_get:send_get,
  296. get_token:get_token,
  297. send_postdecode: send_postdecode,
  298. // send_post_login,
  299. showLoading,
  300. hideLoading,
  301. }
  302. function showLoading() {
  303. wx.showLoading({
  304. title: '加载中...',
  305. mask: true,
  306. })
  307. }
  308. function hideLoading() {
  309. wx.hideLoading()
  310. }
  311. function authToken(res){
  312. if(res.code==401){//登录token过期状态码
  313. wx.clearStorage({
  314. success:(res)=>{
  315. wx.reLaunch({
  316. url: '../denglu/denglu',
  317. })
  318. }
  319. })
  320. }
  321. }
  322. // function wxpost(url, data, successfun) {
  323. // wx.request({
  324. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  325. // method: "POST",
  326. // header: {
  327. // 'content-type': 'application/json',
  328. // 'Authorization': 'Bearer ' + app.globalToken
  329. // },
  330. // data: data,
  331. // success(res) {
  332. // successfun(res.data)
  333. // }
  334. // })
  335. // }