index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. wx.removeStorageSync("token");
  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. app.util.request({
  34. 'url': 'entry/wxapp/user',
  35. 'data': {
  36. controller: 'user.getPhoneNumber',
  37. token: token,
  38. encryptedData: e.detail.encryptedData,
  39. iv: e.detail.iv
  40. },
  41. method: 'post',
  42. dataType: 'json',
  43. success: function (res) {
  44. if (res.data.code == 0) {
  45. let phoneNumber = res.data.phoneNumber;
  46. wx.setStorage({
  47. key: "mobile",
  48. data: phoneNumber
  49. });
  50. that.triggerEvent("confirm", phoneNumber);
  51. } else {
  52. that.setData({
  53. visible: false
  54. });
  55. wx.showToast({
  56. title: "授权失败",
  57. icon: "none"
  58. });
  59. }
  60. }
  61. })
  62. } else {
  63. this.setData({
  64. visible: true
  65. })
  66. }
  67. }
  68. }
  69. });