app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. const webSocket = require('./utils/websocket.js');
  2. App({
  3. //设置全局请求URL
  4. globalData: {
  5. url: 'https://xiaochengxuapi.jrjyjrjy.com/v1/'
  6. },
  7. judge_Land() {
  8. try {
  9. console.log(wx.getStorageSync('access_token'), '注册');
  10. console.log(wx.getStorageSync('wx_id'), '授权');
  11. if (wx.getStorageSync('access_token')) { //是否注册过
  12. console.log('列表页');
  13. wx.reLaunch({
  14. url: '/pages/index/index' //注册过 进入首页
  15. })
  16. } else if (wx.getStorageSync('wx_id')) { //是否授权
  17. console.log('登陆页');
  18. wx.reLaunch({
  19. url: '/pages/Landpage/Land/Land' //授权过 进入登陆页
  20. })
  21. } else {
  22. console.log('授权页');
  23. wx.reLaunch({
  24. url: '/pages/Jurisdiction/click_Jurisdiction/click_Jurisdiction' //未授权 进入授权页
  25. })
  26. }
  27. } catch (e) {
  28. // Do something when catch error
  29. }
  30. },
  31. onLoad: function (options) {
  32. webSocket.connectSocket();
  33. },
  34. onLaunch: function (options) {
  35. wx.showShareMenu();
  36. var _this = this;
  37. // 查看是否授权
  38. },
  39. // 提示框
  40. Tips(text) {
  41. wx.showToast({
  42. title: text,
  43. icon: 'none', // 不显示图标
  44. duration: 2500
  45. })
  46. },
  47. // 加载
  48. showLoading: function () {
  49. wx.showToast({
  50. title: '加载中',
  51. icon: 'loading'
  52. });
  53. },
  54. // 关闭加载
  55. cancelLoading: function () {
  56. wx.hideToast();
  57. },
  58. /**
  59. * 封装wx.request请求
  60. * url: 请求地址
  61. * data: 要传递的参数
  62. * callback: 请求成功回调函数
  63. * errFun: 请求失败回调函数
  64. **/
  65. postRequest(p_url, p_data, callback, errFun) {
  66. p_data.key = wx.getStorageSync('access_key');
  67. p_data.token = wx.getStorageSync('access_token');
  68. wx.request({
  69. url: this.globalData.url + p_url,
  70. method: 'POST',
  71. data: p_data,
  72. header: { 'Content-Type': 'application/x-www-form-urlencoded' },
  73. dataType: 'json',
  74. success: function (res) {
  75. if(res.data.code == "401"){
  76. wx.clearStorageSync();
  77. wx.reLaunch({
  78. url: '/pages/Landpage/Land/Land'
  79. });
  80. }
  81. return typeof callback == "function" && callback(res.data)
  82. },
  83. fail: function (err) {
  84. return typeof errFun == "function" && errFun(err.data)
  85. }
  86. })
  87. },
  88. getRequest(g_url, g_data, callback, errFun) {
  89. g_data.key = wx.getStorageSync('access_key');
  90. g_data.token = wx.getStorageSync('access_token');
  91. wx.request({
  92. url: this.globalData.url + g_url,
  93. method: 'GET',
  94. data: g_data,
  95. header: { 'Content-Type': 'application/x-www-form-urlencoded' },
  96. dataType: 'json',
  97. success: function (res) {
  98. return typeof callback == "function" && callback(res.data)
  99. },
  100. fail: function (err) {
  101. return typeof errFun == "function" && errFun(err.data)
  102. }
  103. })
  104. },
  105. // 图片方法
  106. Multigraph(dt_url, dt_filePath, callback) {
  107. wx.uploadFile({
  108. url: this.globalData.url + dt_url,
  109. filePath: dt_filePath,
  110. name: 'img',
  111. formData: {
  112. key: wx.getStorageSync('access_key'),
  113. token: wx.getStorageSync('access_token'),
  114. },
  115. success: function (res) {
  116. var data = JSON.parse(res.data);
  117. return typeof callback == "function" && callback(data)
  118. wx.showModal({
  119. title: '提示',
  120. content: '上传失败,请从新上传',
  121. showCancel: false,
  122. })
  123. }, fail: function (d) {
  124. wx.showModal({
  125. title: '提示',
  126. content: '上传失败',
  127. showCancel: false,
  128. })
  129. }
  130. })
  131. },
  132. getDistrict(latitude, longitude) {
  133. let _page = this;
  134. let keys = '3VTBZ-JEH6Q-M7D5I-GNPVS-3QWBH-QUFY3';
  135. wx.request({
  136. url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${keys}`,
  137. header: {
  138. 'Content-Type': 'application/json'
  139. },
  140. success: function (res) {
  141. console.log(res);
  142. if (res.data.status == 0) {
  143. console.log(res.data.result.address_component);
  144. _page.postRequest('trajectory/location/position', res.data.result.address_component)
  145. // // 省
  146. // let province = res.data.result.address_component.province;
  147. // // 市
  148. // let city = res.data.result.address_component.city;
  149. // // 区
  150. // let district = res.data.result.address_component.district;
  151. // console.log(province);
  152. }
  153. }
  154. })
  155. },
  156. Imsocket: function () {
  157. return webSocket;
  158. },
  159. UserId: function () {
  160. return wx.getStorageSync('access_token');
  161. },
  162. UserToken: function () {
  163. return wx.getStorageSync('access_user');
  164. }
  165. })
  166. getApp().judge_Land(); //判断登陆