Codes.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\admin\controller\soft;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 软件激活码管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Codes extends Backend
  13. {
  14. /**
  15. * Codes模型对象
  16. * @var \app\common\model\soft\Codes
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\common\model\soft\Codes;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 添加
  32. *
  33. * @return string
  34. * @throws \think\Exception
  35. */
  36. public function add()
  37. {
  38. if (false === $this->request->isPost()) {
  39. return $this->view->fetch();
  40. }
  41. $params = $this->request->post('row/a');
  42. if (empty($params)) {
  43. $this->error(__('Parameter %s can not be empty', ''));
  44. }
  45. $params = $this->preExcludeFields($params);
  46. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  47. $params[$this->dataLimitField] = $this->auth->id;
  48. }
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. //是否采用模型验证
  53. if ($this->modelValidate) {
  54. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  55. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  56. $this->model->validateFailException()->validate($validate);
  57. }
  58. $dataList = [];
  59. $create_num = $params['create_num'] ?? 1;
  60. while ($create_num-- > 0) {
  61. if ($params['create_num'] == 1) {
  62. empty($params['activate_code']) && $params['activate_code'] = generate_code(16);
  63. } else {
  64. $params['activate_code'] = generate_code(16);
  65. }
  66. $dataList[] = $params;
  67. }
  68. $result = $this->model->allowField(true)->saveAll($dataList);
  69. Db::commit();
  70. } catch (ValidateException|PDOException|Exception $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. }
  74. if ($result === false) {
  75. $this->error(__('No rows were inserted'));
  76. }
  77. $this->success();
  78. }
  79. }