Customer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\shop;
  4. use app\common\controller\Backend;
  5. use think\annotation\route\Group;
  6. use think\annotation\route\Route;
  7. use app\admin\traits\Actions;
  8. use think\facade\Db;
  9. use app\common\model\Customer as CustomerModel;
  10. #[Group("shop/customer")]
  11. class Customer extends Backend
  12. {
  13. use Actions;
  14. protected function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->model = new CustomerModel();
  18. }
  19. /**
  20. * 添加
  21. */
  22. #[Route('GET,POST','add')]
  23. public function add()
  24. {
  25. if (false === $this->request->isPost()) {
  26. return $this->fetch();
  27. }
  28. $params = array_merge($this->request->post("row/a"),$this->postParams);
  29. if (empty($params)) {
  30. $this->error(__('提交的参数不能为空'));
  31. }
  32. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  33. $this->error(__('token错误,请刷新页面重试'));
  34. }
  35. foreach ($params as &$value){
  36. if(is_array($value)){
  37. $value=implode(',',$value);
  38. }
  39. if($value===''){
  40. $value=null;
  41. }
  42. }
  43. if($params['account_term'] == 2 && empty($params['cycle'])) $this->error(__('请选择周结日期'));
  44. $result = false;
  45. Db::startTrans();
  46. try {
  47. $result = $this->model->save($params);
  48. if($this->callback){
  49. $callback=$this->callback;
  50. $callback($this->model);
  51. }
  52. Db::commit();
  53. } catch (\Exception $e) {
  54. Db::rollback();
  55. $this->error($e->getMessage());
  56. }
  57. if ($result === false) {
  58. $this->error(__('没有新增任何数据'));
  59. }
  60. $this->success();
  61. }
  62. /**
  63. * 编辑
  64. */
  65. #[Route('GET,POST','edit')]
  66. public function edit(mixed $row=null)
  67. {
  68. $ids = $this->request->get('ids');
  69. if(!$row || is_array($row)){
  70. $row = $this->model->find($ids);
  71. }
  72. if (!$row) {
  73. $this->error(__('没有找到记录'));
  74. }
  75. if(count($this->volidateFields)>0){
  76. foreach ($this->volidateFields as $field=>$value){
  77. if($row[$field]!=$value){
  78. $this->error(__('没有操作权限'));
  79. }
  80. }
  81. }
  82. if (false === $this->request->isPost()) {
  83. $this->assign('row', $row);
  84. return $this->fetch();
  85. }
  86. $params = array_merge($this->request->post("row/a"),$this->postParams);
  87. if (empty($params)) {
  88. $this->error(__('提交的参数不能为空'));
  89. }
  90. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  91. $this->error(__('token错误,请刷新页面重试'));
  92. }
  93. foreach ($params as &$value){
  94. if(is_array($value)){
  95. $value=implode(',',$value);
  96. }
  97. if($value===''){
  98. $value=null;
  99. }
  100. }
  101. if($params['account_term'] == 2 && empty($params['cycle'])) $this->error(__('请选择周结日期'));
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. $result = $row->save($params);
  106. if($this->callback){
  107. $callback=$this->callback;
  108. $callback($row);
  109. }
  110. Db::commit();
  111. } catch (\Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. if (false === $result) {
  116. $this->error(__('没有数据被更新'));
  117. }
  118. $this->success();
  119. }
  120. //导入
  121. #[Route("GET","import")]
  122. public function import()
  123. {
  124. return "导入";
  125. }
  126. //下载
  127. #[Route("GET","download")]
  128. public function download()
  129. {
  130. return "下载";
  131. }
  132. }