app.js 957 B

123456789101112131415161718192021222324252627282930313233343536
  1. //app.js
  2. import { default as commonApi } from "./api/commonApi"
  3. import { default as userApi } from "./api/user"
  4. import { promisifyAll, promisify } from 'miniprogram-api-promise';
  5. const wxp = {}
  6. // promisify all wx's api
  7. promisifyAll(wx, wxp)
  8. App({
  9. onLaunch: async function () {
  10. // 展示本地存储能力
  11. if(!wx.getStorageSync('token')){
  12. const tokenData = await commonApi.getToken();
  13. if(tokenData.token){
  14. wx.setStorageSync('token', tokenData.token)
  15. }
  16. }
  17. // 登录
  18. const auth = await wxp.getSetting()
  19. if (!auth.authSetting['scope.userInfo']){
  20. console.log('您还没有授权');
  21. return
  22. }
  23. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  24. const info = await wxp.getUserInfo()
  25. this.globalData.userInfo = res.userInfo
  26. if (this.userInfoReadyCallback) {
  27. this.userInfoReadyCallback(res)
  28. }
  29. },
  30. globalData: {
  31. userInfo: null
  32. }
  33. })