微信小程序
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.

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