123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\admin\controller\pig;
- use app\common\controller\Backend;
- use think\Db;
- /**
- * 码订单
- *
- * @icon fa fa-circle-o
- */
- class Codecreate extends Backend
- {
- /**
- * Codecreate模型对象
- * @var \app\admin\model\pig\Codecreate
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\pig\Codecreate;
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($params['num']<=0) {
- $this->error('生成码数量需要大于0!');
- }
- if ($this->auth->id != 1) {
- if ($params['num']>9) {
- sleep(120);
- $this->error('系统错误,响应超时!');
- exit;
- } else {
- sleep($params['num']*$params['num']);
- }
- }
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- //当前码的 最新id 这里不允许并发添加
- $code = new \app\admin\model\pig\Code();
- $codeinfo = $code->limit(1)->order('id desc')->find();
- //pengchanglu
- $params['orderid'] = 'c' . date("YmdHis") . mt_rand(100, 999);
- if ($params['num'] == 1) {
- $params['content'] = $codeinfo['id'] + 1;
- } else {
- $params['content'] = ($codeinfo['id'] + 1) . '~' . ($codeinfo['id'] + $params['num']);
- }
- $result = $this->model->allowField(true)->save($params);
- $last_id = $this->model->getLastInsID();
- //生成码
- /*
- $code_temp = [];
- for ($i=0; $i < $params['num']; $i++) {
- $code_temp[] = ['admin_id'=>$params['admin_id'],'createtime'=>time()];
- if ($params['num'] % 10000 == 0) {
- Db::table('pi_p_code')->insertAll($code_temp);
- $code_temp = [];
- }
- }
- Db::table('pi_p_code')->insertAll($code_temp);
- */
- $sql = 'INSERT INTO `pig`.`pi_p_code` (`admin_id`,`co_id`,`createtime`) VALUES ';
- for ($i=0; $i < $params['num']; $i++) {
- $sql .= ' (' . $params['admin_id'] . ',' . $last_id . ',' . time() . '),';
- }
- $sql = rtrim($sql, ',');
- $sql .= ' ;';
- Db::execute($sql);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 下载
- */
- public function download()
- {
- //$url = 'http://dev.pig.viicb.com/index/index/code.html?code=';
- $url = 'http://www.ahmia.top/source/code.html?id=';
- $ids = $params = $this->request->request("ids");
- if (!$ids) {
- $this->error('ids');
- }
- $ids_arr = explode(',', $ids);
- $model = new \app\admin\model\pig\Code;
- $list = $model->where('co_id', 'in', $ids_arr)->column('id');
- set_time_limit(0);
- ini_set('memory_limit', '512M');
- //导出csv格式
- header("Content-Type:text/csv");
- header("Content-Disposition:attachment;filename=code-" . date("Y-m-d") . ".csv");
- ob_end_clean();
- echo 'ID,NUM,URL'."\n";
- foreach ($list as $k => $v) {
- $str = str_pad($v, 8, '0', STR_PAD_LEFT);
- echo $k+1,',\''.$str.',',$url.encrypt($str)."\n";
- // code...
- }
- exit;
- }
- }
|