MemberCard.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\user;
  12. use app\admin\model\user\MemberCardBatch;
  13. use app\admin\model\user\MemberCard as MemberCardModel;
  14. use app\admin\controller\AuthController;
  15. use service\JsonService as Json;
  16. use service\FormBuilder as Form;
  17. use think\Db;
  18. use think\Url;
  19. /**
  20. * 会员卡管理控制器
  21. * Class User
  22. * @package app\admin\controller\user
  23. */
  24. class MemberCard extends AuthController
  25. {
  26. public function batch_index()
  27. {
  28. $this->assign([
  29. 'activity_type' => 1,
  30. ]);
  31. return $this->fetch();
  32. }
  33. public function batch_list()
  34. {
  35. $where = parent::getMore([
  36. ['title', ""],
  37. ['page', 1],
  38. ['limit', 20],
  39. ]);
  40. $batch_list = MemberCardBatch::getBatchList($where);
  41. return Json::successlayui($batch_list);
  42. }
  43. public function add_batch()
  44. {
  45. $id = $this->request->param('id',0);
  46. if ($id){
  47. $batch_data = MemberCardBatch::getBatchOne($id);
  48. $this->assign('batch', json_encode($batch_data));
  49. }
  50. $this->assign([
  51. 'id' => $id,
  52. ]);
  53. return $this->fetch();
  54. }
  55. public function save_batch()
  56. {
  57. $id = $this->request->param('id',0);
  58. $data = parent::postMore([
  59. ['title', ''],
  60. ['use_day', 1],
  61. ['total_num', 1],
  62. ['status', 0],
  63. ]);
  64. if (!isset($data['use_day']) || $data['use_day'] <= 0 || !is_numeric( $data['use_day'])) return Json::fail('体验时间未填写或不合法');
  65. if (!isset($data['total_num']) || $data['total_num'] <= 0 || !is_numeric( $data['total_num'])) return Json::fail('制卡未填写或不合法');
  66. if ($data['total_num'] > 6000) return Json::fail('单次制卡数量最高不得超过6000张');
  67. try{
  68. MemberCardBatch::beginTrans();
  69. if ($id) {
  70. $data['update_time'] = time();
  71. }else{
  72. $data['create_time'] = time();
  73. $batch_id = MemberCardBatch::addBatch($data);
  74. $batch_card = MemberCardModel::addCard($batch_id, $data['total_num']);
  75. if($batch_id && $batch_card){
  76. $qrcodeUrl=MemberCardBatch::qrcodes_url($batch_id,5);
  77. MemberCardBatch::where('id',$batch_id)->update(['qrcode'=>$qrcodeUrl]);
  78. }
  79. }
  80. MemberCardBatch::commitTrans();
  81. return Json::successful('添加成功');
  82. }catch (\Exception $e) {
  83. MemberCardBatch::rollbackTrans();
  84. return Json::fail('添加失败');
  85. }
  86. }
  87. /**
  88. * 快速编辑
  89. * @param string $field 字段名
  90. * @param int $id 修改的主键
  91. * @param string value 修改后的值
  92. * @return json
  93. */
  94. public function set_value($field = '', $id, $value = '', $model_type)
  95. {
  96. if ($field == "use_day" && $id) {
  97. if (!$value || !is_numeric($value) || $value <= 0) return Json::fail('非法数值');
  98. $get_one = MemberCardModel::getCardOne(['card_batch_id' => $id, 'use_uid' => ['>',0]]);
  99. if ($get_one){
  100. return Json::fail('此批次卡片已经在使用当中,无法进行此非法操作');
  101. }
  102. }
  103. $use=MemberCardModel::where('card_batch_id',$id)->where('use_uid','>',0)->count();
  104. if($use) return Json::fail('此批次卡片已经在使用当中,无法进行此非法操作');
  105. if($model_type=='member_card' && $id){
  106. $card = MemberCardModel::where(['id' => $id, 'use_uid' => ['>',0]])->find();
  107. if($card) return Json::fail('此卡片已经在使用当中,无法进行此非法操作');
  108. }
  109. return set_field_value([$field => $value], ['id' => $id], $value, $model_type);
  110. }
  111. public function card_index()
  112. {
  113. $data = parent::getMore([
  114. ['activity_type', 2],
  115. ['card_batch_id', 0],
  116. ]);
  117. $batch_list = MemberCardBatch::getBatchAll([]);
  118. $this->assign([
  119. 'activity_type' => $data['activity_type'],
  120. 'card_batch_id' => $data['card_batch_id'],
  121. 'batch_list' => $batch_list ? $batch_list->toArray() : [],
  122. ]);
  123. return $this->fetch();
  124. }
  125. public function card_list()
  126. {
  127. $card_batch_id = $this->request->param('card_batch_id',0);
  128. $excel = $this->request->param('excel',0);
  129. $where = parent::getMore([
  130. ['card_number', ""],
  131. ['phone', ""],
  132. ['card_batch_id', $card_batch_id],
  133. ['is_use',""],
  134. ['is_status',""],
  135. ['page', 1],
  136. ['limit', 20],
  137. ['excel', $excel],
  138. ]);
  139. $card_list = MemberCardModel::getCardList($where);
  140. return Json::successlayui($card_list);
  141. }
  142. }