getPhoneButton.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // components/getPhoneButton/getPhoneButton.js
  2. import {
  3. wxSetSessionKey
  4. } from "../../utils/wxUtils"
  5. import {
  6. postApi
  7. } from "../../apis/api"
  8. import cloudApi from "../../apis/cloudApi"
  9. Component({
  10. /**
  11. * 组件的属性列表
  12. */
  13. properties: {
  14. style: String,
  15. type: String
  16. },
  17. data: {
  18. },
  19. methods: {
  20. async getPhoneNumber(e) {
  21. try {
  22. if (e.detail.errMsg == "getPhoneNumber:ok") {
  23. let session_key = wx.getStorageSync('session_key')
  24. let {
  25. result
  26. } = await cloudApi("getWxPhoneNumber", {
  27. ...e.detail,
  28. session_key
  29. })
  30. let {
  31. phone,
  32. } = result
  33. let res = await postApi("/user/wx/login", {
  34. openId: wx.getStorageSync('openId'),
  35. phone
  36. })
  37. if (res.data.status == 0) {
  38. let data = res.data.result
  39. Object.keys(data).forEach(function (key) {
  40. wx.setStorageSync(key, data[key])
  41. })
  42. wx.switchTab({
  43. url: '/pages/index/index',
  44. })
  45. } else {
  46. wx.showToast({
  47. icon: "none",
  48. title: res.data.msg
  49. })
  50. }
  51. } else {
  52. wx.showToast({
  53. title: '请授权以登录',
  54. icon: "error"
  55. })
  56. }
  57. } catch (error) {
  58. wxSetSessionKey()
  59. wx.showToast({
  60. icon: "none",
  61. title: '请重试或联系管理员',
  62. })
  63. }
  64. }
  65. }
  66. })