Rechargecard.php 11 KB

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