merchant.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. <?php
  2. /**
  3. * 机构管理界面
  4. *
  5. **by 好商城V3 www.33hao.com 运营版*/
  6. defined('InShopNC') or exit('Access Invalid!');
  7. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  8. class merchantControl extends SystemControl
  9. {
  10. const EXPORT_SIZE = 1000;
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. Language::read('merchant');
  15. }
  16. /**
  17. * 机构列表
  18. */
  19. public function merchantOp()
  20. {
  21. $model_merchant = Model('merchant');
  22. if (trim($_GET['merchant_name']) != '') {
  23. $condition['name'] = ['like', '%' . $_GET['merchant_name'] . '%'];
  24. Tpl::output('merchant_name', $_GET['merchant_name']);
  25. }
  26. $merchant_list = $model_merchant->getMerchantList($condition, 25, 'mchid desc');
  27. $merchant_state_text = ['使用中', '已禁用'];
  28. Tpl::output('merchant_state_text', $merchant_state_text);
  29. Tpl::output('merchant_list', $merchant_list);
  30. Tpl::output('page', $model_merchant->showpage('2'));
  31. Tpl::showpage('merchant.index');
  32. }
  33. public function changeStateOp()
  34. {
  35. $mchid = intval($_GET['mchid']);
  36. $state = intval($_GET['state']);
  37. $model_merchant = Model('merchant');
  38. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  39. if (empty($merchant_info) || !in_array($state, [1, 2])) {
  40. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  41. }
  42. $resp = $model_merchant->editMerchant(['merchant_state' => $state], ['mchid' => $mchid]);
  43. if (!$resp) {
  44. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  45. }
  46. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  47. }
  48. /**
  49. * 新增机构
  50. */
  51. public function merchant_addOp()
  52. {
  53. if (chksubmit()) {
  54. /**
  55. * 验证
  56. */
  57. $obj_validate = new Validator();
  58. $obj_validate->validateparam = [
  59. ["input" => $_POST["name"], "require" => "true", "message" => '机构账号不能为空'],
  60. ["input" => $_POST["company_name"], "require" => "true", "message" => '机构公司名称不能为空'],
  61. ["input" => $_POST["password"], "require" => "true", "message" => '密码不能为空']
  62. ];
  63. $error = $obj_validate->validate();
  64. if ($error != '') {
  65. showMessage($error);
  66. } else {
  67. $name = trim($_POST['name']);
  68. $company_name = trim($_POST['company_name']);
  69. $pwd = trim($_POST['password']);
  70. $alarm_amount = $_POST['alarm_amount'] ?? 0;
  71. $model_merchant = Model('merchant');
  72. $model_member = Model('member');
  73. try {
  74. $trans = new trans_wapper($model_merchant, __METHOD__);
  75. $insert_member['member_name'] = md5($name . time());
  76. $insert_member['member_passwd'] = $pwd;
  77. $insert_id = $model_member->addMember($insert_member);
  78. if ($insert_id == false) {
  79. $trans->rollback();
  80. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  81. }
  82. $insert_array['name'] = $name;
  83. $insert_array['company_name'] = $company_name;
  84. $insert_array['org_pwd'] = $pwd;
  85. $insert_array['password'] = md5($pwd);
  86. $insert_array['admin_id'] = trim($insert_id);
  87. $insert_array['alarm_amount'] = $alarm_amount;
  88. $result = $model_merchant->addMerchant($insert_array);
  89. if ($result) {
  90. $url = [
  91. [
  92. 'url' => 'index.php?act=merchant&op=merchant',
  93. 'msg' => '返回机构列表',
  94. ],
  95. [
  96. 'url' => 'index.php?act=merchant&op=merchant_add',
  97. 'msg' => '继续新增机构',
  98. ],
  99. ];
  100. $this->log('添加机构:' . '[ ' . $_POST['name'] . ']', 1);
  101. showMessage('机构添加成功', $url);
  102. } else {
  103. showMessage('机构添加失败');
  104. }
  105. $trans->commit();
  106. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  107. } catch (Exception $e) {
  108. $trans->rollback();
  109. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  110. }
  111. }
  112. }
  113. Tpl::showpage('merchant.add');
  114. }
  115. public function merchant_editOp()
  116. {
  117. $mchid = $_GET['mchid'] ?? $_POST['mchid'];
  118. $model_merchant = Model('merchant');
  119. $merchant = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  120. if(empty($merchant)){
  121. showMessage('机构信息有误');
  122. }
  123. if (chksubmit()) {
  124. $update['company_name'] = trim($_POST['company_name']);
  125. $update['org_pwd'] = trim($_POST['password']);
  126. $update['password'] = md5($update['org_pwd']);
  127. $update['alarm_amount'] = $_POST['alarm_amount'] ?? 0;
  128. $result = $model_merchant->editMerchant($update,['mchid' => $mchid]);
  129. if ($result) {
  130. $this->log('编辑机构:' . '[ ' . $merchant['name'] . ']', 1);
  131. showMessage('机构编辑成功', 'index.php?act=merchant&op=merchant');
  132. } else {
  133. showMessage('机构添加失败', 'index.php?act=merchant&op=merchant');
  134. }
  135. }
  136. Tpl::output('merchant', $merchant);
  137. Tpl::showpage('merchant.edit');
  138. }
  139. public function orderDayLimitOp()
  140. {
  141. if (chksubmit()) {
  142. $mchids = $_POST['mchid'];
  143. $petrochinas = $_POST['petrochina'];
  144. $sinopecs = $_POST['sinopec'];
  145. foreach ($mchids as $key => $mchid) {
  146. $data['mchid'] = $mchid;
  147. $data['petrochina'] = ['100' => $petrochinas[100][$key] , '200' => $petrochinas[200][$key] , '500' => $petrochinas[500][$key] , '1000' => $petrochinas[1000][$key]];
  148. $data['sinopec'] = ['100' => $sinopecs[100][$key] , '200' => $sinopecs[200][$key] , '500' => $sinopecs[500][$key] , '1000' => $sinopecs[1000][$key]];
  149. $data['time'] = time();
  150. $result[$mchid] = $data;
  151. }
  152. wcache('order_limit' , ['data' => serialize($result)] , 'merchant-');
  153. showMessage('编辑成功', 'index.php?act=merchant&op=merchant');
  154. }
  155. $order_limit = rcache('order_limit' , 'merchant-');
  156. if(empty($order_limit)){
  157. $order_limit = [];
  158. }else{
  159. $order_limit = unserialize($order_limit['data']);
  160. }
  161. $default = function ($merchant) {
  162. $amount = [
  163. '100' => -1,
  164. '200' => -1,
  165. '500' => -1,
  166. '1000' => -1
  167. ];
  168. $data['mchid'] = $merchant['mchid'];
  169. $data['petrochina'] = $data['sinopec'] = $amount;
  170. return $data;
  171. };
  172. $model_merchant = model('merchant');
  173. $merchant_list = $model_merchant->getMerchantList([], 1000, 'mchid asc' , 'mchid,company_name');
  174. foreach ($merchant_list as $merchant)
  175. {
  176. if(array_key_exists($merchant['mchid'] , $order_limit)) {
  177. $result[$merchant['mchid']] = $order_limit[$merchant['mchid']];
  178. }else{
  179. $result[$merchant['mchid']] = $default($merchant);
  180. }
  181. $result[$merchant['mchid']]['company_name'] = $merchant['company_name'];
  182. }
  183. Tpl::output('data', $result);
  184. Tpl::showpage('merchant.orderlimit');
  185. }
  186. public function priceOp()
  187. {
  188. if (chksubmit())
  189. {
  190. $mchid = $_POST['mchid'];
  191. //合并表单数据
  192. $card_types = $_POST['cardtype'];
  193. $specs = $_POST['spec'];
  194. $prices = $_POST['price'];
  195. foreach ($card_types as $key => $card_type) {
  196. $data['card_type'] = $card_type;
  197. $data['spec'] = intval($specs[$key]);
  198. $data['price'] = ncPriceFormat($prices[$key]);
  199. $params[] = $data;
  200. }
  201. foreach ($params as $param)
  202. {
  203. if($param['price'] > 0) {
  204. $insert['mchid'] = $mchid;
  205. $insert['spec'] = $param['spec'];
  206. $insert['price'] = $param['price'];
  207. $insert['card_types'] = $param['card_type'];
  208. $inserts[] = $insert;
  209. }
  210. }
  211. if(empty($inserts)) {
  212. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  213. }
  214. try {
  215. $model_merchant = Model('merchant');
  216. $trans = new trans_wapper($model_merchant, __METHOD__);
  217. //删除旧费率
  218. $model_merchant->delPrices($mchid);
  219. //更新新费率
  220. $model_merchant->insertPrices($inserts);
  221. $trans->commit();
  222. showMessage('操作成功', 'index.php?act=merchant&op=merchant');
  223. } catch (Exception $e) {
  224. $trans->rollback();
  225. showMessage('操作失败', 'index.php?act=merchant&op=merchant', 'html', 'error');
  226. }
  227. }
  228. $mchid = $_GET['mchid'] ?? 0;
  229. $goods = $this->GoodsFormat($mchid);
  230. Tpl::output('goods', $goods);
  231. Tpl::showpage('merchant.price');
  232. }
  233. public function getPriceOp()
  234. {
  235. $mchid = $_GET['mchid'] ?? 0;
  236. $model_merchant = Model('merchant');
  237. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid])->select();
  238. $check = true;
  239. if (empty($items)) {
  240. $check = false;
  241. }
  242. $goods = $this->GoodsFormat($mchid);
  243. echo json_encode(['check' => $check, 'data' => $goods]);
  244. }
  245. private function GoodsFormat($mchid)
  246. {
  247. $all_spector = function ()
  248. {
  249. global $config;
  250. $refill_specs = $config['refill_specs'];
  251. $all_type_specs = [];
  252. foreach ($refill_specs as $scard_type => $specs)
  253. {
  254. if ($scard_type == 'petrochina') { //中石油
  255. $card_type = 1;
  256. } elseif ($scard_type == 'sinopec') { //中石化
  257. $card_type = 2;
  258. } elseif ($scard_type == 'chinamobile') { //中国移动
  259. $card_type = 4;
  260. } elseif ($scard_type == 'chinaunicom') { //中国联通
  261. $card_type = 5;
  262. } elseif ($scard_type == 'chinatelecom') { //中国电信
  263. $card_type = 6;
  264. } else {
  265. continue;
  266. }
  267. $all_type_specs[$card_type] = $specs;
  268. }
  269. $result =[];
  270. foreach ($all_type_specs as $card_type => $specs) {
  271. foreach ($specs as $spec){
  272. $result["{$card_type}-{$spec}"] = ['card_type' => $card_type , 'spec' => $spec];
  273. }
  274. }
  275. return $result;
  276. };
  277. $merch_spector = function ($mchid)
  278. {
  279. $model_merchant = Model('merchant');
  280. $items = $model_merchant->table('merchant_price')->where(['mchid' => $mchid])->select();
  281. $result = [];
  282. foreach ($items as $item)
  283. {
  284. $card_types = explode(',' , $item['card_types']);
  285. foreach ($card_types as $card_type) {
  286. $value['spec'] = intval($item['spec']);
  287. $result["{$card_type}-{$value['spec']}"] = ['card_type'=>$card_type,
  288. 'spec' => intval($item['spec']),
  289. 'price' => $item['price']];
  290. }
  291. }
  292. return $result;
  293. };
  294. $all_cardtype_specs = $all_spector();
  295. $merch_cardtype_specs = $merch_spector($mchid);
  296. $merger = function ($all_specs,$mech_specs)
  297. {
  298. $result = [];
  299. foreach ($all_specs as $cardtype_spec => $value)
  300. {
  301. $card_name = $this->scard_type($value['card_type']);
  302. $data = [];
  303. $data['goods_name'] = $card_name;
  304. $data['card_type'] = $value['card_type'];
  305. $data['spec'] = $value['spec'];
  306. if(array_key_exists($cardtype_spec , $mech_specs)) {
  307. $data['price'] = $mech_specs[$cardtype_spec]['price'];
  308. } else {
  309. $data['price'] = 0;
  310. }
  311. $result[] = $data;
  312. }
  313. return $result;
  314. };
  315. $result = $merger($all_cardtype_specs,$merch_cardtype_specs);
  316. return $result;
  317. }
  318. public function check_merchantOp()
  319. {
  320. $mchid = trim($_GET['mchid']);
  321. if (!$mchid) {
  322. echo '';
  323. die;
  324. }
  325. $model_merchant = Model('merchant');
  326. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], '*');
  327. if (empty($merchant_info)) {
  328. echo '';
  329. die;
  330. }
  331. echo 'true';
  332. }
  333. /**
  334. * ajax操作
  335. */
  336. public function ajaxOp()
  337. {
  338. $model_merchant = Model('merchant');
  339. switch ($_GET['branch']) {
  340. /**
  341. * 验证机构名称是否重复
  342. */
  343. case 'check_mch_name':
  344. $condition['name'] = $_GET['name'];
  345. $list = $model_merchant->getMerchantInfo($condition);
  346. if (empty($list)) {
  347. echo 'true';
  348. } else {
  349. echo 'false';
  350. }
  351. exit;
  352. /**
  353. * 验证机构是否存在
  354. */
  355. case 'check_merchant':
  356. $condition['mchid'] = intval($_GET['mchid']);
  357. $list = $model_merchant->getMerchantInfo($condition);
  358. if (!empty($list)) {
  359. echo 'true';
  360. } else {
  361. echo 'false';
  362. }
  363. exit;
  364. }
  365. }
  366. /**
  367. * 充值申请列表
  368. */
  369. public function refill_evidenceOp()
  370. {
  371. $model_merchant = Model('merchant');
  372. $condition = [];
  373. if (trim($_GET['mch_name']) != '') {
  374. $condition['mch_name'] = array('like', '%' . $_GET['mch_name'] . '%');
  375. Tpl::output('mch_name', $_GET['mch_name']);
  376. }
  377. $state_sel = intval($_REQUEST['state_sel']);
  378. if ($state_sel == 1) {
  379. $condition['check_time'] = 0;
  380. $condition['status'] = 1;
  381. } elseif ($state_sel == 2) {
  382. $condition['check_time'] = ['gt', 0];
  383. $condition['status'] = 2;
  384. } elseif ($state_sel == 3) {
  385. $condition['check_time'] = ['gt', 0];
  386. $condition['status'] = 3;
  387. } else {
  388. }
  389. //充值申请列表
  390. $evidence_list = $model_merchant->getRefillEvidence($condition, 20, '*', 'add_time desc');
  391. $status_text = ['申请中', '已通过', '已驳回'];
  392. $operation_text = ['未预存', '已预存'];
  393. Tpl::output('evidence_list', $evidence_list);
  394. Tpl::output('status_text', $status_text);
  395. Tpl::output('operation_text', $operation_text);
  396. Tpl::output('page', $model_merchant->showpage('2'));
  397. Tpl::showpage('merchant.refill.evidence_list');
  398. }
  399. public function check_evidenceOp()
  400. {
  401. $status = $_GET['status'];
  402. $apply_id = intval($_GET['apply_id']);
  403. if ($apply_id > 0) {
  404. $model_merchant = Model('merchant');
  405. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  406. if (empty($evidence_info)) {
  407. showMessage('充值申请不存在');
  408. }
  409. if ($status == 'pass') {
  410. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 2, 'check_time' => time()]);
  411. } elseif ($status == 'unpass') {
  412. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['status' => 3, 'check_time' => time()]);
  413. } else {
  414. }
  415. }
  416. showMessage(L('nc_common_save_succ'), urlAdmin('merchant', 'refill_evidence'));
  417. }
  418. public function rechargeOp()
  419. {
  420. if (chksubmit()) {
  421. $mchid = $this->add_money();
  422. $model_merchant = Model('merchant');
  423. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  424. if ($merchant_info['alarm_amount'] < $merchant_info['available_predeposit'])
  425. {
  426. //更新预警短信通知限制
  427. $mch_cache = rcache("merchant-notify" , 'refill-');
  428. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  429. $caches[$mchid] = ['last_time' => 0, 'send_count' => 0];
  430. wcache("merchant-notify", ['data' => serialize($caches)], 'refill-');
  431. }
  432. showMessage('操作成功', 'index.php?act=merchant&op=refill_evidence');
  433. } else {
  434. $apply_id = $_GET['apply_id'];
  435. if (!empty($apply_id)) {
  436. $model_merchant = Model('merchant');
  437. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  438. Tpl::output('apply_id', $apply_id);
  439. Tpl::output('amount', $evidence_info['amount']);
  440. }
  441. Tpl::showpage('recharge.add');
  442. }
  443. }
  444. public function add_money()
  445. {
  446. $obj_validate = new Validator();
  447. $obj_validate->validateparam = array(
  448. array("input" => $_POST["apply_id"], "require" => "true", "message" => Language::get('admin_points_member_error_again')),
  449. array("input" => $_POST["pointsnum"], "require" => "true", 'validator' => 'Compare', 'operator' => ' >= ', 'to' => 1, "message" => Language::get('admin_points_points_min_error'))
  450. );
  451. $error = $obj_validate->validate();
  452. if ($error != '') {
  453. showMessage($error, '', '', 'error');
  454. }
  455. $money = abs(floatval($_POST['pointsnum']));
  456. $memo = trim($_POST['pointsdesc']);
  457. if ($money <= 0) {
  458. showMessage('输入的金额必需大于0', '', 'html', 'error');
  459. }
  460. $apply_id = intval($_POST['apply_id']);
  461. $model_merchant = Model('merchant');
  462. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  463. if (!is_array($evidence_info) || count($evidence_info) <= 0) {
  464. showMessage("无效的充值申请信息", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  465. }
  466. if ($evidence_info['status'] != 2) {
  467. showMessage("该充值申请未被审核通过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  468. }
  469. if ($evidence_info['is_operation'] == 2) {
  470. showMessage("该充值申请未已被预存过", "index.php?act=merchant&op=recharge&apply_id={$apply_id}", '', 'error');
  471. }
  472. //查询会员信息
  473. $obj_member = Model('member');
  474. $member_id = intval($evidence_info['member_id']);
  475. $member_info = $obj_member->getMemberInfo(array('member_id' => $member_id));
  476. $available_predeposit = floatval($member_info['available_predeposit']);
  477. $freeze_predeposit = floatval($member_info['freeze_predeposit']);
  478. if ($_POST['operatetype'] == 2 && $money > $available_predeposit) {
  479. showMessage(('预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  480. }
  481. if ($_POST['operatetype'] == 3 && $money > $available_predeposit) {
  482. showMessage(('可冻结预存款不足,会员当前预存款') . $available_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  483. }
  484. if ($_POST['operatetype'] == 4 && $money > $freeze_predeposit) {
  485. showMessage(('可恢复冻结预存款不足,会员当前冻结预存款') . $freeze_predeposit, 'index.php?act=predeposit&op=predeposit_add', '', 'error');
  486. }
  487. $model_pd = Model('predeposit');
  488. $order_sn = $apply_id;
  489. $admininfo = $this->getAdminInfo();
  490. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款,金额为" . $money . ",编号为" . $order_sn;
  491. $admin_act = 'sys_add_money';
  492. switch ($_POST['operatetype']) {
  493. case 1:
  494. $admin_act = "sys_add_money";
  495. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【增加】,金额为" . $money . ",编号为" . $order_sn;
  496. break;
  497. case 2:
  498. $admin_act = "sys_del_money";
  499. $log_msg = "管理员【" . $admininfo['admin_name'] . "】操作会员【" . $member_info['member_name'] . "】预存款【减少】,金额为" . $money . ",编号为" . $order_sn;
  500. break;
  501. default:
  502. showMessage('操作失败', 'index.php?act=merchant&op=refill_evidence');
  503. break;
  504. }
  505. try {
  506. $trans = new trans_wapper($model_pd, __METHOD__);
  507. //扣除冻结的预存款
  508. $data = array();
  509. $data['member_id'] = $member_info['member_id'];
  510. $data['member_name'] = $member_info['member_name'];
  511. $data['amount'] = $money;
  512. $data['order_sn'] = $order_sn;
  513. $data['admin_name'] = $admininfo['name'];
  514. $data['pdr_sn'] = $order_sn;
  515. $data['lg_desc'] = $memo;
  516. $model_pd->changePd($admin_act, $data);
  517. $model_merchant->editRefillEvidence(['apply_id' => $apply_id], ['is_operation' => 2]);
  518. $trans->commit();
  519. $this->log($log_msg, 1);
  520. return $evidence_info['mchid'];
  521. } catch (Exception $e) {
  522. $trans->rollback();
  523. $this->log($log_msg, 0);
  524. showMessage($e->getMessage(), 'index.php?act=merchant&op=refill_evidence', 'html', 'error');
  525. }
  526. }
  527. public function checkevidenceOp()
  528. {
  529. $apply_id = trim($_GET['apply_id']);
  530. if (!$apply_id) {
  531. echo '';
  532. die;
  533. }
  534. $model_merchant = Model('merchant');
  535. $evidence_info = $model_merchant->getRefillEvidenceInfo(['apply_id' => $apply_id], '*');
  536. if (empty($evidence_info)) {
  537. echo '';
  538. die;
  539. }
  540. echo 'true';
  541. }
  542. public function refill_orderOp()
  543. {
  544. $model_refill_order = Model('refill_order');
  545. $condition['inner_status'] = 0;
  546. if (!empty($_GET['order_sn'])) {
  547. $condition['refill_order.order_sn'] = $_GET['order_sn'];
  548. }
  549. if (!empty($_GET['mchid'])) {
  550. $condition['refill_order.mchid'] = $_GET['mchid'];
  551. }
  552. if (!empty($_GET['card_type'])) {
  553. if(in_array($_GET['card_type'] , ['1' , '2' , '4' , '5' , '6'])) {
  554. $condition['refill_order.card_type'] = $_GET['card_type'];
  555. }
  556. if($_GET['card_type'] == 'oil') {
  557. $condition['refill_order.card_type'] = ['in' , ['1' , '2']];
  558. }
  559. if($_GET['card_type'] == 'phone') {
  560. $condition['refill_order.card_type'] = ['in' , ['4' , '5' , '6']];
  561. }
  562. }
  563. if (!empty($_GET['ch_trade_no'])) {
  564. $condition['refill_order.ch_trade_no'] = $_GET['ch_trade_no'];
  565. }
  566. if (!empty($_GET['card_no'])) {
  567. $condition['refill_order.card_no'] = $_GET['card_no'];
  568. }
  569. if (in_array($_GET['order_state'], ['0', '10', '20', '30', '40'])) {
  570. $condition['vr_order.order_state'] = $_GET['order_state'];
  571. }
  572. if (in_array($_GET['refill_amount'], ['10', '20', '30', '50' , '100', '200', '300', '500' , '1000', '20000'])) {
  573. $condition['refill_order.refill_amount'] = $_GET['refill_amount'];
  574. }
  575. $if_start_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_start_time']);
  576. $if_end_time = preg_match('/^20\d{2}-\d{2}-\d{2}$/', $_GET['query_end_time']);
  577. $start_unixtime = $if_start_time ? strtotime($_GET['query_start_time']) : null;
  578. $end_unixtime = $if_end_time ? strtotime($_GET['query_end_time']) : null;
  579. if ($start_unixtime || $end_unixtime) {
  580. $condition['refill_order.order_time'] = ['time', [$start_unixtime, $end_unixtime]];
  581. }
  582. $merchant_list = Model('')->table('merchant')->select();
  583. foreach ($merchant_list as $key =>$value) {
  584. $merchants[$value['mchid']] = $value;
  585. }
  586. $order_list = $model_refill_order->getMerchantOrderList($condition, 30, 'refill_order.*,vr_order.order_state');
  587. $stat = Model('')->table('refill_order,vr_order')->join('inner')
  588. ->on('refill_order.order_id=vr_order.order_id')
  589. ->field('count(*) as order_count ,sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  590. ->where($condition)->select();
  591. $count = $this->refill_stat($condition);
  592. foreach ($order_list as $order_id => $order_info) {
  593. $order_list[$order_id]['card_type_text'] = $this->scard_type($order_info['card_type']);
  594. $order_list[$order_id]['mch_name'] = $merchants[$order_info['mchid']]['company_name'];
  595. if($order_info['notify_time'] > 0)
  596. {
  597. $order_list[$order_id]['diff_time_text'] = $this->elapse_time($order_info['notify_time'] - $order_info['order_time']);
  598. $order_list[$order_id]['diff_time'] = $order_info['notify_time'] - $order_info['order_time'];
  599. }
  600. else
  601. {
  602. $order_list[$order_id]['diff_time_text'] = $this->elapse_time(time() - $order_info['order_time']);
  603. $order_list[$order_id]['diff_time'] = time() - $order_info['order_time'];
  604. }
  605. }
  606. Tpl::output('stat', $stat[0]);
  607. Tpl::output('count', $count);
  608. Tpl::output('order_list', $order_list);
  609. Tpl::output('show_page', $model_refill_order->showpage());
  610. Tpl::showpage('refill.order.index');
  611. }
  612. public function notify_merchantOp()
  613. {
  614. $order_id = $_GET['order_id'];
  615. QueueClient::push("NotifyMerchantComplete", ['order_id' => $order_id,'manual' => true]);
  616. showMessage('操作成功', 'index.php?act=merchant&op=refill_order');
  617. }
  618. public function providerOp()
  619. {
  620. $this->sync_cfgs();
  621. $provider_model = Model('refill_provider');
  622. $condition = [];
  623. if (trim($_GET['name']) != '') {
  624. $condition['name'] = ['like', '%' . $_GET['name'] . '%'];
  625. Tpl::output('name', $_GET['name']);
  626. }
  627. if (in_array($_GET['type'], [1, 2])) {
  628. $condition['type'] = $_GET['type'];
  629. }
  630. $provider_list = $provider_model->getProviderList($condition, 25);
  631. foreach ($provider_list as $key => $provider) {
  632. if(!empty($provider['start_period']) && !empty($provider['end_period'])){
  633. $provider_list[$key]['period'] = $provider['start_period'] . '~' . $provider['end_period'];
  634. }else{
  635. $provider_list[$key]['period'] = '全时间段';
  636. }
  637. }
  638. $opened_text = ['使用中', '已禁用'];
  639. $type_text = ['油卡', '手机充值卡'];
  640. Tpl::output('opened_text', $opened_text);
  641. Tpl::output('type_text', $type_text);
  642. Tpl::output('provider_list', $provider_list);
  643. Tpl::output('show_page', $provider_model->showpage());
  644. Tpl::showpage('provider.index');
  645. }
  646. public function sync_cfgs()
  647. {
  648. $name_val_cfg = function ($items) {
  649. $result = [];
  650. foreach ($items as $item) {
  651. $name = $item->name();
  652. $result[$name] = $item;
  653. }
  654. return $result;
  655. };
  656. $name_val_row = function ($items) {
  657. $result = [];
  658. foreach ($items as $item) {
  659. $name = $item['name'];
  660. $result[$name] = $item;
  661. }
  662. return $result;
  663. };
  664. $match = function ($all,$cur)
  665. {
  666. $insert = [];
  667. foreach ($all as $key => $value)
  668. {
  669. if(!array_key_exists($key,$cur)) {
  670. $insert[] = $key;
  671. }
  672. }
  673. return $insert;
  674. };
  675. $inserter = function ($mod,$type,$names)
  676. {
  677. foreach ($names as $name) {
  678. $data = ['name' => $name,'type' => $type];
  679. $mod->insert($data);
  680. }
  681. };
  682. $providers = refill\RefillFactory::instance()->providers();
  683. $oils = $name_val_cfg($providers['oil']);
  684. $phones = $name_val_cfg($providers['phone']);
  685. $mod_prov = Model('refill_provider');
  686. $oil_items = $mod_prov->getProviderList(['type' => 1]);
  687. $oil_items = $name_val_row($oil_items);
  688. $oil_inserts = $match($oils,$oil_items);
  689. $phone_items = $mod_prov->getProviderList(['type' => 2]);
  690. $phone_items = $name_val_row($phone_items);
  691. $phone_inserts = $match($phones,$phone_items);
  692. $inserter($mod_prov,1,$oil_inserts);
  693. $inserter($mod_prov,2,$phone_inserts);
  694. }
  695. public function changeProviderStateOp()
  696. {
  697. $provider_id = intval($_GET['id']);
  698. $state = intval($_GET['state']);
  699. $provider_model = Model('refill_provider');
  700. $provider_info = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  701. if (empty($provider_info) || !in_array($state, [1, 2])) {
  702. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  703. }
  704. $resp = $provider_model->editProvider(['opened' => $state], ['provider_id' => $provider_id]);
  705. if (!$resp) {
  706. showMessage('操作失败', 'index.php?act=merchant&op=provider', 'html', 'error');
  707. }
  708. showMessage('操作成功', 'index.php?act=merchant&op=provider');
  709. }
  710. /**
  711. * 新增通道
  712. */
  713. public function provider_addOp()
  714. {
  715. $provider_model = Model('refill_provider');
  716. if (chksubmit()) {
  717. $params = $_POST;
  718. unset($params['form_submit']);
  719. $result = $provider_model->addProvider($params);
  720. if ($result) {
  721. $url = [
  722. [
  723. 'url' => 'index.php?act=merchant&op=provider',
  724. 'msg' => '返回通道列表',
  725. ],
  726. [
  727. 'url' => 'index.php?act=merchant&op=provider_add',
  728. 'msg' => '继续新增通道',
  729. ],
  730. ];
  731. $this->log('添加通道:' . '[ ' . $_POST['name'] . ']', 1);
  732. showMessage('通道添加成功', $url);
  733. } else {
  734. showMessage('通道添加失败');
  735. }
  736. }
  737. Tpl::showpage('provider.add');
  738. }
  739. public function provider_editOp()
  740. {
  741. $provider_model = Model('refill_provider');
  742. if (chksubmit()) {
  743. $provider_id = intval($_POST['provider_id']) ?? '';
  744. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  745. if (empty($provider)) {
  746. showMessage('通道信息有误');
  747. }
  748. $params = $_POST;
  749. unset($params['form_submit']);
  750. if (empty($params)) {
  751. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  752. }
  753. $result = $provider_model->editProvider($params, ['provider_id' => $provider_id]);
  754. if ($result) {
  755. $this->log('编辑通道:' . '[ ' . $provider['name'] . ']', 1);
  756. showMessage('通道编辑成功', 'index.php?act=merchant&op=provider');
  757. } else {
  758. showMessage('通道编辑失败', "index.php?act=merchant&op=provider_edit&id={$provider_id}");
  759. }
  760. }
  761. $provider_id = intval($_GET['id']) ?? '';
  762. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  763. if (empty($provider)) {
  764. showMessage('通道信息有误');
  765. }
  766. $type_text = ['油卡', '手机充值卡'];
  767. Tpl::output('type_text', $type_text);
  768. Tpl::output('provider', $provider);
  769. Tpl::showpage('provider.edit');
  770. }
  771. public function provider_delOp()
  772. {
  773. $provider_model = Model('refill_provider');
  774. $provider_id = intval($_GET['id']) ?? '';
  775. $provider = $provider_model->getProviderInfo(['provider_id' => $provider_id]);
  776. if (empty($provider)) {
  777. showMessage('通道信息有误');
  778. }
  779. $result = $provider_model->delProvider(['provider_id' => $provider_id]);
  780. if ($result) {
  781. $this->log('删除通道:' . '[ ' . $provider['name'] . ']', 1);
  782. showMessage('通道删除成功', 'index.php?act=merchant&op=provider');
  783. } else {
  784. showMessage('通道删除失败', "index.php?act=merchant&op=provider");
  785. }
  786. }
  787. private function scard_type(int $card_type)
  788. {
  789. if ($card_type == 1) { //中石油
  790. return '中石油';
  791. } elseif ($card_type == 2) { //中石化
  792. return '中石化';
  793. } elseif ($card_type == 4) { //中国移动
  794. return '中国移动';
  795. } elseif ($card_type == 5) { //中国联通
  796. return '中国联通';
  797. } elseif ($card_type == 6) { //中国电信
  798. return '中国电信';
  799. } else {
  800. return 'unknown';
  801. }
  802. }
  803. private function elapse_time($seconds)
  804. {
  805. $minutes = intval($seconds / 60);
  806. $second = intval($seconds % 60);
  807. if($minutes > 0){
  808. $result = "{$minutes}分钟{$second}秒";
  809. }else{
  810. $result = "{$second}秒";
  811. }
  812. return $result;
  813. }
  814. private function refill_stat($condition)
  815. {
  816. $model_refill_order = Model('refill_order');
  817. $i = $half_count = $hour_count = 0;
  818. while (true)
  819. {
  820. $start = $i * 100000;
  821. $orders = $model_refill_order->getMerchantOrderList($condition, '', 'refill_order.*,vr_order.order_state','order_time desc' ,"{$start},100000");
  822. $i++;
  823. if (empty($orders)) {
  824. break;
  825. }
  826. foreach ($orders as $order)
  827. {
  828. if($order['notify_time'] == 0 || $order['notify_time'] == null)
  829. {
  830. $order['diff_time'] = time() - $order['order_time'];
  831. if($order['order_state'] == ORDER_STATE_SEND) {
  832. if($order['diff_time'] > 1800 && $order['diff_time'] < 3600) {
  833. $half_count++;
  834. }
  835. if($order['diff_time'] > 3600) {
  836. $hour_count++;
  837. }
  838. }
  839. }
  840. }
  841. if (count($orders) < 100000) {
  842. break;
  843. }
  844. }
  845. return ['half_count' => $half_count , 'hour_count' => $hour_count];
  846. }
  847. }