Customer.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 管理用户
  5. */
  6. class Customer extends Admin{
  7. protected $Customer = null;
  8. protected function _initialize(){
  9. parent::_initialize();
  10. $this->Customer = model('Customer');
  11. }
  12. public function index(){
  13. $this->assign('meta_title','客户列表');
  14. return $this->fetch();
  15. }
  16. public function load(){
  17. $where = [];
  18. $page = input('get.page');
  19. $limit = input('get.limit');
  20. $list = $this->Customer->where($where)->paginate($limit,false,['page'=>$page]);
  21. $data = [];
  22. foreach ($list as $key => $value) {
  23. $data[$key]['id'] = $value['id'];
  24. $data[$key]['cname'] = $value['cname'];
  25. $data[$key]['truename'] = $value['truename'];
  26. $data[$key]['mobile'] = $value['mobile'];
  27. $data[$key]['address'] = $value['address'];
  28. $data[$key]['state'] = $value['state'];
  29. $data[$key]['create_time'] = $value['create_time'];
  30. }
  31. $this->output(0,'获取成功',$data,$list->total());
  32. }
  33. public function add(){
  34. if ($this->request->isPost()) {
  35. $cname = input('post.cname');
  36. if (empty($cname)) {
  37. $this->output(1,'名称不能为空');
  38. }
  39. $this->Customer->cname = $cname;
  40. $this->Customer->truename = input('post.truename');
  41. $this->Customer->mobile = input('post.mobile');
  42. $this->Customer->address = input('post.address');
  43. $result = $this->Customer->save();
  44. if (!$result) {
  45. $this->output(1,'保存失败');
  46. }
  47. $this->output(0,'保存成功');
  48. }else{
  49. $this->assign('meta_title','添加客户');
  50. return $this->fetch();
  51. }
  52. }
  53. public function edit(){
  54. if ($this->request->isPost()) {
  55. $id = input('post.id');
  56. $customer = $this->Customer->where(['id'=>$id])->find();
  57. if (!$customer) {
  58. $this->output(1,'参数错误');
  59. }
  60. $cname = input('post.cname');
  61. if (empty($cname)) {
  62. $this->output(1,'名称不能为空');
  63. }
  64. $customer->cname = $cname;
  65. $customer->truename = input('post.truename');
  66. $customer->mobile = input('post.mobile');
  67. $customer->address = input('post.address');
  68. $result = $customer->save();
  69. if (!$result) {
  70. $this->output(1,'保存失败');
  71. }
  72. $this->output(0,'保存成功');
  73. }else{
  74. $id = input('get.id');
  75. $customer = $this->Customer->where(['id'=>$id])->find();
  76. if (!$customer) {
  77. $this->error('参数错误');
  78. }
  79. $this->assign('customer',$customer);
  80. $this->assign('meta_title','添加客户');
  81. return $this->fetch();
  82. }
  83. }
  84. public function delete(){
  85. if ($this->request->isPost()) {
  86. $id = input('post.id');
  87. $customer = $this->Customer->where(['id'=>$id])->find();
  88. if (!$customer) {
  89. $this->output(1,'参数错误');
  90. }
  91. $result = $customer->delete();
  92. if (!$result) {
  93. $this->output(1,'删除失败');
  94. }
  95. $this->output(0,'删除成功');
  96. }
  97. }
  98. }