httputil.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.reLaunch({
  73. // url: '../denglu/denglu',
  74. // })
  75. // }
  76. //如果令牌为空或过期,需要重新请求
  77. wx.request({
  78. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  79. method: "POST",
  80. header: {
  81. 'content-type': 'application/json',
  82. 'Authorization': 'Bearer ' + app.globalToken
  83. },
  84. data: data,
  85. success(res) {
  86. // var d = decodeURIComponent(decodeURIComponent(res.data.data))
  87. // var json = JSON.parse(d)
  88. console.log("TAG","返回数据:"+res.data)
  89. authToken(res.data)
  90. successfun(res.data)
  91. },
  92. fail(res){
  93. wx.showToast({
  94. title: "网络请求失败",
  95. })
  96. }
  97. })
  98. }
  99. function post(url, data, successfun) {
  100. wx.request({
  101. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  102. method: "POST",
  103. header: {
  104. 'content-type': 'application/x-www-form-urlencoded',
  105. 'Authorization': 'Bearer ' + app.globalToken
  106. },
  107. data: data,
  108. success(res) {
  109. successfun(res.data)
  110. },
  111. fail(res){
  112. wx.showToast({
  113. title: "网络请求失败",
  114. })
  115. }
  116. })
  117. }
  118. // function wxpost(url, data, successfun) {
  119. // wx.request({
  120. // url: BASE_Server + url, //仅为示例,并非真实的接口地址
  121. // method: "POST",
  122. // header: {
  123. // 'content-type': 'application/json',
  124. // 'Authorization': 'Bearer ' + app.globalToken
  125. // },
  126. // data: data,
  127. // success(res) {
  128. // successfun(res.data)
  129. // }
  130. // })
  131. // }
  132. function get(url, data, successfun) {
  133. wx.request({
  134. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  135. method: "Get",
  136. header: {
  137. 'content-type': 'application/x-www-form-urlencoded'
  138. },
  139. params: data,
  140. success(res) {
  141. successfun(res.data)
  142. },
  143. fail(res){
  144. wx.showToast({
  145. title: "网络请求失败",
  146. })
  147. }
  148. })
  149. }
  150. function send_postdecode(url, data, successfun) {
  151. wx.request({
  152. url: BASE_Server + url, //仅为示例,并非真实的接口地址
  153. method: "POST",
  154. header: {
  155. 'content-type': 'application/x-www-form-urlencoded'
  156. },
  157. data: data,
  158. success(res) {
  159. var json = JSON.parse(res.data)
  160. successfun(json)
  161. }
  162. })
  163. }
  164. function send_photo(data, successfun) {
  165. var that = this
  166. for (let i = 0; i < data.length; i++) {
  167. console.log("data长度=" + data.length)
  168. console.log(data)
  169. wx.uploadFile({
  170. url: BASE_Server + "/FileUpLoadController/upload.action", //仅为示例,非真实的接口地址
  171. filePath: data[i] + "",
  172. name: 'file',
  173. success: function (res) {
  174. var json = JSON.parse(res.data)
  175. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  176. wx.setStorageSync('filename' + (i + 1), sss.data.filename);
  177. successfun(sss)
  178. }
  179. })
  180. }
  181. }
  182. function send(data, successfun) {
  183. var that = this
  184. wx.uploadFile({
  185. url: BASE_Server + "/CarIDScanController/carIduploadGetNumber.action", //仅为示例,非真实的接口地址
  186. filePath: data[0] + "",
  187. name: 'file',
  188. success: function (res) {
  189. var json = JSON.parse(res.data)
  190. var sss = JSON.parse(decodeURIComponent(decodeURIComponent(json.data)));
  191. // wx.setStorageSync("carnumber", sss.data.carNumber)
  192. successfun(sss)
  193. },
  194. fail: function (res) {}
  195. })
  196. }
  197. module.exports = {
  198. BASE_Server: BASE_Server,
  199. send_post: send_post,
  200. send_photo: send_photo,
  201. send: send,
  202. post: post,
  203. // wxpost: wxpost,
  204. get: get,
  205. send_postdecode: send_postdecode,
  206. // send_post_login,
  207. showLoading,
  208. hideLoading,
  209. }
  210. function showLoading() {
  211. wx.showLoading({
  212. title: '加载中...',
  213. mask: true,
  214. })
  215. }
  216. function hideLoading() {
  217. wx.hideLoading()
  218. }
  219. function authToken(res){
  220. if(res.code==300){
  221. wx.reLaunch({
  222. url: '../denglu/denglu',
  223. })
  224. }
  225. }