model.tpl 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/Apache-2.0
  5. * @link https://www.gougucms.com
  6. */
  7. namespace app\admin\model;
  8. use think\model;
  9. class <model> extends Model
  10. {
  11. /**
  12. * 获取分页列表
  13. * @param $where
  14. * @param $param
  15. */
  16. public function get<model>List($where, $param)
  17. {
  18. $rows = empty($param['limit']) ? get_config('app . page_size') : $param['limit'];
  19. $order = empty($param['order']) ? '<pk> desc' : $param['order'];
  20. $list = self::where($where)->field('<fieldlist>')->order($order)->paginate($rows, false, ['query' => $param]);
  21. return $list;
  22. }
  23. /**
  24. * 添加数据
  25. * @param $param
  26. */
  27. public function add<model>($param)
  28. {
  29. $insertId = 0;
  30. try {
  31. $param['create_time'] = time();
  32. $insertId = self::strict(false)->field(true)->insertGetId($param);
  33. add_log('add', $insertId, $param);
  34. } catch(\Exception $e) {
  35. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  36. }
  37. return to_assign(0,'操作成功',['aid'=>$insertId]);
  38. }
  39. /**
  40. * 编辑信息
  41. * @param $param
  42. */
  43. public function edit<model>($param)
  44. {
  45. try {
  46. $param['update_time'] = time();
  47. self::where('<pk>', $param['<pk>'])->strict(false)->field(true)->update($param);
  48. add_log('edit', $param['id'], $param);
  49. } catch(\Exception $e) {
  50. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  51. }
  52. return to_assign();
  53. }
  54. /**
  55. * 根据id获取信息
  56. * @param $id
  57. */
  58. public function get<model>ById($id)
  59. {
  60. $info = self::where('<pk>', $id)->find();
  61. return $info;
  62. }
  63. /**
  64. * 删除信息
  65. * @param $id
  66. * @return array
  67. */
  68. public function del<model>ById($id,$type=0)
  69. {
  70. if($type==0){
  71. //逻辑删除
  72. try {
  73. $param['delete_time'] = time();
  74. self::where('<pk>', $id)->update(['delete_time'=>time()]);
  75. add_log('delete', $id);
  76. } catch(\Exception $e) {
  77. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  78. }
  79. }
  80. else{
  81. //物理删除
  82. try {
  83. self::where('<pk>', $id)->delete();
  84. add_log('delete', $id);
  85. } catch(\Exception $e) {
  86. return to_assign(1, '操作失败,原因:'.$e->getMessage());
  87. }
  88. }
  89. return to_assign();
  90. }
  91. }