index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var util = require('../../utils/util.js');
  2. var location = require('../../utils/Location.js');
  3. var app = getApp();
  4. Component({
  5. properties: {
  6. needAuth: {
  7. type: Boolean,
  8. value: false
  9. },
  10. needPosition: {
  11. type: Boolean,
  12. value: true
  13. },
  14. navBackUrl: {
  15. type: String,
  16. value: '',
  17. observer: function (t) {
  18. if (t) app.globalData.navBackUrl = t;
  19. }
  20. }
  21. },
  22. attached: function () {
  23. this.setData({
  24. skin: getApp().globalData.skin,
  25. })
  26. this.getBg();
  27. },
  28. data: {
  29. btnLoading: false,
  30. canIUse: wx.canIUse('button.open-type.getUserInfo')
  31. },
  32. methods: {
  33. getBg: function () {
  34. let that = this;
  35. app.util.request({
  36. 'url': 'entry/wxapp/index',
  37. 'data': {
  38. controller: 'index.get_newauth_bg'
  39. },
  40. dataType: 'json',
  41. success: function (res) {
  42. that.setData({ loaded: true })
  43. if (res.data.code == 0) {
  44. let { newauth_bg_image, newauth_confirm_image, newauth_cancel_image } = res.data.data;
  45. that.setData({ newauth_bg_image, newauth_confirm_image, newauth_cancel_image })
  46. }
  47. }
  48. })
  49. },
  50. close: function () {
  51. this.triggerEvent("cancel");
  52. },
  53. bindGetUserInfo: function (t) {
  54. var that = this;
  55. if (!this.data.btnLoading) {
  56. var n = t.detail;
  57. if (this.setData({ btnLoading: true }), "getUserInfo:ok" === n.errMsg) {
  58. util.login_prosime(that.data.needPosition).then(function () {
  59. console.log("授权成功")
  60. that.setData({ btnLoading: false });
  61. wx.showToast({
  62. title: '登录成功',
  63. icon: 'success',
  64. duration: 2000
  65. })
  66. that.triggerEvent("authSuccess");
  67. app.globalData.changedCommunity = true;
  68. //检查获取位置权限
  69. that.data.needPosition && location.getGps();
  70. }).catch(function () {
  71. that.triggerEvent("cancel");
  72. console.log('授权失败');
  73. })
  74. } else {
  75. wx.showToast({
  76. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  77. icon: "none"
  78. });
  79. this.triggerEvent("cancel");
  80. this.setData({ btnLoading: false });
  81. }
  82. }
  83. },
  84. getProfile: function() {
  85. var that = this;
  86. console.log(wx.canIUse("getUserProfile"));
  87. if (!that.data.btnLoading) {
  88. // if(wx.canIUse("getUserProfile")) {
  89. wx.getUserProfile({
  90. desc: "获取你的昵称、头像、地区及性别",
  91. success: function (msg) {
  92. var userInfo = msg.userInfo
  93. wx.setStorage({
  94. key: "userInfo",
  95. data: userInfo
  96. })
  97. that.setData({ btnLoading: true });
  98. util.login_prosime(that.data.needPosition, userInfo).then( res => {
  99. console.log("授权成功")
  100. that.setData({ btnLoading: false });
  101. wx.showToast({
  102. title: '登录成功',
  103. icon: 'success',
  104. duration: 2000
  105. })
  106. that.triggerEvent("authSuccess", res);
  107. app.globalData.changedCommunity = true;
  108. //检查获取位置权限
  109. that.data.needPosition && location.getGps();
  110. }).catch(function () {
  111. that.triggerEvent("cancel");
  112. console.log('授权失败');
  113. })
  114. },
  115. fail: ()=>{
  116. wx.showToast({
  117. title: "授权失败,为了完整体验,获取更多优惠活动,需要您的授权。",
  118. icon: "none"
  119. });
  120. that.triggerEvent("cancel");
  121. that.setData({ btnLoading: false });
  122. }
  123. })
  124. }
  125. // } else {
  126. // wx.showModal({
  127. // title: '提示',
  128. // content: '请升级微信',
  129. // showCancel: false
  130. // })
  131. // }
  132. },
  133. checkWxLogin: function() {
  134. return new Promise((resolve, reject) => {
  135. wx.getSetting({
  136. success(res) {
  137. if (!res.authSetting['scope.userInfo']) {
  138. return reject({
  139. authSetting: false
  140. });
  141. } else {
  142. wx.getStorage({
  143. key: 'token',
  144. success(token) {
  145. util.check_login_new().then(isLogin=>{
  146. if (isLogin) {
  147. // 已登录未过期
  148. return resolve(false);
  149. console.log('已登录未过期')
  150. } else {
  151. console.log('过期')
  152. if (token) {
  153. return resolve(token);
  154. } else {
  155. return reject(res);
  156. }
  157. }
  158. })
  159. },
  160. fail(res) {
  161. return reject(res);
  162. }
  163. })
  164. }
  165. },
  166. fail(res) {
  167. return reject(res);
  168. }
  169. })
  170. })
  171. }
  172. }
  173. });