app.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. addForm( e ){
  94. let { formId } = e.detail;
  95. if (!formId || formId == "the formId is a mock one" ){
  96. return;
  97. }
  98. util.http("Form/addForm", {formId}, (err, res) => {
  99. console.log( "addForm", err, res);
  100. });
  101. },
  102. goHome() {
  103. wx.navigateTo({
  104. url: '/pages/index/index',
  105. })
  106. },
  107. getLocation: function (cb) {
  108. var that = this;
  109. wx.getLocation({
  110. type: 'wgs84',
  111. success: function (res) {
  112. console.log(res)
  113. typeof cb == "function" && cb(res);
  114. }
  115. })
  116. },
  117. checkPermission( cb ){
  118. wx.getSetting({
  119. success: function (res) {
  120. if (!res.authSetting['scope.userLocation']) {
  121. //申请授权
  122. wx.authorize({
  123. scope: 'scope.userLocation',
  124. success() {
  125. console.log("authorize success")
  126. cb&&cb()
  127. },
  128. fail(){
  129. console.log("authorize fail")
  130. }
  131. })
  132. }else{
  133. cb&&cb()
  134. }
  135. },
  136. fail: function(res){
  137. console.log("fail", res)
  138. }
  139. })
  140. },
  141. setGbInfo:function( info ){
  142. this.globalData.info = info;
  143. },
  144. getGbInfo: function(){
  145. if( !this.globalData.info ){
  146. let data = wx.getStorageSync( "@data")||{};
  147. let {thumb, image, wiki} = data;
  148. this.globalData.info = {thumb,image, wiki};
  149. }
  150. return this.globalData.info;
  151. },
  152. getGbVote(){
  153. return this.globalData.voteInfo;
  154. },
  155. setGbVote( voteInfo ){
  156. this.globalData.voteInfo = voteInfo;
  157. },
  158. getLocation(){
  159. return this.globalData.location;
  160. },
  161. setLocaltion( location ){
  162. this.globalData.location = location;
  163. },
  164. getUserInfo( ){
  165. return this.globalData.userInfo
  166. },
  167. setUserInfo( userInfo ){
  168. userInfo = Object.assign(this.globalData.userInfo, userInfo )
  169. wx.setStorageSync('@flogin', userInfo);
  170. this.globalData.userInfo = userInfo;
  171. },
  172. globalData: {
  173. userInfo: {},
  174. location: {},
  175. info: null,
  176. voteInfo: null,
  177. wiki:[],
  178. Custom:0,
  179. CustomBar:0,
  180. CustomRate:0,
  181. StatusBar:0
  182. }
  183. })