ExpressController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * 店铺物流公司管理
  4. */
  5. namespace app\admin\controller;
  6. use cmf\controller\AdminBaseController;
  7. use think\facade\Db;
  8. class ExpressController extends AdminbaseController {
  9. protected function getStatus($k=''){
  10. $status=array(
  11. '0'=>'隐藏',
  12. '1'=>'显示',
  13. );
  14. if($k===''){
  15. return $status;
  16. }
  17. return isset($status[$k])?$status[$k]:'';
  18. }
  19. /*分类列表*/
  20. function index(){
  21. $data = $this->request->param();
  22. $map=[];
  23. $keyword=isset($data['keyword']) ? $data['keyword']: '';
  24. if($keyword!=''){
  25. $map[]=['express_name','like','%'.$keyword.'%'];
  26. }
  27. $lists = Db::name("shop_express")
  28. ->where($map)
  29. ->order("list_order asc,id DESC")
  30. ->paginate(20);
  31. $lists->each(function($v,$k){
  32. $v['express_thumb']=get_upload_path($v['express_thumb']);
  33. return $v;
  34. });
  35. $lists->appends($data);
  36. $page = $lists->render();
  37. $this->assign('lists', $lists);
  38. $this->assign("page", $page);
  39. $this->assign("status", $this->getStatus());
  40. return $this->fetch();
  41. }
  42. //分类排序
  43. function listOrder() {
  44. $model = DB::name('shop_express');
  45. parent::listOrders($model);
  46. $this->resetcache();
  47. $action="更新物流公司列表顺序";
  48. setAdminLog($action);
  49. $this->success("排序更新成功!");
  50. }
  51. /*分类删除*/
  52. function del(){
  53. $id = $this->request->param('id', 0, 'intval');
  54. $rs = DB::name('shop_express')->where("id={$id}")->delete();
  55. if(!$rs){
  56. $this->error("删除失败!");
  57. }
  58. $this->resetcache();
  59. $action="删除物流公司ID: ".$id;
  60. setAdminLog($action);
  61. $this->success("删除成功!");
  62. }
  63. /*分类添加*/
  64. function add(){
  65. $this->assign("status", $this->getStatus());
  66. return $this->fetch();
  67. }
  68. /*分类添加提交*/
  69. function add_post(){
  70. if ($this->request->isPost()) {
  71. $data = $this->request->param();
  72. $express_name=$data['express_name'];
  73. if($express_name==""){
  74. $this->error("请填写快递公司名称");
  75. }
  76. $isexist=DB::name('shop_express')->where(['express_name'=>$express_name])->find();
  77. if($isexist){
  78. $this->error("快递公司名称已存在");
  79. }
  80. $express_thumb=$data['express_thumb'];
  81. if($express_thumb==""){
  82. $this->error("请上传快递公司图标");
  83. }
  84. $express_phone=$data['express_phone'];
  85. if($express_phone==""){
  86. $this->error("请填写快递公司电话");
  87. }
  88. if(!preg_match("/^\d*$/",$express_phone)){
  89. $this->error("请填写正确的快递公司电话");
  90. }
  91. $express_code=$data['express_code'];
  92. if($express_code==""){
  93. $this->error("请填写快递公司编码");
  94. }
  95. $data['addtime']=time();
  96. $id = DB::name('shop_express')->insertGetId($data);
  97. if(!$id){
  98. $this->error("添加失败!");
  99. }
  100. $action="添加物流公司ID: ".$id;
  101. setAdminLog($action);
  102. $this->resetcache();
  103. $this->success("添加成功!");
  104. }
  105. }
  106. /*分类编辑*/
  107. function edit(){
  108. $id = $this->request->param('id', 0, 'intval');
  109. $data=Db::name('shop_express')
  110. ->where("id={$id}")
  111. ->find();
  112. if(!$data){
  113. $this->error("信息错误");
  114. }
  115. $this->assign('status',$this->getStatus());
  116. $this->assign('data', $data);
  117. return $this->fetch();
  118. }
  119. /*分类编辑提交*/
  120. function edit_post(){
  121. if ($this->request->isPost()){
  122. $data = $this->request->param();
  123. $express_name=$data['express_name'];
  124. $id=$data['id'];
  125. if($express_name==""){
  126. $this->error("请填写物流公司名称");
  127. }
  128. $isexist=DB::name('shop_express')->where([['id','<>',$id],['express_name','=',$express_name]])->find();
  129. if($isexist){
  130. $this->error("物流公司名称已存在");
  131. }
  132. $express_thumb=$data['express_thumb'];
  133. if($express_thumb==""){
  134. $this->error("请上传物流公司图标");
  135. }
  136. $express_phone=$data['express_phone'];
  137. if($express_phone==""){
  138. $this->error("请填写物流公司电话");
  139. }
  140. $express_code=$data['express_code'];
  141. if($express_code==""){
  142. $this->error("请填写快递公司编码");
  143. }
  144. $data['edittime']=time();
  145. $rs = DB::name('shop_express')->update($data);
  146. if($rs===false){
  147. $this->error("修改失败!");
  148. }
  149. $this->resetcache();
  150. $action="编辑物流公司ID: ".$data['id'];
  151. setAdminLog($action);
  152. $this->success("编辑成功!");
  153. }
  154. }
  155. //获取物流公司编码列表
  156. function expresslist(){
  157. $json_string=file_get_contents(CMF_ROOT."/public/static/express.json");
  158. $expresslist = json_decode($json_string, true);
  159. $lists=$expresslist['data'];
  160. $keyword=$this->request->param("keyword");
  161. if($keyword){
  162. $newlist=[];
  163. foreach ($lists as $k => $v) {
  164. if(strpos($v['name'],$keyword)!==false){
  165. $newlist[]=$v;
  166. }
  167. }
  168. $lists=$newlist;
  169. }
  170. $this->assign('lists',$lists);
  171. return $this->fetch();
  172. }
  173. // 写入物流信息缓存
  174. function resetcache(){
  175. $key='getExpressList';
  176. $rs=DB::name('shop_express')
  177. ->field("id,express_name,express_phone,express_thumb")
  178. ->where('express_status=1')
  179. ->order("list_order asc,id desc")
  180. ->select();
  181. if($rs){
  182. setcaches($key,$rs);
  183. }
  184. return 1;
  185. }
  186. }