merchant_info.php 21 KB

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