Customer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\CustomerModel;
  5. use app\common\model\StoreModel;
  6. use app\common\model\UserModel;
  7. use think\App;
  8. use think\facade\View;
  9. use think\Request;
  10. class Customer extends BaseController
  11. {
  12. private $customerModel;
  13. private $storeModel;
  14. private $userModel;
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. $this->customerModel = new CustomerModel();
  19. $this->storeModel = new StoreModel();
  20. $this->userModel = new UserModel();
  21. }
  22. /**
  23. * @param Request $request
  24. * @return \think\response\View
  25. * @throws \think\db\exception\DbException
  26. */
  27. public function index(Request $request) {
  28. $params = $request->param();
  29. $format_params = [
  30. 'name_zh' => format_string($params['name_zh'] ?? null),
  31. 'name_en' => format_string($params['name_en'] ?? null),
  32. 'mobile' => format_string($params['mobile'] ?? null),
  33. ];
  34. View::assign([
  35. 'list' => $this->customerModel->findByPaginate($format_params),
  36. 'params' => $format_params
  37. ]);
  38. return view();
  39. }
  40. /**
  41. * @param Request $request
  42. * @return \think\response\Json|\think\response\View
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function add(Request $request) {
  48. if($request->isAjax()) {
  49. $params = $request->param();
  50. $isExist = $this->customerModel->doesItExist($params['mobile']);
  51. if ($isExist)
  52. return $this->fail(lang("Fail to add. Data duplication"));
  53. $user = $this->userModel->findById($params['follow_user_id']);
  54. if (!$user)
  55. return $this->fail(lang("The employees dont exist"));
  56. $params['follow_username'] = $user->username;
  57. $params['store_id'] = $user->store_id;
  58. $params['store_abbr'] = $user->store_abbr;
  59. $this->customerModel->save($params);
  60. return $this->ok(true);
  61. } else {
  62. View::assign('stores', $this->storeModel->findAllStore());
  63. return view();
  64. }
  65. }
  66. /**
  67. * @param Request $request
  68. * @return \think\response\Json|\think\response\View
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function edit(Request $request) {
  74. $params = $request->param();
  75. if(!isset($params['id']))
  76. return $this->fail(lang('ID not exist'));
  77. $customer = $this->customerModel->findById($params['id']);
  78. if(!$customer)
  79. return $this->fail(lang('"The customer doesn t exist"'));
  80. if ($request->isAjax()) {
  81. $user = $this->userModel->findById($params['follow_user_id']);
  82. if (!$user)
  83. return $this->fail(lang("The employees dont exist"));
  84. $params['follow_username'] = $user->username;
  85. $params['store_id'] = $user->store_id;
  86. $params['store_abbr'] = $user->store_abbr;
  87. $params['update_time'] = time();
  88. $this->customerModel->where('id',$params['id'])->update($params);
  89. return $this->ok(true);
  90. }
  91. View::assign([
  92. 'stores' => $this->storeModel->findAllStore(),
  93. 'customer' => $customer
  94. ]);
  95. return view();
  96. }
  97. public function delete(Request $request) {
  98. $params = $request->param();
  99. if(!isset($params['ids']))
  100. return $this->fail(lang("Please select the data you want to delete"));
  101. $this->customerModel->deleteByIds(explode(',',$params['ids']));
  102. return $this->ok(true);
  103. }
  104. }