AddressController.php 3.8 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\ShoppingModel;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
use think\Db;
use think\Session;

//地址
class AddressController extends CommentController
{
//    地址列表
    public function index(){
        $where_address['user_id'] = Session::get('uid');
        $where_address['status'] = array('neq',9);
        $address = Db::name('Address')->where($where_address)->order('is_check desc,create_time desc')->select()->toArray();
        $this->assign('list',$address);
        return $this->fetch();
    }
    //    添加地址
    public function add(){
        $data = $this->request->param();
        if($this->request->isPost()){
            $add = $data;
            $add['user_id'] = Session::get('uid');
            $add['create_time'] = time();
            $add['update_time'] = time();
            $res = Db::name('Address')->insertGetId($add);
            if($res){
                $this->apiResponse('1','添加成功');
            }else{
                $this->apiResponse('0','添加失败');
            }
        }else{
            return $this->fetch();
        }
    }

    //    修改地址
    public function edit(){
        $data = $this->request->param();
        $where_find['id'] = $data['id'];
        $where_find['status'] = array('neq',9);
        $address = Db::name('Address')->where($where_find)->find();
        $this->assign('list',$address);
        if($this->request->isPost()){
            $where_up['user_id'] = Session::get('uid');
            $where_up['id'] = $address['id'];
            $add = $data;
            $add['update_time'] = time();
            $res = Db::name('Address')->where($where_up)->update($add);
            if($res){
                $this->apiResponse('1','修改成功');
            }else{
                $this->apiResponse('0','添加失败');
            }
        }else{

            return $this->fetch();
        }
    }

//    默认地址
    public function isCheck(){
        $data = $this->request->param();
        $user_id = Session::get('uid');
        $where_find['user_id'] = $user_id;
        $where_find['status'] = array('neq',9);
        $up_find['is_check'] = 0;
        $up_find['update_time'] = time();
        $find = Db::name('Address')->where($where_find)->update($up_find);
        if($find){
            $where_check['id'] = $data['address_id'];
            $where_check['user_id'] = $user_id;
            $where_check['status'] = array('neq',9);
            $up_check['is_check'] = 1;
            $up_check['update_time'] = time();
            $res = Db::name('Address')->where($where_check)->update($up_check);
            if($res){
                $this->apiResponse('1','成功');
            }else{
                $this->apiResponse('0','失败');
            }
        }
    }
//    地址删除
    public function del(){
        $data = $this->request->param();
        $where_find['id'] = $data['id'];
        $where_find['user_id'] = Session::get('uid');
        $where_find['status'] = array('neq',9);
        $update['status'] = 9;
        $update['update_time'] = time();
        $address = Db::name('Address')->where($where_find)->update($update);
        if($address){
            $this->apiResponse('1','删除成功');
        }else{
            $this->apiResponse('0','删除失败');
        }
    }
}