app.js 4.7 KB

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