httputil.js 7.3 KB

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