/**
  * 用户登录
  * type = 0 标识 用户
  * type = 2 标识 商户
  */
 public function login()
 {
     $password = isset($_POST['password']) ? htmlspecialchars($_POST['password']) : '';
     $mobile = isset($_POST['mobile']) ? htmlspecialchars($_POST['mobile']) : '';
     if (empty($mobile) || empty($password)) {
         $this->jsonUtils->echo_json_msg(4, '用户名或者密码为空...');
         exit;
     }
     //	$map ['nick_name'] = $mobile;
     $map['mobile'] = $mobile;
     //	$map ['_logic'] = 'or';
     $is_exits = $this->dao->where($map)->select();
     if (!$is_exits) {
         $this->jsonUtils->echo_json_msg(1, '用户名或者密码错误');
         exit;
     }
     $password = md5($password);
     $arr = $this->dao->query("select id,nick_name,header from " . C('DB_PREFIX') . "member where  mobile='{$mobile}' and pwd='{$password}'");
     if ($arr) {
         $member_id = $arr[0]['id'];
         $jid = CommonController::getJid($member_id, 0);
         if (empty($jid)) {
             $this->jsonUtils->echo_json_msg(4, 'jid出错');
         }
         $s_arr = $this->session_dao->where("type=0 and userid={$member_id}")->find();
         if ($s_arr) {
             // 是否登录过
             $data['member_session_id'] = $s_arr['sessionid'];
             $data['jid'] = $jid;
             $data['header'] = imgUrl($arr[0]['header']);
             $data['name'] = $arr[0]['nick_name'];
             $this->session_handle->save($s_arr['sessionid']);
             $this->jsonUtils->echo_json_data(0, 'ok', $data);
             exit;
         } else {
             session_start();
             $session_id = session_id() . time();
             session_destroy();
             $this->session_handle->write($member_id, $session_id, '', 0);
             // 记录
             // 用户登录id
             // $data['login_times']=array('exp','login_times+1');
             // $data['point']=array('exp','point+1');
             // $this->dao->where("id=$member_id")->save($data);//增加积分和记录登录次数
             $json_data['header'] = imgUrl($arr[0]['header']);
             $json_data['name'] = $arr[0]['nick_name'];
             $json_data['member_session_id'] = $session_id;
             $json_data['jid'] = $jid;
         }
         $this->jsonUtils->echo_json_data(0, 'ok', $json_data);
         exit;
     } else {
         $this->jsonUtils->echo_json_msg(1, '用户名或者密码错误...');
         exit;
     }
 }
 /**
  * 商家登录
  * 
  * @return [type] [description]
  */
 public function login()
 {
     $username = isset($_POST['username']) ? htmlspecialchars(trim($_POST['username'])) : '';
     $password = isset($_POST['password']) ? htmlspecialchars(trim($_POST['password'])) : '';
     if (empty($username)) {
         $this->jsonUtils->echo_json_msg(1, '用户名为空');
         exit;
     }
     if (empty($password)) {
         $this->jsonUtils->echo_json_msg(4, '密码为空');
         exit;
     }
     // 		if (! preg_match ( '|^\d{11}$|', $username )) {
     // 			$this->jsonUtils->echo_json_msg ( 2, '手机号码不符合格式!' );
     // 			exit ();
     // 		}
     if (!preg_match('|^[0-9a-zA-z]{6,16}$|', $password)) {
         $this->jsonUtils->echo_json_msg(5, '请输入6-16位数字和字母密码!');
         exit;
     }
     $password = md5($password);
     //$condition ['merchant_name'] = $username;
     $condition['mobile'] = $username;
     //	$condition ['_logic'] = 'OR';
     $arr = $this->dao->where($condition)->getField('id');
     if (!$arr) {
         $this->jsonUtils->echo_json_msg(3, '输入的手机号未注册!');
         exit;
     }
     $result = $this->dao->query("select id as merchant_id,mobile,header,merchant_name,is_salesman,is_check,status from " . C('DB_PREFIX') . "merchant where   (mobile='{$username}' and pwd='{$password}' ) ");
     if ($result) {
         $result[0]['header'] = imgUrl($result[0]['header']);
         if (!$result[0]['is_check']) {
             $this->jsonUtils->echo_json_msg(7, '账号未激活,请联系我们');
             die;
         }
         if ($result[0]['status'] == -1) {
             $this->jsonUtils->echo_json_msg(4, '账号已被封停,请联系我们');
             die;
         }
         if ($result[0]['is_salesman']) {
             $loginType = 1;
             // 业务端
         } else {
             $loginType = 2;
             // 商务端
         }
         $id = $result[0]['merchant_id'];
         // 商家ID
         $s_arr = $this->session_dao->where("userid={$id} and type = {$loginType}")->find();
         if ($loginType == 2 || $loginType == 1) {
             $jid = CommonController::getJid($id, $loginType);
             if ($loginType == 1 && empty($jid)) {
                 $jid = '';
             } else {
                 if (empty($jid)) {
                     $this->jsonUtils->echo_json_msg(4, 'jid出错');
                     exit;
                 }
             }
         }
         if ($s_arr) {
             $result[0]['mer_session_id'] = $s_arr['sessionid'];
             $result[0]['jid'] = $jid;
             $this->session_handle->save($s_arr['sessionid']);
             // $_SESSION['merchant']=$result;
             $this->jsonUtils->echo_json_data(0, '已经登录!', $result[0]);
             exit;
         } else {
             //session_start ();
             $session_id = session_id() . time();
             //session_destroy ();
             $this->session_handle->write($id, $session_id, '', $loginType);
             // session保存数据库
             $result[0]['mer_session_id'] = $session_id;
             $result[0]['jid'] = $jid;
             // $_SESSION['merchant']=$result;
             $this->jsonUtils->echo_json_data(0, '登录成功!', $result[0]);
             exit;
         }
     } else {
         $this->jsonUtils->echo_json_msg(6, '用户名或者密码错误!');
         exit;
     }
 }
 /**
  * 用户确定需求
  */
 public function confirm_demand()
 {
     $id = (int) $_POST['id'];
     $merchant_id = isset($_POST['merchant_id']) ? htmlspecialchars($_POST['merchant_id']) : '';
     $member_session_id = $_POST['member_session_id'];
     $member_id = $this->session_handle->getsession_userid($member_session_id);
     if (empty($id)) {
         $this->jsonUtils->echo_json_msg(4, '需求ID为空...');
         exit;
     }
     if (empty($merchant_id)) {
         $this->jsonUtils->echo_json_msg(4, '商家ID为空...');
         exit;
     }
     $order = M('order');
     // 需求是否属于这个用户
     $demand = $this->dao->where(array('id' => $id, 'member_id' => $member_id))->find();
     if (!$demand) {
         $this->jsonUtils->echo_json_msg(4, '数据异常1');
         exit;
     }
     if (time() > $demand['expire_time']) {
         $this->jsonUtils->echo_json_msg(4, '已过期');
         exit;
     }
     if (time() > $demand['reach_time']) {
         $this->jsonUtils->echo_json_msg(4, '到店时间已过期');
         exit;
     }
     if ($demand['merchant_id']) {
         $this->jsonUtils->echo_json_msg(4, '已选择商户');
         exit;
     }
     // 检测商家是否有报价
     $bidding = M('MerchantBidding')->where(array('demand_id' => $id, 'merchant_id' => $merchant_id))->find();
     if (!$bidding) {
         $this->jsonUtils->echo_json_msg(4, '数据异常2');
         exit;
     }
     $o_arr = $order->where("sub_id={$id} and member_id={$member_id}")->select();
     if ($o_arr) {
         $this->jsonUtils->echo_json_msg(1, '此需求你已经提交过订单');
         exit;
     }
     // 查询需求
     $arr = $this->dao->query("select id,reach_time  as reach_time ,cart_data,description,pics,publish,member_id,longitude,latitude,cart_id ,merchant_id as demand_status from " . C('DB_PREFIX') . "member_demand  where id={$id}");
     if ($arr) {
         $arr[0]['demand_status'] = $arr[0]['demand_status'] > 0 ? 1 : 0;
         $model = new Model();
         $merchant = M('merchant');
         $mer_arr = $merchant->field("longitude,latitude")->where("id={$merchant_id}")->select();
         $longitude = $arr[0]['longitude'];
         // 用户发布需求的经纬度
         $latitude = $arr[0]['latitude'];
         $demand_id = $arr[0]['id'];
         // 计算商家店铺和用户需求距离
         $arr[0]['distance'] = getDistance($latitude, $longitude, $mer_arr[0]['latitude'], $mer_arr[0]['longitude']);
         // 需求详情项目
         if ($arr[0]['publish'] == 0) {
             $s_arr = $model->query("select b.name,b.id from " . C('DB_PREFIX') . "member_demand_subitems as a left join " . C('DB_PREFIX') . "category as b on a.category_id=b.id  where a.demand_id={$demand_id} ");
         } else {
             $s_arr = $model->query("select b.name,b.id from " . C('DB_PREFIX') . "member_demand_subitems as a left join " . C('DB_PREFIX') . "car_maintain_category as b on a.category_id=b.id  where a.demand_id={$demand_id} ");
         }
         $perlist = array();
         if ($s_arr) {
             // 查询 商家已有的服务 ,商家未有的服务 不允许报价
             // $server_ids = CommonController::getServerListByMer (
             // $merchant_id );
             // $string = implode ( ',', $server_ids );
             // 允许报价 所提示的价格
             // 已报价 bidding
             $arr[0]['merchant_remark'] = M('MerchantBiddingRemark')->where(array('demand_id' => $id))->getField('remark');
             $merchant_remark = empty($arr[0]['merchant_remark']) ? '' : $arr[0]['merchant_remark'];
             $map['demand_id'] = $id;
             // $map ['sub_id'] = array (
             // 'in',
             // $string
             // );
             $map['merchant_id'] = $merchant_id;
             $alert_price = M('MerchantBidding')->where($map)->field('id as bidding_id,price,sub_id as cat_id,out_time as time')->select();
             foreach ($alert_price as $tem) {
                 $price[$tem['cat_id']] = $tem;
             }
             // 区分是报价为0 还是未报价,-1标识未报价
             // foreach ( $server_ids as $tem => $row ) {
             // if (! isset ( $price [$row] )) {
             // $price [$row] = array (
             // 'cat_id' => $row,
             // 'price' => - 1,
             // 'bidding_id' => '-1'
             // );
             // }
             // }
             $total_price = 0;
             $total_time = 0;
             $service_name = array();
             // dump($price);
             foreach ($s_arr as $key => $row) {
                 $perlist[$key]['id'] = $row['id'];
                 $perlist[$key]['name'] = $row['name'];
                 $service_name[] = $row['name'];
                 // if (in_array ( $row ['id'], $server_ids )) {
                 $perlist[$key]['is_server'] = 1;
                 $perlist[$key]['time'] = !isset($price[$row['id']]['time']) ? '-1' : $price[$row['id']]['time'];
                 $perlist[$key]['price'] = !isset($price[$row['id']]['price']) ? '-1' : $price[$row['id']]['price'];
                 $perlist[$key]['bidding_id'] = !isset($price[$row['id']]['bidding_id']) ? '-1' : $price[$row['id']]['bidding_id'];
                 // } else {
                 // $perlist [$key] ['is_server'] = 0;
                 // $perlist [$key] ['price'] = 0;
                 // }
                 if ($perlist[$key]['price'] > 0) {
                     $total_price += $perlist[$key]['price'];
                 }
                 if ($perlist[$key]['price'] > 0) {
                     $total_time += $perlist[$key]['time'];
                 }
             }
         }
         // 拼接过程
         $arr[0]['list'] = $perlist;
         if ($arr[0]['pics']) {
             $json_obj = json_decode($arr[0]['pics'], true);
         } else {
             $json_obj = array();
         }
     } else {
         $this->jsonUtils->echo_json_msg(1, '没有该用户需求...');
         exit;
     }
     $param = json_decode($demand['param'], true);
     $rel['param'] = count($param) > 0 ? $param : array();
     $rel['distance'] = $arr[0]['distance'];
     $rel['pics'] = $json_obj;
     $rel['list'] = $perlist;
     $order_no = time() . rand(1000, 9999);
     $data['order_no'] = $order_no;
     $data['service_name'] = !empty($service_name) ? implode('、', $service_name) : '';
     $data['status'] = 0;
     $data['merchant_id'] = $merchant_id;
     $data['member_id'] = $member_id;
     if ($arr[0]['publish'] == 0) {
         $data['type'] = 1;
         // 项目需求订单
     } else {
         $data['type'] = 3;
         // 保养需求订单
     }
     $data['goods_count'] = 1;
     $data['total_price'] = $total_price;
     $data['unit_price'] = $total_price;
     $data['total_time'] = $total_time;
     $data['sub_id'] = $id;
     $data['sub_data'] = json_encode($rel);
     $data['reach_time'] = $arr[0]['reach_time'];
     $data['cart_id'] = $arr[0]['cart_id'];
     $data['cart_data'] = $arr[0]['cart_data'];
     // dump($arr[0]['cart_data']);
     $data['merchant_remark'] = $merchant_remark;
     $data['member_remark'] = $arr[0]['description'];
     $data['addtime'] = time();
     $result = $order->add($data);
     // echo $order->getLastSql();
     if ($result) {
         // 修改需求 确认订单已被商家完成
         CommonController::order_done($id, $merchant_id);
         $jid = CommonController::getJid($merchant_id, 2);
         //云推送
         $jpush = new \App\Model\JpushModel();
         $jpush->user = 2;
         $jpush->push(3, array($jid), array('order_no' => $order_no));
         $xmpp = new \App\Model\XmppApiModel();
         $xmpp->requestPush(3, array($jid), array('order_no' => $order_no));
         $this->jsonUtils->echo_json_msg(0, 'ok');
         exit;
     } else {
         $this->jsonUtils->echo_json_msg(1, '确认需求错误...');
         exit;
     }
 }