login.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import HttpRequest from '@/common/httpRequest'
  2. /**
  3. * 处理微信登录
  4. */
  5. export function wxLogin(callback) {
  6. uni.showLoading({
  7. title: '登录中'
  8. })
  9. uni.login({
  10. provider: 'weixin',
  11. success: (wxRes) => {
  12. setData('openId', wxRes.authResult.openid)
  13. setData('unionId', wxRes.authResult.unionId)
  14. let data = {
  15. wxOpenId: loginRes.authResult.openid,
  16. token: loginRes.authResult.access_token
  17. }
  18. HttpRequest
  19. .postT('/app/Login/wxAppLogin', data)
  20. .then(res => {
  21. uni.hideLoading()
  22. if (typeof callback == 'function') {
  23. callback(res)
  24. }
  25. login(res)
  26. })
  27. .catch(() => {
  28. if (typeof callback == 'function') {
  29. callback(false)
  30. }
  31. uni.hideLoading()
  32. })
  33. },
  34. fail: () => {
  35. uni.hideLoading()
  36. }
  37. })
  38. }
  39. /**
  40. * 正常登录处理
  41. */
  42. export function phoneLogin(params) {
  43. uni.showLoading({
  44. title: '登录中'
  45. })
  46. HttpRequest
  47. .post('/app/Login/registerAndLogin', {
  48. phone: params.phone,
  49. msg: params.msg,
  50. platform: 'app'
  51. })
  52. .then(res => {
  53. login(res)
  54. })
  55. .finally(() => {
  56. uni.hideLoading()
  57. })
  58. }
  59. /**
  60. * 处理登录逻辑
  61. */
  62. export function login(res) {
  63. if (res.code == 0) {
  64. setData("userId", res.user.userId)
  65. setData("token", res.token)
  66. setData("phone", res.user.phone)
  67. setData("userName", res.user.userName)
  68. setData("avatar", res.user.avatar)
  69. setData("invitationCode", res.user.invitationCode)
  70. setData("inviterCode", res.user.inviterCode)
  71. getIsVip()
  72. uni.hideLoading()
  73. if (res.user.userType) {
  74. setData("userType", res.user.userType)
  75. uni.reLaunch({
  76. url: '/pages/my/index',
  77. })
  78. } else {
  79. uni.reLaunch({
  80. url: '/pages/my/jobApplicant/guidePage',
  81. })
  82. }
  83. } else {
  84. plus.nativeUI.toast('出错了:', res.msg)
  85. }
  86. }
  87. /**
  88. * 获取vip数据
  89. */
  90. function getIsVip() {
  91. HttpRequest.get("/app/UserVip/isUserVip").then((res) => {
  92. if (res.code == 0) {
  93. setData("isVip", res.data);
  94. }
  95. });
  96. }
  97. /**
  98. * 会员登录到期提醒
  99. */
  100. export function membershipExpirationReminder(data) {
  101. if (data?.daysLeft) {
  102. setTimeout(() => {
  103. uni.showModal({
  104. title: '会员到期提醒',
  105. content: data.remind
  106. })
  107. }, 1000)
  108. }
  109. }
  110. /**
  111. * 获取极光推送相关登录信息
  112. */
  113. export function getJPLoginParams() {
  114. // 极光推送SDK初始化注册ID
  115. let registrationId = getData('registerID') || ''
  116. // 设备类型:0 安卓、1 iOS、2 鸿蒙
  117. let deviceType = 0
  118. if (uni.getSystemInfoSync().platform == "ios") {
  119. deviceType = 1
  120. }
  121. return {
  122. registrationId,
  123. deviceType
  124. }
  125. }
  126. /**
  127. * 退出登录,断开极光推送连接
  128. */
  129. export function logoutJP() {
  130. // 退出登录,断开极光推送连接
  131. // #ifdef APP-PLUS
  132. HttpRequest
  133. .getT('/app/Login/out')
  134. .then(res => {
  135. console.log('退出登录成功', res)
  136. })
  137. // #endif
  138. }
  139. function setData(key, value) {
  140. try {
  141. uni.setStorageSync(key, value);
  142. } catch (e) {
  143. console.log(e)
  144. }
  145. }
  146. function getData(key) {
  147. try {
  148. const value = uni.getStorageSync(key);
  149. if (value) {
  150. return value;
  151. }
  152. } catch (e) {
  153. console.log(e)
  154. }
  155. }