httputil.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // let BASE_Server = "http://127.0.0.1:8329"
  2. //let BASE_Server = "https://192.168.1.100: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. wx.showToast({
  84. title: "网络请求失败",
  85. })
  86. }
  87. })
  88. }
  89. /**
  90. * 携带token表单数据POST请求
  91. * @param {*} url
  92. * @param {*} data
  93. * @param {*} successfun
  94. */
  95. function post(url, data, successfun) {
  96. console.log("令牌:", app.globalToken)
  97. if (app.globalToken == null) {
  98. wx.showToast({
  99. title: '尚未登录,登录后即可使用',
  100. icon: 'none'
  101. })
  102. return
  103. }
  104. wx.request({
  105. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  106. method: "POST",
  107. header: {
  108. 'content-type': 'application/x-www-form-urlencoded',
  109. 'Authorization': 'Bearer ' + app.globalToken
  110. },
  111. data: data,
  112. success(res) {
  113. successfun(res.data)
  114. },
  115. fail(res){
  116. wx.showToast({
  117. title: "网络请求失败",
  118. })
  119. }
  120. })
  121. }
  122. /**
  123. * 无token json数据使用的POST请求
  124. * @param {*} url
  125. * @param {*} data
  126. * @param {*} successfun
  127. */
  128. function post_token(url, data, successfun) {
  129. console.log("令牌:", app.globalToken)
  130. wx.request({
  131. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  132. method: "POST",
  133. header: {
  134. 'content-type': 'application/json',
  135. 'Authorization': 'Bearer ' + app.globalToken
  136. },
  137. data: data,
  138. success(res) {
  139. console.log("TAG","返回数据:"+res.data)
  140. authToken(res.data)
  141. successfun(res.data)
  142. },
  143. fail(res){
  144. wx.showToast({
  145. title: "网络请求失败",
  146. icon:'none',
  147. })
  148. }
  149. })
  150. }
  151. /**
  152. * 备用的GET请求
  153. * @param {*} url
  154. * @param {*} data
  155. * @param {*} successfun
  156. */
  157. function get(url, data, successfun) {
  158. console.log("令牌:", app.globalToken)
  159. if (app.globalToken == null) {
  160. wx.showToast({
  161. title: '尚未登录,登录后即可使用',
  162. icon: 'none'
  163. })
  164. return
  165. }
  166. wx.request({
  167. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  168. method: "GET",
  169. header: {
  170. 'content-type': 'application/x-www-form-urlencoded',
  171. 'Authorization': 'Bearer ' + app.globalToken
  172. },
  173. params: data,
  174. success(res) {
  175. successfun(res.data)
  176. },
  177. fail(res){
  178. wx.showToast({
  179. title: "网络请求失败",
  180. })
  181. }
  182. })
  183. }
  184. /**
  185. * 携带token使用的GET请求
  186. * @param {*} url
  187. * @param {*} data
  188. * @param {*} successfun
  189. */
  190. function send_get(url, data, successfun) {
  191. console.log("令牌:", app.globalToken)
  192. if (app.globalToken == null) {
  193. wx.showToast({
  194. title: '尚未登录,登录后即可使用',
  195. icon: 'none'
  196. })
  197. return
  198. }
  199. wx.request({
  200. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  201. method: "GET",
  202. header: {
  203. 'content-type': 'application/json',
  204. 'Authorization': 'Bearer ' + app.globalToken
  205. },
  206. data: data,
  207. success(res) {
  208. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  209. // var json = JSON.parse(d)
  210. console.log("TAG","返回数据:"+res.data)
  211. authToken(res.data)
  212. successfun(res.data)
  213. },
  214. fail(res){
  215. wx.showToast({
  216. title: "网络请求失败",
  217. icon:'none',
  218. })
  219. }
  220. })
  221. }
  222. /**
  223. * 无token 可使用的 GET 请求
  224. * @param {*} url
  225. * @param {*} data
  226. * @param {*} successfun
  227. */
  228. function get_token(url, data, successfun) {
  229. console.log("令牌:", app.globalToken)
  230. wx.request({
  231. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  232. method: "GET",
  233. header: {
  234. 'content-type': 'application/json',
  235. 'Authorization': 'Bearer ' + app.globalToken
  236. },
  237. data: data,
  238. success(res) {
  239. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  240. // var json = JSON.parse(d)
  241. console.log("TAG","返回数据:"+res.data)
  242. authToken(res.data)
  243. successfun(res.data)
  244. },
  245. fail(res){
  246. wx.showToast({
  247. title: "网络请求失败",
  248. icon:'none',
  249. })
  250. }
  251. })
  252. }
  253. function send_postdecode(url, data, successfun) {
  254. wx.request({
  255. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  256. method: "POST",
  257. header: {
  258. 'content-type': 'application/x-www-form-urlencoded'
  259. },
  260. data: data,
  261. success(res) {
  262. var json = JSON.parse(res.data)
  263. successfun(json)
  264. }
  265. })
  266. }
  267. function send_photo(data, successfun) {
  268. var that = this
  269. for (let i = 0; i < data.length; i++) {
  270. console.log("data长度=" + data.length)
  271. console.log(data)
  272. wx.uploadFile({
  273. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  274. filePath: data[i] + "",
  275. name: 'file',
  276. success: function (res) {
  277. var json = JSON.parse(res.data)
  278. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  279. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  280. successfun(sss)
  281. }
  282. })
  283. }
  284. }
  285. function send(data, successfun) {
  286. var that = this
  287. wx.uploadFile({
  288. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  289. filePath: data[0] + "",
  290. name: 'file',
  291. success: function (res) {
  292. var json = JSON.parse(res.data)
  293. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  294. // wx.setStorageSync("carnumber", sss.data.carNumber)
  295. successfun(sss)
  296. },
  297. fail: function (res) {}
  298. })
  299. }
  300. module.exports = {
  301. BASE_Server: BASE_Server,
  302. send_photo: send_photo,
  303. send: send,
  304. post: post,
  305. send_post: send_post,
  306. post_token: post_token,
  307. // wxpost: wxpost,
  308. get: get,
  309. send_get:send_get,
  310. get_token:get_token,
  311. send_postdecode: send_postdecode,
  312. // send_post_login,
  313. showLoading,
  314. hideLoading,
  315. }
  316. function showLoading() {
  317. wx.showLoading({
  318. title: '加载中...',
  319. mask: true,
  320. })
  321. }
  322. function hideLoading() {
  323. wx.hideLoading()
  324. }
  325. function authToken(res){
  326. if(res.code==401){//登录token过期状态码
  327. wx.clearStorage({
  328. success:(res)=>{
  329. wx.reLaunch({
  330. url: '../denglu/denglu',
  331. })
  332. }
  333. })
  334. }
  335. }
  336. // function wxpost(url, data, successfun) {
  337. // wx.request({
  338. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  339. // method: "POST",
  340. // header: {
  341. // 'content-type': 'application/json',
  342. // 'Authorization': 'Bearer ' + app.globalToken
  343. // },
  344. // data: data,
  345. // success(res) {
  346. // successfun(res.data)
  347. // }
  348. // })
  349. // }