httputil.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // let BASE_Server = "http://127.0.0.1:8329"
  2. let BASE_Server = "http://192.168.1.102:8080"
  3. let BASE_Server_Login = "http://192.168.1.102:9200"
  4. // let BASE_Server = "http://192.168.54.126:8080"
  5. // let BASE_Server_Login = "http://192.168.54.126:9200"
  6. let app = getApp()
  7. function send_post_login(url, data, successfun) {
  8. wx.request({
  9. url: BASE_Server_Login + url, //仅为示例,并非真实的接口地址
  10. method: "POST",
  11. header: {
  12. 'content-type': 'application/json'
  13. },
  14. data: data,
  15. success(res) {
  16. successfun(res)
  17. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  18. // console.log("*****", d)
  19. // var json = JSON.parse(d)
  20. // successfun(json)
  21. }
  22. })
  23. }
  24. //-----------------------------登录,获取token-----------------------------------
  25. function init_userInfo() {
  26. if (app.globalToken == null) {
  27. console.log(1111111)
  28. wx.getUserProfile({
  29. desc: 'desc',
  30. success: (res) => {
  31. getSysUserInfo(res.userInfo)
  32. },
  33. fail: res => {
  34. console.log(res)
  35. }
  36. })
  37. }
  38. }
  39. function getSysUserInfo(info) {
  40. console.log(2222)
  41. let that = this
  42. wx.login({
  43. success(res) {
  44. var code = res.code
  45. var data = {
  46. wxCode: code,
  47. wxNickName: info.nickName,
  48. wxAvatarUrl: info.avatarUrl
  49. }
  50. // http.send_post_login("/minapp/AppLoginController/appLogin",data)
  51. send_post_login("/applogin", data, loginSuccess)
  52. },
  53. fail(res) {
  54. console.log("ffff", res)
  55. }
  56. })
  57. }
  58. function loginSuccess(res) {
  59. if (res.data.code != 200) {
  60. wx.showToast({
  61. title: res.data.msg,
  62. icon: "none"
  63. })
  64. } else {
  65. app.globalToken = res.data.data.access_token
  66. //登录成功,获取首页数据
  67. this.initIndexPage()
  68. }
  69. console.log("!!!!!", app.globalToken)
  70. }
  71. //------------------------------------------------------------------
  72. function send_post(url, data, successfun) {
  73. console.log("令牌:", app.globalToken)
  74. //如果令牌为空或过期,需要重新请求
  75. wx.request({
  76. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  77. method: "POST",
  78. header: {
  79. 'content-type': 'application/json',
  80. 'Authorization': 'Bearer ' + app.globalToken
  81. },
  82. data: data,
  83. success(res) {
  84. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  85. // var json = JSON.parse(d)
  86. successfun(res)
  87. }
  88. })
  89. }
  90. function post(url, data, successfun) {
  91. wx.request({
  92. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  93. method: "POST",
  94. header: {
  95. 'content-type': 'application/x-www-form-urlencoded',
  96. 'Authorization': 'Bearer ' + app.globalToken
  97. },
  98. data: data,
  99. success(res) {
  100. successfun(res.data)
  101. }
  102. })
  103. }
  104. function wxpost(url, data, successfun) {
  105. wx.request({
  106. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  107. method: "POST",
  108. header: {
  109. 'content-type': 'application/json',
  110. 'Authorization': 'Bearer ' + app.globalToken
  111. },
  112. data: data,
  113. success(res) {
  114. successfun(res.data)
  115. }
  116. })
  117. }
  118. function get(url, data, successfun) {
  119. wx.request({
  120. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  121. method: "Get",
  122. header: {
  123. 'content-type': 'application/x-www-form-urlencoded'
  124. },
  125. params: data,
  126. success(res) {
  127. successfun(res.data)
  128. }
  129. })
  130. }
  131. function send_postdecode(url, data, successfun) {
  132. wx.request({
  133. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  134. method: "POST",
  135. header: {
  136. 'content-type': 'application/x-www-form-urlencoded'
  137. },
  138. data: data,
  139. success(res) {
  140. var json = JSON.parse(res.data)
  141. successfun(json)
  142. }
  143. })
  144. }
  145. function send_photo(data, successfun) {
  146. var that = this
  147. for (let i = 0; i < data.length; i++) {
  148. console.log("data长度=" + data.length)
  149. console.log(data)
  150. wx.uploadFile({
  151. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  152. filePath: data[i] + "",
  153. name: 'file',
  154. success: function (res) {
  155. var json = JSON.parse(res.data)
  156. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  157. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  158. successfun(sss)
  159. }
  160. })
  161. }
  162. }
  163. function send(data, successfun) {
  164. var that = this
  165. wx.uploadFile({
  166. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  167. filePath: data[0] + "",
  168. name: 'file',
  169. success: function (res) {
  170. var json = JSON.parse(res.data)
  171. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  172. // wx.setStorageSync("carnumber", sss.data.carNumber)
  173. successfun(sss)
  174. },
  175. fail: function (res) {}
  176. })
  177. }
  178. module.exports = {
  179. BASE_Server: BASE_Server,
  180. send_post: send_post,
  181. send_photo: send_photo,
  182. send: send,
  183. post: post,
  184. wxpost: wxpost,
  185. get: get,
  186. send_postdecode: send_postdecode,
  187. send_post_login,
  188. showLoading,
  189. hideLoading,
  190. }
  191. function showLoading() {
  192. wx.showLoading({
  193. title: '加载中...',
  194. mask: true,
  195. })
  196. }
  197. function hideLoading() {
  198. wx.hideLoading()
  199. }