<?php
namespace Home\Controller;
use Think\Controller;
class AjaxController extends Controller
{
	public function addsave()
	{
		if (!$user = $this->get_user_info()) {
			exit("{\"saveOK\":-3,\"msg\":\"请先登录！\"}");
		}

		$uin = i("post.uin", "", "get_safe_str");
		$sid = i("post.sid", "", "get_safe_str");
		$skey = i("post.skey", "", "get_safe_str");
		$pwd = i("post.pwd", "", "get_safe_str");

		if (!$uin) {
			exit("{\"saveOK\":-1,\"msg\":\"uin不能为空\"}");
		}

		if (!$sid) {
			exit("{\"saveOK\":-1,\"msg\":\"sid不能为空\"}");
		}

		if (!$skey) {
			exit("{\"saveOK\":-1,\"msg\":\"skey不能为空\"}");
		}

		if ($row = m("qqs")->field("qid,iszan")->where("qq='$uin' and uid='{$user["uid"]}'")->find()) {
			$data["sid"] = $sid;
			$data["skey"] = $skey;
			$data["qq"] = $uin;
			$data["pwd"] = md5($pwd);
			$data["sidzt"] = 0;
			$data["skeyzt"] = 0;

			if ($row["iszan"]) {
				$data["iszan"] = 2;
			}

			m("qqs")->where("qid='{$row["qid"]}'")->save($data);
			exit("{\"saveOK\":0,\"msg\":\"更新成功\",\"url\":\"" . u("index/qqset") . "?qid=" . $row[qid] . "\"}");
		}
		else {
			$count = m("qqs")->where("uid='{$user["uid"]}'")->count("qid");

			if ($user["peie"] <= $count) {
				exit("{\"saveOK\":-2,\"msg\":\"对不起，你最大允许添加" . $user[peie] . "个QQ!\"}");
			}

			$data["uid"] = $user[uid];
			$data["qq"] = $uin;
			$data["sid"] = $sid;
			$data["skey"] = $skey;
			$data["pwd"] = md5($pwd);
			$data["sidzt"] = 0;
			$data["skeyzt"] = 0;
			$data["addtime"] = date("Y-m-d H:i:s");

			if (m("qqs")->data($data)->add()) {
				$row = m("qqs")->field("qid")->where("qq='$uin' and uid='{$user["uid"]}'")->find();
				exit("{\"saveOK\":0,\"msg\":\"添加成功\",\"url\":\"" . u("index/qqset") . "?qid=" . $row[qid] . "\"}");
			}
			else {
				exit("{\"saveOK\":-1,\"msg\":\"保存数据库失败\"}");
			}
		}
	}
}


?>
