Rechargecard.php 11 KB

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