index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var util = require('../../utils/util.js');
  2. var wcache = require('../../utils/wcache.js');
  3. var app = getApp();
  4. var flag = true;
  5. Component({
  6. properties: {
  7. needAuth: {
  8. type: Boolean,
  9. value: false
  10. },
  11. needPosition: {
  12. type: Boolean,
  13. value: true
  14. },
  15. navBackUrl: {
  16. type: String,
  17. value: '',
  18. observer: function (t) {
  19. if (t) app.globalData.navBackUrl = t;
  20. }
  21. }
  22. },
  23. data: {
  24. btnLoading: false,
  25. isIpx: false,
  26. auth_bg: '',
  27. loaded: false
  28. },
  29. attached: function() {
  30. this.getBg();
  31. app.globalData.isIpx && this.setData({
  32. isIpx: true
  33. });
  34. },
  35. methods: {
  36. getBg: function(){
  37. let that = this;
  38. let auth_bg = wcache.get('auth_bg', false);
  39. if (!auth_bg){
  40. app.util.request({
  41. 'url': 'entry/wxapp/index',
  42. 'data': {
  43. controller: 'index.get_auth_bg'
  44. },
  45. dataType: 'json',
  46. success: function (res) {
  47. that.setData({ loaded: true })
  48. if (res.data.code == 0) {
  49. wcache.put('auth_bg', res.data.data, 600);
  50. res.data.data && that.setData({ auth_bg: res.data.data })
  51. }
  52. }
  53. })
  54. } else {
  55. that.setData({ auth_bg, loaded: true })
  56. }
  57. },
  58. bindGetUserInfo: function (t) {
  59. var that = this;
  60. if (!this.data.btnLoading) {
  61. wx.getUserProfile({
  62. desc: "获取你的昵称、头像、地区及性别",
  63. success: function (msg) {
  64. var userInfo = msg.userInfo
  65. wx.setStorage({
  66. key: "userInfo",
  67. data: userInfo
  68. })
  69. that.setData({ btnLoading: true });
  70. util.login_prosime(that.data.needPosition, userInfo).then(function () {
  71. console.log("授权成功")
  72. that.setData({ btnLoading: false });
  73. wx.showToast({
  74. title: '登录成功',
  75. icon: 'success',
  76. duration: 2000
  77. })
  78. that.triggerEvent("authSuccess");
  79. app.globalData.changedCommunity = true;
  80. //检查获取位置权限
  81. that.data.needPosition && location.getGps();
  82. }).catch(function () {
  83. that.triggerEvent("cancel");
  84. console.log('授权失败');
  85. })
  86. },
  87. fail: ()=>{
  88. wx.showToast({
  89. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  90. icon: "none"
  91. });
  92. that.triggerEvent("cancel");
  93. that.setData({ btnLoading: false });
  94. }
  95. })
  96. }
  97. },
  98. bindGetUserInfoTwo: function (t) {
  99. var that = this;
  100. wx.showLoading({ title: '授权中' });
  101. if (flag) {
  102. flag = false;
  103. var n = t.detail;
  104. if ("getUserInfo:ok" === n.errMsg) {
  105. util.login_prosime().then(function () {
  106. console.log("授权成功")
  107. wx.hideLoading();
  108. flag = true;
  109. wx.showToast({
  110. title: '登录成功',
  111. icon: 'success',
  112. duration: 2000
  113. })
  114. that.triggerEvent("authSuccess");
  115. }).catch(function () {
  116. flag = true;
  117. wx.hideLoading();
  118. console.log('授权失败')
  119. })
  120. } else {
  121. wx.hideLoading();
  122. wx.showToast({
  123. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  124. icon: "none"
  125. });
  126. flag = true;
  127. }
  128. }
  129. }
  130. // openSetting: function () {
  131. // location.openSetting().then(function (t) {
  132. // app.globalData.location = {
  133. // lat: t.latitude,
  134. // lng: t.longitude
  135. // };
  136. // app.globalData.community.communityId || wx.redirectTo({
  137. // url: "/lionfish_comshop/pages/position/communities"
  138. // });
  139. // app.globalData.canGetGPS = true;
  140. // });
  141. // }
  142. }
  143. });