Supplier.php 2.8 KB

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