app.js 5.2 KB

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