IncomePageController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. *
  5. * User: king/QQ:995265288
  6. * Date: 2018/5/8 下午2:11
  7. * Email: livsyitian@163.com
  8. */
  9. namespace app\frontend\modules\finance\controllers;
  10. use app\common\components\ApiController;
  11. use app\common\exceptions\AppException;
  12. use app\common\helpers\ImageHelper;
  13. use app\common\models\Income;
  14. use app\common\services\popularize\PortType;
  15. use app\framework\Http\Request;
  16. use app\frontend\models\Member;
  17. use app\frontend\models\MemberRelation;
  18. use app\frontend\models\MemberShopInfo;
  19. use app\frontend\modules\finance\factories\IncomePageFactory;
  20. use app\frontend\modules\finance\services\ExtensionCenterService;
  21. use app\frontend\modules\finance\services\PluginSettleService;
  22. use app\frontend\modules\member\models\MemberModel;
  23. use app\frontend\modules\member\services\MemberService;
  24. use Yunshop\Designer\home\IndexController;
  25. use Yunshop\HighLight\services\SetService;
  26. use Yunshop\WithdrawalLimit\Common\models\MemberWithdrawalLimit;
  27. class IncomePageController extends ApiController
  28. {
  29. private $relationSet;
  30. private $is_agent;
  31. private $grand_total;
  32. private $usable_total;
  33. public function preAction()
  34. {
  35. parent::preAction();
  36. $this->relationSet = $this->getRelationSet();
  37. }
  38. /**
  39. * 收入页面接口
  40. *
  41. * @return \Illuminate\Http\JsonResponse
  42. * @throws \app\common\exceptions\AppException
  43. */
  44. public function index(Request $request)
  45. {
  46. if (miniVersionCompare('1.1.115') && versionCompare('1.1.115')) {
  47. //版本符合
  48. if (app('plugins')->isEnabled('decorate') && \Setting::get('plugin.decorate.is_open') == "1") {
  49. //推广中心模版
  50. $view_set = \Yunshop\Decorate\models\DecorateTempletModel::getList(['is_default'=>1,'type'=>2],'*',false);
  51. if (empty($view_set) || $view_set->code == 'extension01') {
  52. return $this->newIndex();
  53. }
  54. } else {
  55. return $this->newIndex();
  56. }
  57. }
  58. $this->dataIntegrated(['status' => 1, 'json' => ''],'template_set');
  59. $this->dataIntegrated($this->getIncomePage($request, true),'income_page');
  60. if(app('plugins')->isEnabled('designer'))
  61. {
  62. $this->dataIntegrated((new IndexController())->templateSet($request, true),'template_set');
  63. }
  64. if (app('plugins')->isEnabled('high-light') && SetService::getStatus()) {
  65. $this->dataIntegrated(\Yunshop\HighLight\services\WithdrawService::getHighLightUrl(),'high_light');
  66. }
  67. return $this->successJson('', $this->apiData);
  68. }
  69. /**
  70. * @param $request
  71. * @return \Illuminate\Http\JsonResponse
  72. * @throws AppException
  73. */
  74. public function newIndex()
  75. {
  76. $this->dataIntegrated(['status' => 1, 'json' => ''],'template_set');
  77. ExtensionCenterService::init(request());
  78. $this->apiData['income_page'] = ExtensionCenterService::getIncomePage();
  79. return $this->successJson('', $this->apiData);
  80. }
  81. /**
  82. * 收入统计
  83. * @return \Illuminate\Http\JsonResponse
  84. */
  85. public function incomeStatistic()
  86. {
  87. ExtensionCenterService::init(request());
  88. $data = ExtensionCenterService::incomeStatistic();
  89. return $this->successJson('ok', $data);
  90. }
  91. /**
  92. * 收入统计图(动态、占比)
  93. * @return \Illuminate\Http\JsonResponse
  94. */
  95. public function incomeCharts()
  96. {
  97. ExtensionCenterService::init(request());
  98. if (!empty(request()->charts_type) && request()->charts_type == 1) {
  99. $data = ExtensionCenterService::incomeProportion();
  100. } else {
  101. $data = ExtensionCenterService::incomeDynamic();
  102. }
  103. return $this->successJson('ok', $data);
  104. }
  105. /**
  106. * 粉丝数据统计图(裂变、转化)
  107. * @return \Illuminate\Http\JsonResponse
  108. */
  109. public function fansCharts()
  110. {
  111. ExtensionCenterService::init(request());
  112. if (!empty(request()->charts_type) && request()->charts_type == 1) {
  113. $data = ExtensionCenterService::fansConversion();
  114. } else {
  115. $data = ExtensionCenterService::fansFission();
  116. }
  117. return $this->successJson('ok', $data);
  118. }
  119. /**
  120. * 推广订单
  121. * @return \Illuminate\Http\JsonResponse
  122. */
  123. public function extension()
  124. {
  125. ExtensionCenterService::init(request());
  126. if (!empty(request()->extension_type) && request()->extension_type == 1) {
  127. $data = ExtensionCenterService::extensionFans();
  128. } else {
  129. $data = ExtensionCenterService::extensionOrder();
  130. }
  131. return $this->successJson('ok',$data);
  132. }
  133. /**
  134. * @param $request
  135. * @param null $integrated
  136. * @return array|\Illuminate\Http\JsonResponse
  137. * @throws \app\common\exceptions\AppException
  138. */
  139. public function getIncomePage(Request $request, $integrated = null)
  140. {
  141. //检测是否推广员
  142. $this->is_agent = $this->isAgent();
  143. //不是推广员且有设置跳转链接时
  144. $relation_set = \Setting::get('member.relation');
  145. $extension_set = \Setting::get('popularize.mini');
  146. $jump_link = '';
  147. $small_jump_link = '';
  148. $small_extension_link = '';
  149. if ($relation_set['is_jump'] && !empty($relation_set['jump_link'])) {
  150. if (!$this->is_agent) {
  151. $jump_link = $relation_set['jump_link'];
  152. $small_jump_link = $relation_set['small_jump_link'];
  153. $small_extension_link = $extension_set['small_extension_link'];
  154. if(is_null($integrated)){
  155. return $this->successJson('ok', ['jump_link' => $jump_link,'small_jump_link'=>$small_jump_link,'small_extension_link'=>$small_extension_link]);
  156. }else{
  157. return show_json(1,['jump_link' => $jump_link,'small_jump_link'=>$small_jump_link,'small_extension_link'=>$small_extension_link]);
  158. }
  159. }
  160. }
  161. list($available, $unavailable) = $this->getIncomeInfo();
  162. //添加商城营业额
  163. $is_show_performance = OrderAllController::isShow();
  164. //更多权限是否显示
  165. $is_show_unable = PortType::isShowUnable(\YunShop::request()->type);
  166. //提现额度
  167. $withdraw_limit = [
  168. 'is_show' => false
  169. ];
  170. if(app('plugins')->isEnabled('withdrawal-limit'))
  171. {
  172. $limit_set = array_pluck(\Setting::getAllByGroup('withdrawal-limit')->toArray(), 'value', 'key');
  173. if($limit_set['is_open'] == 1 && $limit_set['is_show'] == 1)
  174. {
  175. $memberModel = MemberWithdrawalLimit::uniacid()->where('member_id',\YunShop::app()->getMemberId())->first();
  176. if($memberModel){
  177. $limit = $memberModel->total_amount;
  178. }else{
  179. $limit = 0;
  180. }
  181. $withdraw_limit = [
  182. 'is_show' => true,
  183. 'amount' => $limit
  184. ];
  185. }
  186. }
  187. $data = [
  188. 'info' => $this->getPageInfo(),
  189. 'parameter' => $this->getParameter(),
  190. 'available' => $available,
  191. 'unavailable' => $unavailable,
  192. 'is_show_performance' => $is_show_performance,
  193. 'jump_link' => $jump_link,
  194. 'small_jump_link' => $small_jump_link,
  195. 'small_extension_link' => $small_extension_link,
  196. 'is_show_unable' => $is_show_unable,
  197. 'withdraw_limit' => $withdraw_limit,
  198. 'withdraw_date' => $this->getWithdrawDate(),
  199. 'show_member_id' => PortType::showMemberId(\YunShop::request()->type),
  200. 'member_auth_pop_switch' => \Setting::get('plugin.min_app.member_auth_pop_switch') ? 1 : 0,
  201. ];
  202. if(is_null($integrated)){
  203. return $this->successJson('ok', $data);
  204. }else{
  205. return show_json(1,$data);
  206. }
  207. }
  208. /**
  209. * 页面信息
  210. *
  211. * @return array
  212. */
  213. private function getPageInfo()
  214. {
  215. $autoWithdraw = 0;
  216. if (app('plugins')->isEnabled('mryt')) {
  217. $uid = \YunShop::app()->getMemberId();
  218. $autoWithdraw = (new \Yunshop\Mryt\services\AutoWithdrawService())->isWithdraw($uid);
  219. }
  220. if (app('plugins')->isEnabled('team-dividend')) {
  221. $uid = \YunShop::app()->getMemberId();
  222. $autoWithdraw = (new \Yunshop\TeamDividend\services\AutoWithdrawService())->isWithdraw($uid);
  223. }
  224. $member_id = \YunShop::app()->getMemberId();
  225. $memberModel = Member::select('nickname', 'avatar', 'uid')->whereUid($member_id)->first();
  226. //IOS时,把微信头像url改为https前缀
  227. $avatar = ImageHelper::iosWechatAvatar($memberModel->avatar);
  228. return [
  229. 'avatar' => $avatar,
  230. 'nickname' => $memberModel->nickname,
  231. 'member_id' => $memberModel->uid,
  232. 'grand_total' => $this->grand_total,
  233. 'usable_total' => $this->usable_total,
  234. 'auto_withdraw' => $autoWithdraw,
  235. ];
  236. }
  237. private function getParameter()
  238. {
  239. return [
  240. 'share_page' => $this->getSharePageStatus(),
  241. 'plugin_settle_show' => PluginSettleService::doesIsShow(), //领取收益 开关是否显示
  242. ];
  243. }
  244. /**
  245. * 收入信息
  246. * @return array
  247. * @throws \app\common\exceptions\AppException
  248. */
  249. private function getIncomeInfo()
  250. {
  251. $lang_set = $this->getLangSet();
  252. $is_relation = $this->isOpenRelation();
  253. $config = $this->getIncomePageConfig();
  254. $total_income = $this->getTotalIncome();
  255. //是否显示推广插件入口
  256. $popularize_set = PortType::popularizeSet(\YunShop::request()->type);
  257. $available = [];
  258. $unavailable = [];
  259. foreach ($config as $key => $item) {
  260. $incomeFactory = new IncomePageFactory(new $item['class'], $lang_set, $is_relation, $this->is_agent, $total_income,$key);
  261. if (!$incomeFactory->isShow()) {
  262. continue;
  263. }
  264. //不显示
  265. if (in_array($incomeFactory->getAppUrl(), $popularize_set)) {
  266. continue;
  267. }
  268. $income_data = $incomeFactory->getIncomeData();
  269. if ($incomeFactory->isAvailable()) {
  270. $available[] = $income_data;
  271. } else {
  272. $unavailable[] = $income_data;
  273. }
  274. //unset($incomeFactory);
  275. //unset($income_data);
  276. }
  277. return [$available, $unavailable];
  278. }
  279. /**
  280. * 获取商城中的插件名称自定义设置
  281. *
  282. * @return mixed
  283. */
  284. private function getLangSet()
  285. {
  286. $lang = \Setting::get('shop.lang', ['lang' => 'zh_cn']);
  287. return $lang[$lang['lang']];
  288. }
  289. private function getWithdrawDate()
  290. {
  291. $income_set = \Setting::get('withdraw.income');
  292. $withdraw_date = [
  293. 'day' => 0, //可提现日期
  294. 'disable' => 0 //是否禁用
  295. ];
  296. $day_msg = '无提现限制';
  297. if (is_array($income_set['withdraw_date'])) {
  298. $day = date('d');
  299. $day_msg = '可提现日期为:'.implode(',',$income_set['withdraw_date']).'号';
  300. $withdraw_date = [
  301. 'day' => min($income_set['withdraw_date']),
  302. 'disable' => 1
  303. ];
  304. foreach ($income_set['withdraw_date'] as $date) {
  305. if ($day == $date) {
  306. $withdraw_date = [
  307. 'day' => $date,
  308. 'disable' => 0,
  309. ];
  310. break;
  311. }
  312. if ($day < $date) {
  313. $withdraw_date = [
  314. 'day' => $date,
  315. 'disable' => 1,
  316. ];
  317. }
  318. }
  319. }
  320. $withdraw_date['day_msg'] = $day_msg;
  321. return $withdraw_date;
  322. }
  323. /**
  324. * 是否开启关系链 todo 应该提出一个公用的服务
  325. *
  326. * @return bool
  327. */
  328. private function isOpenRelation()
  329. {
  330. if (!is_null($this->relationSet) && 1 == $this->relationSet->status) {
  331. return true;
  332. }
  333. return false;
  334. }
  335. private function getSharePageStatus()
  336. {
  337. if (!is_null($this->relationSet) && 1 == $this->relationSet->share_page) {
  338. return true;
  339. }
  340. return false;
  341. }
  342. private function getRelationSet()
  343. {
  344. return MemberRelation::uniacid()->first();
  345. }
  346. private function getTotalIncome()
  347. {
  348. $incomeConfig = \app\backend\modules\income\Income::current()->getItems();
  349. $incomeConfig = collect($incomeConfig)->pluck('class')->toArray();
  350. $total_income =Income::uniacid()->selectRaw('member_id, incometable_type, sum(amount) as total_amount, sum(if(status = 0, amount, 0)) as usable_total')
  351. ->whereIn('incometable_type', $incomeConfig)
  352. ->whereMember_id(\YunShop::app()->getMemberId())
  353. ->groupBy('incometable_type', 'member_id')
  354. ->get();
  355. //计算累计收入
  356. $this->grand_total = sprintf("%.2f",$total_income->sum('total_amount'));
  357. $this->usable_total = sprintf("%.2f",$total_income->sum('usable_total'));
  358. return $total_income;
  359. }
  360. /**
  361. * 登陆会员是否是推客
  362. *
  363. * @return bool
  364. */
  365. private function isAgent()
  366. {
  367. return MemberModel::isAgent();
  368. }
  369. /**
  370. * 收入页面配置 config
  371. *
  372. * @return mixed
  373. */
  374. private function getIncomePageConfig()
  375. {
  376. return \app\backend\modules\income\Income::current()->getPageItems();
  377. }
  378. //累计收入
  379. private function getGrandTotal()
  380. {
  381. return $this->getIncomeModel()->sum('amount');
  382. }
  383. //可提现收入
  384. private function getUsableTotal()
  385. {
  386. return $this->getIncomeModel()->where('status', 0)->sum('amount');
  387. }
  388. private function getIncomeModel()
  389. {
  390. $member_id = \YunShop::app()->getMemberId();
  391. return Income::uniacid()->where('member_id',$member_id);
  392. }
  393. }