login-mp-wx.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export default{
  2. // #ifdef MP-WEIXIN
  3. data(){
  4. return {
  5. mpCodeTimer: 0,
  6. mpWxCode: '',
  7. }
  8. },
  9. computed: {
  10. timerIdent(){
  11. return this.$store.state.timerIdent;
  12. }
  13. },
  14. watch: {
  15. timerIdent(){
  16. this.mpCodeTimer ++;
  17. if(this.mpCodeTimer % 30 === 0){
  18. this.getMpWxCode();
  19. }
  20. }
  21. },
  22. onShow(){
  23. this.getMpWxCode();
  24. },
  25. methods: {
  26. //微信小程序登录
  27. mpWxGetUserInfo(){
  28. if(!this.agreement){
  29. this.$util.msg('请阅读并同意用户服务及隐私协议');
  30. return;
  31. }
  32. this.$util.throttle(()=>{
  33. uni.getUserProfile({
  34. desc: '用于展示您的头像及昵称',
  35. success: async profileRes=> {
  36. const res = await this.$request('user', 'loginByWeixin', {
  37. code: this.mpWxCode,
  38. ...profileRes.userInfo
  39. }, {
  40. showLoading: true
  41. });
  42. if(res.status === 0){
  43. this.$util.msg(res.msg);
  44. return;
  45. }
  46. if(res.hasBindMobile && res.data.token){
  47. this.loginSuccessCallBack({
  48. token: res.data.token,
  49. tokenExpired: res.data.tokenExpired
  50. });
  51. }else{
  52. this.navTo('/pages/auth/bindMobile?data='+JSON.stringify(res.data))
  53. }
  54. console.log(res)
  55. }
  56. })
  57. })
  58. },
  59. //获取code
  60. getMpWxCode(){
  61. uni.login({
  62. provider: 'weixin',
  63. success: res=> {
  64. this.mpWxCode = res.code;
  65. }
  66. })
  67. },
  68. }
  69. // #endif
  70. }