# -*- coding: utf-8 -*-
"""
    demeter web page
    name:user.py
    author:rabin
"""
from .__load__ import *

class login_path(Load):
	def get(self):
		#url = Demeter.config['web']['url']
		#self.redirect(url)
		self.data['url'] = self.input('url', '/device')
		self.view("login.html")
	def post(self):
		mobile = self.input('username')
		password = self.input('password')
		if mobile and password:
			user = self.service('common').one('farm_user', mobile=mobile)
			if user:
				temp = user['password'].split('_')
				if Demeter.md5(password, temp[1]) == user['password']:
					self.set_secure_cookie('user', str(user['id']))
					farm = self.service('common').one('farm', id=user['farm_id'])
					if 'eros_status' in farm and farm['eros_status'] == 1 and farm['eros_server']:
						url = 'http://' + farm['eros_server'] + ':8081/user/login?username='+mobile+'&password='+temp[0]
						#self.redirect(url)
						self.out('yes', {'id':user['id'],'url':url})
					else:
						self.out('yes', {'id':user['id']})
					return
		self.out('手机号或安全码错误，登录失败')

		


class loginout_path(Load):
	def get(self):
		self.set_secure_cookie('user', '')
		self.redirect('/user/login')