merchant_info.php 22 KB

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