httputil.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. icon:'none',
  146. })
  147. }
  148. })
  149. }
  150. function get(url, data, successfun) {
  151. wx.request({
  152. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  153. method: "GET",
  154. header: {
  155. 'content-type': 'application/x-www-form-urlencoded',
  156. 'Authorization': 'Bearer ' + app.globalToken
  157. },
  158. params: data,
  159. success(res) {
  160. successfun(res.data)
  161. },
  162. fail(res){
  163. wx.showToast({
  164. title: "网络请求失败",
  165. })
  166. }
  167. })
  168. }
  169. function send_get(url, data, successfun) {
  170. console.log("令牌:", app.globalToken)
  171. wx.request({
  172. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  173. method: "GET",
  174. header: {
  175. 'content-type': 'application/json',
  176. 'Authorization': 'Bearer ' + app.globalToken
  177. },
  178. data: data,
  179. success(res) {
  180. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  181. // var json = JSON.parse(d)
  182. console.log("TAG","返回数据:"+res.data)
  183. authToken(res.data)
  184. successfun(res.data)
  185. },
  186. fail(res){
  187. wx.showToast({
  188. title: "网络请求失败",
  189. })
  190. }
  191. })
  192. }
  193. function send_postdecode(url, data, successfun) {
  194. wx.request({
  195. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  196. method: "POST",
  197. header: {
  198. 'content-type': 'application/x-www-form-urlencoded'
  199. },
  200. data: data,
  201. success(res) {
  202. var json = JSON.parse(res.data)
  203. successfun(json)
  204. }
  205. })
  206. }
  207. function send_photo(data, successfun) {
  208. var that = this
  209. for (let i = 0; i < data.length; i++) {
  210. console.log("data长度=" + data.length)
  211. console.log(data)
  212. wx.uploadFile({
  213. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  214. filePath: data[i] + "",
  215. name: 'file',
  216. success: function (res) {
  217. var json = JSON.parse(res.data)
  218. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  219. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  220. successfun(sss)
  221. }
  222. })
  223. }
  224. }
  225. function send(data, successfun) {
  226. var that = this
  227. wx.uploadFile({
  228. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  229. filePath: data[0] + "",
  230. name: 'file',
  231. success: function (res) {
  232. var json = JSON.parse(res.data)
  233. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  234. // wx.setStorageSync("carnumber", sss.data.carNumber)
  235. successfun(sss)
  236. },
  237. fail: function (res) {}
  238. })
  239. }
  240. module.exports = {
  241. BASE_Server: BASE_Server,
  242. send_post: send_post,
  243. send_photo: send_photo,
  244. send: send,
  245. post: post,
  246. post_token: post_token,
  247. // wxpost: wxpost,
  248. get: get,
  249. send_get:send_get,
  250. send_postdecode: send_postdecode,
  251. // send_post_login,
  252. showLoading,
  253. hideLoading,
  254. }
  255. function showLoading() {
  256. wx.showLoading({
  257. title: '加载中...',
  258. mask: true,
  259. })
  260. }
  261. function hideLoading() {
  262. wx.hideLoading()
  263. }
  264. function authToken(res){
  265. if(res.code==401){//登录token过期状态码
  266. wx.clearStorage({
  267. success:(res)=>{
  268. wx.reLaunch({
  269. url: '../denglu/denglu',
  270. })
  271. }
  272. })
  273. }
  274. }
  275. // function wxpost(url, data, successfun) {
  276. // wx.request({
  277. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  278. // method: "POST",
  279. // header: {
  280. // 'content-type': 'application/json',
  281. // 'Authorization': 'Bearer ' + app.globalToken
  282. // },
  283. // data: data,
  284. // success(res) {
  285. // successfun(res.data)
  286. // }
  287. // })
  288. // }