Vip.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use Think\Db;
  5. /**
  6. * 会员管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Vip extends Backend
  11. {
  12. /**
  13. * Vip模型对象
  14. * @var \app\admin\model\Vip
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Vip;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 编辑
  30. */
  31. public function edit($ids = null) {
  32. $row = $this->model->get($ids);
  33. if (!$row) {
  34. $this->error(__('No Results were found'));
  35. }
  36. $adminIds = $this->getDataLimitAdminIds();
  37. if (is_array($adminIds)) {
  38. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  39. $this->error(__('You have no permission'));
  40. }
  41. }
  42. if ($this->request->isPost()) {
  43. $params = $this->request->post("row/a");
  44. if ($params) {
  45. $params = $this->preExcludeFields($params);
  46. if ($ids == 1) {
  47. if (isset($params['growthvalue']) && $params['growthvalue'] != 0) {
  48. $this->error('普通会员等级所需成长值只能为0');
  49. }
  50. }
  51. $last_vip = Db::name('vip')->find($ids - 1);
  52. if ($last_vip) {
  53. if (isset($params['growthvalue']) && $params['growthvalue'] <= $last_vip['growthvalue']) {
  54. $this->error('当前等级所需成长值必须大于上一级');
  55. }
  56. }
  57. $next_vip = Db::name('vip')->find($ids + 1);
  58. if ($next_vip) {
  59. if (isset($params['growthvalue']) && $params['growthvalue'] >= $next_vip['growthvalue']) {
  60. $this->error('当前等级所需成长值必须小于下一级');
  61. }
  62. }
  63. $result = false;
  64. Db::startTrans();
  65. try {
  66. //是否采用模型验证
  67. if ($this->modelValidate) {
  68. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  69. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  70. $row->validateFailException(true)->validate($validate);
  71. }
  72. $result = $row->allowField(true)->save($params);
  73. Db::commit();
  74. } catch (ValidateException $e) {
  75. Db::rollback();
  76. $this->error($e->getMessage());
  77. } catch (PDOException $e) {
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. } catch (Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result !== false) {
  85. $this->success();
  86. } else {
  87. $this->error(__('No rows were updated'));
  88. }
  89. }
  90. $this->error(__('Parameter %s can not be empty', ''));
  91. }
  92. $this->view->assign("row", $row);
  93. return $this->view->fetch();
  94. }
  95. }