/**
 * Created by JianJia.Zhou<jianjia.zhou> on 2022/8/14.
 */
import { getToken } from '../../utils/auth'

const request = uni.$u.http

import episode from './episode'
import consume from './consume'
import recharge from './recharge'
import collect from './collect'
import favorite from './favorite'
import vip from './vip'

export async function login() {
  console.log('-->data', 1)
  return new Promise(resolve => {
    uni.showLoading({
      title: '数据加载中...',
      mask: true
    })
    uni.login({
      provider: uni.$u.platform,
      success: loginRes => {
        console.log('-->data', loginRes)
        uni.hideLoading()
        // #ifdef  MP-KUAISHOU
        const url = '/auth/kuaishou'
        // #endif
        // #ifdef  MP-TOUTIAO
        const url = '/auth/bytedance'
        // #endif
        // #ifdef  MP-WEIXIN
        const url = '/auth/wechat'
        // #endif

        return request.post(
          url,
          { code: loginRes.code, anonymousCode: loginRes.anonymousCode }
        ).then(res => {
          resolve(res)
        })
      }
    })
  })
}

export function update(data) {
  return request.post(
    'user/update',
    data
  )
}

export async function info() {
  return request.get(
    'user/info'
  )
}

export async function bind(id) {
  return request.post(
    `user/${id}/bind`
  )
}

export function isLogin() {
  return !!getToken()
}

export default {
  login,
  update,
  bind,
  info,
  isLogin,
  episode,
  consume,
  recharge,
  collect,
  favorite,
  vip
}
