App.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <style lang="scss">
  2. /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  3. @import "uview-ui/index.scss";
  4. </style>
  5. <script>
  6. export default {
  7. onLaunch: function () {
  8. console.log('App Launch')
  9. },
  10. onShow: function () {
  11. console.log('App Show 小程序显示了+++++++++++++++++++++++')
  12. let that = this;
  13. // 检查是否已登录(有 accessToken)
  14. const accessToken = uni.getStorageSync('accessToken')
  15. if (accessToken) {
  16. // 已登录,直接使用本地数据执行拓客逻辑,不再调用 wxLogin
  17. console.log('++++++++++已登录用户,直接使用本地数据执行拓客逻辑++++++++++')
  18. that.handleShareBind()
  19. that.handleExpandLogic()
  20. return
  21. }
  22. // 未登录,走静默登录
  23. wx.login({
  24. success(re) {
  25. that.$api.wxLogin({code: re.code}).then(res => {
  26. console.log('++++++++++登录返回了++++++++++++++++', res)
  27. uni.setStorageSync('userInfo', res.data.data.userInfo)
  28. uni.setStorageSync('accessToken', res.data.data.access_token)
  29. uni.setStorageSync('phoneIsBind', res.data.data.phoneIsBind)
  30. uni.setStorageSync('wxLoginIsNew', res.data.data.isNew)
  31. let isNew = res.data.data.isNew
  32. uni.removeStorageSync('parent_member_id');
  33. that.handleShareBind(res.data.data.userInfo.otherId)
  34. that.handleExpandLogic(isNew)
  35. }).catch(err => {
  36. console.log('++++++++++App.vue wxLogin 失败++++++++++++++++')
  37. console.log('err.data:', err.data)
  38. console.log('err.statusCode:', err.statusCode)
  39. console.log('err.header:', err.header)
  40. })
  41. },
  42. })
  43. },
  44. methods: {
  45. // 处理用户分享绑定
  46. handleShareBind(openId) {
  47. let shareUserId = uni.getStorageSync('shareUserId')
  48. if (!shareUserId) return
  49. let userInfo = uni.getStorageSync('userInfo')
  50. let targetOpenId = openId || (userInfo && userInfo.otherId)
  51. if (!targetOpenId) return
  52. this.$api.scanCode({
  53. openId: targetOpenId,
  54. memberId: shareUserId
  55. }).then(res => {
  56. console.log('用户分享用户绑定成功', res)
  57. uni.removeStorageSync('shareUserId');
  58. })
  59. },
  60. // 处理拓客端扫码逻辑
  61. // isNew: 是否新用户(true=新用户弹窗领券,false=老用户记录推广失败)
  62. handleExpandLogic(isNew) {
  63. let expandParam = uni.getStorageSync('expandParam')
  64. if (!expandParam) return
  65. // 使用传入的参数或本地缓存
  66. let _isNew = isNew !== undefined ? isNew : uni.getStorageSync('wxLoginIsNew')
  67. if (expandParam.isUse != 1){
  68. console.log('获取到的拓客端参数',expandParam)
  69. if (new Date().getTime() <= expandParam.timestamp){
  70. if (_isNew){
  71. //登录成功,且是新用户,触发首页的自定义事件(弹出新人专享窗口)
  72. uni.$emit("loginSuccess")
  73. // 标记为已使用,避免重复处理
  74. expandParam.isUse = 1
  75. uni.setStorageSync('expandParam', expandParam)
  76. }else {
  77. this.$api.addRecordForAttach({
  78. ...expandParam,
  79. isNew:0,
  80. isBindSuccess:0,
  81. }).then(res=>{
  82. expandParam.isUse = 1
  83. uni.setStorageSync('expandParam', expandParam)
  84. console.log('+++++++++老用户,扫码推广失败记录!++++++++++++++++')
  85. })
  86. // uni.removeStorageSync('expandParam');
  87. }
  88. }else {
  89. uni.showModal({
  90. title: '温馨提示',
  91. content: ' 当前二维码已过期,请重新扫码',
  92. showCancel:false,
  93. success: function (res) {
  94. if (res.confirm) {
  95. console.log('用户点击确定');
  96. } else if (res.cancel) {
  97. console.log('用户点击取消');
  98. }
  99. }
  100. });
  101. expandParam.isUse = 1
  102. uni.setStorageSync('expandParam', expandParam)
  103. // uni.removeStorageSync('expandParam');
  104. }
  105. }
  106. }
  107. },
  108. onHide: function () {
  109. console.log('App Hide')
  110. }
  111. }
  112. </script>