import http from '@/utils/http';
import { encodePhone } from '@/utils/transform';
import { AreaCode } from '@/types/common';

export async function changePhone(areacode: AreaCode, phone: string, code: string) {
  const { ok, msg } = await http.post('/user/chphone', {
    phone: encodePhone(areacode, phone),
    code,
  });
  if (ok) return;
  throw Error(msg);
}

export async function sendCode(areacode: AreaCode, phone: string) {
  const { ok, msg } = await http.post(
    '/sign/send',
    {
      phone: encodePhone(areacode, phone),
      type: 'changePhone',
    },
  );
  if (ok) {
    return true;
  }
  throw Error(msg);
}
