PlaceController.php 5.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/10/13
 * Time: 11:44
 */

namespace app\address\controller;
use cmf\controller\PayBaseController;
use think\Db;

class PlaceController extends PayBaseController
{

    function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
        // 响应类型
        header('Access-Control-Allow-Methods:POST');
        // 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');

    }

    public function add()
    {
        $request = request();
        if ($request->isPost()) {
            $post = $request->param();
            $code = $post['code'];
            $label = $post['label'];
            $arr1 = explode(',', $code);
            $post['province_id'] = $arr1[0];
            $post['city_id'] = $arr1[1];
            $post['county_id'] = $arr1[2];
            $arr2 = explode(' ', $label);
            $post['province'] = $arr2[0];
            $post['city'] = $arr2[1];
            $post['county'] = $arr2[2];
            unset($post['code']);
            unset($post['label']);
            if (Db::name('zj_user_place')->insert($post)) {
                echo json_encode(['msg' => 'insert ok', 'code' => 20000, 'id' => Db::name('zj_user_place')->getLastInsID()]);
                exit();
            } else {
                echo json_encode(['msg' => 'insert failed', 'code' => 40000]);
                exit();
            }
        } else {
            echo '
                <strong>接口说明:POST 添加地址接口传值方式(把省市区的id用逗号隔开参数名叫code,省市区用空格隔开参数名叫label,其他不变)</strong><br/>
                 name:收货人姓名<br/>
                 mobile:电话<br/>
                 code:省id,市id,区id<br/>
                 label:省 市 区<br/>
                 place:详细地址<br/>
            ';
        }
    }

    public function edit()
    {
        $data = Db::name('zj_user_place')
            ->field('id,name,mobile,place,province_id,city_id,county_id,province,city,county')
            ->where(['id' => request()->param('id')])
            ->find();
        if (request()->isPost()) {
            echo json_encode(['data' => $data]);
        } else {
            echo '
                <strong>接口说明:POST 编辑地址接口传值方式</strong><br/>
                 id:地址id<br/>
                <strong>返回结果:</strong>
                ';
            print_r($data);
        }
    }

    public function editDo()
    {
        if (request()->isPost()) {
            $request = request();
            $post = $request->param();
            $code = $post['code'];
            $label = $post['label'];
            $arr1 = explode(',', $code);
            $post['province_id'] = $arr1[0];
            $post['city_id'] = $arr1[1];
            $post['county_id'] = $arr1[2];
            $arr2 = explode(' ', $label);
            $post['province'] = $arr2[0];
            $post['city'] = $arr2[1];
            $post['county'] = $arr2[2];
            unset($post['code']);
            unset($post['label']);
            if (Db::name('zj_user_place')->update($post) >= 0) {
                echo json_encode(['msg' => 'edit ok', 'code' => 20000]);
                exit();
            } else {
                echo json_encode(['msg' => 'no changed', 'code' => 40000]);
                exit();
            }
        } else {
            echo '
                <strong>接口说明:POST 更新地址接口传值方式(把省市区的id用逗号隔开参数名叫code,省市区用空格隔开参数名叫label,其他不变)</strong><br/>
                 id:地址id<br/>
                 name:收货人姓名<br/>
                 mobile:电话<br/>
                 code:省id,市id,区id<br/>
                 label:省 市 区<br/>
                 place:详细地址<br/>
            ';
        }
    }

    public function del()
    {
        if (Db::name('zj_user_place')->delete(request()->param('id'))) {
            echo json_encode(['msg' => 'del ok', 'code' => 20000]);
            exit();
        } else {
            echo json_encode(['msg' => 'del failed', 'code' => 40000]);
            exit();
        }
    }

    public function setDefault()
    {
        if (request()->isPost()) {
            //默认is_sta=2;
            Db::name('zj_user_place')->where(['uid' => request()->param('uid')])->update(['is_sta' => 1]);
            if (Db::name('zj_user_place')->where(['id' => request()->param('id')])->update(['is_sta' => request()->param('is_sta')])) {
                echo json_encode(['msg' => 'set def ok', 'code' => 20000]);
                exit();
            } else {
                echo json_encode(['msg' => 'set def failed', 'code' => 40000]);
                exit();
            }
        } else {
            echo '
                <strong>接口说明:POST 设置默认地址接口传值方式</strong><br/>
                 id:地址id<br/>
                 is_sta:2
            ';
        }
    }

}