index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var app = getApp();
  2. Component({
  3. properties: {
  4. visible: {
  5. type: Boolean,
  6. value: false
  7. }
  8. },
  9. methods: {
  10. close: function() {
  11. this.triggerEvent("cancel");
  12. },
  13. getPhoneNumber: function(res) {
  14. var that = this;
  15. wx.checkSession({
  16. success: function() {
  17. console.log("session:", 1), that.getMobile(res);
  18. },
  19. fail: function() {
  20. console.log("session:", 2), wx.removeStorageSync("token");
  21. that.triggerEvent("cancel");
  22. that.triggerEvent("needAuth");
  23. }
  24. });
  25. },
  26. getMobile: function(e) {
  27. var that = this;
  28. if ("getPhoneNumber:ok" === e.detail.errMsg) {
  29. var token = wx.getStorageSync('token');
  30. this.setData({
  31. loading: true
  32. });
  33. wx.checkSession({
  34. success() {
  35. console.log("session_key有效");
  36. app.util.request({
  37. 'url': 'entry/wxapp/user',
  38. 'data': {
  39. controller: 'user.getPhoneNumber',
  40. token: token,
  41. encryptedData: e.detail.encryptedData,
  42. iv: e.detail.iv
  43. },
  44. method: 'post',
  45. dataType: 'json',
  46. success: function (res) {
  47. if (res.data.code == 0) {
  48. let phoneNumber = res.data.phoneNumber;
  49. wx.setStorage({
  50. key: "mobile",
  51. data: phoneNumber
  52. });
  53. that.triggerEvent("confirm", phoneNumber);
  54. } else {
  55. that.setData({
  56. visible: false
  57. });
  58. wx.showToast({
  59. title: "授权失败",
  60. icon: "none"
  61. });
  62. }
  63. }
  64. })
  65. },
  66. fail() {
  67. console.log("session_key 已经失效");
  68. wx.removeStorageSync("token");
  69. that.triggerEvent("needAuth");
  70. }
  71. })
  72. } else {
  73. that.setData({
  74. visible: false
  75. });
  76. wx.showToast({
  77. title: "授权失败",
  78. icon: "none"
  79. });
  80. }
  81. }
  82. }
  83. });