帮你贷的小程序

app.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //app.js
  2. var appInstance = getApp()
  3. // console.log(appInstance.onShow())
  4. App({
  5. data:{
  6. },
  7. globalData: {
  8. common: {},
  9. userInfo: null,
  10. HOST: 'https://chaoshi.henhaojie.com',
  11. platform: 0,
  12. title: '',
  13. img: ''
  14. },
  15. // 在这个app里面this就是app实例
  16. // 监听小程序初始化
  17. onLaunch: function (){
  18. let system = '', app = this;
  19. const shareInfo = wx.getStorageSync('shareInfo');
  20. if (shareInfo) {
  21. app.globalData.title = shareInfo.title;
  22. app.globalData.img = shareInfo.img;
  23. }else{
  24. wx.redirectTo({url: '/pages/login/login'})
  25. }
  26. wx.getSystemInfo({
  27. success: function(res) {
  28. system = res.system
  29. console.log(system)
  30. if (system.indexOf('iOS') == -1) {
  31. app.globalData.platform = 1;
  32. }else{
  33. app.globalData.platform = 2;
  34. }
  35. console.log(app.globalData.platform);
  36. }
  37. })
  38. },
  39. // 监听小程序显示
  40. onShow: function (options) {
  41. },
  42. // 监听小程序隐藏
  43. onHide: function () {
  44. },
  45. onUnload () {
  46. wx.clearStorage();
  47. },
  48. // 错误监听函数
  49. onError: function (msg) {
  50. console.log(msg)
  51. },
  52. login: function (that, func) {
  53. let obj = {}, app = this, user = '', source = '';
  54. if (that.data.source) {
  55. source = that.data.source
  56. }
  57. wx.login({
  58. success: function(res) {
  59. if (res.code) {
  60. wx.request({
  61. url: app.globalData.HOST + '/user/wx/login',
  62. method: "POST",
  63. data: {
  64. code: res.code,
  65. iv: app.globalData.userInfo.iv,
  66. encryptedData: app.globalData.userInfo.encryptedData,
  67. source: source
  68. },
  69. success: function (resLogin) {
  70. if (resLogin.data.errno == 0) {
  71. user = resLogin.data.rst;
  72. wx.setStorageSync("user", user)
  73. app.globalData.common['token'] = user.token
  74. wx.switchTab({'url': '/pages/index/index'})
  75. if (func) {
  76. func();
  77. }
  78. app.initShare();
  79. }
  80. }
  81. })
  82. } else {
  83. console.log('登录失败!' + res.errMsg)
  84. }
  85. }
  86. });
  87. },
  88. initShare () {
  89. var app = this;
  90. var title='', img = '';
  91. wx.request({
  92. url: app.globalData.HOST + '/user/wx/share',
  93. method: 'POST',
  94. data: app.globalData.common,
  95. success: function (res) {
  96. if (res.data.errno == '0'){
  97. title = res.data.rst.title;
  98. img = res.data.rst.pic;
  99. }
  100. app.globalData.title = title;
  101. app.globalData.img = img;
  102. wx.setStorage({
  103. key: 'shareInfo',
  104. data: {
  105. title: title,
  106. img: img
  107. }
  108. })
  109. }
  110. });
  111. },
  112. confirmModal (content, func) {
  113. wx.showModal({
  114. title: '小提示',
  115. content: content,
  116. success: function(res) {
  117. if (res.confirm) {
  118. func();
  119. } else if (res.cancel) {
  120. }
  121. }
  122. })
  123. },
  124. noticeModal (content, func) {
  125. wx.showModal({
  126. title: '小提示',
  127. content: content,
  128. showCancel: false,
  129. confirmText: '我知道了',
  130. success: function(res) {
  131. if (res.confirm) {
  132. if (func) {
  133. func();
  134. }
  135. }
  136. }
  137. })
  138. }
  139. })