| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace app\admin\controller\pickup;
- use app\common\controller\Backend;
- use think\Db;
- /**
- * 兑换卡密
- *
- * @icon fa fa-circle-o
- */
- class Cardcode extends Backend {
- /**
- * GiftCardcode模型对象
- * @var \app\admin\model\GiftCardcode
- */
- protected $model = null;
- // protected $multiFields = "card_status";
- public function _initialize() {
- parent::_initialize();
- $this->multiFields = "card_status";
- // $this->model = new \app\admin\model\GiftCardcode;
- $this->model = new \app\admin\model\pickup\Cardcode;
- $this->view->assign("cardStatusList", $this->model->getCardStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看列表
- */
- public function index($ids = null) {
- $ids = $ids ? $ids : $this->request->param("ids");
- if ($ids) {
- //检查商品是否存在
- $modelGiftPackage = new \app\admin\model\pickup\Package;
- $giftPackage = $modelGiftPackage->get($ids);
- if ($giftPackage) {
- $hasGiftPackage = true;
- $giftPackage = $giftPackage->toArray();
- }
- }
- if (!isset($giftPackage) || $giftPackage == false) {
- $hasGiftPackage = false;
- $giftPackage = ["id" => 0];
- }
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- $where2 = [];
- if ($hasGiftPackage) {
- $where2['gp_id'] = $giftPackage['id'];
- }
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->where($where2)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->where($where2)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $list = collection($list)->toArray();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- $this->view->assign("giftPackage", $giftPackage);
- return $this->view->fetch();
- }
- /**
- * 添加卡密
- */
- public function add($gpid = null) {
- $gpid = $gpid ? $gpid : $this->request->param("gpid");
- if ($gpid) {
- //检查商品是否存在
- $modelGiftPackage = new \app\admin\model\pickup\Package;
- $giftPackage = $modelGiftPackage->get($gpid);
- if ($giftPackage) {
- $hasGiftPackage = true;
- $giftPackage = $giftPackage->toArray();
- }
- }
- if (!isset($giftPackage) || $giftPackage == false) {
- $hasGiftPackage = false;
- $giftPackage = ["id" => 0];
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- 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);
- }
- $result = $this->model->allowField(true)->save($params);
- 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', ''));
- }
- $this->view->assign("giftPackage", $giftPackage);
- return $this->view->fetch();
- }
- /**
- * 批量生成卡密
- */
- public function batchcreate($gpid = null) {
- $gpid = $gpid ? $gpid : $this->request->param("gpid");
- if ($gpid) {
- //检查商品是否存在
- $modelGiftPackage = new \app\admin\model\pickup\Package;
- $giftPackage = $modelGiftPackage->get($gpid);
- if ($giftPackage) {
- $giftPackage = $giftPackage->toArray();
- }
- }
- if (!isset($giftPackage) || $giftPackage == false) {
- $giftPackage = ["id" => 0];
- }
- if ($this->request->isAjax()) {
- $params = $this->request->post("row/a");
- $params['gp_id'] = intval($params['gp_id']);
- $params['number'] = intval($params['number']);
- $params['card_prefix'] = intval($params['card_prefix']);
- $params['card_length'] = intval($params['card_length']);
- $params['pwd_length'] = intval($params['pwd_length']);
- if ($params['number'] <= 0) {
- $this->error('生成数量不正确!');
- }
- if ($params['number'] > 10000) {
- $this->error('每次最多只能生成10000个!');
- }
- Db::startTrans();
- try {
- $try = 0;
- $creNum = 0;
- for ($i = 0; $i < $params['number']; $i++) {
- $try++;
- $data = [
- "gp_id" => $params['gp_id'],
- "card_id" => $params['card_prefix'] . $this->getRandNo($params['card_length']),
- "card_pwd" => $this->getRandNo($params['pwd_length']),
- "card_status" => 1,
- "createtime" => time(),
- "usedtime" => 0,
- ];
- //检查数据库是否存在
- $mod = new \app\admin\model\pickup\Cardcode;
- $res = $mod->get(['card_id' => $data['card_id']]);
- if ($res) {
- $i--;
- } else {
- $mod = new \app\admin\model\pickup\Cardcode;
- $res2 = $mod->save($data);
- if ($res2) {
- $creNum++;
- }
- }
- }
- Db::commit();
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success("生成卡密完成,共生成{$creNum}个");
- }
- $this->view->assign("giftPackage", $giftPackage);
- return $this->view->fetch();
- }
- /**
- * 批量导出
- */
- // public function batchexport($gpid = null, $status = null) {
- // $gpid = $gpid ? $gpid : $this->request->param("gpid");
- // }
- private function getRandNo($length = 6) {
- return mt_rand(pow(10, ($length - 1)), pow(10, $length) - 1);
- }
- }
|