login.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='AI玩具' :showback="false"></cus-header>
  4. <div class="content">
  5. <button class="zt_btn" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">手机号一键登录</button>
  6. <div class="qx_btn" @click="cancelLogin">取消</div>
  7. </div>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data(){
  13. return {
  14. }
  15. },
  16. onShow() {
  17. if(uni.getStorageSync('userInfo')){
  18. uni.redirectTo({
  19. url:'/pages/home'
  20. })
  21. }
  22. },
  23. methods:{
  24. decryptPhoneNumber(e){
  25. if(e.detail.code) this.wxLogin(e.detail.code);
  26. },
  27. wxLogin(code){
  28. uni.showLoading({ title:'登录中' });
  29. let that = this;
  30. wx.login({
  31. success(res){
  32. that.$api.get('/sys/wx/login',{
  33. code:res.code,
  34. phoneCode:code
  35. },false).then(res=>{
  36. if(res.data.code===0){
  37. uni.setStorageSync('token',res.data.data.token);
  38. uni.setStorageSync('userInfo',JSON.stringify({
  39. id:res.data.data.id,
  40. mobile:res.data.data.mobile,
  41. username:res.data.data.username
  42. }));
  43. uni.hideLoading();
  44. that.$showToast('登录成功');
  45. setTimeout(()=>{
  46. uni.reLaunch({ url:'/pages/home' })
  47. },1500)
  48. }else that.$showToast(res.data.msg)
  49. })
  50. }
  51. })
  52. },
  53. cancelLogin(){
  54. uni.exitMiniProgram();
  55. }
  56. }
  57. }
  58. </script>
  59. <style scoped lang="scss">
  60. .page{
  61. background: #FFFFFF;
  62. display: flex;
  63. flex-direction: column;
  64. box-sizing: border-box;
  65. .content{
  66. padding: 0 45rpx;
  67. flex: 1;
  68. overflow-y: auto;
  69. .zt_btn{
  70. margin-top: 216rpx;
  71. }
  72. .qx_btn{
  73. margin-top: 36rpx;
  74. }
  75. }
  76. }
  77. </style>