Index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 WeappConfigModel;
  16. use app\model\goods\Goods as GoodsModel;
  17. use app\model\member\Member;
  18. use app\model\member\Member as MemberModel;
  19. use app\model\order\OrderCommon;
  20. use app\model\shop\Shop as ShopModel;
  21. use app\model\system\Addon;
  22. use app\model\system\Promotion as PrmotionModel;
  23. use app\model\system\Stat;
  24. use app\model\web\Config as WebConfigModel;
  25. use Carbon\Carbon;
  26. use app\model\order\OrderRefund as OrderRefundModel;
  27. use think\facade\Cache;
  28. use addon\wechat\model\Config as WechatConfig;
  29. use addon\weapp\model\Config as WeappConfig;
  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. header('Location:../../shop/stat/shop');
  94. exit;
  95. }
  96. }
  97. private function init()
  98. {
  99. $is_new_version = 0; // 检查小程序是否有新版本
  100. if (addon_is_exit("weapp")) {
  101. $weapp_config_model = new WeappConfigModel();
  102. // 获取站点小程序版本信息
  103. $version_info = $weapp_config_model->getWeappVersion($this->site_id);
  104. $version_info = $version_info[ 'data' ][ 'value' ];
  105. $currrent_version_info = config('info');
  106. if (!isset($version_info[ 'version' ]) || ( isset($version_info[ 'version' ]) && $version_info[ 'version' ] != $currrent_version_info[ 'version_no' ] )) {
  107. $is_new_version = 1;
  108. }
  109. }
  110. $this->assign('is_new_version', $is_new_version);
  111. $is_new_domain = 0; // 检查域名是否发生变化
  112. $web_config_model = new WebConfigModel();
  113. $shop_domain_config = $web_config_model->getShopDomainConfig()[ 'data' ][ 'value' ];
  114. if ($shop_domain_config[ 'domain_name' ] != __ROOT__) {
  115. $is_new_domain = 1;
  116. }
  117. $this->assign('is_new_domain', $is_new_domain);
  118. //商城状态
  119. $shop_model = new ShopModel();
  120. $shop_status_result = $shop_model->getShopStatus($this->site_id, $this->app_module);
  121. $shop_status = $shop_status_result[ 'data' ][ 'value' ];
  122. $this->assign('shop_status', $shop_status);
  123. }
  124. /**
  125. * 获取营销活动 添加快捷菜单的优先排序
  126. */
  127. public function handlePromotion()
  128. {
  129. $promotion_model = new PrmotionModel();
  130. $promotions = $promotion_model->getSitePromotions($this->site_id);
  131. $promotion = array_values(array_filter(array_map(function($item) { if ($item[ 'show_type' ] == 'shop' || $item[ 'show_type' ] == 'member') return $item; }, $promotions)));
  132. $tool = array_values(array_filter(array_map(function($item) { if ($item[ 'show_type' ] == 'tool') return $item; }, $promotions)));
  133. $promotion = array_column($promotion, null, 'name');
  134. $tool = array_column($tool, null, 'name');
  135. $addon_model = new Addon();
  136. $value = $addon_model->getAddonQuickMenuConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  137. $promotion_addon = $value[ 'promotion' ];
  138. $tool_addon = $value[ 'tool' ];
  139. if (!empty($promotion_addon)) {
  140. foreach ($promotion_addon as $name) {
  141. if (isset($promotion[ $name ])) {
  142. array_unshift($promotion, $promotion[ $name ]);
  143. unset($promotion[ $name ]);
  144. }
  145. }
  146. }
  147. if (!empty($tool_addon)) {
  148. foreach ($tool_addon as $name) {
  149. if (isset($tool[ $name ])) {
  150. array_unshift($tool, $tool[ $name ]);
  151. unset($tool[ $name ]);
  152. }
  153. }
  154. }
  155. $this->assign("promotion", $promotion);
  156. $this->assign("tool", $tool);
  157. }
  158. /**
  159. * 今日昨日统计
  160. * @return array
  161. */
  162. public function dayCount()
  163. {
  164. if (request()->isAjax()) {
  165. //基础统计信息
  166. $stat_shop_model = new Stat();
  167. $today = Carbon::now();
  168. $yesterday = Carbon::yesterday();
  169. $stat_today = $stat_shop_model->getShopStatSum($this->site_id, $today->startOfDay()->timestamp, $today->endOfDay()->timestamp);
  170. $stat_yesterday = $stat_shop_model->getShopStatSum($this->site_id, $yesterday->startOfDay()->timestamp, $yesterday->endOfDay()->timestamp);
  171. $order = new Order();
  172. //获取总数
  173. $shop_stat_sum = $stat_shop_model->getShopStatSum($this->site_id);
  174. $goods_model = new GoodsModel();
  175. $goods_sum = $goods_model->getGoodsTotalCount([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  176. $shop_stat_sum[ 'data' ][ 'goods_count' ] = $goods_sum[ 'data' ];
  177. $shop_stat_sum[ 'data' ]['member_count'] = (new Member())->getMemberCount([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ])[ 'data' ];
  178. $shop_stat_sum[ 'data' ][ 'order_pay_count' ] = $order->getOrderCount([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ], [ 'pay_status', '=', 1 ] ])['data'];
  179. $shop_stat_sum[ 'data' ][ 'earnings_total_money' ] = $order->getOrderMoneySum([ [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ], [ 'pay_status', '=', 1 ] ], 'pay_money')['data'];
  180. //日同比
  181. $day_rate[ 'order_pay_count' ] = diff_rate($stat_today[ 'data' ][ 'order_pay_count' ], $stat_yesterday[ 'data' ][ 'order_pay_count' ]);
  182. $day_rate[ 'order_total' ] = diff_rate($stat_today[ 'data' ][ 'order_total' ], $stat_yesterday[ 'data' ][ 'order_total' ]);
  183. $day_rate[ 'earnings_total_money' ] = diff_rate($stat_today[ 'data' ][ 'earnings_total_money' ], $stat_yesterday[ 'data' ][ 'earnings_total_money' ]);
  184. $day_rate[ 'collect_goods' ] = diff_rate($stat_today[ 'data' ][ 'collect_goods' ], $stat_yesterday[ 'data' ][ 'collect_goods' ]);
  185. $day_rate[ 'visit_count' ] = diff_rate($stat_today[ 'data' ][ 'visit_count' ], $stat_yesterday[ 'data' ][ 'visit_count' ]);
  186. $day_rate[ 'member_count' ] = diff_rate($stat_today[ 'data' ][ 'member_count' ], $stat_yesterday[ 'data' ][ 'member_count' ]);
  187. //会员总数
  188. $member_model = new MemberModel();
  189. $member_count = $member_model->getMemberCount([ [ 'site_id', '=', $this->site_id ] ]);
  190. $res = [
  191. 'stat_day' => $stat_today[ 'data' ],
  192. 'stat_yesterday' => $stat_yesterday[ 'data' ],
  193. 'today' => $today,
  194. 'shop_stat_sum' => $shop_stat_sum[ 'data' ],
  195. 'day_rate' => $day_rate,
  196. 'member_count' => $member_count[ 'data' ]
  197. ];
  198. }
  199. return $res;
  200. }
  201. /**
  202. * 综合统计
  203. * @return array
  204. */
  205. public function sumCount()
  206. {
  207. if (request()->isAjax()) {
  208. $goods_model = new GoodsModel();
  209. $order = new OrderCommon();
  210. $waitpay = $order->getOrderCount([ [ 'order_status', '=', 0 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ], [ 'order_scene', '=', 'online' ] ]);
  211. $waitsend = $order->getOrderCount([ [ 'order_status', '=', 1 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  212. $order_refund_model = new OrderRefundModel();
  213. $refund_num = $order_refund_model->getRefundOrderGoodsCount([
  214. [ "site_id", "=", $this->site_id ],
  215. [ "refund_status", "not in", [ 0, 3 ] ]
  216. ]);
  217. $goods_stock_alarm = $goods_model->getGoodsStockAlarm($this->site_id);
  218. $goods_total = $goods_model->getGoodsTotalCount([ [ 'goods_state', '=', 1 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  219. $warehouse_goods = $goods_model->getGoodsTotalCount([ [ 'goods_state', '=', 0 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  220. $num_data = [
  221. 'waitpay' => $waitpay[ 'data' ],
  222. 'waitsend' => $waitsend[ 'data' ],
  223. 'refund' => $refund_num[ 'data' ],
  224. 'goods_stock_alarm' => count($goods_stock_alarm[ 'data' ]),
  225. 'goods_total' => $goods_total[ 'data' ],
  226. 'warehouse_goods' => $warehouse_goods[ 'data' ]
  227. ];
  228. //分销插件是否存在
  229. $is_fenxiao = addon_is_exit('fenxiao', $this->site_id);
  230. $this->assign('is_fenxiao', $is_fenxiao);
  231. if ($is_fenxiao) {
  232. //提现待审核
  233. $fenxiao_model = new FenxiaoWithdraw();
  234. $withdraw_count = $fenxiao_model->getFenxiaoWithdrawCount([ [ 'site_id', '=', $this->site_id ], [ 'status', '=', 1 ] ], 'id');
  235. $num_data[ 'withdraw_count' ] = $withdraw_count[ 'data' ];
  236. //分销商申请
  237. $fenxiao_apply_model = new FenxiaoApply();
  238. $fenxiao_count = $fenxiao_apply_model->getFenxiaoApplyCount([ [ 'site_id', '=', $this->site_id ], [ 'status', '=', 1 ] ], 'apply_id');
  239. $num_data[ 'apply_count' ] = $fenxiao_count[ 'data' ];
  240. } else {
  241. $waitconfirm = $order->getOrderCount([ [ 'order_status', "=", 3 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  242. $complete = $order->getOrderCount([ [ 'order_status', "=", 10 ], [ 'site_id', '=', $this->site_id ], [ 'is_delete', '=', 0 ] ]);
  243. $num_data[ 'waitconfirm' ] = $waitconfirm[ 'data' ];
  244. $num_data[ 'complete' ] = $complete[ 'data' ];
  245. }
  246. }
  247. return $num_data;
  248. }
  249. /**
  250. * 图形统计
  251. *
  252. * @return void
  253. */
  254. public function chartCount()
  255. {
  256. if (request()->isAjax()) {
  257. //近十天的订单数以及销售金额
  258. $stat_shop_model = new Stat();
  259. $date_day = getweeks();
  260. $order_total = '';
  261. $order_pay_count = '';
  262. foreach ($date_day as $k => $day) {
  263. $dayarr = explode('-', $day);
  264. $stat_day[ $k ] = $stat_shop_model->getStatShop($this->site_id, $dayarr[ 0 ], $dayarr[ 1 ], $dayarr[ 2 ]);
  265. $order_total .= $stat_day[ $k ][ 'data' ][ 'order_total' ] . ',';
  266. $order_pay_count .= $stat_day[ $k ][ 'data' ][ 'order_pay_count' ] . ',';
  267. }
  268. $ten_day[ 'order_total' ] = explode(',', substr($order_total, 0, strlen($order_total) - 1));
  269. $ten_day[ 'order_pay_count' ] = explode(',', substr($order_pay_count, 0, strlen($order_pay_count) - 1));
  270. return $ten_day;
  271. }
  272. }
  273. /**
  274. * 营销插件
  275. * @return array
  276. */
  277. public function marketingPlug()
  278. {
  279. if (request()->isAjax()) {
  280. //营销活动
  281. $promotion_model = new PrmotionModel();
  282. $promotions = $promotion_model->getSitePromotions($this->site_id);
  283. $toolcount = 0;
  284. $shopcount = 0;
  285. //营销插件数量
  286. foreach ($promotions as $k => $v) {
  287. if ($v[ "show_type" ] == 'tool') {
  288. $toolcount += 1;
  289. }
  290. if ($v[ "show_type" ] == 'member' || $v[ "show_type" ] == 'shop') {
  291. $shopcount += 1;
  292. }
  293. }
  294. $count = [
  295. 'toolcount' => $toolcount,
  296. 'shopcount' => $shopcount
  297. ];
  298. $res = [
  299. 'count' => $count,
  300. 'promotions' => $promotions
  301. ];
  302. return $res;
  303. }
  304. }
  305. }