DashboardController.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace backend\modules\user\controllers;
  3. use backend\models\MemberIdentity;
  4. use backend\models\Mt4tradeApi;
  5. use Yii;
  6. class DashboardController extends BaseController
  7. {
  8. /**
  9. * @return string
  10. */
  11. public function actionIndex()
  12. {
  13. /** @var MemberIdentity $identity */
  14. $identity = Yii::$app->getUser()->getIdentity(false);
  15. $login = Yii::$app->getRequest()->get('login');
  16. if ($login != null) {
  17. // 切换mt4账号
  18. $identity->switchMt4user($login);
  19. }
  20. $api = new Mt4tradeApi();
  21. $result = $api->getHistoryTotalCount($identity->getMain_login());
  22. $historyTotalCount = isset($result['data']) ? intval($result['data']) : 0;
  23. $result = $api->getHistoryWinCount($identity->getMain_login());
  24. $historyWinCount = isset($result['data']) ? intval($result['data']) : 0;
  25. $result = $api->getHistoryLossCount($identity->getMain_login());
  26. $historyLossCount = isset($result['data']) ? intval($result['data']) : 0;
  27. $historyWinPercent = $historyTotalCount == 0 ? 0 : round($historyWinCount / $historyTotalCount * 100, 5);
  28. $historyLossPercent = $historyTotalCount == 0 ? 0 : round($historyLossCount / $historyTotalCount * 100, 5);
  29. $result = $api->sumProfitByDay($identity->getMain_login());
  30. $sumProfitByDay = isset($result['data']) ? (array)$result['data'] : [];
  31. $result = $api->volumeSumByDay($identity->getMain_login());
  32. $volumeSumByDay = isset($result['data']) ? (array) $result['data'] : [];
  33. $result = $api->symbolCount($identity->getMain_login());
  34. $symbolCount = isset($result['data']) ? (array) $result['data'] : [];
  35. $result = $api->reasonCount($identity->getMain_login());
  36. $reasonCount = isset($result['data']) ? (array) $result['data'] : [];
  37. // 交易手数
  38. foreach ($volumeSumByDay as $k => $v) {
  39. $volumeSumByDay[$k][1] = number_format($v[1] / 100, 2, '.', '');
  40. }
  41. // 交易货币分布
  42. $symbolCount2 = [];
  43. foreach ($symbolCount as $k => $v) {
  44. $symbolCount2[$k]['label'] = $v['SYMBOL'];
  45. $symbolCount2[$k]['data'] = $v['cnt'];
  46. }
  47. // 交易来源分布
  48. $reasonCount2 = [];
  49. foreach ($reasonCount as $k => $v) {
  50. $reasonCount2[$k]['label'] = $v['REASON'];
  51. $reasonCount2[$k]['data'] = $v['cnt'];
  52. }
  53. return $this->render('index', [
  54. 'historyTotalCount' => $historyTotalCount,
  55. 'historyWinCount' => $historyWinCount,
  56. 'historyLossCount' => $historyLossCount,
  57. 'historyWinPercent' => $historyWinPercent,
  58. 'historyLossPercent' => $historyLossPercent,
  59. 'sumProfitByDay' => json_encode($sumProfitByDay),
  60. 'volumeSumByDay' => json_encode($volumeSumByDay),
  61. 'symbolCount' => json_encode($symbolCount2),
  62. 'reasonCount' => json_encode($reasonCount2),
  63. ]);
  64. }
  65. }