CenterController.php 5.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: yhbr
 * Date: 2018/9/27
 * Time: 15:18
 */

namespace app\user\controller;
use cmf\controller\HomeBaseController;
use think\Db;

class CenterController extends HomeBaseController
{

    function _initialize()
    {
        if (empty(session('user.id'))) {
            $this->error('登录失败');
        }
    }

    /**
     * 个人中心首页
     */
    public function index()
    {
        $userInfo = Db::name('user')->field('user_nickname,avatar,balance')->where(['id' => session('user.id')])->find();
        return $this->fetch('index', [
            'data' => $userInfo
        ]);
    }

    /**
     * 个人信息
     */
    public function userInfo()
    {
        $userInfo = Db::name('user')->field('user_nickname,avatar,sex,mobile')->where(['id' => session('user.id')])->find();
        return $this->fetch('info', [
            'data' => $userInfo
        ]);
    }

    public function editName()
    {
        $request = request();
        if ($request->isAjax()) {
            if (Db::name('user')->update(['id' => session('user.id'), 'user_nickname' => $request->param('user_nickname')])) {
                $this->success('编辑成功', url('user/Center/userInfo'));
            }
        } else {
            return $this->fetch('edit_name', [
                'nickname' => Db::name('user')->where(['id' => session('user.id')])->value('user_nickname')
            ]);
        }
    }

    public function editPhone()
    {
        $request = request();
        if ($request->isAjax()) {
            if (Db::name('user')->update(['id' => session('user.id'), 'mobile' => $request->param('mobile')])) {
                $this->success('编辑成功', url('user/Center/userInfo'));
            }
        } else {
            return $this->fetch('edit_phone', [
                'mobile' => Db::name('user')->where(['id' => session('user.id')])->value('mobile')
            ]);
        }
    }

    public function aboutUs()
    {
        $request = request();
        if ($request->isAjax()) {
            $data = [
                'uid' => session('user.id'),
                'content' => $request->param('content'),
                'create_time' => time()
            ];
            if (Db::name('zj_msg')->insert($data)) {
                $this->success('感谢您的反馈', url('user/Center/userInfo'));
            }
        } else {
            return $this->fetch('about_us', [
                'data' => html_entity_decode(Db::name('zj_system')->value('about')),
                'mobile' => Db::name('zj_system')->value('mobile')
            ]);
        }
    }

    /**
     * 地址管理
     */
    public function addressManage()
    {
        $address = Db::name('zj_user_place')
            ->field('id,name,province,city,county,mobile,is_sta,place')
            ->where(['uid' => session('user.id')])
            ->select()
            ->toArray();
        return $this->fetch('address_manage', [
            'data' => $address
        ]);
    }

    public function addAddress()
    {
        $request = request();
        if ($request->isAjax()) {
            $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']);
            $post['uid'] = session('user.id');
            if (Db::name('zj_user_place')->insert($post)) {
                $this->success('', url('user/Center/addressManage'), true);
            }

        } else {
            return $this->fetch('add_address');
        }
    }

    public function delAddress()
    {
        if (Db::name('zj_user_place')->delete(request()->param('id'))) {
            $this->success('', '', true);
        }
    }

    public function editAddress()
    {
        $request = request();
        if ($request->isAjax()) {
            $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']);
            $post['uid'] = session('user.id');
            if (Db::name('zj_user_place')->update($post)) {
                $this->success('', url('user/Center/addressManage'), true);
            }
        } else {
            return $this->fetch('edit_address', [
                '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()
            ]);
        }
    }

    public function setDefault()
    {
        Db::name('zj_user_place')->where(['uid' => session('user.id')])->update(['is_sta' => 1]);
        Db::name('zj_user_place')->where(['id' => request()->param('id')])->update(['is_sta' => request()->param('is_sta')]);
    }

    public function myBalance()
    {
        return $this->fetch('my_balance', [
            'balance' => Db::name('user')->where(['id' => session('user.id')])->value('balance')
        ]);
    }

}