<script>
// import toolMixin from '@/mixins/tool';
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex';

export default {
  // mixins: [toolMixin],

  async onLaunch(options) {
    // TODO: this 先判断是不是通过扫二维码进来的
    console.log('options: ', options);
    this.setScene(options.scene);
    if (options.scene === 1047) {
      // 扫描小程序码
      if (options.query && options.query.scene) {
        const scene = options.query.scene;
        console.log('scene: ', scene);
        const toolCode = scene.substring(scene.length - 10);
        this.setEnterByCode(true);
        this.setToolCode(toolCode);
      }
    }

    this.getSystemInfo(); // 获取系统设备信息

    /* #ifdef MP-WEIXIN */
    await this.signin();
    /* #endif */

    /* #ifdef H5 */
    options.query.url && (this.$t.domain = options.query.url);
    if (!this.token) {
      // 不论有没有token都直接从userId获取token
      // token有过期时间 从本地获取可能是过期 干脆直接从userId获取
      if (!options.query || !options.query.u) {
        // 参数里没有u (userId)提示
        this.$t.ui.showToast('缺少用户信息参数');
      } else {
        const data = await this.getToken(options.query.u);
        this.noPhone(data);
      }
    }
    /* #endif */
    this.initSocket();
  },

  computed: {
    ...mapGetters(['useStorage']),
    ...mapState('user', ['token']),
  },

  methods: {
    ...mapActions('user', ['getToken']),
    ...mapActions('socket', ['initSocket']),
    ...mapMutations(['setSystemInfo']),
    ...mapMutations('user', ['setToken', 'setUser']),
    ...mapMutations('yanyuan', ['setToolCode', 'setEnterByCode', 'setScene']),

    // 登录
    async signin() {
      try {
        const data = await this.$u.api.signin();
        console.log('登录data: ', data);
        if (data && data.token) {
          this.setUser(data);
          this.setToken(data.token);
          this.noPhone(data);
        } else {
          this.$t.ui.showToast('返回数据异常');
        }
      } catch (error) {
        console.error('error: ', error);
        this.$t.ui.showToast(error || '登录失败');
      }
    },

    /**
     * 没有手机号 跳转绑定手机号的界面
     * @param {string} phone
     */
    async noPhone(data) {
      if (!data.wxInfo || !data.wxInfo.nickname || !data.phone) {
        this.$u.route('/pagesUser/git-phone-power/git-phone-power');
      }
    },

    // 获取系统设备信息
    getSystemInfo() {
      uni.getSystemInfo({
        success: result => {
          this.setSystemInfo(result);
        },
        fail: error => {
          console.error('getSystemInfo fail:', error);
        },
      });
    },
  },
};
</script>

<style lang="scss">
@import 'uview-ui/index.scss';

.min-0 {
  min-width: 0;
}

.flex-shrink-0 {
  flex-shrink: 0;
}
</style>

<style>
@import '@/common/styles/tailwind.scss';

.collapse-box >>> .u-collapse-body {
  height: auto !important;
}
</style>
