DefaultController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\SalesCompany\api\modules\v1\controllers;
  3. use Yii;
  4. use api\controllers\OnAuthController;
  5. use addons\SalesCompany\common\models\CommissionLog;
  6. use addons\SalesCompany\common\models\SalesCompany;
  7. use common\models\user\User;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class DefaultController
  12. * @package addons\SalesCompany\api\modules\v1\controllers
  13. */
  14. class DefaultController extends OnAuthController
  15. {
  16. public $modelClass = '';
  17. /**
  18. * 不用进行登录验证的方法
  19. *
  20. * 例如: ['index', 'update', 'create', 'view', 'delete']
  21. * 默认全部需要验证
  22. *
  23. * @var array
  24. */
  25. protected $authOptional = ['index'];
  26. /**
  27. * 首页
  28. *
  29. * @return string
  30. */
  31. public function actionIndex()
  32. {
  33. return 'Hello world';
  34. }
  35. /**
  36. *我的收益列表
  37. * @return array|mixed
  38. */
  39. public function actionList(){
  40. try{
  41. if (\Yii::$app->request->isGet) {
  42. $res = SalesCompany::findOne(['user_id'=>$this->user->id]);
  43. if(!$res){
  44. throw new \Exception('您还未绑定销售公司');
  45. }
  46. $param = \Yii::$app->request->get();
  47. $where[] = ['=', 'mall_id', $this->mall_id];
  48. $where[] = ['=', 'user_id', $this->user->id];
  49. $params['where'] = $where;
  50. $params['order'] = ' created_at desc';
  51. $params['select'] = 'id,user_id,created_at,desc,money';
  52. $data = CommissionLog::listPage($params, false);
  53. if(!empty($data['list'])){
  54. foreach($data['list'] as &$list){
  55. $list['created_at'] = date('Y-m-d H:i:s',$list['created_at']);
  56. $list['type'] = 1;
  57. }
  58. }
  59. $data['total_money'] = 0;
  60. $data['today_money'] = 0;
  61. $data['rate_money'] = 0;
  62. $commissionLog = CommissionLog::find()->select('sum(money) as money,sum(total_money) as total_money')->where(['user_id'=>$this->user->id,'mall_id'=>$this->mall_id])->asArray()->one();
  63. if($commissionLog){
  64. $data['total_money'] = $commissionLog['money'] ?? 0;
  65. $data['rate_money'] = $commissionLog['total_money'] - $commissionLog['money'] ?? 0;
  66. }
  67. $start_time = strtotime(date('Y-m-d 00:00:00',time()));
  68. $end_time = $start_time + 24 * 60 * 60;
  69. $commissionLog = CommissionLog::find()
  70. ->select('sum(money) as money')
  71. ->where(['user_id'=>$this->user->id,'mall_id'=>$this->mall_id])
  72. ->andWhere(['>=','created_at',$start_time])
  73. ->andWhere(['<','created_at',$end_time])
  74. ->asArray()
  75. ->one();
  76. if($commissionLog){
  77. $data['today_money'] = $commissionLog['money'] ?? 0;
  78. }
  79. $user = User::findOne(['id'=>$this->user->id]);
  80. $data['user'] = [
  81. 'nickname' => $user->nickname,
  82. 'id' => $user->id,
  83. 'avatar_url' => $user->avatar_url,
  84. ];
  85. return $this->success('操作成功', $data);
  86. }else{
  87. throw new \Exception("请求方法错误!");
  88. }
  89. }catch(\Exception $e){
  90. return $this->error($e->getMessage());
  91. }
  92. }
  93. }