Cardcode.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace app\admin\controller\pickup;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 兑换卡密
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Cardcode extends Backend {
  11. /**
  12. * GiftCardcode模型对象
  13. * @var \app\admin\model\GiftCardcode
  14. */
  15. protected $model = null;
  16. // protected $multiFields = "card_status";
  17. public function _initialize() {
  18. parent::_initialize();
  19. $this->multiFields = "card_status";
  20. // $this->model = new \app\admin\model\GiftCardcode;
  21. $this->model = new \app\admin\model\pickup\Cardcode;
  22. $this->view->assign("cardStatusList", $this->model->getCardStatusList());
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 查看列表
  31. */
  32. public function index($ids = null) {
  33. $ids = $ids ? $ids : $this->request->param("ids");
  34. if ($ids) {
  35. //检查商品是否存在
  36. $modelGiftPackage = new \app\admin\model\pickup\Package;
  37. $giftPackage = $modelGiftPackage->get($ids);
  38. if ($giftPackage) {
  39. $hasGiftPackage = true;
  40. $giftPackage = $giftPackage->toArray();
  41. }
  42. }
  43. if (!isset($giftPackage) || $giftPackage == false) {
  44. $hasGiftPackage = false;
  45. $giftPackage = ["id" => 0];
  46. }
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags']);
  49. if ($this->request->isAjax()) {
  50. $where2 = [];
  51. if ($hasGiftPackage) {
  52. $where2['gp_id'] = $giftPackage['id'];
  53. }
  54. //如果发送的来源是Selectpage,则转发到Selectpage
  55. if ($this->request->request('keyField')) {
  56. return $this->selectpage();
  57. }
  58. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  59. $total = $this->model
  60. ->where($where)
  61. ->where($where2)
  62. ->order($sort, $order)
  63. ->count();
  64. $list = $this->model
  65. ->where($where)
  66. ->where($where2)
  67. ->order($sort, $order)
  68. ->limit($offset, $limit)
  69. ->select();
  70. $list = collection($list)->toArray();
  71. $result = array("total" => $total, "rows" => $list);
  72. return json($result);
  73. }
  74. $this->view->assign("giftPackage", $giftPackage);
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 添加卡密
  79. */
  80. public function add($gpid = null) {
  81. $gpid = $gpid ? $gpid : $this->request->param("gpid");
  82. if ($gpid) {
  83. //检查商品是否存在
  84. $modelGiftPackage = new \app\admin\model\pickup\Package;
  85. $giftPackage = $modelGiftPackage->get($gpid);
  86. if ($giftPackage) {
  87. $hasGiftPackage = true;
  88. $giftPackage = $giftPackage->toArray();
  89. }
  90. }
  91. if (!isset($giftPackage) || $giftPackage == false) {
  92. $hasGiftPackage = false;
  93. $giftPackage = ["id" => 0];
  94. }
  95. if ($this->request->isPost()) {
  96. $params = $this->request->post("row/a");
  97. if ($params) {
  98. $params = $this->preExcludeFields($params);
  99. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  100. $params[$this->dataLimitField] = $this->auth->id;
  101. }
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. //是否采用模型验证
  106. if ($this->modelValidate) {
  107. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  108. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  109. $this->model->validateFailException(true)->validate($validate);
  110. }
  111. $result = $this->model->allowField(true)->save($params);
  112. Db::commit();
  113. } catch (ValidateException $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. } catch (PDOException $e) {
  117. Db::rollback();
  118. $this->error($e->getMessage());
  119. } catch (Exception $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. }
  123. if ($result !== false) {
  124. $this->success();
  125. } else {
  126. $this->error(__('No rows were inserted'));
  127. }
  128. }
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. $this->view->assign("giftPackage", $giftPackage);
  132. return $this->view->fetch();
  133. }
  134. /**
  135. * 批量生成卡密
  136. */
  137. public function batchcreate($gpid = null) {
  138. $gpid = $gpid ? $gpid : $this->request->param("gpid");
  139. if ($gpid) {
  140. //检查商品是否存在
  141. $modelGiftPackage = new \app\admin\model\pickup\Package;
  142. $giftPackage = $modelGiftPackage->get($gpid);
  143. if ($giftPackage) {
  144. $giftPackage = $giftPackage->toArray();
  145. }
  146. }
  147. if (!isset($giftPackage) || $giftPackage == false) {
  148. $giftPackage = ["id" => 0];
  149. }
  150. if ($this->request->isAjax()) {
  151. $params = $this->request->post("row/a");
  152. $params['gp_id'] = intval($params['gp_id']);
  153. $params['number'] = intval($params['number']);
  154. $params['card_prefix'] = intval($params['card_prefix']);
  155. $params['card_length'] = intval($params['card_length']);
  156. $params['pwd_length'] = intval($params['pwd_length']);
  157. if ($params['number'] <= 0) {
  158. $this->error('生成数量不正确!');
  159. }
  160. if ($params['number'] > 10000) {
  161. $this->error('每次最多只能生成10000个!');
  162. }
  163. Db::startTrans();
  164. try {
  165. $try = 0;
  166. $creNum = 0;
  167. for ($i = 0; $i < $params['number']; $i++) {
  168. $try++;
  169. $data = [
  170. "gp_id" => $params['gp_id'],
  171. "card_id" => $params['card_prefix'] . $this->getRandNo($params['card_length']),
  172. "card_pwd" => $this->getRandNo($params['pwd_length']),
  173. "card_status" => 1,
  174. "createtime" => time(),
  175. "usedtime" => 0,
  176. ];
  177. //检查数据库是否存在
  178. $mod = new \app\admin\model\pickup\Cardcode;
  179. $res = $mod->get(['card_id' => $data['card_id']]);
  180. if ($res) {
  181. $i--;
  182. } else {
  183. $mod = new \app\admin\model\pickup\Cardcode;
  184. $res2 = $mod->save($data);
  185. if ($res2) {
  186. $creNum++;
  187. }
  188. }
  189. }
  190. Db::commit();
  191. } catch (PDOException $e) {
  192. Db::rollback();
  193. $this->error($e->getMessage());
  194. } catch (Exception $e) {
  195. Db::rollback();
  196. $this->error($e->getMessage());
  197. }
  198. $this->success("生成卡密完成,共生成{$creNum}个");
  199. }
  200. $this->view->assign("giftPackage", $giftPackage);
  201. return $this->view->fetch();
  202. }
  203. /**
  204. * 批量导出
  205. */
  206. // public function batchexport($gpid = null, $status = null) {
  207. // $gpid = $gpid ? $gpid : $this->request->param("gpid");
  208. // }
  209. private function getRandNo($length = 6) {
  210. return mt_rand(pow(10, ($length - 1)), pow(10, $length) - 1);
  211. }
  212. }