import request from './request.js'
import md5 from 'js-md5'
const version = '1.0'
const secret = '017c0743819088468e590246464cc75f';

export function httpServer(method, data) {
  let uid = localStorage.uid||0;
  let token = localStorage.token||''
  let role = 'user'
  // 未登入
  if (method.split(".")[0] == 'Auth') {
    uid = '0'
    token = ''
    role = ''
  }
  let body = JSON.stringify(data);
  let mtime = parseInt(Date.now() / 1000);
  let sigureStr = "hall_" + version + token + body + method + mtime + secret
  
  let sign = md5( sigureStr )
  let url = `/rental/${method}?t=${mtime}&u=${uid}&v=${version}&s=${sign}`
  return request({
    url: url,
    method: 'post',
    data: body
  })
}

export function uploadImg( rand, formData ) {
  let uid = localStorage.uid
  let token = localStorage.token
  let mtime = parseInt(Date.now() / 1000);
  // 未登入
  if( !token || !uid ) return;
  let sign = "hall_" + version + token + rand  + mtime + secret;
  return request({
    url:`/upload?t=${mtime}&u=${uid}&v=${version}&s=${sign}&r=${rand}`,
    method: 'post',
    data:formData,
    dataType:"multipart/form-data"
  })
 }
