app.js 4.9 KB

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