Index.php 15 KB

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