Platform.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\PlatformSource;
  4. use app\admin\model\UserPlatform;
  5. use think\App;
  6. use think\facade\Validate;
  7. class Platform extends Base{
  8. public function __construct(App $app) {
  9. parent::__construct($app);
  10. $this->model=new \app\admin\model\Platform();
  11. }
  12. /**@param platform_name 平台名称
  13. * @param use_type 对接类型
  14. * @param platform_type 对接平台
  15. * 0:无
  16. * 1:有赞平台
  17. * 2:其他
  18. * @param is_select_pay_rate 是否开启支付渠道
  19. * @param desc 权重
  20. * @param status 状态
  21. * @param pay_list 渠道配置
  22. * @return \think\response\Json
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function create(){
  28. $param =$this->request->only([
  29. 'platform_name'=>'',
  30. 'platform_type'=>'',
  31. 'use_type'=>'',
  32. 'is_select_pay_rate'=>'',
  33. 'desc'=>'',
  34. "status"=>0,
  35. 'is_del'=>'0',
  36. 'pay_list' => []
  37. ],'post','trim');
  38. $valid = Validate::rule([
  39. 'platform_name|平台名称' => 'require|max:255|unique:app\admin\model\Platform,platform_name^is_del',
  40. 'platform_type|对接平台' => 'require|number|in:0,1,2',
  41. 'use_type|对接类型' => 'require|number|in:0,1,2',
  42. 'is_select_pay_rate|是否开启支付渠道' => 'require|number|in:0,1',
  43. 'desc|权重' => 'float',
  44. 'status|状态' => 'number|in:0,1',
  45. 'pay_list|渠道配置' => 'requireIf:is_select_pay_rate,1|array|max:100'
  46. ]);
  47. if(!$valid->check($param)) return error($valid->getError());
  48. $channelArr=[];
  49. if($param['is_select_pay_rate']==1){
  50. $pay_list = $param['pay_list'];
  51. foreach ($pay_list as $k=>$v){
  52. $channelArr[]=[
  53. "channel_id"=>$v['channel_id'],
  54. "platform_id"=>0,
  55. 'apply_id' => $this->uid,
  56. 'apply_name' => $this->uname,
  57. 'weight' => $k++,
  58. ];
  59. }
  60. }
  61. $this->model->startTrans();
  62. try{
  63. $platCode=makeNo("PT");
  64. $param['platform_code']=$platCode;
  65. $param['createrid']=$this->uid;
  66. $param['creater']=$this->uname;
  67. $plat=$this->model->create($param);
  68. if($plat===false)throw new \Exception("创建失败");
  69. if($param['is_select_pay_rate']==1){
  70. $channelModel=new \app\admin\model\PlatformChannel();
  71. $channelArr=array_map(function($v)use($plat){
  72. $v['platform_id']=$plat->id;
  73. return $v;
  74. },$channelArr);
  75. $channle= $channelModel->saveAll($channelArr);
  76. if(!$channle)throw new \Exception("创建失败");
  77. }
  78. $this->model->commit();
  79. }catch (\Exception $e){
  80. $this->model->rollback();
  81. return error($e->getMessage());
  82. }
  83. if($param['platform_type']==1) event('PlatCat',['type'=>'add','info'=>'plat','data'=>$plat]);
  84. return success("创建成功");
  85. }
  86. /**@param platform_name 平台名称
  87. * @param platform_type 对接平台
  88. * 0:无
  89. * 1:有赞平台
  90. * 2:其他
  91. * @param is_select_pay_rate 是否开启支付渠道
  92. * @param desc 权重
  93. * @param status 状态
  94. * @param pay_list 渠道配置 **/
  95. public function save(){
  96. $param =$this->request->only([
  97. 'id'=>'',
  98. 'platform_name'=>'',
  99. 'platform_type'=>'',
  100. 'use_type'=>'',
  101. 'is_select_pay_rate'=>'',
  102. 'desc'=>'',
  103. "status"=>0,
  104. 'is_del'=>'0',
  105. 'pay_list' => []
  106. ],'post','trim');
  107. $valid = Validate::rule([
  108. 'id|平台id' => 'require|number',
  109. 'platform_name|平台名称' => 'require|max:255|unique:app\admin\model\Platform,platform_name^is_del',
  110. 'platform_type|对接平台' => 'require|number|in:0,1,2',
  111. 'use_type|对接类型' => 'require|number|in:0,1,2',
  112. 'is_select_pay_rate|是否开启支付渠道' => 'require|number|in:0,1',
  113. 'desc|权重' => 'float',
  114. 'status|状态' => 'number|in:0,1',
  115. 'pay_list|渠道配置' => 'requireIf:is_select_pay_rate,1|array|max:100'
  116. ]);
  117. if(!$valid->check($param)) return error($valid->getError());
  118. $info=$this->model->findOrEmpty($param['id']);
  119. if($info->isEmpty())return error("平台不存在");
  120. if($info->is_del==1)return error("平台已删除");
  121. $channelArr=[];
  122. if($param['is_select_pay_rate']==1){
  123. $pay_list = $param['pay_list'];
  124. $weight=0;
  125. foreach ($pay_list as $k=>$v){
  126. $channelArr[]=[
  127. "id"=>$v["id"]??null,
  128. 'platform_id' => $info->id,
  129. 'channel_id' => $v['channel_id'],
  130. 'apply_id' => $v['apply_id']??$this->uid,
  131. 'apply_name' => $v['apply_name']??$this->uname,
  132. 'weight' =>$weight++,
  133. 'is_del' => $pay['is_del']??0,
  134. ];
  135. }
  136. }
  137. $this->model->startTrans();
  138. try{
  139. $plat=$info->save($param);
  140. if($plat===false)throw new \Exception("修改失败");
  141. if($param['is_select_pay_rate']==1){
  142. $channelModel=new \app\admin\model\PlatformChannel();
  143. $channel = $channelModel->saveAll($channelArr);
  144. if(!$channel)throw new \Exception("修改失败");
  145. }
  146. $this->model->commit();
  147. }catch (\Exception $e){
  148. $this->model->rollback();
  149. return error($e->getMessage());
  150. }
  151. if($param['platform_type']==1) event('PlatCat',['type'=>'save','info'=>'plat','data'=>$info]);
  152. return success("修改成功");
  153. }
  154. /**@param platform_name 平台名称
  155. * @param platform_type 对接平台
  156. * 0:无
  157. * 1:有赞平台
  158. * 2:其他
  159. *@param status 状态
  160. * 0:禁用
  161. * 1:启用
  162. * @param createrid 创建人id
  163. * @param start 开始时间
  164. * @param end 结束时间
  165. */
  166. public function list(){
  167. $param =$this->request->only([
  168. 'platform_name'=>'',
  169. 'platform_type'=>'',
  170. "status"=>0,
  171. 'start'=>'',
  172. 'end'=>'',
  173. "createrid"=>"",
  174. "is_show"=>1,
  175. 'page'=>1,
  176. 'size'=>10
  177. ],'post','trim');
  178. $where=[["is_del","=",0]];
  179. if($param['platform_name']!=="") $where[]=["platform_name","like","%".$param['platform_name']."%"];
  180. if($param['platform_type']!=="") $where[]=["platform_type","=",$param['platform_type']];
  181. if($param['status']!=="") $where[]=["status","=",$param['status']];
  182. if($param['createrid']!=="") $where[]=["createrid","=",$param['createrid']];
  183. if($param['start']!=="" && $param['end']!==""){
  184. $where[]=["createtime","between",[startTime($param['start']),endTime($param['end'])]];
  185. }elseif($param['start']!==""){
  186. $where[]=["createtime",">=",startTime($param['start'])];
  187. }elseif($param['end']!==""){
  188. $where[]=["createtime","<=",endTime($param['end'])];
  189. }
  190. if($param['is_show']==1){
  191. $role = $this->checkRole();
  192. if (!empty($role['platform'])) $where[] = ['id', 'in', $role['platform']];
  193. }
  194. $list = $this->model->where($where)->order(["desc"=>"desc",'id' => 'desc'])
  195. ->paginate(["page"=>$param['page'],"list_rows"=>$param['size']]);
  196. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  197. }
  198. public function info(){
  199. $id = $this->request->param('id','', 'int');
  200. if(empty($id)) return error("参数错误");
  201. $info = $this->model->with(["platChannel"=>["channel"]])->findOrEmpty($id);
  202. if($info->isEmpty()) return error("平台不存在");
  203. if($info->is_del==1) return error("平台已删除");
  204. return success("获取成功",$info);
  205. }
  206. public function delete(){
  207. $id = $this->request->param('id','', 'int');
  208. if(empty($id)) return error("参数错误");
  209. $info = $this->model->findOrEmpty($id);
  210. if($info->isEmpty()) return error("平台不存在");
  211. if($info->is_del==1) return error("平台已删除");
  212. $info->is_del=1;
  213. if(!$info->save()) return error("删除失败");
  214. if($info->platform_type==1) event('PlatCat',['type'=>'save','info'=>'plat','data'=>$info]);
  215. return success("删除成功");
  216. }
  217. public function status(){
  218. $param =$this->request->only([
  219. 'id'=>'',
  220. 'status'=>'',
  221. ],'post','trim');
  222. $valid = Validate::rule([
  223. 'id|平台id' => 'require|number',
  224. 'status|状态' => 'require|number|in:0,1',
  225. ]);
  226. if(!$valid->check($param))return error($valid->getError());
  227. $info = $this->model->findOrEmpty($param['id']);
  228. if($info->isEmpty()) return error("平台不存在");
  229. if($info->is_del==1) return error("平台已删除");
  230. $info->status=$param['status'];
  231. if(!$info->save()) return error("修改失败");
  232. if($info->platform_type==1) event('PlatCat',['type'=>'save','info'=>'plat','data'=>$info]);
  233. return success("修改成功");
  234. }
  235. /**@param platform_id 平台id
  236. * @param source 渠道名称
  237. * **/
  238. public function platformSourceCreate(){
  239. $param=$this->request->param(['platform_id','source'],'post','trim');
  240. $valid=Validate::rule(['platform_id|平台id'=>'require|number|gt:0','source|渠道名称'=>'require|max:255']);
  241. if($valid->check($param)==false)$this->error($valid->getError());
  242. $data=[
  243. 'platform_id'=>$param['platform_id'],
  244. 'source'=>$param['source'],
  245. 'apply_id'=>$this->uid,
  246. 'apply_name'=>$this->uname];
  247. $create=PlatformSource::create($data);
  248. if($create->isEmpty())return error('创建失败');
  249. return success('创建成功');
  250. }
  251. public function platformSourceDelete(){
  252. $param=$this->request->param(['id'],'post','trim');
  253. $info =PlatformSource::where('id',$param['id'])->findOrEmpty();
  254. if($info->isEmpty())return error('未找到数据');
  255. $info['is_del']=1;
  256. $info['updatetime']=date('Y-m-d H:i:s');
  257. if(!$info->save())return error('删除失败');
  258. return success('删除成功');
  259. }
  260. public function platformSourceList(){
  261. $param=$this->request->param(['platform_id'],'post','trim');
  262. $valid=Validate::rule(['platform_id|平台id'=>'require|number|gt:0']);
  263. if($valid->check($param)==false)$this->error($valid->getError());
  264. $list=PlatformSource::where('platform_id',$param['platform_id'])->where('is_del',0)->select();
  265. return success('获取成功',$list);
  266. }
  267. public function platformList(){
  268. $param = $this->request->param(['platform_name'=>"",'platform_type'=>'','platform_code'=>'','page'=>'1','size'=>'10','use_type'=>''],'post','trim');
  269. $where=[['is_del','=',0]];
  270. if($param['platform_name']!=="") $where[]=["platform_name","like","%".$param['platform_name']."%"];
  271. if($param['platform_type']!=="") $where[]=["platform_type","=",$param['platform_type']];
  272. if($param['platform_code']!=="") $where[]=["platform_code","like","%".$param['platform_code']."%"];
  273. if($param['use_type']!=="") $where[]=["use_type","=",$param['use_type']];
  274. if($this->level==2){
  275. $platrform = UserPlatform::where(["uid"=>$this->uid,"is_del"=>0])->findOrEmpty();
  276. $where[]=['id','in',$platrform->platform??[]];
  277. }
  278. $list = $this->model->where($where)->order(["desc"=>"desc",'id' => 'desc'])
  279. ->paginate(["page"=>$param['page'],"list_rows"=>$param['size']]);
  280. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  281. }
  282. public function platformCreate(){
  283. $param = $this->request->only(['uid', 'platform_id'], 'post', 'trim');
  284. $val = Validate::rule([
  285. 'uid|用户ID' => 'require|number|gt:0',
  286. 'platform_id|平台id集合' => 'require|array|max:9999'
  287. ]);
  288. if ($val->check($param) == false) return error($val->getError());
  289. $platform = $this->model->where('id', 'in', $param['platform_id'])->where('is_del',0)->select();
  290. if ($platform->isEmpty()) return error('平台不存在');
  291. $info = UserPlatform::where(['uid' => $param['uid'], 'is_del' => 0])->findOrEmpty();
  292. $info->platform = array_column($platform,"id");
  293. if(!$info->save()) return error('创建失败');
  294. return success('创建成功');
  295. }
  296. }