index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // components/popup/index.js
  2. const app = getApp();
  3. Component({
  4. observers: {
  5. 'globalData.innerShow': function (value) {
  6. // 当globalData.someData发生变化时,这个函数会被调用
  7. //console.log(`globalData.innerShow changed to: ${value}`);
  8. if (app.globalData.showModal) {
  9. return;
  10. }
  11. this.setData({
  12. userInfo: wx.getStorageSync('userInfo'),
  13. innerShow: app.globalData.innerShow
  14. })
  15. this.init();
  16. },
  17. 'globalData.showModal': function (value) {
  18. // 当globalData.someData发生变化时,这个函数会被调用
  19. //console.log(`globalData.innerShow changed to: ${value}`);
  20. if (app.globalData.value) {
  21. return;
  22. }
  23. this.setData({
  24. userInfo: wx.getStorageSync('userInfo'),
  25. innerShow: app.globalData.innerShow
  26. })
  27. this.init();
  28. }
  29. },
  30. /**
  31. * 组件的属性列表
  32. */
  33. properties: {
  34. },
  35. /**
  36. * 组件的初始数据
  37. */
  38. data: {
  39. userInfo: wx.getStorageSync('userInfo'),
  40. innerShow: false,
  41. showModal: app.globalData.showModal,
  42. },
  43. lifetimes: {
  44. attached: function () {
  45. let that = this;
  46. this.setData({
  47. userInfo: wx.getStorageSync('userInfo'),
  48. });
  49. app.watch("innerShow", (n, v) => {
  50. that.setData({
  51. userInfo: wx.getStorageSync('userInfo')
  52. })
  53. if (app.globalData.innerShow) {
  54. that.init();
  55. }
  56. })
  57. },
  58. },
  59. /**
  60. * 组件的方法列表
  61. */
  62. methods: {
  63. confirm(e) {
  64. this.disPopUp()
  65. this.triggerEvent("ok")
  66. },
  67. async confirmMobile(e) {
  68. let that = this;
  69. //console.log(e)
  70. if (!e.detail.code) {
  71. return;
  72. }
  73. wx.showLoading({
  74. title: '登录中',
  75. mask: true
  76. })
  77. wx.login({
  78. async success(res3) {
  79. //console.log(res3)
  80. let {
  81. data
  82. } = await app.getApiAsync("/wx/getUserInfo", {
  83. code: res3.code,
  84. pcode: e.detail.code,
  85. shareId: app.globalData.shareId || ''
  86. });
  87. that.setData({
  88. userInfo: data.data
  89. });
  90. app.globalData.userInfo = data.data;
  91. wx.setStorageSync('userInfo', data.data);
  92. app.globalData.token = data.token;
  93. wx.setStorageSync('token', data.token);
  94. that.disPopUp()
  95. that.triggerEvent("ok")
  96. }
  97. })
  98. },
  99. init() {
  100. let that = this;
  101. if (wx.getPrivacySetting) {
  102. wx.getPrivacySetting({
  103. success: res => {
  104. //console.log("是否需要授权:", res.needAuthorization, "隐私协议的名称为:", res.privacyContractName)
  105. if (res.needAuthorization) {
  106. this.popUp()
  107. } else if (!that.data.userInfo || !that.data.userInfo.userId) {
  108. this.popUp()
  109. } else {
  110. this.triggerEvent("ok")
  111. }
  112. },
  113. fail: () => {},
  114. complete: () => {},
  115. })
  116. } else {
  117. // 低版本基础库不支持 wx.getPrivacySetting 接口,隐私接口可以直接调用
  118. this.triggerEvent("ok")
  119. }
  120. },
  121. openUserXieyi() {
  122. wx.navigateTo({
  123. url: '/pages/autoPage/autoPage?code=USER_SERVICE_AGREEMENT',
  124. })
  125. },
  126. handleDisagree(e) {
  127. this.triggerEvent("nok")
  128. this.disPopUp()
  129. },
  130. handleAgree(e) {
  131. //console.log("ok")
  132. this.triggerEvent("ok")
  133. this.disPopUp()
  134. },
  135. popUp() {
  136. this.setData({
  137. innerShow: true
  138. })
  139. },
  140. disPopUp() {
  141. this.setData({
  142. innerShow: false
  143. })
  144. },
  145. openPrivacyContract() {
  146. wx.openPrivacyContract({
  147. success: res => {
  148. //console.log('openPrivacyContract success')
  149. },
  150. fail: res => {
  151. console.error('openPrivacyContract fail', res)
  152. }
  153. })
  154. }
  155. }
  156. })