app.js 5.1 KB

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