merchant_info.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], 'mchid,admin_id,name,alarm_amount,ip_white_list,use_key,contact_name,contact_phone,warning_phone,company_name');
  14. $model_member = Model('member');
  15. $member_info = $model_member->getMemberInfo(['member_id' => $merchant_info['admin_id']], 'available_predeposit');
  16. if (empty($member_info)) {
  17. $member_info['available_predeposit'] = 0;
  18. }
  19. $merchant_info['member'] = $member_info;
  20. if (empty($merchant_info['ip_white_list'])) {
  21. $merchant_info['ips'] = [];
  22. } else {
  23. $merchant_info['ips'] = unserialize($merchant_info['ip_white_list']);
  24. }
  25. if (empty($merchant_info['warning_phone'])) {
  26. $merchant_info['warning_phone'] = [];
  27. } else {
  28. $merchant_info['warning_phone'] = unserialize($merchant_info['warning_phone']);
  29. }
  30. $cond['status'] = $cond['is_operation'] = 2;
  31. $cond['mchid'] = $mchid;
  32. $stats = Model('')->table('refill_evidence')
  33. ->field('sum(amount) as amounts')
  34. ->where($cond)->select();
  35. $merchant_info['evidence_amounts'] = empty($stats) ? 0 : $stats[0]['amounts'];
  36. $cond['add_time'] = ['between', [strtotime(date("Y-m-d",time())), time()]];
  37. $day_evidence = $model_merchant->getRefillEvidence($cond);
  38. $merchant_info['evidence_count'] = count($day_evidence);
  39. return self::outsuccess($merchant_info);
  40. }
  41. public function homeOp()
  42. {
  43. $mchid = $this->mchid();
  44. $model_merchant = Model('merchant');
  45. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid], 'mchid,admin_id,name,company_name');
  46. $model_member = Model('member');
  47. $member_info = $model_member->getMemberInfo(['member_id' => $merchant_info['admin_id']], 'available_predeposit');
  48. if (empty($member_info)) {
  49. $merchant_info['available_predeposit'] = 0;
  50. } else {
  51. $merchant_info['available_predeposit'] = ncPriceFormat($member_info['available_predeposit']);
  52. }
  53. $statistics = $this->statistics();
  54. ksort($statistics);
  55. $result['merchant_info'] = $merchant_info;
  56. $result['todayStatistics'] = $statistics[strtotime(date("Y-m-d",time()))];
  57. $result['weeksStatistics'] = $statistics;
  58. $max = 0;
  59. foreach ($statistics as $key => $val){
  60. if($max == 0){
  61. $max = $val['count'];
  62. }
  63. if((int)$max < $val['count']){
  64. $max = $val['count'];
  65. }
  66. }
  67. $result['max'] = ceil($max * 1.1);
  68. $week_month_stats = $this->refillStats();
  69. $result['month'] = $week_month_stats['month'];
  70. $result['week'] = $week_month_stats['week'];
  71. return self::outsuccess($result);
  72. }
  73. private function statistics()
  74. {
  75. $date = date('Y-m-d',time());
  76. $today = strtotime("{$date}");
  77. $times_begin = function ($start) {
  78. $times = [];
  79. $begin = $start;
  80. for($i = 0; $i < 7; $i++) {
  81. $times[] = $begin;
  82. $begin -= 86400;
  83. }
  84. return $times;
  85. };
  86. $reader = function ($mchid,$time) {
  87. $cond['mchid'] = $mchid;
  88. $cond['inner_status'] = 0;
  89. $cond['refill_order.order_time'] = ['between', [$time, $time + 86400 -1]];
  90. $items = Model('')->table('refill_order,vr_order')->join('inner')
  91. ->on('refill_order.order_id=vr_order.order_id')
  92. ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
  93. ->group('order_state')
  94. ->where($cond)->select();
  95. return $this->refillCountDispose($items);
  96. };
  97. $begins = $times_begin($today);
  98. $states = rcache($this->mchid() , 'refillstat-');
  99. if(empty($states)){
  100. $states = [];
  101. }else{
  102. $states = unserialize($states['data']);
  103. }
  104. $result = [];
  105. $cache = [];
  106. foreach ($begins as $begin)
  107. {
  108. if(array_key_exists($begin,$states)) {
  109. $item = $states[$begin];
  110. }
  111. else {
  112. $item = $reader($this->mchid(),$begin);
  113. }
  114. $result[$begin] = $item;
  115. //判断item 中充值中的状态是否为0,为0且不是今天的情况下放进cache
  116. if($item['sendCount'] == 0 && $begin != $today){
  117. $cache[$begin] = $item;
  118. }
  119. }
  120. if(!empty($cache) && !$this->isSame($states,$cache)) {
  121. wcache($this->mchid() ,['data' => serialize($cache)], 'refillstat-');
  122. }
  123. return $result;
  124. }
  125. private function isSame($arr1, $arr2)
  126. {
  127. ksort($arr1);
  128. ksort($arr2);
  129. return $arr1 == $arr2;
  130. }
  131. private function refillCountDispose($stats)
  132. {
  133. $result['count'] = $result['sendCount'] = $result['errorCount'] = $result['successCount'] = $result['amountCount'] = $result['errorAmounts'] = $result['successAmounts'] = 0;
  134. foreach ($stats as $count) {
  135. $result['count'] += $count['order_count'];
  136. $result['amountCount'] += ncPriceFormat($count['mch_amounts']);
  137. if($count['order_state'] == ORDER_STATE_SEND) {
  138. $result['sendCount'] = $count['order_count'];
  139. }
  140. if($count['order_state'] == ORDER_STATE_CANCEL) {
  141. $result['errorCount'] = $count['order_count'];
  142. $result['errorAmounts'] = ncPriceFormat($count['mch_amounts']);
  143. }
  144. if($count['order_state'] == ORDER_STATE_SUCCESS) {
  145. $result['successCount'] = $count['order_count'];
  146. $result['successAmounts'] = ncPriceFormat($count['mch_amounts']);
  147. }
  148. }
  149. return $result;
  150. }
  151. private function refillStats()
  152. {
  153. $reader = function ($mchid,$time,$end_time) {
  154. $cond['mchid'] = $mchid;
  155. $cond['inner_status'] = 0;
  156. $cond['refill_order.order_time'] = ['between', [$time, $end_time]];
  157. $items = Model('')->table('refill_order,vr_order')->join('inner')
  158. ->on('refill_order.order_id=vr_order.order_id')
  159. ->field('count(*) as order_count, vr_order.order_state, sum(mch_amount) as mch_amounts')
  160. ->group('order_state')
  161. ->where($cond)->select();
  162. return $this->refillCountDispose($items);
  163. };
  164. $date = date("Y-m-d");
  165. $first = 1;
  166. $w = date('w',strtotime($date)); ;
  167. $week_start = strtotime(date('Y-m-d',strtotime("$date -".($w ? $w - $first : 6).' days')));
  168. $week_end = strtotime(date('Y-m-d',strtotime("$week_start +7 days"))) - 1;
  169. $week = $reader($this->mchid(), $week_start,$week_end);
  170. $month_start = strtotime(date("Y-m-1"));
  171. $month_end = strtotime(date("Y-m-1",strtotime("+1month")))-1;
  172. $month = $reader($this->mchid(), $month_start, $month_end);
  173. return ['week' => $week , 'month' => $month];
  174. }
  175. public function addipOp()
  176. {
  177. $mchid = $this->mchid();
  178. $ip = $_POST['ip'];
  179. if (empty($ip)) {
  180. return self::outerr(errcode::ErrParamter, "参数错误");
  181. }
  182. $ip = trim($ip);
  183. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
  184. {
  185. $model_merchant = Model('merchant');
  186. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  187. if (empty($merchant_info['ip_white_list'])) {
  188. $ips = [];
  189. } else {
  190. $ips = unserialize($merchant_info['ip_white_list']);
  191. }
  192. $ips[] = $ip;
  193. $ips = array_unique($ips);
  194. $model_merchant->editMerchant(['ip_white_list' => serialize($ips)], ['mchid' => $merchant_info['mchid']]);
  195. return self::outsuccess([]);
  196. } else {
  197. return self::outerr(errcode::ErrParamter, "ip地址错误");
  198. }
  199. }
  200. public function ipdelOp()
  201. {
  202. $mchid = $this->mchid();
  203. $ip = $_POST['ip'];
  204. if (empty($ip)) {
  205. return self::outerr(errcode::ErrParamter, "参数错误");
  206. }
  207. $ip = trim($ip);
  208. $model_merchant = Model('merchant');
  209. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  210. $ips = unserialize($merchant_info['ip_white_list']);
  211. $new_ips = [];
  212. foreach ($ips as $value) {
  213. if ($value != $ip) {
  214. $new_ips[] = $value;
  215. }
  216. }
  217. if (empty($new_ips)) {
  218. $new_ips = '';
  219. } else {
  220. $new_ips = serialize($new_ips);
  221. }
  222. $model_merchant->editMerchant(['ip_white_list' => $new_ips], ['mchid' => $merchant_info['mchid']]);
  223. return self::outsuccess([]);
  224. }
  225. public function setcontactOp(){
  226. $mchid = $this->mchid();
  227. if(!empty($_POST['contact_name']))
  228. {
  229. $params['contact_name'] = trim($_POST['contact_name']);
  230. }
  231. if(!empty($_POST['contact_phone']))
  232. {
  233. $params['contact_phone'] = trim($_POST['contact_phone']);
  234. }
  235. if(!empty($_POST['alarm_amount']))
  236. {
  237. $params['alarm_amount'] = trim($_POST['alarm_amount']);
  238. }
  239. if(empty($params))
  240. {
  241. return self::outsuccess([]);
  242. }
  243. $model_merchant = Model('merchant');
  244. $ret = $model_merchant->editMerchant($params, ['mchid' => $mchid]);
  245. if ($ret) {
  246. return self::outsuccess([]);
  247. } else {
  248. return self::outerr(errcode::ErrOperation, "系统错误.");
  249. }
  250. }
  251. public function addwphoneOp()
  252. {
  253. $mchid = $this->mchid();
  254. $phone = $_POST['phone'];
  255. if (empty($phone)) {
  256. return self::outerr(errcode::ErrParamter, "参数错误");
  257. }
  258. $phone = explode(',' ,trim($phone));
  259. $model_merchant = Model('merchant');
  260. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  261. if (empty($merchant_info['warning_phone'])) {
  262. $phones = [];
  263. } else {
  264. $phones = unserialize($merchant_info['warning_phone']);
  265. }
  266. foreach ($phone as $item) {
  267. $phones[] = $item;
  268. }
  269. $phones = array_unique($phones);
  270. $model_merchant->editMerchant(['warning_phone' => serialize($phones)], ['mchid' => $merchant_info['mchid']]);
  271. return self::outsuccess([]);
  272. }
  273. public function wphonedelOp()
  274. {
  275. $mchid = $this->mchid();
  276. $phone = $_POST['phone'];
  277. if (empty($phone)) {
  278. return self::outerr(errcode::ErrParamter, "参数错误");
  279. }
  280. $phone = trim($phone);
  281. $model_merchant = Model('merchant');
  282. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  283. $phones = unserialize($merchant_info['warning_phone']);
  284. $new_phones = [];
  285. foreach ($phones as $value) {
  286. if ($value != $phone) {
  287. $new_phones[] = $value;
  288. }
  289. }
  290. if (empty($new_phones)) {
  291. $new_phones = '';
  292. } else {
  293. $new_phones = serialize($new_phones);
  294. }
  295. $model_merchant->editMerchant(['warning_phone' => $new_phones], ['mchid' => $merchant_info['mchid']]);
  296. return self::outsuccess([]);
  297. }
  298. public function setkeyOp()
  299. {
  300. $mchid = $this->mchid();
  301. $secure_key = $_POST['secure_key'];
  302. if (empty($secure_key)) {
  303. return self::outerr(errcode::ErrParamter, "参数错误");
  304. }
  305. $model_merchant = Model('merchant');
  306. $ret = $model_merchant->editMerchant(['secure_key' => $secure_key, 'use_key' => 1], ['mchid' => $mchid]);
  307. if ($ret) {
  308. return self::outsuccess([]);
  309. } else {
  310. return self::outerr(errcode::ErrOperation, "系统错误.");
  311. }
  312. }
  313. public function modifypwOp()
  314. {
  315. $mchid = $this->mchid();
  316. $new_pw = $_POST['new_pw'];
  317. $new_pw2 = $_POST['new_pw2'];
  318. if (trim($new_pw) !== trim($new_pw2)) {
  319. return self::outerr(errcode::ErrPasswd, "密码错误");
  320. }
  321. $model_merchant = Model('merchant');
  322. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  323. if (!$merchant_info) {
  324. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  325. }
  326. $pwd = trim($new_pw);
  327. if (md5($pwd) == $merchant_info['password']) {
  328. return self::outsuccess([]);
  329. }
  330. $ret = $model_merchant->editMerchant(['password' => md5($pwd), 'org_pwd' => $pwd], ['mchid' => $merchant_info['mchid']]);
  331. if ($ret) {
  332. return self::outsuccess([]);
  333. } else {
  334. return self::outerr(errcode::ErrOperation, "系统错误.");
  335. }
  336. }
  337. public function pdlogOp()
  338. {
  339. $mchid = $this->mchid();
  340. $model_merchant = Model('merchant');
  341. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => $mchid]);
  342. if (!$merchant_info) {
  343. return self::outerr(errcode::ErrMemberNotExist, "用户不存在.");
  344. }
  345. $model_pd = Model('merchant');
  346. if (empty($merchant_info['admin_id'])) {
  347. $result['data'] = [];
  348. $result['total'] = 0;
  349. return self::outsuccess($result);
  350. }
  351. $cond['lg_member_id'] = $merchant_info['admin_id'];
  352. if (!empty($_GET['lg_type'])) {
  353. $cond['lg_type'] = $_GET['lg_type'];
  354. }
  355. if ($_GET['start_time'] && $_GET['end_time']) {
  356. $cond['lg_add_time'] = ['between', [$_GET['start_time'], $_GET['end_time']]];
  357. }
  358. $list = $model_pd->getPdlog($cond, $this->page, '*', 'lg_id desc');
  359. $list = $this->PdLogFormat($list);
  360. $result['data'] = $list;
  361. $result['total'] = $model_pd->gettotalpage();
  362. return self::outsuccess($result);
  363. }
  364. private function PdLogFormat($pdlog)
  365. {
  366. $data = [];
  367. foreach ($pdlog as $key => $value) {
  368. if (isset($value['lg_add_time'])) {
  369. $value['lg_add_time'] = date('Y-m-d H:i:s', $value['lg_add_time']);
  370. }
  371. switch ($value['lg_type']) {
  372. case 'order_pay':
  373. $value['lg_type_text'] = '下单减款';
  374. break;
  375. case 'order_cancel':
  376. $value['lg_type_text'] = '下单失败返回余款';
  377. break;
  378. case 'recharge':
  379. $value['lg_type_text'] = '余款充值';
  380. break;
  381. case 'sys_add_money':
  382. $value['lg_type_text'] = '管理员调节预存款【增加】';
  383. break;
  384. case 'sys_del_money':
  385. $value['lg_type_text'] = '管理员调节预存款【减少】';
  386. break;
  387. case 'sys_freeze_money':
  388. $value['lg_type_text'] = '管理员调节预存款【冻结】';
  389. break;
  390. case 'sys_unfreeze_money':
  391. $value['lg_type_text'] = '管理员调节预存款【解冻】';
  392. break;
  393. default:
  394. $value['lg_type_text'] = 'unknown';
  395. break;
  396. }
  397. $data[] = $value;
  398. }
  399. return $data;
  400. }
  401. }