Product.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ProductModel;
  4. use app\admin\model\UserModel;
  5. use think\Db;
  6. use think\Session;
  7. use app\admin\model\Types;
  8. class Product extends Base
  9. {
  10. /**
  11. * 行业显示
  12. */
  13. public function index()
  14. {
  15. if (request()->isAjax()) {
  16. $product = new ProductModel();
  17. $lists = $product->alias('d')
  18. ->join('admin a','a.id = d.admin_id')
  19. ->whereOr('d.admin_id',session('uid'))
  20. ->whereOr('a.groupid','3')
  21. ->whereOr('a.groupid','5')
  22. ->select();
  23. $count = count($lists);
  24. return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
  25. }
  26. return $this->fetch('index');
  27. }
  28. /**
  29. * 添加
  30. */
  31. public function add()
  32. {
  33. if (request()->isAjax()) {
  34. $name = input('name');
  35. if (!isset($name) || empty($name)) return json(['code' => 100, 'msg' => "请输入行业名"]);
  36. if (mb_strlen($name) <=1 || mb_strlen($name)>8) return json(['code' => 100, 'msg' => "字符长度最大为8最小为1"]);
  37. $da_isset = ProductModel::where('product_name', $name)->find();
  38. if ($da_isset) return json(['code' => 100, 'msg' => "此产品已存在"]);
  39. $product = new ProductModel();
  40. $add = $product->save(['product_name' => $name,'admin_id' => session('uid')]);
  41. if ($add) return json(['code' => 200, 'msg' => "添加成功"]);
  42. if (!$add) return json(['code' => 100, 'msg' => "添加失败"]);
  43. }
  44. return $this->fetch('add');
  45. }
  46. /**
  47. * 修改
  48. */
  49. public function edit()
  50. {
  51. $product = new ProductModel();
  52. if (request()->isAjax()) {
  53. $name = input('name');
  54. $id = input('id');
  55. if (!isset($name) || empty($name)) return json(['code' => 100, 'msg' => "请输入行业名"]);
  56. if (mb_strlen($name) <=1 || mb_strlen($name)>8) return json(['code' => 100, 'msg' => "字符长度最大为8最小为1"]);
  57. $da_isset = ProductModel::where('product_name', $name)->select();
  58. if (count($da_isset)>1) return json(['code' => 100, 'msg' => "此行业已存在"]);
  59. $add = $product->save(['product_name' => $name],['id'=> $id]);
  60. if ($add) return json(['code' => 200, 'msg' => "编辑成功"]);
  61. if (!$add) return json(['code' => 100, 'msg' => "编辑失败"]);
  62. }
  63. $id = input('id');
  64. $this->assign(['data' => $product->where('id',$id)->find()]);
  65. return $this->fetch('edit');
  66. }
  67. /**
  68. * 删除
  69. */
  70. public function del()
  71. {
  72. $id = input('id');
  73. // $user = new UserModel();
  74. // $is_use = Db::name('customer')->where('type',$id)->find(); // 判断是否由用户使用此字段
  75. // if ($is_use) return json(['code' => 100, 'msg' => "还有用户正在使用此行业呢!"]);
  76. $del = ProductModel::where('id',$id)->delete();
  77. if ($del) return json(['code' => 200, 'msg' => "删除成功"]);
  78. if (!$del) return json(['code' => 100, 'msg' => "删除失败"]);
  79. }
  80. }