DefaultController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace addons\CircleDistribution\api\modules\v1\controllers;
  3. use addons\CircleDistribution\common\enums\DistributionEnum;
  4. use addons\CircleDistribution\common\models\DistributionApply;
  5. use addons\CircleDistribution\common\models\Distribution;
  6. use addons\CircleDistribution\common\models\DistributionCommissionBasisLog;
  7. use common\enums\AddonsEnum;
  8. use common\enums\StatusEnum;
  9. use common\logic\common\GoodsLogic;
  10. use common\models\common\CapitalLog;
  11. use common\models\goods\Goods;
  12. use common\models\user\User;
  13. use common\models\user\UserLevel;
  14. use Yii;
  15. use api\controllers\OnAuthController;
  16. /**
  17. * 默认控制器
  18. *
  19. * Class DefaultController
  20. * @package addons\CircleDistribution\api\modules\v1\controllers
  21. */
  22. class DefaultController extends OnAuthController
  23. {
  24. public $modelClass = '';
  25. public $from_type = ['Distribution'];
  26. /**
  27. * 不用进行登录验证的方法
  28. *
  29. * 例如: ['index', 'update', 'create', 'view', 'delete']
  30. * 默认全部需要验证
  31. *
  32. * @var array
  33. */
  34. protected $authOptional = ['index'];
  35. /**
  36. * 首页
  37. *
  38. * @return string
  39. */
  40. public function actionIndex()
  41. {
  42. return 'Hello world';
  43. }
  44. /**
  45. * @desc:经销商详情
  46. * @Author: hua
  47. * @Date: 2021/11/4
  48. * @Time: 16:47
  49. * @Copyright: copyright (c) 2021 广东七件事集团
  50. * @return mixed|void
  51. */
  52. public function actionInfos()
  53. {
  54. $data['user_id'] = $this->user_id;
  55. $data['nickname'] = empty($this->user->nickname) ? '' : $this->user->nickname;
  56. $data['avatar_url'] = empty($this->user->avatar_url) ? '' : $this->user->avatar_url;
  57. $distribution = Distribution::getOne([['user_id' => $this->user_id], ['status' => StatusEnum::ENABLED], ['mall_id' => $this->mall_id]], true);
  58. if (empty($distribution)) {
  59. $res = DistributionApply::getOne([['user_id' => $this->user_id], ['status' => StatusEnum::ENABLED], ['mall_id' => $this->mall_id]], true, [], 'id desc');
  60. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, DistributionEnum::ADDONS_NAME, 'basic');
  61. if (!empty($res)) {
  62. if ($res['check_status'] == 0) return $this->error('', $result = ['is_apply' => 2, 'msg' => '您已经申请了,请等待审核', 'is_distribution' => 0]);
  63. if ($res['check_status'] == 2) return $this->error('', $result = ['is_apply' => 3, 'protocol' => $setting['protocol'], 'msg' => $res['check_remark'], 'is_distribution' => 0]);
  64. }
  65. $result = ['is_apply' => $setting['is_apply'], 'protocol' => $setting['protocol'], 'is_distribution' => 0];
  66. return $this->error('您不是分销商', $result);
  67. }
  68. $data['parent_name'] = '平台方';
  69. $data['parent_avatar_url'] = '';
  70. $data['parent_level_name'] = '';
  71. $parent_id = $this->user->parent_id ?? 0;
  72. if ($parent_id) {
  73. $parent = User::findOne($parent_id);
  74. $data['parent_name'] = $parent['nickname'] ?? '';
  75. $data['parent_avatar_url'] = $parent['avatar_url'] ?? '';
  76. $parent_distribution = Distribution::findOne(['user_id' => $parent_id]);
  77. $data['parent_level_name'] = $parent_distribution->distributionLevel->name ?? '默认等级';
  78. }
  79. $data['is_distribution'] = 0;
  80. $data['level_name'] = '';
  81. $data['frozen_price'] = 0;
  82. $data['total_price'] = 0;
  83. $data['total_income'] = 0;
  84. $data['yesterday_price'] = 0;
  85. if ($distribution) {
  86. $data['is_distribution'] = 1;
  87. $data['level_name'] = $distribution['distributionLevel']['name'] ?? '默认等级';
  88. $wallet_type = 'commission';
  89. $yesterday = date('Y-m-d', strtotime('-1 day'));
  90. $start_time = "{$yesterday} 00:00:00";
  91. $end_time = "{$yesterday} 23:59:59";
  92. CapitalLog::$capitalType = "{$wallet_type}_frozen";
  93. $where = ['user_id' => $this->user_id, 'from_type' => DistributionEnum::ADDONS_NAME, 'status' => CapitalLog::STATUS_WAIT_SEND];
  94. $query = CapitalLog::find();
  95. $frozen_price = $query->where($where)->sum('change_num');
  96. $yesterday_frozen_price = $query->where($where)
  97. ->andWhere(['<=', 'created_at', $end_time])
  98. ->andWhere(['>=', 'created_at', $start_time])
  99. ->sum('change_num');
  100. if (empty($yesterday_frozen_price)) $yesterday_frozen_price = 0;
  101. CapitalLog::$capitalType = "{$wallet_type}";
  102. $where = ['user_id' => $this->user_id, 'from_type' => DistributionEnum::ADDONS_NAME];
  103. $query = CapitalLog::find();
  104. $total_price = $query->where($where)->sum('change_num');
  105. $yesterday_total_price = $query->where($where)
  106. ->andWhere(['<=', 'created_at', $end_time])
  107. ->andWhere(['>=', 'created_at', $start_time])
  108. ->sum('change_num');
  109. if (empty($yesterday_total_price)) $yesterday_total_price = 0;
  110. $data['total_price'] = empty($total_price) ? 0 : $total_price;
  111. $data['frozen_price'] = empty($frozen_price) ? 0 : $frozen_price;
  112. $data['total_income'] = bcadd($data['total_price'], $data['frozen_price'], 2);
  113. $data['yesterday_price'] = $yesterday_total_price + $yesterday_frozen_price;
  114. }
  115. $is_show_share_level = \Yii::$app->services->setting->addons->get($this->mall_id, DistributionEnum::ADDONS_NAME, 'basic.is_show_share_level');
  116. $data['is_show_share_level'] = $is_show_share_level ?? 0;
  117. return $this->success('操作成功', $data);
  118. }
  119. public function actionInfo()
  120. {
  121. $data = Distribution::distributionInfo($this->user, $this->mall_id);
  122. if (!empty($data['msg'])) return $this->error($data['msg'], $data['result']);
  123. return $this->success('操作成功', $data);
  124. }
  125. /**
  126. * @desc:奖励列表
  127. * @Author: hua
  128. * @Date: 2021/11/4
  129. * @Time: 17:00
  130. * @Copyright: copyright (c) 2021 广东七件事集团
  131. * @return mixed|void
  132. */
  133. public function actionLogList()
  134. {
  135. $get = \Yii::$app->request->get();
  136. $status = $get['status'] ?? 1;
  137. $wallet_type = $status == 1 ? 'commission' : 'commission_frozen';
  138. if($wallet_type == 'commission_frozen') $params['status'] = 0;
  139. //$wallet_type = 'commission';
  140. $params['mall_id'] = $this->mall_id;
  141. $params['user_id'] = $this->user_id;
  142. $params['page'] = $get['page'] ?? 1;
  143. $params['limit'] = $get['limit'] ?? 5;
  144. $list = CapitalLog::getCapitalLogList($params, $wallet_type, DistributionEnum::ADDONS_NAME);
  145. foreach($list['list'] as $key => $value){
  146. $value['is_percent'] = 0;
  147. $value['now_commission'] = '';
  148. $value['repurchase_msg'] = '';
  149. $basic_log = DistributionCommissionBasisLog::findOne(['mall_id' => $this->mall_id,'user_id' => $this->user_id,'source_table_id' => $value['source_table_id'],'source_table' => $value['source_table']]);
  150. if($basic_log){
  151. $reward_config = json_decode($basic_log->reward_config,true);
  152. $value['is_percent'] = $reward_config['is_percent'] ?? 0;
  153. $value['commission_percent'] = $reward_config['first_prize'] ?? 0;
  154. $value['now_commission'] = $value['is_percent'] == 0 ? $value['commission_percent'].'%' : $value['commission_percent'].'元';
  155. $value['repurchase_msg'] = $basic_log['need_repurchase'] > 0 ? '需要复购一次指定商品结算奖励' : '';
  156. }
  157. $list['list'][$key] = $value;
  158. }
  159. $list['map'] = DistributionEnum::getMap();
  160. return $this->success('操作成功', $list);
  161. }
  162. /**
  163. * @desc:申请接口
  164. * @Author: hua
  165. * @Date: 2021/11/25
  166. * @Time: 16:39
  167. * @Copyright: copyright (c) 2021 广东七件事集团
  168. * @return mixed|void
  169. */
  170. public function actionApply()
  171. {
  172. $mall_id = $this->mall_id;
  173. if (\Yii::$app->request->isPost) {
  174. $params = \Yii::$app->request->post();
  175. $res = Yii::$app->services->validate->validate($params, [
  176. [['name', 'mobile'], 'required'],
  177. [['remark'], 'safe'],
  178. ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  179. ]);
  180. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  181. $params['mall_id'] = $mall_id;
  182. $params['user_id'] = $this->user_id;
  183. if (empty($this->user)) return $this->error('不存在此会员');
  184. $distribution = Distribution::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true);
  185. if (!empty($distribution)) return $this->error('您已经是分销商');
  186. $distribution_apply = DistributionApply::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true, [], 'id desc');
  187. if (!empty($distribution_apply) && empty($distribution_apply['check_status'])) return $this->error('您已经提交申请,请等待客服审核');
  188. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, DistributionEnum::ADDONS_NAME, 'basic');
  189. if (empty($setting['is_check'])) $params['check_status'] = 1; //不需要审核
  190. $res = DistributionApply::setData($params);
  191. if ($res === false) return $this->error(DistributionApply::getStaticError());
  192. return $this->success('操作成功');
  193. }
  194. if (\Yii::$app->request->isGet) {
  195. $params['mall_id'] = $mall_id;
  196. $params['user_id'] = $this->user_id;
  197. if (empty($this->user)) return $this->error('不存在此会员');
  198. $distribution_apply = DistributionApply::getOne([
  199. ['user_id' => $this->user_id],
  200. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  201. ['mall_id' => $this->mall_id]
  202. ], true, [], 'id desc', 'id,user_id,check_status,check_remark');
  203. return $this->success('操作成功', $distribution_apply);
  204. }
  205. }
  206. /**
  207. * 复购商品列表
  208. * @return mixed|void
  209. */
  210. public function actionRepurchaseGoods()
  211. {
  212. try {
  213. $mall_id = $this->mall_id;
  214. $setting = \Yii::$app->services->setting->addons->get($mall_id, DistributionEnum::ADDONS_NAME, 'basic');
  215. $thaw_goods_ids = $setting['thaw_goods_ids'] ? json_decode($setting['thaw_goods_ids'], true) : [];
  216. $goods_list = [];
  217. if ($setting['is_circle_repurchase'] && $thaw_goods_ids) {
  218. $where[] = ['g.mall_id' => $mall_id];
  219. $where[] = ['g.status' => [StatusEnum::ENABLED]];
  220. $where[] = ['g.is_on_sale' => 1];
  221. $where[] = ['g.is_show' => 1];
  222. $where[] = ['g.id' => $thaw_goods_ids];
  223. $params['where'] = $where;
  224. $params['alias'] = 'g';
  225. $params['select'] = 'g.id,g.goods_name,g.cover_pic,g.price,g.original_price,g.virtual_sales,g.sales_num,is_show_sales,g.original_price';
  226. $goods_list = Goods::listPage($params);
  227. if (!empty($goods_list['list'])) {
  228. $user_level = null;
  229. if (!empty($this->user)) {
  230. //基础会员等级折扣
  231. $user_level = UserLevel::findOne(['mall_id' => $this->mall_id, 'level' => $this->user['level'], 'status' => StatusEnum::ENABLED]);
  232. }
  233. foreach ($goods_list['list'] as &$item) {
  234. $item['sales_num'] += $item['virtual_sales'];
  235. unset($item['virtual_sales']);
  236. //额外会员设置
  237. if ($user_level && !empty($user_level['member_price_is_show'])) {
  238. $item['price'] = GoodsLogic::getMemberPriceList($this->mall_id, $item, $this->user, true);
  239. }
  240. }
  241. }
  242. }
  243. return $this->success('操作成功', $goods_list);
  244. } catch (\Exception $e) {
  245. return $this->success('操作成功', []);
  246. }
  247. }
  248. }