import Http from '../components/http';
import _ from '../utils/util';
const Promise = require('../components/promise').Promise;

const WX_URL = `https://api.damabtc.com`;

const API_METHODS = {
  'user': `${WX_URL}/wx/connect`,
  'userSetting': `${WX_URL}/wx/mode`,
  'index': `${WX_URL}/article/list`,
  'detail': `${WX_URL}/article/detail`,
  'mineGet': `${WX_URL}/mine/get`,
  'mineGo': `${WX_URL}/mine/go`,
  'mineStop': `${WX_URL}/mine/stop`
};

const authorize = (code, iv, info) => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['user'];
    Http.instance.post({
      url,
      data: {
        code: code,
        iv: iv,
        info: info
      }
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

const putUserInfo = (data) => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['userSetting'];

    const mode = parseInt(data.mode, 10);
    const circle = data.menstrual_cycle;
    const duration = data.duration_of_menstruation;
    const lastMenses = data.last_menses;
    const duedate = data.duedate;
    const babyBirthday = data.baby_birthday;
    const babySex = data.baby_sex;
    let dataObj = {};

    if (mode === 1) { // 怀孕
      dataObj = {
        mode: 1,
        duration_of_menstruation: parseInt(duration) || 5,
        menstrual_cycle: parseInt(circle) || 28,
        last_menses: lastMenses || '',
        duedate: duedate || ''
      };
    } else if (mode === 2) { // 备孕
      dataObj = {
        mode: 2,
        duration_of_menstruation: parseInt(duration) || 5,
        menstrual_cycle: parseInt(circle) || 28,
        last_menses: lastMenses || '',
        duedate: duedate || ''
      };
    } else if (mode === 3) { // 辣妈
      dataObj = {
        mode: 3,
        baby_birthday: babyBirthday || '',
        baby_sex: babySex + 1 || 0
      };
    }

    Http.instance.put({
      url,
      data: dataObj
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

const fetchIndex = (page = 1, cid) => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['index'];
    Http.instance.get({
      url: url,
      data: {
        page: page,
        cid: cid
      }
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

const fetchDetail = id => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['detail'];
    Http.instance.get({
      url: url,
      data: {
        id: id
      }
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

const handleMineGet = () => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['mineGet'];
    Http.instance.get({
      url: url,
      data: {}
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

const handleMineGo = () => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['mineGo'];
    Http.instance.post({
      url: url,
      data: {}
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

const handleMineStop = () => {
  return new Promise((resolve, reject) => {
    const url = API_METHODS['mineStop'];
    Http.instance.post({
      url: url,
      data: {}
    }).then(data => {
      resolve(data);
    }, err => {
      reject(err);
    });
  });
};

module.exports = {
  fetchIndex,
  fetchDetail,
  handleMineGet,
  handleMineGo,
  handleMineStop,
  authorize,
  putUserInfo
};

@@ -225,46 +225,8 @@
 t%60,%0A
-  'userSetting': %60$%7BWX_URL%7D/wx/mode%60,%0A
   'i
@@ -258,16 +258,16 @@
 /list%60,%0A
+
   'detai
@@ -740,1264 +740,8 @@
 %7D;%0A%0A
-const putUserInfo = (data) =%3E %7B%0A  return new Promise((resolve, reject) =%3E %7B%0A    const url = API_METHODS%5B'userSetting'%5D;%0A%0A    const mode = parseInt(data.mode, 10);%0A    const circle = data.menstrual_cycle;%0A    const duration = data.duration_of_menstruation;%0A    const lastMenses = data.last_menses;%0A    const duedate = data.duedate;%0A    const babyBirthday = data.baby_birthday;%0A    const babySex = data.baby_sex;%0A    let dataObj = %7B%7D;%0A%0A    if (mode === 1) %7B // %E6%80%80%E5%AD%95%0A      dataObj = %7B%0A        mode: 1,%0A        duration_of_menstruation: parseInt(duration) %7C%7C 5,%0A        menstrual_cycle: parseInt(circle) %7C%7C 28,%0A        last_menses: lastMenses %7C%7C '',%0A        duedate: duedate %7C%7C ''%0A      %7D;%0A    %7D else if (mode === 2) %7B // %E5%A4%87%E5%AD%95%0A      dataObj = %7B%0A        mode: 2,%0A        duration_of_menstruation: parseInt(duration) %7C%7C 5,%0A        menstrual_cycle: parseInt(circle) %7C%7C 28,%0A        last_menses: lastMenses %7C%7C '',%0A        duedate: duedate %7C%7C ''%0A      %7D;%0A    %7D else if (mode === 3) %7B // %E8%BE%A3%E5%A6%88%0A      dataObj = %7B%0A        mode: 3,%0A        baby_birthday: babyBirthday %7C%7C '',%0A        baby_sex: babySex + 1 %7C%7C 0%0A      %7D;%0A    %7D%0A%0A    Http.instance.put(%7B%0A      url,%0A      data: dataObj%0A    %7D).then(data =%3E %7B%0A      resolve(data);%0A    %7D, err =%3E %7B%0A      reject(err);%0A    %7D);%0A  %7D);%0A%7D;%0A%0A
 cons
@@ -2233,16 +2233,16 @@
 neStop,%0A
+
   author
@@ -2248,23 +2248,8 @@
 rize
-,%0A  putUserInfo
 %0A%7D;%0A

删除多余的接口信息。