wxLogin.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view class="container">
  3. </view>
  4. </template>
  5. <script>
  6. import { loginByMp } from '@/api/user'
  7. export default {
  8. data() {
  9. return {
  10. code:null,
  11. }
  12. },
  13. computed: {
  14. },
  15. onLoad(option)
  16. {
  17. // #ifdef H5
  18. if (this.checkWeixin()) {
  19. let code = this.getUrlCode('code')
  20. if (code==null||code==undefined) {
  21. uni.showToast({
  22. icon:'none',
  23. title: "code不存在",
  24. });
  25. } else {
  26. this.code = code
  27. this.loginByMp();
  28. }
  29. } else {
  30. uni.showToast({
  31. icon:'none',
  32. title: "请在微信中打开",
  33. });
  34. //跳转到手机号密码登录
  35. }
  36. // #endif
  37. },
  38. onUnload() {
  39. },
  40. mounted() {
  41. },
  42. methods: {
  43. loginByMp(){
  44. let code = this.code // 获取开发code
  45. var tuiUserId=uni.getStorageSync('tuiUserId');
  46. uni.showLoading({
  47. title:"处理中..."
  48. })
  49. loginByMp({
  50. code: code,
  51. tuiUserId:tuiUserId
  52. })
  53. .then( res => {
  54. if(res.code==200||res.code==40163){
  55. uni.hideLoading();
  56. uni.showToast({
  57. icon:'none',
  58. title: "登录成功",
  59. });
  60. uni.setStorageSync('AppToken',res.token);
  61. uni.setStorageSync('userInfo',JSON.stringify(res.user));
  62. //that.getUserInfo()
  63. uni.$emit('refreshLogin');
  64. uni.reLaunch({
  65. url:"/pages/home/index"
  66. })
  67. }
  68. else{
  69. uni.hideLoading();
  70. uni.showToast({
  71. icon:'none',
  72. title: res.msg,
  73. });
  74. }
  75. })
  76. .catch(error => {
  77. console.log(error)
  78. uni.hideLoading();
  79. uni.showToast({
  80. icon:'none',
  81. title: "登录接口调用失败",
  82. });
  83. })
  84. },
  85. checkWeixin(){
  86. var ua = window.navigator.userAgent.toLowerCase();
  87. if (ua.match(/micromessenger/i) == 'micromessenger') {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. },
  93. //URL地址是否存在CODE
  94. getUrlCode(name) {
  95. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1]
  96. .replace(/\+/g, '%20')) || null
  97. }
  98. }
  99. }
  100. </script>
  101. <style lang="scss">
  102. </style>