app.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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, info ={} ){
  53. let that = this
  54. wx.login({
  55. success: res => {
  56. if (res.code) {
  57. let param = Object.assign({ code: res.code }, info);
  58. //发起网络请求
  59. util.http("/base/wxLogin", param, (errCode, data) => {
  60. if( errCode == 0){
  61. let {user, token,expiresAt} = data
  62. user.token = token;
  63. user.expiresAt = expiresAt;
  64. this.globalData.userInfo = user
  65. console.log("login_ok_u", user)
  66. wx.setStorageSync("@yunyuanqu", user)
  67. this.getMakerInfo(cb, true);
  68. }else{
  69. util.showMsg( data )
  70. cb&&cb( {} )
  71. }
  72. });
  73. } else {
  74. console.log('登录失败!' + res.errMsg)
  75. }
  76. }
  77. })
  78. },
  79. checkLogin:function( cb ){
  80. let info = this.globalData.userInfo;
  81. if (!info.token){
  82. let cacheUser = wx.getStorageSync("@yunyuanqu");
  83. console.log("cacheUser", cacheUser)
  84. if( !cacheUser || cacheUser.expiresAt < Date.now() ){
  85. wx.navigateTo({
  86. url: '/pages/login/login',
  87. })
  88. }else{
  89. this.globalData.userInfo = cacheUser
  90. cb&&cb( cacheUser )
  91. }
  92. }else{
  93. cb&&cb( info )
  94. }
  95. },
  96. getSysInfo: function (cb) {
  97. wx.getSystemInfo({
  98. success: function (res) {
  99. typeof cb == "function" && cb(res);
  100. }
  101. })
  102. },
  103. goHome() {
  104. wx.navigateTo({
  105. url: '/pages/index/index',
  106. })
  107. },
  108. getLocation: function (cb) {
  109. var that = this;
  110. wx.getLocation({
  111. type: 'wgs84',
  112. success: function (res) {
  113. console.log(res)
  114. typeof cb == "function" && cb(res);
  115. }
  116. })
  117. },
  118. checkPermission( cb ){
  119. wx.getSetting({
  120. success: function (res) {
  121. if (!res.authSetting['scope.userLocation']) {
  122. //申请授权
  123. wx.authorize({
  124. scope: 'scope.userLocation',
  125. success() {
  126. console.log("authorize success")
  127. cb&&cb()
  128. },
  129. fail(){
  130. console.log("authorize fail")
  131. }
  132. })
  133. }else{
  134. cb&&cb()
  135. }
  136. },
  137. fail: function(res){
  138. console.log("fail", res)
  139. }
  140. })
  141. },
  142. getLocation(){
  143. return this.globalData.location;
  144. },
  145. setLocaltion( location ){
  146. this.globalData.location = location;
  147. },
  148. getUserInfo( ){
  149. return this.globalData.userInfo
  150. },
  151. setUserInfo( userInfo ){
  152. userInfo = Object.assign(this.globalData.userInfo, userInfo )
  153. this.globalData.userInfo = userInfo;
  154. },
  155. setMakerInfo( maker ){
  156. maker = Object.assign(this.globalData.maker, maker )
  157. this.globalData.maker = maker;
  158. console.log( "setMakerInfo", maker)
  159. },
  160. identifyUser(){
  161. this.globalData.userInfo.identify=1
  162. },
  163. getMakerInfo( cb, noCache ){
  164. if( !noCache && this.globalData.maker.id ){
  165. cb&&cb( this.globalData.maker);
  166. }else{
  167. util.http( '/wx/getMakerInfo', {}, (err,res)=>{
  168. if( err == 0 ) {
  169. this.globalData.maker = res.info
  170. cb&&cb( res.info );
  171. }else{
  172. cb&&cb( {} );
  173. }
  174. })
  175. }
  176. },
  177. setTaskTab(val){
  178. this.globalData.taskTab = val||0;
  179. },
  180. setAction(val){
  181. this.globalData.action = val;
  182. },
  183. getAction(){
  184. return this.globalData.action;
  185. },
  186. globalData: {
  187. taskTab:0,
  188. signUrl:"https://yyq2.oss-cn-shanghai.aliyuncs.com/pdf/sign.jpg",
  189. action: false,
  190. userInfo: {},
  191. location: {},
  192. maker: {},
  193. Custom:0,
  194. CustomBar:0,
  195. CustomRate:0,
  196. StatusBar:0
  197. }
  198. })