Code.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\controller\Admin;
  4. /**
  5. * 码
  6. */
  7. class Code extends Admin{
  8. protected $CouponCode = null;
  9. protected function _initialize(){
  10. parent::_initialize();
  11. $this->CouponCode = model('coupon.Code');
  12. }
  13. public function index(){
  14. $this->assign('meta_title','券码管理');
  15. return $this->fetch();
  16. }
  17. public function load(){
  18. $page = input('get.page');
  19. $limit = input('get.limit');
  20. $where = [];
  21. $no = input('param.no');
  22. if (!empty($no)) {
  23. $where['cc.no|cc.secret'] = $no;
  24. }
  25. $search = input('get.search');
  26. if (!empty($search)) {
  27. $where['c.cname|c.id'] = ['like','%'.$search.'%'];
  28. }
  29. $list = $this->CouponCode->field('cc.*')
  30. ->alias('cc')
  31. ->join('Coupon c','c.id = cc.coupon_id')
  32. ->where($where)->order('id desc')->paginate($limit,false,['page'=>$page]);
  33. $data = [];
  34. foreach ($list as $key => $value) {
  35. $data[$key]['id'] = $value['id'];
  36. $coupon = $value['coupon'];
  37. $data[$key]['coupon'] = $coupon['cname'].'['.$coupon['id'].']';
  38. $coupon_type = $coupon['type'];
  39. $data[$key]['type'] = $coupon_type['cname'];
  40. $data[$key]['sale_price'] = $coupon['sale_price'];
  41. $data[$key]['no'] = $value['no'];
  42. $data[$key]['secret'] = $value['secret'];
  43. $data[$key]['qrcode'] = '<img src="'.$value['qrcode'].'">';
  44. $data[$key]['barcode'] = '<img src="'.$value['barcode'].'">';
  45. $data[$key]['url'] = $value['url'];
  46. $data[$key]['expire_time'] = $value['expire_time'];
  47. $data[$key]['state'] = $value['state'];
  48. $data[$key]['state_text'] = $value['state_text'];
  49. $data[$key]['sale_time'] = date('Y-m-d H:i:s',$value['sale_time']);
  50. $data[$key]['create_time'] = $value['create_time'];
  51. }
  52. $this->output(0,'获取成功',$data,$list->total());
  53. }
  54. public function add(){
  55. if($this->request->isPost()) {
  56. $coupon_id = input('post.coupon_id');
  57. if (empty($coupon_id)) {
  58. $this->output(1,'请选择电子券');
  59. }
  60. $Coupon = model('Coupon');
  61. $coupon = $Coupon->where(['id'=>$coupon_id])->find();
  62. if (!$coupon) {
  63. $this->output(1,'参数错误');
  64. }
  65. $coupon_type = $coupon['type'];
  66. $type_name = $coupon_type['name'];
  67. $type_value = input('post.'.$type_name);
  68. if (empty($type_value)) {
  69. $this->output(1,'请输入'.$type['cname']);
  70. }
  71. if ($type_name == 'secret') {
  72. $no = input('post.no');
  73. if (empty($no)) {
  74. $this->output(1,'请输入卡号');
  75. }
  76. $this->CouponCode->no = $no;
  77. }
  78. $expire_time = input('post.expire_time');
  79. list($expire_start_time,$expire_end_time) = explode('~',$expire_time);
  80. if (strtotime($expire_end_time) < $this->request->time()) {
  81. $this->output(1,'有效结束时间必须大于当期时间');
  82. }
  83. $this->CouponCode->coupon_id = $coupon['id'];
  84. $this->CouponCode->$type_name = $type_value;
  85. $this->CouponCode->expire_time = $expire_time;
  86. $result = $this->CouponCode->save();
  87. if (!$result) {
  88. $this->output(1,'保存失败');
  89. }
  90. $this->output(0,'保存成功');
  91. }else{
  92. $Coupon = model('Coupon');
  93. $coupons = $Coupon->where(['state'=>1])->select();
  94. $coupon_group = [];
  95. foreach ($coupons as $key => $value) {
  96. $supplier = $value['supplier'];
  97. $coupon_group[$supplier['id']]['cname'] = $supplier['cname'];
  98. $option['id'] = $value['id'];
  99. $option['cname'] = $value['cname'];
  100. $option['type_name'] = $value['type']['name'];
  101. $coupon_group[$supplier['id']]['options'][] = $option;
  102. }
  103. $this->assign('coupon_group',$coupon_group);
  104. $this->assign('meta_title','添加券码');
  105. return $this->fetch();
  106. }
  107. }
  108. public function edit(){
  109. if($this->request->isPost()) {
  110. $id = input('post.id');
  111. $code = $this->CouponCode->where(['id'=>$id])->find();
  112. if (!$code) {
  113. $this->output(1,'参数错误');
  114. }
  115. $coupon_id = input('post.coupon_id');
  116. if (empty($coupon_id)) {
  117. $this->output(1,'请选择电子券');
  118. }
  119. $Coupon = model('Coupon');
  120. $coupon = $Coupon->where(['id'=>$coupon_id])->find();
  121. if (!$coupon) {
  122. $this->output(1,'参数错误');
  123. }
  124. $coupon_type = $coupon['type'];
  125. $type_name = $coupon_type['name'];
  126. if ($type_name == 'secret') {
  127. $no = input('post.no');
  128. if (empty($no)) {
  129. $this->output(1,'请输入卡号');
  130. }
  131. $code->no = $no;
  132. }
  133. $type_value = input('post.'.$type_name);
  134. if (empty($type_value)) {
  135. $this->output(1,'请输入'.$type['cname']);
  136. }
  137. $expire_time = input('post.expire_time');
  138. list($expire_start_time,$expire_end_time) = explode('~',$expire_time);
  139. if (strtotime($expire_end_time) < $this->request->time()) {
  140. $this->output(1,'有效结束时间必须大于当期时间');
  141. }
  142. $code->coupon_id = $coupon['id'];
  143. $code->$type_name = $type_value;
  144. $code->expire_time = $expire_time;
  145. $result = $code->save();
  146. if (!$result) {
  147. $this->output(1,'保存失败');
  148. }
  149. $this->output(0,'保存成功');
  150. }else{
  151. $id = input('get.id');
  152. $code = $this->CouponCode->where(['id'=>$id])->find();
  153. if (!$code) {
  154. $this->output(1,'参数错误');
  155. }
  156. $this->assign('code',$code);
  157. $Coupon = model('Coupon');
  158. $coupons = $Coupon->where(['state'=>1])->group('supplier_id')->select();
  159. $coupon_group = [];
  160. foreach ($coupons as $key => $value) {
  161. $supplier = $value['supplier'];
  162. $coupon_group[$supplier['id']]['cname'] = $supplier['cname'];
  163. $option['id'] = $value['id'];
  164. $option['cname'] = $value['cname'];
  165. $option['type_name'] = $value['type']['name'];
  166. $coupon_group[$supplier['id']]['options'][] = $option;
  167. }
  168. $this->assign('coupon_group',$coupon_group);
  169. $this->assign('meta_title','编辑券码');
  170. return $this->fetch();
  171. }
  172. }
  173. public function state(){
  174. if($this->request->isPost()){
  175. $id = input('post.id');
  176. $code = $this->CouponCode->where(['id'=>$id])->find();
  177. if (!$code) {
  178. $this->output(1,'参数错误');
  179. }
  180. $state = input('post.state');
  181. $code->state = $state == 'true'?1:0;
  182. $result = $code->save();
  183. if (!$result) {
  184. $this->output(1,'保存失败');
  185. }
  186. $this->output(0,'保存成功');
  187. }
  188. }
  189. public function delete(){
  190. if ($this->request->isPost()) {
  191. $id = input('post.id');
  192. $code = $this->CouponCode->where(['id'=>$id])->find();
  193. if (!$code) {
  194. $this->output(1,'参数错误');
  195. }
  196. $result = $code->delete();
  197. if (!$result) {
  198. $this->output(1,'删除失败');
  199. }
  200. $this->output(0,'删除成功');
  201. }
  202. }
  203. public function import(){
  204. vendor('PHPExcel.PHPExcel');
  205. $path = input('param.path');
  206. if (empty($path)) {
  207. $this->output(1,'文件地址不能为空');
  208. }
  209. $path_arr = explode('.',$path);
  210. $exts = $path_arr[count($path_arr) - 1];
  211. //导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
  212. vendor('PHPExcel.PHPExcel');
  213. //创建PHPExcel对象,注意,不能少了\
  214. $PHPExcel = new \PHPExcel();
  215. //如果excel文件后缀名为.xls,导入这个类
  216. if ($exts == 'xls') {
  217. Vendor('PHPExcel.PHPExcel.Reader.Excel5');
  218. $PHPReader = new \PHPExcel_Reader_Excel5();
  219. } else if ($exts == 'xlsx') {
  220. Vendor('PHPExcel.PHPExcel.Reader.Excel2007');
  221. $PHPReader = new \PHPExcel_Reader_Excel2007();
  222. }
  223. //载入文件
  224. $PHPExcel = $PHPReader->load(ROOT_PATH.'public'.$path);
  225. //获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
  226. $currentSheet = $PHPExcel->getSheet(0);
  227. //获取总列数
  228. $allColumn = $currentSheet->getHighestColumn();
  229. //获取总行数
  230. $allRow = $currentSheet->getHighestRow();
  231. $fields = ['A'=>'coupon_id','B'=>'no','C'=>'secret','D'=>'qrcode','E'=>'barcode','F'=>'expire_time'];
  232. //循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
  233. $data = [];
  234. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  235. //从哪列开始,A表示第一列
  236. for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
  237. //数据坐标
  238. $address = $currentColumn . $currentRow;
  239. //读取到的数据,保存到数组$data中
  240. $cell = $currentSheet->getCell($address)->getValue();
  241. if ($cell instanceof PHPExcel_RichText) {
  242. $cell = $cell->__toString();
  243. }
  244. if (!isset($fields[$currentColumn])) {
  245. break;
  246. }
  247. $field = $fields[$currentColumn];
  248. $data[$currentRow - 1][$field] = $cell;
  249. // print_r($cell);
  250. }
  251. }
  252. var_dump($data);exit();
  253. $count = count($data);
  254. if ($count > 0) {
  255. $this->CouponCode->saveAll($data);
  256. }
  257. $this->output(0,'导入成功 条数:'.$count);
  258. }
  259. }