app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //app.js
  2. const util = require("util/util.js")
  3. App({
  4. onLaunch: function() {
  5. // 初始化配置
  6. this.autoUpgrade()
  7. wx.getSystemInfo({
  8. success: e => {
  9. this.globalData.StatusBar = e.statusBarHeight;
  10. let capsule = wx.getMenuButtonBoundingClientRect();
  11. if (capsule) {
  12. this.globalData.Custom = capsule;
  13. this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
  14. } else {
  15. this.globalData.CustomBar = e.statusBarHeight + 50;
  16. }
  17. this.globalData.CustomRate = e.screenHeight / e.screenWidth;
  18. }
  19. })
  20. },
  21. autoUpgrade: function(){
  22. if (wx.canIUse('getUpdateManager')) {
  23. const updateManager = wx.getUpdateManager()
  24. updateManager.onCheckForUpdate(function (res) {
  25. // 请求完新版本信息的回调
  26. if (res.hasUpdate) {
  27. console.log('res.hasUpdate====')
  28. updateManager.onUpdateReady(function () {
  29. wx.showModal({
  30. title: '更新提示',
  31. content: '新版本已经准备好,是否重启应用?',
  32. success: function (res) {
  33. console.log('success====', res)
  34. if (res.confirm) {
  35. updateManager.applyUpdate()
  36. }
  37. }
  38. })
  39. })
  40. updateManager.onUpdateFailed(function () {
  41. // 新的版本下载失败
  42. wx.showModal({
  43. title: '已经有新版本了哟~',
  44. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  45. })
  46. })
  47. }
  48. })
  49. }
  50. },
  51. // 微信登入
  52. doLogin: function( cb ){
  53. let that = this
  54. wx.login({
  55. success: res => {
  56. if (res.code) {
  57. let param = { code: res.code };
  58. //发起网络请求
  59. util.http("/base/wxLogin", param, (errCode, data) => {
  60. if( errCode == 0){
  61. let {user, token} = data
  62. user.token = token;
  63. wx.setStorageSync('@yunyuanqu', user);
  64. }else{
  65. util.showMsg( data )
  66. }
  67. cb&&cb(errCode, data );
  68. });
  69. } else {
  70. console.log('登录失败!' + res.errMsg)
  71. }
  72. }
  73. })
  74. },
  75. checkLogin:function( cb ){
  76. var info = wx.getStorageSync('@yunyuanqu')||{};
  77. let isLogin = !!info.token;
  78. this.globalData.userInfo = info;
  79. if (!isLogin){
  80. this.doLogin( cb )
  81. }else{
  82. cb&&cb( )
  83. }
  84. },
  85. getSysInfo: function (cb) {
  86. wx.getSystemInfo({
  87. success: function (res) {
  88. console.log(res)
  89. typeof cb == "function" && cb(res);
  90. }
  91. })
  92. },
  93. goHome() {
  94. wx.navigateTo({
  95. url: '/pages/index/index',
  96. })
  97. },
  98. getLocation: function (cb) {
  99. var that = this;
  100. wx.getLocation({
  101. type: 'wgs84',
  102. success: function (res) {
  103. console.log(res)
  104. typeof cb == "function" && cb(res);
  105. }
  106. })
  107. },
  108. checkPermission( cb ){
  109. wx.getSetting({
  110. success: function (res) {
  111. if (!res.authSetting['scope.userLocation']) {
  112. //申请授权
  113. wx.authorize({
  114. scope: 'scope.userLocation',
  115. success() {
  116. console.log("authorize success")
  117. cb&&cb()
  118. },
  119. fail(){
  120. console.log("authorize fail")
  121. }
  122. })
  123. }else{
  124. cb&&cb()
  125. }
  126. },
  127. fail: function(res){
  128. console.log("fail", res)
  129. }
  130. })
  131. },
  132. getLocation(){
  133. return this.globalData.location;
  134. },
  135. setLocaltion( location ){
  136. this.globalData.location = location;
  137. },
  138. getUserInfo( ){
  139. return this.globalData.userInfo
  140. },
  141. setUserInfo( userInfo ){
  142. userInfo = Object.assign(this.globalData.userInfo, userInfo )
  143. wx.setStorageSync('@flogin', userInfo);
  144. this.globalData.userInfo = userInfo;
  145. },
  146. setMakerInfo( maker ){
  147. maker = Object.assign(this.globalData.maker, maker )
  148. this.globalData.maker = maker;
  149. },
  150. getMakerInfo( cb ){
  151. if( this.globalData.maker.id ){
  152. cb&&cb( this.globalData.maker);
  153. }else{
  154. util.http( '/wx/getMakerInfo', {}, (err,res)=>{
  155. if( err == 0 ) {
  156. this.globalData.maker = res.info
  157. cb&&cb( res.info );
  158. }else{
  159. cb&&cb( {} );
  160. }
  161. })
  162. }
  163. },
  164. globalData: {
  165. userInfo: {},
  166. location: {},
  167. maker: {},
  168. Custom:0,
  169. CustomBar:0,
  170. CustomRate:0,
  171. StatusBar:0
  172. }
  173. })