Rechargecard.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Rechargecard extends AdminControl
  16. {
  17. const EXPORT_SIZE = 100;
  18. public function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/rechargecard.lang.php');
  22. }
  23. public function index()
  24. {
  25. $rechargecard_model = model('rechargecard');
  26. $condition = array();
  27. if (request()->isGet()) {
  28. $sn = trim((string)input('param.sn'));
  29. $batchflag = trim((string)input('param.batchflag'));
  30. $state = input('param.state');
  31. if (strlen($sn) > 0) {
  32. $condition[] = array('rc_sn', 'like', "%{$sn}%");
  33. }
  34. if (strlen($batchflag) > 0) {
  35. $condition[] = array('rc_batchflag', 'like', "%{$batchflag}%");
  36. }
  37. if ($state == '0' || $state == '1') {
  38. $condition[] = array('rc_state', '=', $state);
  39. }
  40. }
  41. $cardList = $rechargecard_model->getRechargecardList($condition, 20);
  42. View::assign('card_list', $cardList);
  43. View::assign('show_page', $rechargecard_model->page_info->render());
  44. $this->setAdminCurItem('index');
  45. return View::fetch();
  46. }
  47. public function add_card()
  48. {
  49. if (!request()->isPost()) {
  50. return View::fetch();
  51. } else {
  52. $denomination = (float)input('post.denomination');
  53. if ($denomination < 0.01) {
  54. $this->error(lang('rechargecard_denomination_min'));
  55. }
  56. if ($denomination > 1000) {
  57. $this->error(lang('rechargecard_denomination_max'));
  58. }
  59. $snKeys = array();
  60. switch (input('post.type')) {
  61. case '0':
  62. $total = (int)input('post.total');
  63. if ($total < 1 || $total > 5000) {
  64. $this->error(lang('r0total_message'));
  65. }
  66. $prefix = (string)input('post.prefix');
  67. if (!preg_match('/^[0-9a-zA-Z]{0,16}$/', $prefix)) {
  68. $this->error(lang('r0prefix_message'));
  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. $this->error(lang('file_upload_fail'));
  78. }
  79. if (!is_uploaded_file($f['tmp_name'])) {
  80. $this->error(lang('file_empty'));
  81. }
  82. foreach (file($f['tmp_name']) as $sn) {
  83. $sn = trim($sn);
  84. if (preg_match('/^[0-9a-zA-Z]{1,50}$/', $sn))
  85. $snKeys[$sn] = null;
  86. }
  87. break;
  88. case '2':
  89. foreach (explode("\n", (string)input('post.manual')) as $sn) {
  90. $sn = trim($sn);
  91. if (preg_match('/^[0-9a-zA-Z]{1,50}$/', $sn))
  92. $snKeys[$sn] = null;
  93. }
  94. break;
  95. default:
  96. $this->error(lang('param_error'));
  97. exit;
  98. }
  99. $totalKeys = count($snKeys);
  100. if ($totalKeys < 1 || $totalKeys > 9999) {
  101. $this->error(lang('recharge_number_error'));
  102. }
  103. if (empty($snKeys)) {
  104. $this->error(lang('recharge_number_invalid'));
  105. }
  106. $snOccupied = 0;
  107. $rechargecard_model = model('rechargecard');
  108. // chunk size = 50
  109. foreach (array_chunk(array_keys($snKeys), 50) as $snValues) {
  110. foreach ($rechargecard_model->getOccupiedRechargecardSNsBySNs($snValues) as $sn) {
  111. $snOccupied++;
  112. unset($snKeys[$sn]);
  113. }
  114. }
  115. if (empty($snKeys)) {
  116. $this->error(lang('recharge_number_exist'));
  117. }
  118. $batchflag = input('post.batchflag');
  119. $adminName = $this->admin_info['admin_name'];
  120. $ts = TIMESTAMP;
  121. $snToInsert = array();
  122. foreach (array_keys($snKeys) as $sn) {
  123. $snToInsert[] = array(
  124. 'rc_sn' => $sn, 'rc_denomination' => $denomination, 'rc_batchflag' => $batchflag, 'rc_admin_name' => $adminName,
  125. 'rc_tscreated' => $ts,
  126. );
  127. }
  128. if (!$rechargecard_model->addRechargecardAll($snToInsert)) {
  129. $this->error(lang('ds_common_op_fail'));
  130. }
  131. $countInsert = count($snToInsert);
  132. $this->log("新增{$countInsert}张充值卡(面额¥{$denomination},批次标识“{$batchflag}”)");
  133. $msg = lang('ds_common_op_succ');
  134. if ($snOccupied > 0)
  135. $msg .= sprintf(lang('rechargecard_number_exist_error'), $snOccupied);
  136. dsLayerOpenSuccess($msg);
  137. }
  138. }
  139. /**
  140. * 删除平台充值卡
  141. */
  142. public function del_card()
  143. {
  144. $rechargecard_model = model('rechargecard');
  145. $rc_id = input('param.rc_id');
  146. $rc_id_array = ds_delete_param($rc_id);
  147. if ($rc_id_array === FALSE) {
  148. ds_json_encode(10001, lang('param_error'));
  149. }
  150. $condition = array(array('rc_id', 'in', $rc_id_array));
  151. $condition[] = array('rc_state', '=', 0);
  152. $result = $rechargecard_model->delRechargecard($condition);
  153. if ($result) {
  154. ds_json_encode(10000, lang('ds_common_del_succ'));
  155. } else {
  156. ds_json_encode(10001, lang('ds_common_del_fail'));
  157. }
  158. }
  159. /**
  160. * 导出
  161. */
  162. public function export_step1()
  163. {
  164. $rechargecard_model = model('rechargecard');
  165. $condition = array();
  166. if (request()->isPost()) {
  167. $sn = trim((string)input('param.sn'));
  168. $batchflag = trim((string)input('param.batchflag'));
  169. $state = trim((string)input('param.state'));
  170. if (strlen($sn) > 0) {
  171. $condition[] = array('rc_sn', 'like', "%{$sn}%");
  172. View::assign('sn', $sn);
  173. }
  174. if (strlen($batchflag) > 0) {
  175. $condition[] = array('rc_batchflag', 'like', "%{$batchflag}%");
  176. View::assign('batchflag', $batchflag);
  177. }
  178. if ($state === '0' || $state === '1') {
  179. $condition[] = array('rc_state', '=', $state);
  180. View::assign('state', $state);
  181. }
  182. }
  183. if (!is_numeric(input('param.page'))) {
  184. $count = $rechargecard_model->getRechargecardCount($condition);
  185. $export_list = array();
  186. if ($count > self::EXPORT_SIZE) { //显示下载链接
  187. $page = ceil($count / self::EXPORT_SIZE);
  188. for ($i = 1; $i <= $page; $i++) {
  189. $limit1 = ($i - 1) * self::EXPORT_SIZE + 1;
  190. $limit2 = $i * self::EXPORT_SIZE > $count ? $count : $i * self::EXPORT_SIZE;
  191. $export_list[] = array($i, '=', $limit1 . ' ~ ' . $limit2);
  192. }
  193. View::assign('export_list', $export_list);
  194. return View::fetch('/public/excel');
  195. } else { //如果数量小,直接下载
  196. $data = $rechargecard_model->getRechargecardList($condition, 0, self::EXPORT_SIZE);
  197. $this->createExcel($data);
  198. }
  199. } else { //下载
  200. $limit1 = (input('param.page') - 1) * self::EXPORT_SIZE;
  201. $limit2 = self::EXPORT_SIZE;
  202. $data = $rechargecard_model->getRechargecardList($condition, $limit2);
  203. $this->createExcel($data);
  204. }
  205. }
  206. /**
  207. * 生成excel
  208. *
  209. * @param array $data
  210. */
  211. private function createExcel($data = array())
  212. {
  213. $excel_obj = new \excel\Excel();
  214. $excel_data = array();
  215. //设置样式
  216. $excel_obj->setStyle(array(
  217. 'id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')
  218. ));
  219. //header
  220. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_sn'));
  221. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_batchflag'));
  222. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_denomination'));
  223. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_admin_name'));
  224. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('rc_tscreated'));
  225. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('receive_user'));
  226. //data
  227. foreach ((array)$data as $k => $v) {
  228. $tmp = array();
  229. $tmp[] = array('data' => "\t" . $v['rc_sn']);
  230. $tmp[] = array('data' => "\t" . $v['rc_batchflag']);
  231. $tmp[] = array('data' => "\t" . $v['rc_denomination']);
  232. $tmp[] = array('data' => "\t" . $v['rc_admin_name']);
  233. $tmp[] = array('data' => "\t" . date('Y-m-d H:i:s', $v['rc_tscreated']));
  234. if ($v['rc_state'] == 1 && $v['member_id'] > 0 && $v['rc_tsused'] > 0) {
  235. $tmp[] = array('data' => "\t" . $v['member_name']);
  236. } else {
  237. $tmp[] = array('data' => "\t-");
  238. }
  239. $excel_data[] = $tmp;
  240. }
  241. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  242. $excel_obj->addArray($excel_data);
  243. $excel_obj->addWorksheet($excel_obj->charset(lang('rechargecard'), CHARSET));
  244. $excel_obj->generateXML($excel_obj->charset(lang('rechargecard'), CHARSET) . input('param.page') . '-' . date('Y-m-d-H', TIMESTAMP));
  245. }
  246. protected function getAdminItemList()
  247. {
  248. $menu_array = array(
  249. array(
  250. 'name' => 'index', 'text' => lang('ds_list'), 'url' => (string)url('Rechargecard/index')
  251. ), array(
  252. 'name' => 'add_card', 'text' => lang('ds_new'), 'url' => "javascript:dsLayerOpen('" . (string)url('Rechargecard/add_card') . "','" . lang('ds_new') . "')"
  253. ),
  254. );
  255. return $menu_array;
  256. }
  257. }