Index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\shop\controller;
  11. use addon\fenxiao\model\FenxiaoApply;
  12. use addon\fenxiao\model\FenxiaoWithdraw;
  13. use addon\niusms\model\Config as NiuSmsConfig;
  14. use addon\niusms\model\Sms as NiuSms;
  15. use addon\weapp\model\Config as WeappConfig;
  16. use addon\weapp\model\Config as WeappConfigModel;
  17. use app\model\goods\Goods as GoodsModel;
  18. use app\model\member\Member;
  19. use app\model\member\Member as MemberModel;
  20. use app\model\order\OrderCommon;
  21. use app\model\shop\Shop as ShopModel;
  22. use app\model\system\Addon;
  23. use app\model\system\Promotion as PrmotionModel;
  24. use app\model\system\Stat;
  25. use app\model\web\Config as WebConfigModel;
  26. use Carbon\Carbon;
  27. use app\model\order\OrderRefund as OrderRefundModel;
  28. use think\facade\Cache;
  29. use addon\wechat\model\Config as WechatConfig;
  30. use addon\alipay\model\Config as AlipayConfig;
  31. use addon\wechatpay\model\Config as WechatpayConfig;
  32. use app\model\order\Order;
  33. class Index extends BaseShop
  34. {
  35. /**
  36. * 首页
  37. * @return mixed
  38. */
  39. public function index()
  40. {
  41. $demain = $_SERVER['HTTP_HOST'];
  42. Cache::tag("cache_tablestat_shop")->clear();
  43. Cache::tag("cache_tablemember")->clear();
  44. Cache::tag("cache_tablegoods")->clear();
  45. Cache::tag("cache_tableorder")->clear();
  46. Cache::tag("cache_tablefenxiao_apply")->clear();
  47. Cache::tag("cache_tablefenxiao_withdraw")->clear();
  48. $shop_info = $this->shop_info;
  49. $time = time();
  50. $user_info = $this->user_info;
  51. if($user_info['is_admin']){
  52. $this->assign('shop_status', 1);
  53. $this->handlePromotion();
  54. //分销插件是否存在
  55. $is_fenxiao = addon_is_exit('fenxiao', $this->site_id);
  56. $this->assign('is_fenxiao', $is_fenxiao);
  57. //基础统计信息
  58. $today = Carbon::now();
  59. $this->assign("today", $today);
  60. // 牛云短信余额查询
  61. if (addon_is_exit('niusms', $this->site_id)) {
  62. $sms_config = ( new NiuSmsConfig() )->getSmsConfig($this->site_id)[ 'data' ];
  63. if ($sms_config[ 'is_use' ]) {
  64. $account_info = ( new NiuSms() )->getChildAccountInfo([
  65. 'username' => $sms_config[ 'value' ][ 'username' ],
  66. ]);
  67. $this->assign('sms_num', $account_info[ 'data' ][ 'balance' ] ?? 0);
  68. }
  69. }
  70. $this->assign('guide_close', cookie('guideClose'));
  71. if (!cookie('guideClose')) {
  72. $goods = ( new GoodsModel() )->getGoodsInfo([ [ 'site_id', '=', $this->site_id ] ], 'goods_id')[ 'data' ];
  73. $this->assign('goods_complete', !empty($goods));
  74. $wechat_config = ( new WechatConfig() )->getWechatConfig($this->site_id)[ 'data' ][ 'value' ];
  75. $this->assign('wechat_complete', !empty($wechat_config));
  76. if (addon_is_exit('weapp', $this->site_id)) {
  77. $weapp_config = ( new WeappConfig() )->getWeappConfig($this->site_id)[ 'data' ][ 'value' ];
  78. $this->assign('weapp_complete', !empty($weapp_config));
  79. } else {
  80. $this->assign('weapp_complete', false);
  81. }
  82. $alipay_config = addon_is_exit('alipay', $this->site_id) ? ( new AlipayConfig() )->getPayConfig($this->site_id)[ 'data' ][ 'value' ] : [];
  83. $wechatpay_config = [];
  84. $wechatpay_config = ( new WechatpayConfig() )->getPayConfig($this->site_id)[ 'data' ][ 'value' ];
  85. unset($wechatpay_config[ 'transfer_type' ]);
  86. $this->assign('pay_complete', ( !( empty($alipay_config) ) || !( empty($wechatpay_config) ) ));
  87. $this->assign('site_complete', !empty($this->shop_info[ 'logo' ]));
  88. }
  89. $this->init();
  90. $this->assign('img_extension_error', config('upload.driver') == 'imagick' && !extension_loaded('imagick'));
  91. return $this->fetch("index/index");
  92. }else{
  93. $demain = $_SERVER['HTTP_HOST'];
  94. var_dump($demain);
  95. // header('Localtion:zhongshen.demo.com/shop/stat/shop.html');
  96. header('Location:../../shop/stat/shop');
  97. exit;
  98. }
  99. }
  100. private function init()
  101. {
  102. $is_new_version = 0; // 检查小程序是否有新版本
  103. if (addon_is_exit("weapp")) {
  104. $weapp_config_model = new WeappConfigModel();
  105. // 获取站点小程序版本信息
  106. $version_info = $weapp_config_model->getWeappVersion($this->site_id);
  107. $version_info = $version_info[ 'data' ][ 'value' ];
  108. $currrent_version_info = config('info');
  109. if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
  110. $is_new_version = 1;
  111. }
  112. }
  113. $this->assign('is_new_version', $is_new_version);
  114. $is_new_domain = 0; // 检查域名是否发生变化
  115. $web_config_model = new WebConfigModel();
  116. $shop_domain_config = $web_config_model->getShopDomainConfig()[ 'data' ][ 'value' ];
  117. if ($shop_domain_config[ 'domain_name' ] != __ROOT__) {
  118. $is_new_domain = 1;
  119. }
  120. $this->assign('is_new_domain', $is_new_domain);
  121. //商城状态
  122. $shop_model = new ShopModel();
  123. $shop_status_result = $shop_model->getShopStatus($this->site_id, $this->app_module);
  124. $shop_status = $shop_status_result[ 'data' ][ 'value' ];
  125. $this->assign('shop_status', $shop_status);
  126. }
  127. /**
  128. * 获取营销活动 添加快捷菜单的优先排序
  129. */
  130. public function handlePromotion()
  131. {
  132. $promotion_model = new PrmotionModel();
  133. $promotions = $promotion_model->getSitePromotions($this->site_id);
  134. $promotion = array_values(array_filter(array_map(function($item) { if ($item[ 'show_type' ] == 'shop' || $item[ 'show_type' ] == 'member') return $item; }, $promotions)));
  135. $tool = array_values(array_filter(array_map(function($item) { if ($item[ 'show_type' ] == 'tool') return $item; }, $promotions)));
  136. $promotion = array_column($promotion, null, 'name');
  137. $tool = array_column($tool, null, 'name');
  138. $addon_model = new Addon();
  139. $value = $addon_model->getAddonQuickMenuConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  140. $promotion_addon = $value[ 'promotion' ];
  141. $tool_addon = $value[ 'tool' ];
  142. if (!empty($promotion_addon)) {
  143. foreach ($promotion_addon as $name) {
  144. if (isset($promotion[ $name ])) {
  145. array_unshift($promotion, $promotion[ $name ]);
  146. unset($promotion[ $name ]);
  147. }
  148. }
  149. }
  150. if (!empty($tool_addon)) {
  151. foreach ($tool_addon as $name) {
  152. if (isset($tool[ $name ])) {
  153. array_unshift($tool, $tool[ $name ]);
  154. unset($tool[ $name ]);
  155. }
  156. }
  157. }
  158. $this->assign("promotion", $promotion);
  159. $this->assign("tool", $tool);
  160. }
  161. /**
  162. * 今日昨日统计
  163. * @return array
  164. */
  165. public function dayCount()
  166. {
  167. if (request()->isAjax()) {
  168. //基础统计信息
  169. $stat_shop_model = new Stat();
  170. $today = Carbon::now();
  171. $yesterday = Carbon::yesterday();
  172. $stat_today = $stat_shop_model->getShopStatSum($this->site_id, $today->startOfDay()->timestamp, $today->endOfDay()->timestamp);
  173. $stat_yesterday = $stat_shop_model->getShopStatSum($this->site_id, $yesterday->startOfDay()->timestamp, $yesterday->endOfDay()->timestamp);
  174. $order = new Order();
  175. //获取总数
  176. $shop_stat_sum = $stat_shop_model->getShopStatSum($this->site_id);
  177. $goods_model = new GoodsModel();
  178. $goods_sum = $goods_model->getGoodsTotalCount([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  179. $shop_stat_sum[ 'data' ][ 'goods_count' ] = $goods_sum[ 'data' ];
  180. $shop_stat_sum[ 'data' ]['member_count'] = (new Member())->getMemberCount([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ])[ 'data' ];
  181. $shop_stat_sum[ 'data' ][ 'order_pay_count' ] = $order->getOrderCount([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ], [ 'pay_status', '=', 1 ] ])['data'];
  182. $shop_stat_sum[ 'data' ][ 'earnings_total_money' ] = $order->getOrderMoneySum([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ], [ 'pay_status', '=', 1 ] ], 'pay_money')['data'];
  183. //日同比
  184. $day_rate[ 'order_pay_count' ] = diff_rate($stat_today[ 'data' ][ 'order_pay_count' ], $stat_yesterday[ 'data' ][ 'order_pay_count' ]);
  185. $day_rate[ 'order_total' ] = diff_rate($stat_today[ 'data' ][ 'order_total' ], $stat_yesterday[ 'data' ][ 'order_total' ]);
  186. $day_rate[ 'earnings_total_money' ] = diff_rate($stat_today[ 'data' ][ 'earnings_total_money' ], $stat_yesterday[ 'data' ][ 'earnings_total_money' ]);
  187. $day_rate[ 'collect_goods' ] = diff_rate($stat_today[ 'data' ][ 'collect_goods' ], $stat_yesterday[ 'data' ][ 'collect_goods' ]);
  188. $day_rate[ 'visit_count' ] = diff_rate($stat_today[ 'data' ][ 'visit_count' ], $stat_yesterday[ 'data' ][ 'visit_count' ]);
  189. $day_rate[ 'member_count' ] = diff_rate($stat_today[ 'data' ][ 'member_count' ], $stat_yesterday[ 'data' ][ 'member_count' ]);
  190. //会员总数
  191. $member_model = new MemberModel();
  192. $member_count = $member_model->getMemberCount([ [ 'site_id', '=', $this->site_id ] ]);
  193. $res = [
  194. 'stat_day' => $stat_today[ 'data' ],
  195. 'stat_yesterday' => $stat_yesterday[ 'data' ],
  196. 'today' => $today,
  197. 'shop_stat_sum' => $shop_stat_sum[ 'data' ],
  198. 'day_rate' => $day_rate,
  199. 'member_count' => $member_count[ 'data' ]
  200. ];
  201. }
  202. return $res;
  203. }
  204. /**
  205. * 综合统计
  206. * @return array
  207. */
  208. public function sumCount()
  209. {
  210. if (request()->isAjax()) {
  211. $goods_model = new GoodsModel();
  212. $order = new OrderCommon();
  213. $waitpay = $order->getOrderCount([ [ 'order_status', '=', 0 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ], [ 'order_scene', '=', 'online' ] ]);
  214. $waitsend = $order->getOrderCount([ [ 'order_status', '=', 1 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  215. $order_refund_model = new OrderRefundModel();
  216. $refund_num = $order_refund_model->getRefundOrderGoodsCount([
  217. [ "site_id", "=", $this->site_id ],
  218. [ "refund_status", "not in", [ 0, 3 ] ]
  219. ]);
  220. $goods_stock_alarm = $goods_model->getGoodsStockAlarm($this->site_id);
  221. $goods_total = $goods_model->getGoodsTotalCount([ [ 'goods_state', '=', 1 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  222. $warehouse_goods = $goods_model->getGoodsTotalCount([ [ 'goods_state', '=', 0 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  223. $num_data = [
  224. 'waitpay' => $waitpay[ 'data' ],
  225. 'waitsend' => $waitsend[ 'data' ],
  226. 'refund' => $refund_num[ 'data' ],
  227. 'goods_stock_alarm' => count($goods_stock_alarm[ 'data' ]),
  228. 'goods_total' => $goods_total[ 'data' ],
  229. 'warehouse_goods' => $warehouse_goods[ 'data' ]
  230. ];
  231. //分销插件是否存在
  232. $is_fenxiao = addon_is_exit('fenxiao', $this->site_id);
  233. $this->assign('is_fenxiao', $is_fenxiao);
  234. if ($is_fenxiao) {
  235. //提现待审核
  236. $fenxiao_model = new FenxiaoWithdraw();
  237. $withdraw_count = $fenxiao_model->getFenxiaoWithdrawCount([ [ 'site_id', '=', $this->site_id ], [ 'status', '=', 1 ] ], 'id');
  238. $num_data[ 'withdraw_count' ] = $withdraw_count[ 'data' ];
  239. //分销商申请
  240. $fenxiao_apply_model = new FenxiaoApply();
  241. $fenxiao_count = $fenxiao_apply_model->getFenxiaoApplyCount([ [ 'site_id', '=', $this->site_id ], [ 'status', '=', 1 ] ], 'apply_id');
  242. $num_data[ 'apply_count' ] = $fenxiao_count[ 'data' ];
  243. } else {
  244. $waitconfirm = $order->getOrderCount([ [ 'order_status', "=", 3 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  245. $complete = $order->getOrderCount([ [ 'order_status', "=", 10 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  246. $num_data[ 'waitconfirm' ] = $waitconfirm[ 'data' ];
  247. $num_data[ 'complete' ] = $complete[ 'data' ];
  248. }
  249. }
  250. return $num_data;
  251. }
  252. /**
  253. * 图形统计
  254. *
  255. * @return void
  256. */
  257. public function chartCount()
  258. {
  259. if (request()->isAjax()) {
  260. //近十天的订单数以及销售金额
  261. $stat_shop_model = new Stat();
  262. $date_day = getweeks();
  263. $order_total = '';
  264. $order_pay_count = '';
  265. foreach ($date_day as $k => $day) {
  266. $dayarr = explode('-', $day);
  267. $stat_day[ $k ] = $stat_shop_model->getStatShop($this->site_id, $dayarr[ 0 ], $dayarr[ 1 ], $dayarr[ 2 ]);
  268. $order_total .= $stat_day[ $k ][ 'data' ][ 'order_total' ] . ',';
  269. $order_pay_count .= $stat_day[ $k ][ 'data' ][ 'order_pay_count' ] . ',';
  270. }
  271. $ten_day[ 'order_total' ] = explode(',', substr($order_total, 0, strlen($order_total) - 1));
  272. $ten_day[ 'order_pay_count' ] = explode(',', substr($order_pay_count, 0, strlen($order_pay_count) - 1));
  273. return $ten_day;
  274. }
  275. }
  276. /**
  277. * 营销插件
  278. * @return array
  279. */
  280. public function marketingPlug()
  281. {
  282. if (request()->isAjax()) {
  283. //营销活动
  284. $promotion_model = new PrmotionModel();
  285. $promotions = $promotion_model->getSitePromotions($this->site_id);
  286. $toolcount = 0;
  287. $shopcount = 0;
  288. //营销插件数量
  289. foreach ($promotions as $k => $v) {
  290. if ($v[ "show_type" ] == 'tool') {
  291. $toolcount += 1;
  292. }
  293. if ($v[ "show_type" ] == 'member' || $v[ "show_type" ] == 'shop') {
  294. $shopcount += 1;
  295. }
  296. }
  297. $count = [
  298. 'toolcount' => $toolcount,
  299. 'shopcount' => $shopcount
  300. ];
  301. $res = [
  302. 'count' => $count,
  303. 'promotions' => $promotions
  304. ];
  305. return $res;
  306. }
  307. }
  308. }