Libao.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * 公众号管理
  4. * 礼包 控制器
  5. */
  6. namespace app\admin\controller;
  7. use think\Config;
  8. use app\common\library\MakeReport;
  9. use app\common\model\Links as LinksModel;
  10. use Overtrue\Pinyin\Pinyin;
  11. use think\Db;
  12. use think\Exception;
  13. class Libao extends Admin
  14. {
  15. /**
  16. * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
  17. */
  18. protected function _initialize()
  19. {
  20. parent::_initialize();
  21. }
  22. /**
  23. * 分享礼包-列表
  24. */
  25. public function index()
  26. {
  27. $wechatDb = Config::get('db_config_wechat.database'); // 角色库
  28. $where['a.is_del'] = 0;
  29. $where['b.is_del'] = 0;
  30. // 标题
  31. if (input('request.title') != '') {
  32. $where['b.title'] = ['like', '%' . input('request.title') .'%'];
  33. }
  34. if (input('request.name') != '') {
  35. $where['a.name'] = ['like', '%' . input('request.name') .'%'];
  36. }
  37. if (input('request.usetype') != '') {
  38. $where['a.usetype'] = input('request.usetype');
  39. }
  40. $start = $this->request->param('cr_start');
  41. $end = $this->request->param('cr_end');
  42. //开始时间和结束时间不为空时
  43. if ($start != '' && $end != '') {
  44. $where['a.create_time'] = [
  45. ['>=', strtotime($start)],
  46. ['<=', strtotime($end . ' 23:59:59')],
  47. ];
  48. } //开始时间不为空时
  49. elseif ($start != '') {
  50. $where['a.create_time'] = ['>=', strtotime($start)];
  51. } //结束时间不为空时
  52. elseif ($end != '') {
  53. $where['a.create_time'] = ['<=', strtotime($end . ' 23:59:59')];
  54. }
  55. $infoList = Db::table($wechatDb . '.nw_wx_sharelibao')->alias('a')
  56. ->join($wechatDb.'.nw_wx_article b','a.article_id = b.id')
  57. ->field('a.*,b.title')
  58. ->where($where)
  59. ->order('a.create_time', 'desc')
  60. ->paginate(10, false, ['query' => input('get.')]);
  61. $this->assign('list', $infoList);
  62. $this->assign('page', $infoList->render());
  63. return $this->fetch('index');
  64. }
  65. /**
  66. * 分享礼包 - 删除
  67. */
  68. public function del()
  69. {
  70. $id = input('id', 0, 'intval');
  71. if(empty($id)) $this->error('记录ID不能为空');
  72. $wechatDb = Config::get('db_config_wechat.database'); // 微信公众号数据库
  73. $ret = Db::table($wechatDb . '.nw_wx_sharelibao')->where(['id' => $id])->setField('is_del', 1);
  74. if (!$ret) {
  75. $this->error('删除错误!');
  76. }
  77. $this->success('删除成功', url('wxArticleList'));
  78. }
  79. /**
  80. * 分享礼包 - 添加
  81. */
  82. public function add()
  83. {
  84. if (request()->isPost()){
  85. $data = [
  86. 'name' => input('post.name') ,
  87. 'article_id' => input('post.article_id' , 0 , 'intval') ,
  88. 'usetype' => input('post.usetype' , 0 , 'intval') ,
  89. 'response' => input('post.response') ,
  90. 'remark' => input('post.remark') ,
  91. 'create_time' => NOW_TIMESTAMP
  92. ];
  93. if (true !== ($res = $this->validate($data, 'Wxarticle'))) {
  94. $this->error($res);
  95. }
  96. $codeStr = input('post.code');
  97. $codeArray = explode("\r\n", $codeStr);
  98. $codeArray = array_map('filterAndTrimInput', $codeArray);
  99. $codeArray = array_filter($codeArray);
  100. if (empty($codeArray)){
  101. $this->error('礼包码不能为空');
  102. }
  103. $GiftModel = model('Common/WxShareLibao');
  104. // 判断礼包名是否重复
  105. $isExit = $GiftModel->where('name',$data['name'])->find();
  106. if (!empty($isExit)){
  107. $this->error('礼包名不能重复');
  108. }
  109. // 判断文章是否重复
  110. $isExit_1 = $GiftModel->where(['article_id'=>$data['article_id'],'is_del'=>0])->find();
  111. if (!empty($isExit_1)){
  112. $this->error('文章不能重复');
  113. }
  114. if ($GiftModel->add($data,$codeArray)) {
  115. $this->success('添加成功',url('index'));
  116. }
  117. $this->error($GiftModel->getError() ?: '添加失败');
  118. }
  119. $articleList = model('WxArticle')->getList();
  120. $this->assign('arList',$articleList);
  121. return $this->fetch();
  122. }
  123. /**
  124. * 分享礼包 - 编辑
  125. */
  126. public function edit()
  127. {
  128. $id = $this->request->param('id',0,'intval');
  129. if (!$id){
  130. $this->error('ID不能为空');
  131. }
  132. $GiftModel = model('Common/WxShareLibao');
  133. $info = $GiftModel->find($id);
  134. if (request()->isPost()){
  135. $usetype = $this->request->param('usetype',0,'intval');
  136. if (!$usetype){
  137. $this->error('参数错误');
  138. }
  139. $data = [
  140. 'name' => input('post.name') ,
  141. 'article_id' => input('post.article_id' , 0 , 'intval') ,
  142. 'response' => input('post.response') ,
  143. 'remark' => input('post.remark') ,
  144. 'update_time' => NOW_TIMESTAMP,
  145. 'update_id' => session('ADMIN_ID'),
  146. ];
  147. if (true !== ($res = $this->validate($data, 'Wxarticle.edit'))) {
  148. $this->error($res);
  149. }
  150. // 判断礼包名是否重复
  151. $isExit = $GiftModel->where('name',$data['name'])->find();
  152. if (!empty($isExit) && $id != $isExit['id']){
  153. $this->error('礼包名不能重复');
  154. }
  155. // 判断文章是否重复
  156. if ($data['article_id'] != $info['article_id']){
  157. $isExit_1 = $GiftModel->where(['article_id'=>$data['article_id'],'is_del'=>0])->find();
  158. if (!empty($isExit_1) ) $this->error('文章不能重复');
  159. }
  160. // 判断是否是无限次礼包,无限次礼包可以修改礼包码
  161. $code = $this->request->param('code');
  162. if ($usetype == 2){
  163. empty($code) ? $this->error('礼包码不能为空') : true;
  164. }
  165. Db::startTrans();
  166. try{
  167. if ($usetype == 2){
  168. model('WxShareLibaoCode')->save(['code'=>$code],['giftid'=>$id]);
  169. }elseif ($usetype == 1 && !empty($code)){
  170. $res = $this->addLibaoCode($code,$id);
  171. if (!$res){
  172. Db::rollback();
  173. $this->error('礼包码添加失败');
  174. }
  175. }
  176. model('WxShareLibao')->save($data,['id'=>$id]);
  177. // 提交事务
  178. Db::commit();
  179. } catch (\Exception $e) {
  180. // 回滚事务
  181. Db::rollback();
  182. $this->error('修改失败');
  183. }
  184. $this->success('添加成功',url('index'));
  185. }
  186. $code = $info->wxShareLibaoCode()->where(['status'=>['in',[0,2]]])->column('code', 'id');
  187. $this->assign('code', implode("\r\n", $code));
  188. $articleList = model('WxArticle')->getList();
  189. $this->assign('arList',$articleList);
  190. $this->assign('info',$info);
  191. return $this->fetch();
  192. }
  193. /**
  194. * 分享礼包 - 添加礼包码
  195. */
  196. public function addLibaoCode($codeStr,$giftid)
  197. {
  198. /*$id = $this->request->param('id',0,'intval');
  199. if (!$id){
  200. $this->error('ID不能为空');
  201. }
  202. $codeStr = $this->request->param('code');*/
  203. if (!$codeStr){
  204. $this->error('礼包码不能为空');
  205. }
  206. $codeArray = explode("\r\n", $codeStr);
  207. $codeArray = array_map('filterAndTrimInput', $codeArray);
  208. $codeArray = array_filter($codeArray);
  209. $codes = [];
  210. foreach ($codeArray as $v) {
  211. $codes[] = ['code' => $v,'giftid'=>$giftid];
  212. }
  213. $res = model('WxShareLibaoCode')->saveAll($codes);
  214. return $res;
  215. }
  216. }