微信小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.2 KiB

  1. import * as STORAGE from './utils/storage'
  2. import * as UTIL from './utils/util'
  3. import * as API from './utils/API'
  4. App({
  5. onLaunch() {
  6. var that = this;
  7. //存储storage初始化globalData数据--
  8. //何时存储,用来判断,不用获取code
  9. that.initGlobalData();
  10. //获取code
  11. UTIL.getCOdeFromWX({
  12. complate: (code) => {
  13. console.log('app:微信code,' + code);
  14. // //获取openId
  15. that.getOpenIdFromFW(code);
  16. }
  17. });
  18. //获取设备信息
  19. wx.getSystemInfo({
  20. success: function (res) {
  21. that.globalData.systemType = res.system.indexOf("Android") >= 0 ? "Android" : "IOS";
  22. that.globalData.isIphoneX = res.model.indexOf("iPhone X") >= 0 || res.model.indexOf("iPhone 1") >= 0;
  23. }
  24. });
  25. },
  26. onShow() {
  27. //更新机制
  28. this.wxappUpdateManager();
  29. },
  30. globalData: {
  31. // 系统用户登录信息(用户id、token)
  32. userInfo: {
  33. memberId: '',
  34. token: '',
  35. toastTimeout:null
  36. },
  37. //微信用户登陆信息(昵称、头像、省、城市)
  38. wxUserInfo: {
  39. nickName: '',
  40. avatarUrl: '',
  41. province: '',
  42. city: ''
  43. }
  44. ,
  45. /**
  46. * 小程序设置
  47. */
  48. setInfo: {
  49. //定位授权
  50. locationOpenIdWX: false,
  51. //纬度
  52. latitude:'',
  53. //经度
  54. longitude:'',
  55. }
  56. ,
  57. systemType:'',//设备类型 Android IOS
  58. isIphoneX: false, // 用来标识当前手机机型是否为 iPhone X
  59. },
  60. /**
  61. * 从服务端获取openId
  62. */
  63. getOpenIdFromFW(code) {
  64. let sendData = {
  65. code: code
  66. }
  67. UTIL.httpRequestNoneDetal(API.URL_GET_OPENID, sendData, "POST", {
  68. success: (res) => {
  69. if (res._code == API.SUCCESS_CODE) {
  70. UTIL.showToastNoneIcon("openId:" + res._data.openid);
  71. } else {
  72. //未获取到openId
  73. // console.log("失败,获取到openId:" + res.msg);
  74. STORAGE.setOpenId(res.data.openId)
  75. STORAGE.setSessionKey(res.data.sessionKey)
  76. // UTIL.showToastNoneIcon("openId:失败");
  77. }
  78. }
  79. })
  80. },
  81. /**
  82. * 初始化globalData
  83. */
  84. initGlobalData() {
  85. var userInfo = {
  86. memberId: STORAGE.getMemberId(),
  87. token: STORAGE.getToken()
  88. }
  89. console.log(userInfo)
  90. this.globalData.userInfo = userInfo;
  91. }
  92. ,
  93. /**
  94. * 小程序更新机制
  95. * 获取小程序更新机制兼容
  96. */
  97. wxappUpdateManager() {
  98. if (wx.canIUse('getUpdateManager')) {
  99. const updateManager = wx.getUpdateManager();
  100. if (!!updateManager) {
  101. updateManager.onCheckForUpdate(function (res) {
  102. // 请求完新版本信息的回调
  103. if (res.hasUpdate) {
  104. updateManager.onUpdateReady(function () {
  105. wx.showModal({
  106. title: '更新提示',
  107. content: '新版本已经准备好,是否重启应用?',
  108. success: function (res) {
  109. if (res.confirm) {
  110. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  111. updateManager.applyUpdate()
  112. }
  113. }
  114. })
  115. })
  116. updateManager.onUpdateFailed(function () {
  117. // 新的版本下载失败
  118. wx.showModal({
  119. title: '已经有新版本了哟~',
  120. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  121. })
  122. })
  123. }
  124. })
  125. }
  126. } else {
  127. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  128. wx.showModal({
  129. title: '提示',
  130. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  131. })
  132. }
  133. },
  134. showToast(msg, selfClass = '') {
  135. clearTimeout(this.globalData.toastTimeout);
  136. const page = getCurrentPages();
  137. const currPage = page[page.length - 1];
  138. currPage.setData({
  139. toastData: {
  140. showFlag: true,
  141. toastMsg: msg,
  142. selfClass,
  143. },
  144. });
  145. this.globalData.toastTimeout = setTimeout(() => {
  146. currPage.setData({
  147. toastData: {
  148. showFlag: false,
  149. selfClass: '',
  150. },
  151. });
  152. }, 2000);
  153. },
  154. })