login.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import HttpRequest from '@/common/httpRequest'
  2. import queue from '@/common/queue'
  3. /**
  4. * 处理微信登录
  5. */
  6. export function wxLogin(callback) {
  7. uni.showLoading({
  8. title: '登录中'
  9. })
  10. uni.login({
  11. provider: 'weixin',
  12. success: (wxRes) => {
  13. queue.setData('openId', wxRes.authResult.openid)
  14. queue.setData('unionId', wxRes.authResult.unionId)
  15. let data = {
  16. wxOpenId: loginRes.authResult.openid,
  17. token: loginRes.authResult.access_token
  18. }
  19. HttpRequest
  20. .postT('/app/Login/wxAppLogin', data)
  21. .then(res => {
  22. uni.hideLoading()
  23. if (typeof callback == 'function') {
  24. callback(res)
  25. }
  26. login(res)
  27. })
  28. .catch(() => {
  29. if (typeof callback == 'function') {
  30. callback(false)
  31. }
  32. uni.hideLoading()
  33. })
  34. },
  35. fail: () => {
  36. uni.hideLoading()
  37. }
  38. })
  39. }
  40. /**
  41. * 正常登录处理
  42. */
  43. export function phoneLogin(params) {
  44. uni.showLoading({
  45. title: '登录中'
  46. })
  47. HttpRequest
  48. .post('/app/Login/registerAndLogin', {
  49. phone: params.phone,
  50. msg: params.msg,
  51. platform: 'app'
  52. })
  53. .then(res => {
  54. login(res)
  55. })
  56. .finally(() => {
  57. uni.hideLoading()
  58. })
  59. }
  60. /**
  61. * 处理登录逻辑
  62. */
  63. export function login(res) {
  64. if (res.code == 0) {
  65. queue.setData("userId", res.user.userId)
  66. queue.setData("token", res.token)
  67. queue.setData("phone", res.user.phone)
  68. queue.setData("userName", res.user.userName)
  69. queue.setData("avatar", res.user.avatar)
  70. queue.setData("invitationCode", res.user.invitationCode)
  71. queue.setData("inviterCode", res.user.inviterCode)
  72. getIsVip()
  73. uni.hideLoading()
  74. if (res.user.userType) {
  75. queue.setData("userType", res.user.userType)
  76. uni.reLaunch({
  77. url: '/pages/my/index',
  78. })
  79. } else {
  80. uni.reLaunch({
  81. url: '/pages/my/jobApplicant/guidePage',
  82. })
  83. }
  84. } else {
  85. plus.nativeUI.toast('出错了:', res.msg)
  86. }
  87. }
  88. /**
  89. * 获取vip数据
  90. */
  91. function getIsVip() {
  92. HttpRequest.get("/app/UserVip/isUserVip").then((res) => {
  93. if (res.code == 0) {
  94. queue.setData("isVip", res.data);
  95. }
  96. });
  97. }
  98. /**
  99. * 会员登录到期提醒
  100. */
  101. export function membershipExpirationReminder(data) {
  102. if (data?.daysLeft) {
  103. setTimeout(() => {
  104. uni.showModal({
  105. title: '会员到期提醒',
  106. content: data.remind
  107. })
  108. }, 1000)
  109. }
  110. }