app.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. this.globalData.userInfo = user
  64. wx.setStorageSync("@yunyuanqu", user)
  65. cb&&cb( user )
  66. }else{
  67. util.showMsg( data )
  68. cb&&cb( {} )
  69. }
  70. });
  71. } else {
  72. console.log('登录失败!' + res.errMsg)
  73. }
  74. }
  75. })
  76. },
  77. checkLogin:function( cb ){
  78. let info = this.globalData.userInfo;
  79. if (!info.token){
  80. this.doLogin( cb )
  81. }else{
  82. cb&&cb( info )
  83. }
  84. },
  85. getSysInfo: function (cb) {
  86. wx.getSystemInfo({
  87. success: function (res) {
  88. typeof cb == "function" && cb(res);
  89. }
  90. })
  91. },
  92. goHome() {
  93. wx.navigateTo({
  94. url: '/pages/index/index',
  95. })
  96. },
  97. getLocation: function (cb) {
  98. var that = this;
  99. wx.getLocation({
  100. type: 'wgs84',
  101. success: function (res) {
  102. console.log(res)
  103. typeof cb == "function" && cb(res);
  104. }
  105. })
  106. },
  107. checkPermission( cb ){
  108. wx.getSetting({
  109. success: function (res) {
  110. if (!res.authSetting['scope.userLocation']) {
  111. //申请授权
  112. wx.authorize({
  113. scope: 'scope.userLocation',
  114. success() {
  115. console.log("authorize success")
  116. cb&&cb()
  117. },
  118. fail(){
  119. console.log("authorize fail")
  120. }
  121. })
  122. }else{
  123. cb&&cb()
  124. }
  125. },
  126. fail: function(res){
  127. console.log("fail", res)
  128. }
  129. })
  130. },
  131. getLocation(){
  132. return this.globalData.location;
  133. },
  134. setLocaltion( location ){
  135. this.globalData.location = location;
  136. },
  137. getUserInfo( ){
  138. return this.globalData.userInfo
  139. },
  140. setUserInfo( userInfo ){
  141. userInfo = Object.assign(this.globalData.userInfo, userInfo )
  142. this.globalData.userInfo = userInfo;
  143. },
  144. setMakerInfo( maker ){
  145. maker = Object.assign(this.globalData.maker, maker )
  146. this.globalData.maker = maker;
  147. },
  148. identifyUser(){
  149. this.globalData.userInfo.identify=1
  150. },
  151. getMakerInfo( cb ){
  152. if( this.globalData.maker.id ){
  153. cb&&cb( this.globalData.maker);
  154. }else{
  155. util.http( '/wx/getMakerInfo', {}, (err,res)=>{
  156. if( err == 0 ) {
  157. this.globalData.maker = res.info
  158. cb&&cb( res.info );
  159. }else{
  160. cb&&cb( {} );
  161. }
  162. })
  163. }
  164. },
  165. setTaskTab(val){
  166. this.globalData.taskTab = val||0;
  167. },
  168. setAction(val){
  169. this.globalData.action = val;
  170. },
  171. getAction(){
  172. return this.globalData.action;
  173. },
  174. globalData: {
  175. taskTab:0,
  176. action: false,
  177. userInfo: {},
  178. location: {},
  179. maker: {},
  180. Custom:0,
  181. CustomBar:0,
  182. CustomRate:0,
  183. StatusBar:0
  184. }
  185. })