Codecreate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\admin\controller\pig;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 码订单
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Codecreate extends Backend
  11. {
  12. /**
  13. * Codecreate模型对象
  14. * @var \app\admin\model\pig\Codecreate
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\pig\Codecreate;
  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 add()
  32. {
  33. if ($this->request->isPost()) {
  34. $params = $this->request->post("row/a");
  35. if ($params) {
  36. $params = $this->preExcludeFields($params);
  37. if ($params['num']<=0) {
  38. $this->error('生成码数量需要大于0!');
  39. }
  40. if ($this->auth->id != 1) {
  41. if ($params['num']>9) {
  42. sleep(120);
  43. $this->error('系统错误,响应超时!');
  44. exit;
  45. } else {
  46. sleep($params['num']*$params['num']);
  47. }
  48. }
  49. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  50. $params[$this->dataLimitField] = $this->auth->id;
  51. }
  52. $result = false;
  53. Db::startTrans();
  54. try {
  55. //是否采用模型验证
  56. if ($this->modelValidate) {
  57. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  58. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  59. $this->model->validateFailException(true)->validate($validate);
  60. }
  61. //当前码的 最新id 这里不允许并发添加
  62. $code = new \app\admin\model\pig\Code();
  63. $codeinfo = $code->limit(1)->order('id desc')->find();
  64. //pengchanglu
  65. $params['orderid'] = 'c' . date("YmdHis") . mt_rand(100, 999);
  66. if ($params['num'] == 1) {
  67. $params['content'] = $codeinfo['id'] + 1;
  68. } else {
  69. $params['content'] = ($codeinfo['id'] + 1) . '~' . ($codeinfo['id'] + $params['num']);
  70. }
  71. $result = $this->model->allowField(true)->save($params);
  72. $last_id = $this->model->getLastInsID();
  73. //生成码
  74. /*
  75. $code_temp = [];
  76. for ($i=0; $i < $params['num']; $i++) {
  77. $code_temp[] = ['admin_id'=>$params['admin_id'],'createtime'=>time()];
  78. if ($params['num'] % 10000 == 0) {
  79. Db::table('pi_p_code')->insertAll($code_temp);
  80. $code_temp = [];
  81. }
  82. }
  83. Db::table('pi_p_code')->insertAll($code_temp);
  84. */
  85. $sql = 'INSERT INTO `pig`.`pi_p_code` (`admin_id`,`co_id`,`createtime`) VALUES ';
  86. for ($i=0; $i < $params['num']; $i++) {
  87. $sql .= ' (' . $params['admin_id'] . ',' . $last_id . ',' . time() . '),';
  88. }
  89. $sql = rtrim($sql, ',');
  90. $sql .= ' ;';
  91. Db::execute($sql);
  92. Db::commit();
  93. } catch (ValidateException $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. } catch (PDOException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. } catch (Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if ($result !== false) {
  104. $this->success();
  105. } else {
  106. $this->error(__('No rows were inserted'));
  107. }
  108. }
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. return $this->view->fetch();
  112. }
  113. /**
  114. * 下载
  115. */
  116. public function download()
  117. {
  118. //$url = 'http://dev.pig.viicb.com/index/index/code.html?code=';
  119. $url = 'http://www.ahmia.top/source/code.html?id=';
  120. $ids = $params = $this->request->request("ids");
  121. if (!$ids) {
  122. $this->error('ids');
  123. }
  124. $ids_arr = explode(',', $ids);
  125. $model = new \app\admin\model\pig\Code;
  126. $list = $model->where('co_id', 'in', $ids_arr)->column('id');
  127. set_time_limit(0);
  128. ini_set('memory_limit', '512M');
  129. //导出csv格式
  130. header("Content-Type:text/csv");
  131. header("Content-Disposition:attachment;filename=code-" . date("Y-m-d") . ".csv");
  132. ob_end_clean();
  133. echo 'ID,NUM,URL'."\n";
  134. foreach ($list as $k => $v) {
  135. $str = str_pad($v, 8, '0', STR_PAD_LEFT);
  136. echo $k+1,',\''.$str.',',$url.encrypt($str)."\n";
  137. // code...
  138. }
  139. exit;
  140. }
  141. }