帮你贷的小程序

app.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. var system = '', app = this;
  19. wx.getSystemInfo({
  20. success: function(res) {
  21. system = res.system
  22. console.log(system)
  23. if (system.indexOf('iOS') == -1) {
  24. app.globalData.platform = 1;
  25. }else{
  26. app.globalData.platform = 2;
  27. }
  28. console.log(app.globalData.platform);
  29. }
  30. })
  31. },
  32. // 监听小程序显示
  33. onShow: function (options) {
  34. },
  35. // 监听小程序隐藏
  36. onHide: function () {
  37. },
  38. // 错误监听函数
  39. onError: function (msg) {
  40. console.log(msg)
  41. },
  42. login: function (that, func) {
  43. let obj = {}, app = this, user = '', source = '';
  44. if (that.data.source) {
  45. source = that.data.source
  46. }
  47. wx.login({
  48. success: function(res) {
  49. if (res.code) {
  50. wx.getUserInfo({
  51. success: function(resInfo) {
  52. // console.log(resInfo);
  53. wx.request({
  54. url: app.globalData.HOST + '/user/wx/login',
  55. method: "POST",
  56. data: {
  57. code: res.code,
  58. iv: resInfo.iv,
  59. encryptedData: resInfo.encryptedData,
  60. source: source
  61. },
  62. success: function (resLogin) {
  63. if (resLogin.data.errno == 0) {
  64. user = resLogin.data.rst;
  65. wx.setStorageSync("user", user)
  66. app.globalData.common['token'] = user.token
  67. if (func) {
  68. func();
  69. }
  70. app.initShare();
  71. }
  72. }
  73. })
  74. },
  75. fail: function () {
  76. // 用户拒绝授权后再次确认
  77. app.confirmModal('必须授权登录之后才正常使用,是否重新授权', function(){
  78. if (wx.openSetting) {
  79. wx.openSetting({
  80. success: (ress) => {
  81. if (ress.authSetting["scope.userInfo"]) {
  82. wx.getUserInfo({
  83. success: function(resInfo) {
  84. wx.request({
  85. url: app.globalData.HOST + '/user/wx/login',
  86. method: "POST",
  87. data: {
  88. code: res.code,
  89. iv: resInfo.iv,
  90. encryptedData: resInfo.encryptedData
  91. },
  92. success: function (resLogin) {
  93. if (resLogin.data.errno == 0) {
  94. user = resLogin.data.rst;
  95. wx.setStorageSync("user", user)
  96. app.globalData.common['token'] = user.token
  97. if (func) {
  98. func();
  99. }
  100. }
  101. }
  102. })
  103. }
  104. })
  105. }
  106. }
  107. })
  108. }
  109. });
  110. }
  111. })
  112. } else {
  113. console.log('登录失败!' + res.errMsg)
  114. }
  115. }
  116. });
  117. },
  118. initShare () {
  119. var app = this;
  120. var title='', img = '';
  121. wx.request({
  122. url: app.globalData.HOST + '/user/wx/share',
  123. method: 'POST',
  124. data: app.globalData.common,
  125. success: function (res) {
  126. if (res.data.errno == '0'){
  127. title = res.data.rst.title;
  128. img = res.data.rst.pic;
  129. }
  130. app.globalData.title = title;
  131. app.globalData.img = img;
  132. }
  133. });
  134. },
  135. confirmModal (content, func) {
  136. wx.showModal({
  137. title: '小提示',
  138. content: content,
  139. success: function(res) {
  140. if (res.confirm) {
  141. func();
  142. } else if (res.cancel) {
  143. }
  144. }
  145. })
  146. },
  147. noticeModal (content, func) {
  148. wx.showModal({
  149. title: '小提示',
  150. content: content,
  151. showCancel: false,
  152. confirmText: '我知道了',
  153. success: function(res) {
  154. if (res.confirm) {
  155. if (func) {
  156. func();
  157. }
  158. }
  159. }
  160. })
  161. }
  162. })