GiftController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * 礼物列表
  4. */
  5. namespace app\admin\controller;
  6. use cmf\controller\AdminBaseController;
  7. use think\facade\Db;
  8. use think\db\Query;
  9. class GiftController extends AdminbaseController {
  10. protected function getTypes($k=''){
  11. $type=[
  12. '0'=>'普通礼物',
  13. '1'=>'豪华礼物',
  14. '2'=>'手绘礼物',
  15. ];
  16. if($k===''){
  17. return $type;
  18. }
  19. return isset($type[$k]) ? $type[$k]: '';
  20. }
  21. protected function getMark($k=''){
  22. $mark=[
  23. '0'=>'普通',
  24. '2'=>'守护',
  25. ];
  26. if($k===''){
  27. return $mark;
  28. }
  29. return isset($mark[$k]) ? $mark[$k]: '';
  30. }
  31. protected function getSwftype($k=''){
  32. $swftype=[
  33. '0'=>'GIF',
  34. '1'=>'SVGA',
  35. ];
  36. if($k===''){
  37. return $swftype;
  38. }
  39. return isset($swftype[$k]) ? $swftype[$k]: '';
  40. }
  41. function index(){
  42. $lists = Db::name('gift')
  43. ->where(function (Query $query) {
  44. $data = $this->request->param();
  45. $keyword=isset($data['keyword']) ? $data['keyword']: '';
  46. if (!empty($keyword)) {
  47. $query->where('giftname', 'like', "%$keyword%");
  48. }
  49. })
  50. ->order("orderno,addtime DESC")
  51. ->paginate(20);
  52. $lists->each(function($v,$k){
  53. $v['gifticon']=get_upload_path($v['gifticon']);
  54. $v['swf']=get_upload_path($v['swf']);
  55. return $v;
  56. });
  57. //分页-->筛选条件参数
  58. $data = $this->request->param();
  59. $lists->appends($data);
  60. // 获取分页显示
  61. $page = $lists->render();
  62. $this->assign('lists', $lists);
  63. $this->assign("page", $page);
  64. $this->assign('type', $this->getTypes());
  65. $this->assign('mark', $this->getMark());
  66. $this->assign('swftype', $this->getSwftype());
  67. return $this->fetch();
  68. }
  69. public function del(){
  70. $id=$this->request->param('id',0,'intval');
  71. if($id){
  72. $result=Db::name("gift")->where(["id"=>$id])->delete();
  73. if($result){
  74. $this->resetcache();
  75. $this->success('删除成功');
  76. }else{
  77. $this->error('删除失败');
  78. }
  79. }else{
  80. $this->error('数据传入失败!');
  81. }
  82. }
  83. //排序
  84. public function listorder() {
  85. $ids=$this->request->param('listorders');
  86. foreach ($ids as $key => $r) {
  87. $data['orderno'] = $r;
  88. Db::name("gift")->where(array('id' => $key))->update($data);
  89. }
  90. $status = true;
  91. if ($status) {
  92. $this->resetcache();
  93. $this->success("排序更新成功!");
  94. } else {
  95. $this->error("排序更新失败!");
  96. }
  97. }
  98. public function add(){
  99. $this->assign("type",$this->getTypes());
  100. $this->assign("mark",$this->getMark());
  101. $this->assign("swftype",$this->getSwftype());
  102. $this->assign("time",time());
  103. return $this->fetch();
  104. }
  105. public function add_post(){
  106. if($this->request->ispost()){
  107. $data=$this->request->param();
  108. $giftname=$data['giftname'];
  109. $type=$data['type'];
  110. $swftype=$data['swftype'];
  111. $needcoin=$data['needcoin'];
  112. $gifticon=$data['gifticon'];
  113. if(!$giftname){
  114. $this->error("请填写礼物名称");
  115. }
  116. if(!is_numeric($needcoin)){
  117. $this->error("礼物所需点数必须为数字");
  118. }
  119. if($needcoin<1){
  120. $this->error("礼物所需点数必须大于0");
  121. }
  122. if(floor($needcoin)!=$needcoin){
  123. $this->error("礼物所需点数必须为正整数");
  124. }
  125. /*if(!$gifticon){
  126. $this->error("请上传礼物封面");
  127. }*/
  128. if($type==1 && $swftype==1){
  129. if($_FILES){
  130. $files["file"]=$_FILES["file"];
  131. $type='image';
  132. $uploadSetting = cmf_get_upload_setting();
  133. $extensions=$uploadSetting['file_types']['file']['extensions'];
  134. $allow=explode(",",$extensions);
  135. if (!get_file_suffix($files['file']['name'],$allow)){
  136. $this->error("请上传正确格式的附件或检查上传设置中附件设置的文件类型");
  137. }
  138. $rs=adminUploadFiles($files,$type);
  139. if($rs['code']!=0){
  140. $this->error($rs['msg']);
  141. }
  142. $data['swf']=$rs['filepath'];
  143. }
  144. }
  145. unset($data['file']);
  146. $data['addtime']=time();
  147. $result=Db::name("gift")->insert($data);
  148. if($result){
  149. $this->resetcache();
  150. $this->success('添加成功');
  151. }else{
  152. $this->error('添加失败');
  153. }
  154. }
  155. }
  156. public function edit(){
  157. $id=$this->request->param('id',0,'intval');
  158. $gift=Db::name("gift")->where(['id'=>$id])->find();
  159. if(!$gift){
  160. $this->error('数据传入失败!');
  161. }
  162. $this->assign("type",$this->getTypes());
  163. $this->assign("mark",$this->getMark());
  164. $this->assign("swftype",$this->getSwftype());
  165. $this->assign("time",time());
  166. $this->assign('gift', $gift);
  167. return $this->fetch();
  168. }
  169. public function edit_post(){
  170. if($this->request->ispost()){
  171. $data=$this->request->param();
  172. $type=$data['type'];
  173. $swftype=$data['swftype'];
  174. $giftname=$data['giftname'];
  175. $needcoin=$data['needcoin'];
  176. $gifticon=$data['gifticon'];
  177. if(!$giftname){
  178. $this->error("请填写礼物名称");
  179. }
  180. if(!is_numeric($needcoin)){
  181. $this->error("礼物所需点数必须为数字");
  182. }
  183. if($needcoin<1){
  184. $this->error("礼物所需点数必须大于0");
  185. }
  186. if(floor($needcoin)!=$needcoin){
  187. $this->error("礼物所需点数必须为正整数");
  188. }
  189. if(!$gifticon){
  190. $this->error("请上传礼物封面");
  191. }
  192. if($type==1 && $swftype==1){
  193. if($_FILES){
  194. $files["file"]=$_FILES["file"];
  195. $type='image';
  196. $rs=adminUploadFiles($files,$type);
  197. //var_dump($rs);
  198. if($rs['code']!=0){
  199. $this->error($rs['msg']);
  200. }
  201. $data['swf']=$rs['filepath'];
  202. }
  203. }
  204. unset($data['file']);
  205. $result=Db::name("gift")->update($data);
  206. if($result!==false){
  207. $this->resetcache();
  208. $this->success('修改成功');
  209. }else{
  210. $this->error('修改失败');
  211. }
  212. }
  213. }
  214. public function resetcache(){
  215. $key='getGiftList';
  216. $rs=Db::name('gift')
  217. ->field("id,type,mark,giftname,giftname_en,needcoin,gifticon,swftype,swf,swftime")
  218. ->order("orderno asc,addtime desc")
  219. ->select();
  220. $rs->each(function($v,$k){
  221. $v['gifticon']=get_upload_path($v['gifticon']);
  222. $v['swf']=get_upload_path($v['swf']);
  223. return $v;
  224. });
  225. if($rs){
  226. setcaches($key,$rs);
  227. }
  228. return 1;
  229. }
  230. }