rechargecard.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * 平台充值卡
  4. ***/
  5. defined('InShopNC') or exit('Access Invalid!');
  6. class rechargecardControl extends SystemControl
  7. {
  8. const EXPORT_SIZE = 100;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function indexOp()
  14. {
  15. $model = Model('rechargecard');
  16. $condition = array();
  17. if (isset($_GET['form_submit'])) {
  18. $sn = trim((string) $_GET['sn']);
  19. $batchflag = trim((string) $_GET['batchflag']);
  20. $state = trim((string) $_GET['state']);
  21. if (strlen($sn) > 0) {
  22. $condition['sn'] = array('like', "%{$sn}%");
  23. Tpl::output('sn', $sn);
  24. }
  25. if (strlen($batchflag) > 0) {
  26. $condition['batchflag'] = array('like', "%{$batchflag}%");
  27. Tpl::output('batchflag', $batchflag);
  28. }
  29. if ($state === '0' || $state === '1') {
  30. $condition['state'] = $state;
  31. Tpl::output('state', $state);
  32. }
  33. if ($condition) {
  34. Tpl::output('form_submit', 'ok');
  35. }
  36. }
  37. $cardList = $model->getRechargeCardList($condition, 20);
  38. Tpl::output('card_list', $cardList);
  39. Tpl::output('show_page', $model->showpage());
  40. Tpl::showpage('rechargecard.index');
  41. }
  42. public function add_cardOp()
  43. {
  44. if (!chksubmit()) {
  45. Tpl::showpage('rechargecard.add_card');
  46. return;
  47. }
  48. $denomination = (float) $_POST['denomination'];
  49. if ($denomination < 0.01) {
  50. showMessage('面额不能小于0.01', '', 'html', 'error');
  51. return;
  52. }
  53. if ($denomination > 1000) {
  54. showMessage('面额不能大于1000', '', 'html', 'error');
  55. return;
  56. }
  57. $snKeys = array();
  58. switch ($_POST['type']) {
  59. case '0':
  60. $total = (int) $_POST['total'];
  61. if ($total < 1 || $total > 9999) {
  62. showMessage('总数只能是1~9999之间的整数', '', 'html', 'error');
  63. exit;
  64. }
  65. $prefix = (string) $_POST['prefix'];
  66. if (!preg_match('/^[0-9a-zA-Z]{0,16}$/', $prefix)) {
  67. showMessage('前缀只能是16字之内字母数字的组合', '', 'html', 'error');
  68. exit;
  69. }
  70. while (count($snKeys) < $total) {
  71. $snKeys[$prefix . md5(uniqid(mt_rand(), true))] = null;
  72. }
  73. break;
  74. case '1':
  75. $f = $_FILES['_textfile'];
  76. if (!$f || $f['error'] != 0) {
  77. showMessage('文件上传失败', '', 'html', 'error');
  78. exit;
  79. }
  80. if (!is_uploaded_file($f['tmp_name'])) {
  81. showMessage('未找到已上传的文件', '', 'html', 'error');
  82. exit;
  83. }
  84. foreach (file($f['tmp_name']) as $sn) {
  85. $sn = trim($sn);
  86. if (preg_match('/^[0-9a-zA-Z]{1,50}$/', $sn))
  87. $snKeys[$sn] = null;
  88. }
  89. break;
  90. case '2':
  91. foreach (explode("\n", (string) $_POST['manual']) as $sn) {
  92. $sn = trim($sn);
  93. if (preg_match('/^[0-9a-zA-Z]{1,50}$/', $sn))
  94. $snKeys[$sn] = null;
  95. }
  96. break;
  97. default:
  98. showMessage('参数错误', '', 'html', 'error');
  99. exit;
  100. }
  101. $totalKeys = count($snKeys);
  102. if ($totalKeys < 1 || $totalKeys > 9999) {
  103. showMessage('只能在一次操作中增加1~9999个充值卡号', '', 'html', 'error');
  104. exit;
  105. }
  106. if (empty($snKeys)) {
  107. showMessage('请输入至少一个合法的卡号', '', 'html', 'error');
  108. exit;
  109. }
  110. $snOccupied = 0;
  111. $model = Model('rechargecard');
  112. // chunk size = 50
  113. foreach (array_chunk(array_keys($snKeys), 50) as $snValues) {
  114. foreach ($model->getOccupiedRechargeCardSNsBySNs($snValues) as $sn) {
  115. $snOccupied++;
  116. unset($snKeys[$sn]);
  117. }
  118. }
  119. if (empty($snKeys)) {
  120. showMessage('操作失败,所有新增的卡号都与已有的卡号冲突', '', 'html', 'error');
  121. exit;
  122. }
  123. $batchflag = $_POST['batchflag'];
  124. $adminName = $this->admin_info['name'];
  125. $ts = time();
  126. $snToInsert = array();
  127. foreach (array_keys($snKeys) as $sn) {
  128. $snToInsert[] = array(
  129. 'sn' => $sn,
  130. 'denomination' => $denomination,
  131. 'batchflag' => $batchflag,
  132. 'admin_name' => $adminName,
  133. 'tscreated' => $ts,
  134. );
  135. }
  136. if (!$model->insertAll($snToInsert)) {
  137. showMessage('操作失败', '', 'html', 'error');
  138. exit;
  139. }
  140. $countInsert = count($snToInsert);
  141. $this->log("新增{$countInsert}张充值卡(面额¥{$denomination},批次标识“{$batchflag}”)");
  142. $msg = '操作成功';
  143. if ($snOccupied > 0)
  144. $msg .= "有 {$snOccupied} 个卡号与已有的未使用卡号冲突";
  145. showMessage($msg, urlAdmin('rechargecard', 'index'));
  146. }
  147. public function del_cardOp()
  148. {
  149. if (empty($_GET['id'])) {
  150. showMessage('参数错误', '', 'html', 'error');
  151. }
  152. Model('rechargecard')->delRechargeCardById($_GET['id']);
  153. $this->log("删除充值卡(#ID: {$_GET['id']})");
  154. showMessage('操作成功', getReferer());
  155. }
  156. public function del_card_batchOp()
  157. {
  158. if (empty($_POST['ids']) || !is_array($_POST['ids'])) {
  159. showMessage('参数错误', '', 'html', 'error');
  160. }
  161. Model('rechargecard')->delRechargeCardById($_POST['ids']);
  162. $count = count($_POST['ids']);
  163. $this->log("删除{$count}张充值卡");
  164. showMessage('操作成功', getReferer());
  165. }
  166. /**
  167. * 导出
  168. */
  169. public function export_step1Op()
  170. {
  171. $model = Model('rechargecard');
  172. $condition = array();
  173. if (isset($_GET['form_submit'])) {
  174. $sn = trim((string) $_GET['sn']);
  175. $batchflag = trim((string) $_GET['batchflag']);
  176. $state = trim((string) $_GET['state']);
  177. if (strlen($sn) > 0) {
  178. $condition['sn'] = array('like', "%{$sn}%");
  179. Tpl::output('sn', $sn);
  180. }
  181. if (strlen($batchflag) > 0) {
  182. $condition['batchflag'] = array('like', "%{$batchflag}%");
  183. Tpl::output('batchflag', $batchflag);
  184. }
  185. if ($state === '0' || $state === '1') {
  186. $condition['state'] = $state;
  187. Tpl::output('state', $state);
  188. }
  189. if ($condition) {
  190. Tpl::output('form_submit', 'ok');
  191. }
  192. }
  193. if (!is_numeric($_GET['curpage'])){
  194. $count = $model->getRechargeCardCount($condition);
  195. $array = array();
  196. if ($count > self::EXPORT_SIZE ){ //显示下载链接
  197. $page = ceil($count/self::EXPORT_SIZE);
  198. for ($i=1;$i<=$page;$i++){
  199. $limit1 = ($i-1)*self::EXPORT_SIZE + 1;
  200. $limit2 = $i*self::EXPORT_SIZE > $count ? $count : $i*self::EXPORT_SIZE;
  201. $array[$i] = $limit1.' ~ '.$limit2 ;
  202. }
  203. Tpl::output('list',$array);
  204. Tpl::output('murl','index.php?act=rechargecard&op=index');
  205. Tpl::showpage('export.excel');
  206. return;
  207. }else{ //如果数量小,直接下载
  208. $data = $model->getRechargeCardList($condition, self::EXPORT_SIZE);
  209. $this->createExcel($data);
  210. }
  211. }else{ //下载
  212. $limit1 = ($_GET['curpage']-1) * self::EXPORT_SIZE;
  213. $limit2 = self::EXPORT_SIZE;
  214. $data = $model->getRechargeCardList($condition, 20, "{$limit1},{$limit2}");
  215. $this->createExcel($data);
  216. }
  217. }
  218. /**
  219. * 生成excel
  220. *
  221. * @param array $data
  222. */
  223. private function createExcel($data = array()){
  224. Language::read('export');
  225. import('libraries.excel');
  226. $excel_obj = new Excel();
  227. $excel_data = array();
  228. //设置样式
  229. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  230. //header
  231. $excel_data[0][] = array('styleid'=>'s_title','data'=>'充值卡卡号');
  232. $excel_data[0][] = array('styleid'=>'s_title','data'=>'批次标识');
  233. $excel_data[0][] = array('styleid'=>'s_title','data'=>'面额(元)');
  234. $excel_data[0][] = array('styleid'=>'s_title','data'=>'发布管理员');
  235. $excel_data[0][] = array('styleid'=>'s_title','data'=>'发布时间');
  236. $excel_data[0][] = array('styleid'=>'s_title','data'=>'领取人');
  237. //data
  238. foreach ((array)$data as $k=>$v){
  239. $tmp = array();
  240. $tmp[] = array('data'=>"\t".$v['sn']);
  241. $tmp[] = array('data'=>"\t".$v['batchflag']);
  242. $tmp[] = array('data'=>"\t".$v['denomination']);
  243. $tmp[] = array('data'=>"\t".$v['admin_name']);
  244. $tmp[] = array('data'=>"\t".date('Y-m-d H:i:s', $v['tscreated']));
  245. if ($v['state'] == 1 && $v['member_id'] > 0 && $v['tsused'] > 0) {
  246. $tmp[] = array('data'=>"\t".$v['member_name']);
  247. } else {
  248. $tmp[] = array('data'=>"\t-");
  249. }
  250. $excel_data[] = $tmp;
  251. }
  252. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  253. $excel_obj->addArray($excel_data);
  254. $excel_obj->addWorksheet($excel_obj->charset('充值卡',CHARSET));
  255. $excel_obj->generateXML($excel_obj->charset('充值卡',CHARSET).$_GET['curpage'].'-'.date('Y-m-d-H',time()));
  256. }
  257. }