where("user_id", $user['user_id'])->find();
session('user',$user); //覆盖session 中的 user
$this->user = $user;
$this->user_id = $user['user_id'];
$this->assign('user',$user); //存储用户信息
$this->assign('user_id',$this->user_id);
}else{
$nologin = array(
'login','pop_login','do_login','logout','verify','set_pwd','finished',
'verifyHandle','reg','send_sms_reg_code','identity','check_validate_code',
'forget_pwd','check_captcha','check_username','send_validate_code',
);
if(!in_array(ACTION_NAME,$nologin)){
$this->redirect('Home/User/login');
exit;
}
}
//用户中心面包屑导航
$navigate_user = navigate_user();
$this->assign('navigate_user',$navigate_user);
}
/*
* 用户中心首页
*/
public function index(){
$logic = new UsersLogic();
$user = $logic->get_info($this->user_id);
$user = $user['result'];
$level = M('user_level')->select();
$level = convert_arr_key($level,'level_id');
$this->assign('level',$level);
$this->assign('user',$user);
return $this->fetch();
}
public function logout(){
setcookie('uname','',time()-3600,'/');
setcookie('cn','',time()-3600,'/');
setcookie('user_id','',time()-3600,'/');
session_unset();
session_destroy();
//$this->success("退出成功",U('Home/Index/index'));
$this->redirect('Home/Index/index');
exit;
}
/*
* 账户资金
*/
public function account(){
$user = session('user');
//获取账户资金记录
$logic = new UsersLogic();
$data = $logic->get_account_log($this->user_id,I('get.type'));
$account_log = $data['result'];
$this->assign('user',$user);
$this->assign('account_log',$account_log);
$this->assign('page',$data['show']);
$this->assign('active','account');
return $this->fetch();
}
/*
* 优惠券列表
*/
public function coupon(){
$logic = new UsersLogic();
$data = $logic->get_coupon($this->user_id,I('type'));
$coupon_list = $data['result'];
$this->assign('coupon_list',$coupon_list);
$this->assign('page',$data['show']);
$this->assign('active','coupon');
return $this->fetch();
}
/**
* 登录
*/
public function login(){
if($this->user_id > 0){
$this->redirect('Home/User/index');
}
$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U("Home/User/index");
$this->assign('referurl',$referurl);
return $this->fetch();
}
public function pop_login(){
if($this->user_id > 0){
$this->redirect('Home/User/index');
}
$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U("Home/User/index");
$this->assign('referurl',$referurl);
return $this->fetch();
}
public function do_login(){
$username = I('post.username');
$password = I('post.password');
$username = trim($username);
$password = trim($password);
$verify_code = I('post.verify_code');
$verify = new Verify();
if (!$verify->check($verify_code,'user_login'))
{
$res = array('status'=>0,'msg'=>'验证码错误');
exit(json_encode($res));
}
$logic = new UsersLogic();
$res = $logic->login($username,$password);
if($res['status'] == 1){
$res['url'] = urldecode(I('post.referurl'));
session('user',$res['result']);
setcookie('user_id',$res['result']['user_id'],null,'/');
setcookie('is_distribut',$res['result']['is_distribut'],null,'/');
$nickname = empty($res['result']['nickname']) ? $username : $res['result']['nickname'];
setcookie('uname',urlencode($nickname),null,'/');
setcookie('cn',0,time()-3600,'/');
$cartLogic = new CartLogic();
$cartLogic->login_cart_handle($this->session_id,$res['result']['user_id']); //用户登录后 需要对购物车 一些操作
}
exit(json_encode($res));
}
/**
* 注册
*/
public function reg(){
if($this->user_id > 0){
$this->redirect('Home/User/index');
}
if(IS_POST){
$logic = new UsersLogic();
//验证码检验
// $this->verifyHandle('user_reg');
$username = I('post.username','');
$password = I('post.password','');
$password2 = I('post.password2','');
$code = I('post.code','');
$session_id = session_id();
//是否开启注册验证码机制
if(check_mobile($username)){
$reg_sms_enable = tpCache('sms.regis_sms_enable');
if(!$reg_sms_enable){
//邮件功能关闭
$this->verifyHandle('user_reg');
}
$check_code = $logic->check_validate_code($code, $username, $session_id);
if($check_code['status'] != 1){
$this->error($check_code['msg']);
}
}
//是否开启注册邮箱验证码机制
if(check_email($username)){
$reg_smtp_enable = tpCache('smtp.regis_smtp_enable');
if(!$reg_smtp_enable){
//邮件功能关闭
$this->verifyHandle('user_reg');
}
$check_code = $logic->check_validate_code($code, $username, $session_id, 'email');
if($check_code['status'] != 1){
$this->error($check_code['msg']);
}
}
$data = $logic->reg($username,$password,$password2);
if($data['status'] != 1){
$this->error($data['msg']);
}
session('user',$data['result']);
setcookie('user_id',$data['result']['user_id'],null,'/');
setcookie('is_distribut',$data['result']['is_distribut'],null,'/');
$nickname = empty($data['result']['nickname']) ? $username : $data['result']['nickname'];
setcookie('uname',$nickname,null,'/');
$cartLogic = new CartLogic();
$cartLogic->login_cart_handle($this->session_id,$data['result']['user_id']); //用户登录后 需要对购物车 一些操作
$this->success($data['msg'],U('Home/User/index'));
exit;
}
$this->assign('regis_sms_enable',tpCache('sms.regis_sms_enable')); // 注册启用短信:
$this->assign('regis_smtp_enable',tpCache('smtp.regis_smtp_enable')); // 注册启用邮箱:
$sms_time_out = tpCache('sms.sms_time_out')>0 ? tpCache('sms.sms_time_out') : 120;
$this->assign('sms_time_out', $sms_time_out); // 手机短信超时时间
return $this->fetch();
}
/*
* 订单列表
*/
public function order_list(){
$where = ' user_id=:user_id';
$bind['user_id'] = $this->user_id;
//条件搜索
if(I('get.type')){
$where .= C(strtoupper(I('get.type')));
}
// 搜索订单 根据商品名称 或者 订单编号
$search_key = trim(I('search_key'));
if($search_key)
{
$where .= " and (order_sn like :search_key1 or order_id in (select order_id from `".C('database.prefix')."order_goods` where goods_name like :search_key2) ) ";
$bind['search_key1'] = "%$search_key%";
$bind['search_key2'] = "%$search_key%";
}
$count = M('order')->where($where)->bind($bind)->count();
$Page = new Page($count,10);
$show = $Page->show();
$order_str = "order_id DESC";
$order_list = M('order')->order($order_str)->where($where)->bind($bind)->limit($Page->firstRow.','.$Page->listRows)->select();
//获取订单商品
$model = new UsersLogic();
foreach($order_list as $k=>$v)
{
$order_list[$k] = set_btn_order_status($v); // 添加属性 包括按钮显示属性 和 订单状态显示属性
//$order_list[$k]['total_fee'] = $v['goods_amount'] + $v['shipping_fee'] - $v['integral_money'] -$v['bonus'] - $v['discount']; //订单总额
$data = $model->get_order_goods($v['order_id']);
$order_list[$k]['goods_list'] = $data['result'];
}
$this->assign('order_status',C('ORDER_STATUS'));
$this->assign('shipping_status',C('SHIPPING_STATUS'));
$this->assign('pay_status',C('PAY_STATUS'));
$this->assign('page',$show);
$this->assign('lists',$order_list);
$this->assign('active','order_list');
$this->assign('active_status',I('get.type'));
return $this->fetch();
}
/*
* 订单详情
*/
public function order_detail(){
$id = I('get.id/d');
$map['order_id'] = $id;
$map['user_id'] = $this->user_id;
$order_info = M('order')->where($map)->find();
$order_info = set_btn_order_status($order_info); // 添加属性 包括按钮显示属性 和 订单状态显示属性
if(!$order_info){
$this->error('没有获取到订单信息');
exit;
}
//获取订单商品
$model = new UsersLogic();
$data = $model->get_order_goods($order_info['order_id']);
$order_info['goods_list'] = $data['result'];
//$order_info['total_fee'] = $order_info['goods_price'] + $order_info['shipping_price'] - $order_info['integral_money'] -$order_info['coupon_price'] - $order_info['discount'];
//获取订单进度条
$sql = "SELECT action_id,log_time,status_desc,order_status FROM ((SELECT * FROM __PREFIX__order_action WHERE order_id = :id AND status_desc <>'' ORDER BY action_id) AS a) GROUP BY status_desc ORDER BY action_id";
$bind['id'] = $id;
$items = DB::query($sql,$bind);
$items_count = count($items);
$region_list = get_region_list();
$invoice_no = M('DeliveryDoc')->where("order_id", $id)->getField('invoice_no',true);
$order_info[invoice_no] = implode(' , ', $invoice_no);
//获取订单操作记录
$order_action = M('order_action')->where(array('order_id'=>$id))->select();
$this->assign('order_status',C('ORDER_STATUS'));
$this->assign('shipping_status',C('SHIPPING_STATUS'));
$this->assign('pay_status',C('PAY_STATUS'));
$this->assign('region_list',$region_list);
$this->assign('order_info',$order_info);
$this->assign('order_action',$order_action);
$this->assign('active','order_list');
return $this->fetch();
}
/*
* 取消订单
*/
public function cancel_order(){
$id = I('get.id/d');
//检查是否有积分,余额支付
$logic = new UsersLogic();
$data = $logic->cancel_order($this->user_id,$id);
if($data['status'] < 0)
$this->error($data['msg']);
$this->success($data['msg']);
}
/*
* 用户地址列表
*/
public function address_list(){
$address_lists = get_user_address_list($this->user_id);
$region_list = get_region_list();
$this->assign('region_list',$region_list);
$this->assign('lists',$address_lists);
$this->assign('active','address_list');
return $this->fetch();
}
/*
* 添加地址
*/
public function add_address(){
header("Content-type:text/html;charset=utf-8");
if(IS_POST){
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id,0,I('post.'));
if($data['status'] != 1)
exit('');
$call_back = $_REQUEST['call_back'];
echo "";
exit(); // 成功 回调closeWindow方法 并返回新增的id
}
$p = M('region')->where(array('parent_id'=>0,'level'=> 1))->select();
$this->assign('province',$p);
return $this->fetch('edit_address');
}
/*
* 地址编辑
*/
public function edit_address(){
header("Content-type:text/html;charset=utf-8");
$id = I('get.id/d');
$address = M('user_address')->where(array('address_id'=>$id,'user_id'=> $this->user_id))->find();
if(IS_POST){
$logic = new UsersLogic();
$data = $logic->add_address($this->user_id,$id,I('post.'));
if($data['status'] != 1)
exit('');
$call_back = $_REQUEST['call_back'];
echo "";
exit(); // 成功 回调closeWindow方法 并返回新增的id
}
//获取省份
$p = M('region')->where(array('parent_id'=>0,'level'=> 1))->select();
$c = M('region')->where(array('parent_id'=>$address['province'],'level'=> 2))->select();
$d = M('region')->where(array('parent_id'=>$address['city'],'level'=> 3))->select();
if($address['twon']){
$e = M('region')->where(array('parent_id'=>$address['district'],'level'=>4))->select();
$this->assign('twon',$e);
}
$this->assign('province',$p);
$this->assign('city',$c);
$this->assign('district',$d);
$this->assign('address',$address);
return $this->fetch();
}
/*
* 设置默认收货地址
*/
public function set_default(){
$id = I('get.id/d');
M('user_address')->where(array('user_id'=>$this->user_id))->save(array('is_default'=>0));
$row = M('user_address')->where(array('user_id'=>$this->user_id,'address_id'=>$id))->save(array('is_default'=>1));
if(!$row)
$this->error('操作失败');
$this->success("操作成功");
}
/*
* 地址删除
*/
public function del_address(){
$id = I('get.id/d');
$address = M('user_address')->where("address_id", $id)->find();
$row = M('user_address')->where(array('user_id'=>$this->user_id,'address_id'=>$id))->delete();
// 如果删除的是默认收货地址 则要把第一个地址设置为默认收货地址
if($address['is_default'] == 1)
{
$address2 = M('user_address')->where("user_id", $this->user_id)->find();
$address2 && M('user_address')->where("address_id", $address2['address_id'])->save(array('is_default'=>1));
}
if(!$row)
$this->error('操作失败',U('User/address_list'));
else
$this->success("操作成功",U('User/address_list'));
}
public function save_pickup()
{
$post = I('post.');
if (empty($post['consignee'])) {
return array('status' => -1, 'msg' => '收货人不能为空', 'result' => '');
}
if (!$post['province'] || !$post['city'] || !$post['district']) {
return array('status' => -1, 'msg' => '所在地区不能为空', 'result' => '');
}
if(!check_mobile($post['mobile'])){
return array('status'=>-1,'msg'=>'手机号码格式有误','result'=>'');
}
if(!$post['pickup_id']){
return array('status'=>-1,'msg'=>'请选择自提点','result'=>'');
}
$user_logic = new UsersLogic();
$res = $user_logic->add_pick_up($this->user_id, $post);
if($res['status'] != 1){
exit('');
}
$call_back = $_REQUEST['call_back'];
echo "";
exit(); // 成功 回调closeWindow方法 并返回新增的id
}
/*
* 评论晒单
*/
public function comment(){
$user_id = $this->user_id;
$status = I('get.status',-1);
$logic = new UsersLogic();
$data = $logic->get_comment($user_id,$status); //获取评论列表
$this->assign('page',$data['show']);// 赋值分页输出
$this->assign('comment_list',$data['result']);
$this->assign('active','comment');
return $this->fetch();
}
/**
* @time 2017/2/9
* @author lxl
* 订单商品评价列表
*/
public function comment_list()
{
$order_id = I('get.order_id');
$good_id = I('get.good_id');
if (empty($order_id) || empty($good_id)) {
$this->error("参数错误");
} else {
//查找订单
$order_comment_where['order_id'] = $order_id;
$order_info = M('order')->field('order_sn,order_id,add_time') ->where($order_comment_where)->find();
//查找评价商品
$order_comment_where['goods_id'] = $good_id;
$order_goods = M('order_goods')
->field('goods_id,is_comment,goods_name,goods_num,goods_price,spec_key_name')
->where($order_comment_where)
->find();
$order_info = array_merge($order_info,$order_goods);
$this->assign('order_info', $order_info);
return $this->fetch();
}
}
/*
*添加评论
*/
public function add_comment()
{
$user_info = session('user');
$comment_img = serialize(I('comment_img/a')); // 上传的图片文件
$add['goods_id'] = I('goods_id/d');
$add['email'] = $user_info['email'];
//$add['nick'] = $user_info['nickname'];
$add['username'] = $user_info['nickname'];
$add['order_id'] = I('order_id/d');
$add['service_rank'] = I('service_rank');
$add['deliver_rank'] = I('deliver_rank');
$add['goods_rank'] = I('goods_rank');
//$add['content'] = htmlspecialchars(I('post.content'));
$add['content'] = I('content');
$add['img'] = $comment_img;
$add['add_time'] = time();
$add['ip_address'] = $_SERVER['REMOTE_ADDR'];
$add['user_id'] = $this->user_id;
$logic = new UsersLogic();
//添加评论
$row = $logic->add_comment($add);
exit(json_encode($row));
}
/*
* 个人信息
*/
public function info(){
$userLogic = new UsersLogic();
$user_info = $userLogic->get_info($this->user_id); // 获取用户信息
$user_info = $user_info['result'];
if(IS_POST){
I('post.nickname') ? $post['nickname'] = I('post.nickname') : false; //昵称
I('post.qq') ? $post['qq'] = I('post.qq') : false; //QQ号码
I('post.head_pic') ? $post['head_pic'] = I('post.head_pic') : false; //头像地址
I('post.sex') ? $post['sex'] = I('post.sex') : $post['sex'] = 0; // 性别
I('post.birthday') ? $post['birthday'] = strtotime(I('post.birthday')) : false; // 生日
I('post.province') ? $post['province'] = I('post.province') : false; //省份
I('post.city') ? $post['city'] = I('post.city') : false; // 城市
I('post.district') ? $post['district'] = I('post.district') : false; //地区
if(!$userLogic->update_info($this->user_id,$post))
$this->error("保存失败");
$this->success("操作成功");
exit;
}
// 获取省份
$province = M('region')->where(array('parent_id'=>0,'level'=>1))->select();
// 获取订单城市
$city = M('region')->where(array('parent_id'=>$user_info['province'],'level'=>2))->select();
//获取订单地区
$area = M('region')->where(array('parent_id'=>$user_info['city'],'level'=>3))->select();
$this->assign('province',$province);
$this->assign('city',$city);
$this->assign('area',$area);
$this->assign('user',$user_info);
$this->assign('sex',C('SEX'));
$this->assign('active','info');
return $this->fetch();
}
/*
* 邮箱验证
*/
public function email_validate(){
$userLogic = new UsersLogic();
$user_info = $userLogic->get_info($this->user_id); // 获取用户信息
$user_info = $user_info['result'];
$step = I('get.step',1);
if(IS_POST){
$email = I('post.email');
$old_email = I('post.old_email',''); //旧邮箱
$code = I('post.code');
$info = session('validate_code');
if(!$info)
$this->error('非法操作');
if($info['time']