app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //app.js
  2. import md5 from 'utils/md5.js';
  3. App({
  4. globalData: {
  5. isFullSucreen: false, // 当前设备是否为 FullSucreen
  6. key: '12345678901234567890abcdefghijkl',
  7. openid: null,
  8. userInfo: null,
  9. usertype: 1,
  10. nickName: null,
  11. userphone: null,
  12. introducer: '',
  13. xu_content_date: '',
  14. xu_content_type: '',
  15. payurl: 'https://test.yueguanjia.com/jielin-web/pay/third/prepay/approve',
  16. url: 'https://api.zhumi.tech/mili_admin'
  17. // url: 'https://www.e-jinying.com/jy-ty-1'//正式
  18. },
  19. onLaunch: function() {
  20. this.checkFullSucreen();
  21. var logs = wx.getStorageSync('logs') || [];
  22. logs.unshift(Date.now());
  23. wx.setStorageSync('logs', logs);
  24. // 获取今日日期
  25. let date = new Date();
  26. let year = date.getFullYear().toString().padStart(2, '0');
  27. let month = (date.getMonth() + 1).toString().padStart(2, '0');
  28. let day = date.getDate().toString().padStart(2, '0');
  29. let time = year + "-" + month + "-" + day;
  30. wx.setStorageSync('todayDate', time);
  31. wx.getSystemInfo({
  32. success: res => {
  33. //导航高度
  34. this.globalData.navHeight = res.statusBarHeight + 46;
  35. },
  36. fail(err) {
  37. console.log(err);
  38. }
  39. })
  40. },
  41. timeOut: function() {
  42. wx.showToast({
  43. title: '网络开小差啦,请稍后尝试......',
  44. icon: 'none',
  45. duration: 1500
  46. })
  47. },
  48. //检查是否仍有登陆session
  49. checkSession: function(data) {
  50. const _this = this;
  51. return new Promise(function(resolve, reject) {
  52. wx.checkSession({
  53. success(res) {
  54. //session_key 未过期,并且在本生命周期一直有效
  55. _this.setUserInfo().then(() => {
  56. resolve()
  57. });
  58. },
  59. fail(res) {
  60. wx.setStorageSync('resofOpenid', {});
  61. wx.setStorageSync('PHONE', '');
  62. // session_key 已经失效,需要重新执行登录流程
  63. //重新登录
  64. let Url = '/pages/login/login'
  65. if (data) {
  66. Url = '/pages/login/login?fromwhere=' + data.fromwhere
  67. }
  68. wx.navigateTo({
  69. url: Url
  70. })
  71. }
  72. })
  73. })
  74. },
  75. //检查是否授权
  76. ifAuthorization: function() {
  77. var phone = wx.getStorageSync('PHONE');
  78. if (phone) {
  79. this.globalData.userphone = phone;
  80. } else {
  81. wx.navigateTo({
  82. url: '/pages/getphone/getphone'
  83. })
  84. }
  85. },
  86. //给用户信息赋值
  87. setUserInfo: function() {
  88. const _this = this;
  89. return new Promise((resolve, reject) => {
  90. var resofOpenid = wx.getStorageSync('resofOpenid');
  91. _this.globalData.openid = resofOpenid.openid;
  92. if(resofOpenid.phone){
  93. _this.globalData.userphone = resofOpenid.phone;
  94. wx.setStorageSync('PHONE', resofOpenid.phone);
  95. }
  96. // if (resofOpenid.type == 2) {
  97. wx.getSetting({
  98. success: resofGetSetting => {
  99. if (resofGetSetting.authSetting['scope.userInfo']) { //同意
  100. wx.getUserInfo({ //获取用户信息,不包括手机号
  101. success: res => {
  102. _this.globalData.userInfo = res.userInfo;
  103. _this.globalData.usertype = 2;
  104. wx.request({
  105. url: _this.globalData.url + '/updateWXUser',
  106. method: 'post',
  107. header: {
  108. "Content-Type": "application/x-www-form-urlencoded",
  109. "Accept": "application/json"
  110. },
  111. data: {
  112. xu_openid: resofOpenid.openid,
  113. xu_isauthorize: 1,
  114. xu_name: res.userInfo.nickName,
  115. xu_avatarUrl: res.userInfo.avatarUrl,
  116. xu_city: res.userInfo.city,
  117. xu_gender: res.userInfo.gender,
  118. xu_country: res.userInfo.country,
  119. xu_language: res.userInfo.language,
  120. xu_province: res.userInfo.province,
  121. xu_sessionkey: resofOpenid.session_key
  122. },
  123. success: function(res) {
  124. resolve();
  125. },
  126. fail: function(error) {
  127. app.timeOut()
  128. reject(error,2333333333333333); //请求失败
  129. },
  130. })
  131. // })
  132. return true;
  133. },
  134. fail(err) {
  135. app.timeOut();
  136. }
  137. })
  138. } else {
  139. app.globalData.usertype = 1;
  140. app.globalData.nickName = resofOpenid.nickName;
  141. app.globalData.userphone = resofOpenid.phone;
  142. return false;
  143. }
  144. },
  145. fail(err) {
  146. app.timeOut()
  147. }
  148. })
  149. // } else {
  150. // _this.globalData.usertype = 1;
  151. // _this.globalData.nickName = resofOpenid.nickName;
  152. // return false;
  153. // }
  154. })
  155. },
  156. gopage: function(target) {
  157. var pages = getCurrentPages()
  158. var back = 0;
  159. if (pages.length >= 10) {
  160. wx.redirectTo({
  161. url: target
  162. });
  163. } else {
  164. for (var i = 0; i < pages.length; i++) {
  165. pages[i].route == target
  166. if (pages[i].route == target.substr(1, target.length)) {
  167. back = i + 1
  168. break;
  169. }
  170. }
  171. }
  172. if (back == 0) {
  173. wx.navigateTo({
  174. url: target
  175. });
  176. } else {
  177. wx.navigateBack({
  178. delta: pages.length - back
  179. });
  180. }
  181. },
  182. checkFullSucreen: function() {
  183. // const self = this;
  184. let result = false;
  185. wx.getSystemInfo({
  186. success: function(res) {
  187. const rate = res.windowHeight / res.windowWidth;
  188. let limit = res.windowHeight == res.screenHeight ? 1.8 : 1.65; // 临界判断值
  189. if (rate > limit) {
  190. result = true;
  191. }
  192. wx.setStorageSync('isFullscreen', result)
  193. }
  194. })
  195. return result;
  196. }
  197. })