merchant_info.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. require_once(BASE_ROOT_PATH . '/mobile/control/merchantweb.php');
  3. class merchant_infoControl extends mbMerchantControl
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function indexOp()
  10. {
  11. $mchid = $this->mchid();
  12. $model_merchant = Model('merchant');
  13. $field = 'mchid,admin_id,name,alarm_amount,ip_white_list,use_key,contact_name,contact_phone,warning_phone,company_name,quality,time_out,credit_bonus';
  14. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], $field);
  15. $model_member = Model('member');
  16. $member_info = $model_member->getMemberInfo(['member_id' => $merchant_info['admin_id']], 'available_predeposit');
  17. if (empty($member_info)) {
  18. $member_info['available_predeposit'] = 0;
  19. }
  20. $merchant_info['member'] = $member_info;
  21. if (empty($merchant_info['ip_white_list'])) {
  22. $merchant_info['ips'] = [];
  23. } else {
  24. $merchant_info['ips'] = unserialize($merchant_info['ip_white_list']);
  25. }
  26. if (empty($merchant_info['warning_phone'])) {
  27. $merchant_info['warning_phone'] = [];
  28. } else {
  29. $merchant_info['warning_phone'] = unserialize($merchant_info['warning_phone']);
  30. }
  31. $cond['status'] = $cond['is_operation'] = 2;
  32. $cond['mchid'] = $mchid;
  33. $stats = Model('')->table('refill_evidence')
  34. ->field('sum(amount) as amounts')
  35. ->where($cond)->select();
  36. $merchant_info['evidence_amounts'] = empty($stats) ? 0 : $stats[0]['amounts'];
  37. $cond['add_time'] = ['between', [strtotime(date("Y-m-d",time())), time()]];
  38. $day_evidence = $model_merchant->getRefillEvidence($cond);
  39. $merchant_info['evidence_count'] = count($day_evidence);
  40. $merchant_info['time_out'] = intval($merchant_info['time_out'] / 60);
  41. $merchant_info['quality'] = intval($merchant_info['quality']);
  42. $pub = new message\publisher();
  43. $pub->modify_refill_merchant();
  44. return self::outsuccess($merchant_info);
  45. }
  46. public function SetQualityTimeOutOp()
  47. {
  48. $mchid = $this->mchid();
  49. $save = [];
  50. $quality = intval($_POST['quality']);
  51. if($quality > 0) {
  52. if(!in_array($quality , [1,2,3])) {
  53. return self::outerr(errcode::ErrParamter, "通道质量类型错误");
  54. }
  55. $save['quality'] = $quality;
  56. }
  57. $time_out = intval($_POST['time_out']);
  58. if($quality > 0) {
  59. if($time_out <3) {
  60. return self::outerr(errcode::ErrParamter, "超时时间应大于3分钟");
  61. }
  62. $time_out = intval($time_out * 60);
  63. $save['time_out'] = $time_out;
  64. }
  65. if(empty($save)) {
  66. return self::outsuccess([]);
  67. }
  68. $model_merchant = Model('merchant');
  69. $resp = $model_merchant->editMerchant($save, ['mchid' => $mchid]);
  70. if($resp) {
  71. return self::outsuccess([]);
  72. }else{
  73. return self::outerr(errcode::ErrOperation, "系统错误.");
  74. }
  75. }
  76. public function homeOp()
  77. {
  78. $mchid = $this->mchid();
  79. $model_merchant = Model('merchant');
  80. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], 'mchid,admin_id,name,company_name');
  81. $model_member = Model('member');
  82. $member_info = $model_member->getMemberInfo(['member_id' => $merchant_info['admin_id']], 'available_predeposit');
  83. if (empty($member_info)) {
  84. $merchant_info['available_predeposit'] = 0;
  85. } else {
  86. $merchant_info['available_predeposit'] = ncPriceFormat($member_info['available_predeposit']);
  87. }
  88. $statistics = $this->statistics();
  89. ksort($statistics);
  90. $result['merchant_info'] = $merchant_info;
  91. $result['todayStatistics'] = $statistics[strtotime(date("Y-m-d",time()))];
  92. $result['weeksStatistics'] = $statistics;
  93. $max = 0;
  94. foreach ($statistics as $key => $val){
  95. if($max == 0){
  96. $max = $val['successAmounts'];
  97. }
  98. if($max < $val['count']){
  99. $max = $val['successAmounts'];
  100. }
  101. }
  102. $result['max'] = ncPriceFormat(ceil($max * 1.1));
  103. $week_month_stats = $this->refillStats();
  104. $result['month'] = $week_month_stats['month'];
  105. $result['week'] = $week_month_stats['week'];
  106. return self::outsuccess($result);
  107. }
  108. private function statistics()
  109. {
  110. $date = date('Y-m-d',time());
  111. $today = strtotime("{$date}");
  112. $times_begin = function ($start) {
  113. $times = [];
  114. $begin = $start;
  115. for($i = 0; $i < 7; $i++) {
  116. $times[] = $begin;
  117. $begin -= 86400;
  118. }
  119. return $times;
  120. };
  121. $reader = function ($mchid,$time) {
  122. $cond['mchid'] = $mchid;
  123. $cond['inner_status'] = 0;
  124. $cond['refill_order.order_time'] = ['between', [$time, $time + 86400 -1]];
  125. $items = Model('')->table('refill_order,vr_order')->join('inner')
  126. ->on('refill_order.order_id=vr_order.order_id')
  127. ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
  128. ->group('order_state')
  129. ->where($cond)->select();
  130. return $this->refillCountDispose($items);
  131. };
  132. $begins = $times_begin($today);
  133. $states = rcache($this->mchid() , 'refillstat-');
  134. if(empty($states)){
  135. $states = [];
  136. }else{
  137. $states = unserialize($states['data']);
  138. }
  139. $result = [];
  140. $cache = [];
  141. foreach ($begins as $begin)
  142. {
  143. if(array_key_exists($begin,$states)) {
  144. $item = $states[$begin];
  145. }
  146. else {
  147. $item = $reader($this->mchid(),$begin);
  148. }
  149. $result[$begin] = $item;
  150. //判断item 中充值中的状态是否为0,为0且不是今天的情况下放进cache
  151. if($item['sendCount'] == 0 && $begin != $today){
  152. $cache[$begin] = $item;
  153. }
  154. }
  155. if(!empty($cache) && !$this->isSame($states,$cache)) {
  156. wcache($this->mchid() ,['data' => serialize($cache)], 'refillstat-');
  157. }
  158. return $result;
  159. }
  160. private function isSame($arr1, $arr2)
  161. {
  162. ksort($arr1);
  163. ksort($arr2);
  164. return $arr1 == $arr2;
  165. }
  166. private function refillCountDispose($stats)
  167. {
  168. $result['count'] = $result['sendCount'] = $result['errorCount'] = $result['successCount'] = $result['amountCount'] = $result['errorAmounts'] = $result['successAmounts'] = 0;
  169. foreach ($stats as $count) {
  170. $result['count'] += $count['order_count'];
  171. $result['amountCount'] += ncPriceFormat($count['mch_amounts']);
  172. if($count['order_state'] == ORDER_STATE_SEND) {
  173. $result['sendCount'] = $count['order_count'];
  174. }
  175. if($count['order_state'] == ORDER_STATE_CANCEL) {
  176. $result['errorCount'] = $count['order_count'];
  177. $result['errorAmounts'] = ncPriceFormat($count['mch_amounts']);
  178. }
  179. if($count['order_state'] == ORDER_STATE_SUCCESS) {
  180. $result['successCount'] = $count['order_count'];
  181. $result['successAmounts'] = ncPriceFormat($count['mch_amounts']);
  182. }
  183. }
  184. $result['amountCount'] = ncPriceFormat($result['amountCount']);
  185. $result['errorAmounts'] = ncPriceFormat($result['errorAmounts']);
  186. $result['successAmounts'] = ncPriceFormat($result['successAmounts']);
  187. return $result;
  188. }
  189. private function refillStats()
  190. {
  191. $reader = function ($mchid,$time,$end_time) {
  192. $cond['mchid'] = $mchid;
  193. $cond['inner_status'] = 0;
  194. $cond['refill_order.order_time'] = ['between', [$time, $end_time]];
  195. $items = Model('')->table('refill_order,vr_order')->join('inner')
  196. ->on('refill_order.order_id=vr_order.order_id')
  197. ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
  198. ->group('order_state')
  199. ->where($cond)->select();
  200. return $this->refillCountDispose($items);
  201. };
  202. $date = date("Y-m-d");
  203. $first = 1;
  204. $w = date('w',strtotime($date));
  205. $week_start = strtotime(date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')));
  206. $w = 7 - $w + 1;
  207. $week_end = strtotime(date('Y-m-d',strtotime("+{$w} days"))) - 1;
  208. $week = $reader($this->mchid(), $week_start,$week_end);
  209. $month_start = strtotime(date("Y-m-1"));
  210. $month_end = strtotime(date("Y-m-1",strtotime("+1month")))-1;
  211. $month = $reader($this->mchid(), $month_start, $month_end);
  212. return ['week' => $week , 'month' => $month];
  213. }
  214. public function addipOp()
  215. {
  216. $mchid = $this->mchid();
  217. $ip = $_POST['ip'];
  218. if (empty($ip)) {
  219. return self::outerr(errcode::ErrParamter, "参数错误");
  220. }
  221. $ip = trim($ip);
  222. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
  223. {
  224. $model_merchant = Model('merchant');
  225. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  226. if (empty($merchant_info['ip_white_list'])) {
  227. $ips = [];
  228. } else {
  229. $ips = unserialize($merchant_info['ip_white_list']);
  230. }
  231. $ips[] = $ip;
  232. $ips = array_unique($ips);
  233. $model_merchant->editMerchant(['ip_white_list' => serialize($ips)], ['mchid' => $merchant_info['mchid']]);
  234. return self::outsuccess([]);
  235. } else {
  236. return self::outerr(errcode::ErrParamter, "ip地址错误");
  237. }
  238. }
  239. public function ipdelOp()
  240. {
  241. $mchid = $this->mchid();
  242. $ip = $_POST['ip'];
  243. if (empty($ip)) {
  244. return self::outerr(errcode::ErrParamter, "参数错误");
  245. }
  246. $ip = trim($ip);
  247. $model_merchant = Model('merchant');
  248. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  249. $ips = unserialize($merchant_info['ip_white_list']);
  250. $new_ips = [];
  251. foreach ($ips as $value) {
  252. if ($value != $ip) {
  253. $new_ips[] = $value;
  254. }
  255. }
  256. if (empty($new_ips)) {
  257. $new_ips = '';
  258. } else {
  259. $new_ips = serialize($new_ips);
  260. }
  261. $model_merchant->editMerchant(['ip_white_list' => $new_ips], ['mchid' => $merchant_info['mchid']]);
  262. return self::outsuccess([]);
  263. }
  264. public function setcontactOp(){
  265. $mchid = $this->mchid();
  266. if(!empty($_POST['contact_name']))
  267. {
  268. $params['contact_name'] = trim($_POST['contact_name']);
  269. }
  270. if(!empty($_POST['contact_phone']))
  271. {
  272. $params['contact_phone'] = trim($_POST['contact_phone']);
  273. }
  274. if(!empty($_POST['alarm_amount']))
  275. {
  276. $params['alarm_amount'] = trim($_POST['alarm_amount']);
  277. }
  278. if(empty($params))
  279. {
  280. return self::outsuccess([]);
  281. }
  282. $model_merchant = Model('merchant');
  283. $ret = $model_merchant->editMerchant($params, ['mchid' => $mchid]);
  284. if ($ret) {
  285. return self::outsuccess([]);
  286. } else {
  287. return self::outerr(errcode::ErrOperation, "系统错误.");
  288. }
  289. }
  290. public function addwphoneOp()
  291. {
  292. $mchid = $this->mchid();
  293. $phone = $_POST['phone'];
  294. if (empty($phone)) {
  295. return self::outerr(errcode::ErrParamter, "参数错误");
  296. }
  297. $phone = explode(',' ,trim($phone));
  298. $model_merchant = Model('merchant');
  299. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  300. if (empty($merchant_info['warning_phone'])) {
  301. $phones = [];
  302. } else {
  303. $phones = unserialize($merchant_info['warning_phone']);
  304. }
  305. foreach ($phone as $item) {
  306. $phones[] = $item;
  307. }
  308. $phones = array_unique($phones);
  309. $model_merchant->editMerchant(['warning_phone' => serialize($phones)], ['mchid' => $merchant_info['mchid']]);
  310. return self::outsuccess([]);
  311. }
  312. public function wphonedelOp()
  313. {
  314. $mchid = $this->mchid();
  315. $phone = $_POST['phone'];
  316. if (empty($phone)) {
  317. return self::outerr(errcode::ErrParamter, "参数错误");
  318. }
  319. $phone = trim($phone);
  320. $model_merchant = Model('merchant');
  321. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  322. $phones = unserialize($merchant_info['warning_phone']);
  323. $new_phones = [];
  324. foreach ($phones as $value) {
  325. if ($value != $phone) {
  326. $new_phones[] = $value;
  327. }
  328. }
  329. if (empty($new_phones)) {
  330. $new_phones = '';
  331. } else {
  332. $new_phones = serialize($new_phones);
  333. }
  334. $model_merchant->editMerchant(['warning_phone' => $new_phones], ['mchid' => $merchant_info['mchid']]);
  335. return self::outsuccess([]);
  336. }
  337. public function setkeyOp()
  338. {
  339. $mchid = $this->mchid();
  340. $secure_key = $_POST['secure_key'];
  341. if (empty($secure_key)) {
  342. return self::outerr(errcode::ErrParamter, "参数错误");
  343. }
  344. $model_merchant = Model('merchant');
  345. $ret = $model_merchant->editMerchant(['secure_key' => $secure_key, 'use_key' => 1], ['mchid' => $mchid]);
  346. if ($ret) {
  347. return self::outsuccess([]);
  348. } else {
  349. return self::outerr(errcode::ErrOperation, "系统错误.");
  350. }
  351. }
  352. public function modifypwOp()
  353. {
  354. $mchid = $this->mchid();
  355. $new_pw = $_POST['new_pw'];
  356. $new_pw2 = $_POST['new_pw2'];
  357. if (trim($new_pw) !== trim($new_pw2)) {
  358. return self::outerr(errcode::ErrPasswd, "密码错误");
  359. }
  360. $model_merchant = Model('merchant');
  361. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  362. if (!$merchant_info) {
  363. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  364. }
  365. $pwd = trim($new_pw);
  366. if (md5($pwd) == $merchant_info['password']) {
  367. return self::outsuccess([]);
  368. }
  369. $ret = $model_merchant->editMerchant(['password' => md5($pwd), 'org_pwd' => $pwd], ['mchid' => $merchant_info['mchid']]);
  370. if ($ret) {
  371. return self::outsuccess([]);
  372. } else {
  373. return self::outerr(errcode::ErrOperation, "系统错误.");
  374. }
  375. }
  376. public function pdlogOp()
  377. {
  378. $mchid = $this->mchid();
  379. $model_merchant = Model('merchant');
  380. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  381. if (!$merchant_info) {
  382. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  383. }
  384. $model_pd = Model('merchant');
  385. if (empty($merchant_info['admin_id'])) {
  386. $result['data'] = [];
  387. $result['total'] = 0;
  388. return self::outsuccess($result);
  389. }
  390. $cond['lg_member_id'] = $merchant_info['admin_id'];
  391. if (!empty($_GET['lg_type'])) {
  392. $cond['lg_type'] = $_GET['lg_type'];
  393. }
  394. if ($_GET['start_time'] && $_GET['end_time']) {
  395. $cond['lg_add_time'] = ['between', [$_GET['start_time'], $_GET['end_time']]];
  396. }
  397. $list = $model_pd->getPdlog($cond, $this->page, '*', 'lg_id desc');
  398. $list = $this->PdLogFormat($list);
  399. $result['data'] = $list;
  400. $result['total'] = $model_pd->gettotalpage();
  401. return self::outsuccess($result);
  402. }
  403. private function PdLogFormat($pdlog)
  404. {
  405. $data = [];
  406. foreach ($pdlog as $key => $value) {
  407. if (isset($value['lg_add_time'])) {
  408. $value['lg_add_time'] = date('Y-m-d H:i:s', $value['lg_add_time']);
  409. }
  410. switch ($value['lg_type']) {
  411. case 'order_pay':
  412. $value['lg_type_text'] = '下单减款';
  413. break;
  414. case 'order_cancel':
  415. $value['lg_type_text'] = '下单失败返回余款';
  416. break;
  417. case 'recharge':
  418. $value['lg_type_text'] = '余款充值';
  419. break;
  420. case 'sys_add_money':
  421. $value['lg_type_text'] = '管理员调节预存款【增加】';
  422. break;
  423. case 'sys_del_money':
  424. $value['lg_type_text'] = '管理员调节预存款【减少】';
  425. break;
  426. case 'sys_freeze_money':
  427. $value['lg_type_text'] = '管理员调节预存款【冻结】';
  428. break;
  429. case 'sys_unfreeze_money':
  430. $value['lg_type_text'] = '管理员调节预存款【解冻】';
  431. break;
  432. default:
  433. $value['lg_type_text'] = 'unknown';
  434. break;
  435. }
  436. $data[] = $value;
  437. }
  438. return $data;
  439. }
  440. public function pdlogexportOp()
  441. {
  442. $mchid = $this->mchid();
  443. $model_merchant = Model('merchant');
  444. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  445. if (!$merchant_info) {
  446. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  447. }
  448. $condition['lg_member_id'] = $merchant_info['admin_id'];
  449. if (!empty($_GET['lg_type'])) {
  450. $condition['lg_type'] = $_GET['lg_type'];
  451. }
  452. if ($_GET['start_time'] && $_GET['end_time']) {
  453. $condition['lg_add_time'] = ['between', [$_GET['start_time'], $_GET['end_time']]];
  454. }
  455. $list = Model('')->table('pd_log,refill_order')->join('left')
  456. ->on('pd_log.lg_order_sn=refill_order.order_sn')
  457. ->field('pd_log.lg_type,pd_log.lg_av_amount,pd_log.lg_add_time,refill_order.card_no,refill_order.mchid,refill_order.card_type,pd_log.lg_available,refill_order.refill_amount')
  458. ->where($condition)->select();
  459. $result = $this->export_pdlog_exec($list);
  460. return self::outsuccess($result);
  461. }
  462. private function export_pdlog_exec($list)
  463. {
  464. $title = [
  465. ['value' => '代理商账号'],
  466. ['value' => '金额'],
  467. ['value' => '账户余额'],
  468. ['value' => '交易日期'],
  469. ['value' => '类型'],
  470. ['value' => '备注']
  471. ];
  472. $mchid = $this->mchid();
  473. $card_type = ['1'=>'中石油' , '2' =>'中石化' , '4' => '移动' , '5' => '联通' , '6' => '电信'];
  474. foreach ($list as $value) {
  475. if (isset($value['lg_add_time'])) {
  476. $value['lg_add_time'] = date('Y-m-d H:i:s', $value['lg_add_time']);
  477. }
  478. switch ($value['lg_type']) {
  479. case 'order_pay':
  480. $value['lg_type_text'] = '消费';
  481. break;
  482. case 'order_cancel':
  483. $value['lg_type_text'] = '退款';
  484. break;
  485. case 'recharge':
  486. $value['lg_type_text'] = '充值';
  487. break;
  488. case 'sys_add_money':
  489. $value['lg_type_text'] = '管理员充值【增加】';
  490. break;
  491. case 'sys_del_money':
  492. $value['lg_type_text'] = '管理员减款';
  493. break;
  494. default:
  495. $value['lg_type_text'] = '';
  496. break;
  497. }
  498. $data = [];
  499. $str = "{$value['card_no']}-{$card_type[$value['card_type']]}{$value['refill_amount']}元";
  500. if(empty($value['card_no']) || empty($value['card_type']) || empty($value['refill_amount'])) {
  501. $str = '';
  502. }
  503. $data[] = ['value' => $mchid];
  504. $data[] = ['value' => $value['lg_av_amount']];
  505. $data[] = ['value' => ncPriceFormat($value['lg_available'] + $value['lg_av_amount'])];
  506. $data[] = ['value' => $value['lg_add_time']];
  507. $data[] = ['value' => $value['lg_type_text']];
  508. $data[] = ['value' => $str];
  509. $datas[] = $data;
  510. }
  511. Log::record(json_encode($datas),Log::DEBUG);
  512. return ['title' => $title , 'data' => $datas];
  513. }
  514. }