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); } }