import 'dart:convert';

import 'package:autosos_flutter/pages/home/home_page.dart';
import 'package:autosos_flutter/util/sp_util.dart';
import 'package:autosos_flutter/util/xhttp.dart';
import 'package:flutter/material.dart';

class LoginApi {
  static XHttp xHttp = XHttp.getInstance();
  static SPUtil spUtil = SPUtil();

  static void login(BuildContext context,int type, String username, String password, String code,
      String cid, String openid, String unionid) async {
    var data = {
      "type": type,
      "username": username,
      "password": password,
      "code": code,
      "getui_cid": cid,
      "wx_openid": openid,
      "wx_unionid": unionid
    };
    var headers = {'Content-Type': 'application/x-www-form-urlencoded'};
    Result response = await xHttp.post("/v2/auth/get-access-token", data, headers);
    var resData = response.data;
    var resCode = resData['code'];
    if(resCode==1){
      var accessToken = resData['data']['access_token'];
      spUtil.setString("accessToken", accessToken);
      if(!context.mounted) return;
      Navigator.of(context).push(MaterialPageRoute(builder: (context)=> const HomePage()));
    }else{
      var msg = resData['message'];
      Toast.show(msg);
    }
  }
}
