Wxpay.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\main\constants\PayConstants;
  5. use app\main\service\AdminService;
  6. use app\main\service\FufenService;
  7. /**
  8. * 微信支付配置
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Wxpay extends Backend
  13. {
  14. protected $noNeedLogin = ['checkhostunique','checkappidunique'];
  15. /**
  16. * Wxpay模型对象
  17. */
  18. protected $model = null;
  19. protected $relationSearch = true;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = model('Wxpay');
  24. $this->view->assign("isdefaultList", $this->model->getIsdefaultList());
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. $this->view->assign("fufenList", $this->model->getFufenList());
  27. $this->view->assign('paymentMethodList',$this->model->getPaymentMethodList());
  28. $this->assignconfig('paymentMethodList', json_encode($this->model->getPaymentMethodList()));
  29. $this->view->assign('isclosureList',$this->model->getIsclosureList());
  30. $this->view->assign('quartetIsSplit',$this->model->getQuartetIsSplit());
  31. $this->view->assign('dzPalmpaySubAccountList',$this->model->getDzPamlpaySubaccountList()); //获取四方支付分账点众子账户列表
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. */
  41. public function index()
  42. {
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags']);
  45. if ($this->request->isAjax())
  46. {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('pkey_name'))
  49. {
  50. return $this->selectpage();
  51. }
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $total = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->with("platform")
  59. ->where($where)
  60. ->order($sort, $order)
  61. ->limit($offset, $limit)
  62. ->select();
  63. $result = array("total" => $total, "rows" => $list);
  64. return json($result);
  65. }
  66. $this->assignconfig('business',PayConstants::getBusinessLine());
  67. //拉取所有的appid
  68. $this->assignconfig("appidList", json_encode($this->model->column('appid')));
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 添加
  73. */
  74. public function add()
  75. {
  76. if ($this->request->isPost())
  77. {
  78. $params = $this->request->post("row/a");
  79. if ($params)
  80. {
  81. $params['business_line'] = implode(',',$params['business_line']);
  82. /*
  83. * 已经弃用,如果为了兼容老版可取消注释
  84. foreach ($params as $k => &$v)
  85. {
  86. $v = is_array($v) ? implode(',', $v) : $v;
  87. }
  88. */
  89. if ($this->dataLimit)
  90. {
  91. $params[$this->dataLimitField] = $this->auth->id;
  92. }
  93. try
  94. {
  95. //是否采用模型验证
  96. if ($this->modelValidate)
  97. {
  98. $name = basename(str_replace('\\', '/', get_class($this->model)));
  99. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  100. $this->model->validate($validate);
  101. }
  102. if($params['status'] == 1){
  103. if($params['isdefault'] == 1){
  104. $data['isdefault'] = 0;
  105. $defaultRow = $this->model->where(["isdefault"=>1,"platform_id"=>$params['platform_id']])->find();
  106. if($defaultRow) {
  107. $defaultRow->allowField(true)->save($data);
  108. }
  109. $this->model->delCacheAll($defaultRow['id']);
  110. $this->model->delCacheByPlatformId($params['platform_id']);
  111. }
  112. }
  113. $result = $this->model->allowField(true)->save($params);
  114. if ($result !== false) {
  115. $this->model->delCachePayHost();
  116. if($payHosts = $this->model->getHosts()){
  117. foreach($payHosts as $val){
  118. $this->model->delCacheByHost($val);
  119. }
  120. }
  121. $this->success();
  122. }
  123. else
  124. {
  125. $this->error($this->model->getError());
  126. }
  127. }
  128. catch (\think\exception\PDOException $e)
  129. {
  130. $this->error($e->getMessage());
  131. }
  132. }
  133. $this->error(__('Parameter %s can not be empty', ''));
  134. }
  135. $this->assign('business',PayConstants::getBusinessLine());
  136. return $this->view->fetch();
  137. }
  138. /**
  139. * 编辑
  140. */
  141. public function edit($ids = NULL)
  142. {
  143. $row = $this->model->get($ids);
  144. if (!$row)
  145. $this->error(__('No Results were found'));
  146. $adminIds = $this->getDataLimitAdminIds();
  147. if (is_array($adminIds))
  148. {
  149. if (!in_array($row[$this->dataLimitField], $adminIds))
  150. {
  151. $this->error(__('You have no permission'));
  152. }
  153. }
  154. if ($this->request->isPost())
  155. {
  156. $params = $this->request->post("row/a");
  157. if ($params)
  158. {
  159. /*
  160. * 已经弃用,如果为了兼容老版可取消注释
  161. foreach ($params as $k => &$v)
  162. {
  163. $v = is_array($v) ? implode(',', $v) : $v;
  164. }
  165. */
  166. $params['business_line'] = implode(',',$params['business_line']);
  167. try
  168. {
  169. //是否采用模型验证
  170. if ($this->modelValidate)
  171. {
  172. $name = basename(str_replace('\\', '/', get_class($this->model)));
  173. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  174. $row->validate($validate);
  175. }
  176. if($params['platform_id'] != $row['platform_id'] && $params['isdefault'] ==1 && $row['isdefault'] == 1){
  177. $this->error("默认项不可更改平台");
  178. }
  179. //复粉支付修改状态时清空缓存
  180. if ($row['fufen'] && $params['status'] != $row['status']) {
  181. FufenService::instance()->clearChannelCacheByWxpayId($row['id']);
  182. }
  183. if($params['status'] != $row['status'] || $params['isdefault']!=$row['isdefault']){
  184. if($params['status'] == 1 && $params['isdefault'] == 1){
  185. $data['isdefault'] = 0;
  186. $defaultRow = $this->model->where(["isdefault" => 1, "platform_id" => $params['platform_id']])->where("id","not in",$ids)->find();
  187. if($defaultRow) {
  188. $defaultRow->allowField(true)->save($data);
  189. }
  190. $this->model->delCacheAll($defaultRow['id']);
  191. $this->model->delCacheByPlatformId($params['platform_id']);
  192. }elseif ($params['status'] == 0 && $params['isdefault'] == 1 && $row['isdefault'] == 1){
  193. //默认可以关闭 $this->error("默认项不可关闭");
  194. }elseif ($params['isdefault'] == 0 && $row['isdefault'] == 1){
  195. $this->error("默认项不可更改为非默认");
  196. }
  197. if ($params['status'] == 0 && $params['isdefault'] == 1) {
  198. //默认可以关闭 $this->error("变更默认项时,状态不可关闭");
  199. }
  200. if($params['status'] == 0 && $row['status'] == 1){ //关闭支付域名
  201. //检查是否有渠道正在使用
  202. if(model('AdminConfig')->where('wxpay_id',$row['id'])->find()){
  203. $this->error('有渠道的支付域名正在使用中,不能关闭');
  204. }
  205. if(model('AdminConfig')->where('menuwxpay_id',$row['id'])->find()){
  206. $this->error('有渠道的菜单支付域名正在使用中,不能关闭');
  207. }
  208. }
  209. }
  210. $result = $row->allowField(true)->save($params);
  211. if ($result !== false) {
  212. if($params['status'] == 1 && $params['isdefault'] == 1){
  213. $this->model->delCacheByPlatformId($params['platform_id']);
  214. }
  215. $this->model->delCacheAll($row['id']);
  216. $this->success();
  217. }
  218. else
  219. {
  220. $this->error($row->getError());
  221. }
  222. }
  223. catch (\think\exception\PDOException $e)
  224. {
  225. $this->error($e->getMessage());
  226. }
  227. }
  228. $this->error(__('Parameter %s can not be empty', ''));
  229. }
  230. $row['business_line'] = explode(',',$row['business_line']);
  231. if ($row['fufen']) {
  232. $row['fufen_disabled'] = (bool)FufenService::instance()->getFufenGroupModel()->where('find_in_set(' . $row['id'] . ',wxpay_ids)')->value('id');
  233. $row['fufen_tip'] = '复粉在用';
  234. } else {
  235. $row['fufen_disabled'] = (bool)AdminService::instance()->getAdminConfigModel()->where('wxpay_id', $ids)->whereOr('menuwxpay_id', $ids)->value('admin_id');
  236. $row['fufen_tip'] = '渠道在用';
  237. }
  238. $this->assign('business',PayConstants::getBusinessLine());
  239. $this->view->assign("row", $row);
  240. return $this->view->fetch();
  241. }
  242. /**
  243. * 检查支付域名是否唯一
  244. * @return \think\response\Json
  245. */
  246. public function checkhostunique(){
  247. $params = $this->request->param('row/a');
  248. if(isset($params['pay_host']) && !empty($params['pay_host'])){
  249. $map['pay_host'] = $params['pay_host'];
  250. //编辑检查
  251. if($this->request->has('id')){
  252. if($id = $this->request->get('id')){
  253. $map['id'] = ['neq',$id];
  254. }
  255. }
  256. if($this->model->where($map)->find()){
  257. return json(['code'=>600,'msg'=>'该支付域名已存在,请不要重复使用']);
  258. }
  259. return json(['code'=>200,'data'=>['ok'=>'支付域名可以使用']]);
  260. }
  261. return json(['code'=>600,'msg'=>'参数错误']);
  262. }
  263. /**
  264. * 检查支付Appid是否唯一
  265. * @return \think\response\Json
  266. */
  267. public function checkappidunique(){
  268. $params = $this->request->param('row/a');
  269. if(isset($params['appid']) && !empty($params['appid'])){
  270. $map['appid'] = $params['appid'];
  271. //编辑检查
  272. if($this->request->has('id')){
  273. if($id = $this->request->get('id')){
  274. $map['id'] = ['neq',$id];
  275. }
  276. }
  277. if($this->model->where($map)->find()){
  278. return json(['code'=>600,'msg'=>'该AppID已存在,请不要重复使用']);
  279. }
  280. return json(['code'=>200,'data'=>['ok'=>'AppID可以使用']]);
  281. }
  282. return json(['code'=>600,'msg'=>'参数错误']);
  283. }
  284. }