httputil.js 8.5 KB

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