帮你贷的小程序

app.js 4.6KB

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