| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\common\model\AdminModel;
- use app\common\model\CustomerModel;
- use app\common\model\StoreModel;
- use think\App;
- use think\facade\View;
- use think\Request;
- class Customer extends BaseController
- {
- private $customerModel;
- private $storeModel;
- private $adminModel;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->customerModel = new CustomerModel();
- $this->storeModel = new StoreModel();
- $this->adminModel = new AdminModel();
- }
- /**
- * @param Request $request
- * @return \think\response\View
- * @throws \think\db\exception\DbException
- */
- public function index(Request $request) {
- $params = $request->param();
- $format_params = [
- 'name_zh' => format_string($params['name_zh'] ?? null),
- 'name_en' => format_string($params['name_en'] ?? null),
- 'mobile' => format_string($params['mobile'] ?? null),
- ];
- View::assign([
- 'list' => $this->customerModel->findByPaginate($format_params),
- 'params' => $format_params
- ]);
- return view();
- }
- /**
- * @param Request $request
- * @return \think\response\Json|\think\response\View
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function add(Request $request) {
- if($request->isAjax()) {
- $params = $request->param();
- $isExist = $this->customerModel->doesItExist($params['mobile']);
- if ($isExist)
- return $this->fail(lang("Fail to add. Data duplication"));
- $admin = $this->adminModel->findById($params['follow_user_id']);
- if (!$admin)
- return $this->fail(lang("The employees dont exist"));
- $params['follow_username'] = $admin->nickname;
- $params['store_id'] = explode(",", $admin->store_ids ?? '-1')[0];
- $params['store_abbr'] = $admin->store_abbr;
- $this->customerModel->save($params);
- return $this->ok(true);
- } else {
- View::assign('stores', $this->storeModel->findAllStore());
- return view();
- }
- }
- /**
- * @param Request $request
- * @return \think\response\Json|\think\response\View
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function edit(Request $request) {
- $params = $request->param();
- if(!isset($params['id']))
- return $this->fail(lang('ID not exist'));
- $customer = $this->customerModel->findById($params['id']);
- if(!$customer)
- return $this->fail(lang('"The customer doesn t exist"'));
- if ($request->isAjax()) {
- $user = $this->adminModel->findById($params['follow_user_id']);
- if (!$user)
- return $this->fail(lang("The employees dont exist"));
- $params['follow_username'] = $user->username;
- $params['store_id'] = $user->store_id;
- $params['store_abbr'] = $user->store_abbr;
- $params['update_time'] = time();
- $this->customerModel->where('id',$params['id'])->update($params);
- return $this->ok(true);
- }
- View::assign([
- 'stores' => $this->storeModel->findAllStore(),
- 'customer' => $customer
- ]);
- return view();
- }
- public function delete(Request $request) {
- $params = $request->param();
- if(!isset($params['ids']))
- return $this->fail(lang("Please select the data you want to delete"));
- $this->customerModel->deleteByIds(explode(',',$params['ids']));
- return $this->ok(true);
- }
- }
|