DefaultController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace addons\WechatVideoShop\api\modules\v1\controllers;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
  4. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\sharer\SharerInviteModel;
  5. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\sharer\SharerListModel;
  6. use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\sharer\SharerOrderListModel;
  7. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\sharer\SharerInviteRequest;
  8. use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\sharer\SharerOrderListRequest;
  9. use addons\WechatVideoShop\common\helper\WechatRequestHelper;
  10. use addons\WechatVideoShop\common\models\WechatVideoShopOrder;
  11. use addons\WechatVideoShop\common\models\WechatVideoShopSharer;
  12. use addons\WechatVideoShop\common\service\ShopService;
  13. use common\enums\StatusEnum;
  14. use common\models\common\CapitalLog;
  15. use common\models\common\GlobalVarMap;
  16. use common\models\user\User;
  17. use Yii;
  18. use api\controllers\OnAuthController;
  19. /**
  20. * 默认控制器
  21. *
  22. * Class DefaultController
  23. * @package addons\WechatVideoShop\api\modules\v1\controllers
  24. */
  25. class DefaultController extends OnAuthController
  26. {
  27. public $modelClass = '';
  28. /**
  29. * 不用进行登录验证的方法
  30. *
  31. * 例如: ['index', 'update', 'create', 'view', 'delete']
  32. * 默认全部需要验证
  33. *
  34. * @var array
  35. */
  36. protected $authOptional = ['index'];
  37. /**
  38. * 首页
  39. *
  40. * @return string
  41. */
  42. public function actionIndex()
  43. {
  44. return 'Hello world';
  45. }
  46. public function actionHead()
  47. {
  48. // 等级
  49. // 佣金
  50. // 头像
  51. $default_level_name = '默认等级';
  52. $data['user'] = [
  53. 'avatar_url' => $this->user->avatar_url,
  54. 'nickname' => $this->user->nickname,
  55. 'mobile' => $this->user->mobile,
  56. 'level_name' => $default_level_name
  57. ];
  58. $data['total'] = [
  59. 'order_cnt' => 0,
  60. 'sale_amount' => 0,
  61. 'cumulative_income' => 0,
  62. ];
  63. $sharer = WechatVideoShopSharer::getOne([
  64. ['mall_id' => $this->mall_id],
  65. ['user_id' => $this->user_id],
  66. ['status' => StatusEnum::ENABLED],
  67. ], true, ['level' => function($query) {
  68. $query->andWhere(['mall_id' => $this->mall_id]);
  69. }]);
  70. if (!empty($sharer)) {
  71. $data['user']['level_name'] = $sharer['level']['name'] ?? $default_level_name;
  72. $data['total'] = [
  73. 'order_cnt' => $sharer['total_order'],
  74. 'sale_amount' => $sharer['total_sales'],
  75. 'cumulative_income' => $sharer['total_commission'],
  76. ];
  77. }
  78. return $this->success('获取成功', $data);
  79. }
  80. public function actionCommission()
  81. {
  82. $params = \Yii::$app->request->get();
  83. if($params['list_type'] ==1){//未结算
  84. $wallet_type = 'commission_frozen';
  85. $params['status'] = StatusEnum::DISABLED;
  86. }else{
  87. $wallet_type = 'commission';
  88. }
  89. $params['mall_id'] = $this->mall_id;
  90. $params['user_id'] = $this->user_id;
  91. if (!empty($params['keyword'])) { // 贡献者检索
  92. $uids = User::find()->where(['mall_id' => $this->mall_id])
  93. ->andWhere(['status' => StatusEnum::ENABLED])
  94. ->andWhere(['or', ['like', 'id', $params['keyword']], ['like', 'nickname', $params['keyword']]])
  95. ->select('id')
  96. ->column();
  97. if (empty($uids)) $this->success();
  98. $params['contributor_id'] = $uids;
  99. }
  100. if (!empty($params['month'])) { // 按月筛选
  101. $params['month'] = [
  102. strtotime($params['month']),
  103. strtotime("+1 months", strtotime($params['month'])),
  104. ];
  105. }
  106. $list = CapitalLog::getCapitalLogList($params, $wallet_type, WechatVideoShopEnums::ADDONS_NAME);
  107. if (!empty(CapitalLog::getStaticError())) return $this->error(CapitalLog::getStaticError());
  108. if (!empty($list['list'])) {
  109. $commission_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
  110. $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
  111. foreach ($list['list'] as &$item) {
  112. $item['source_text'] = ($commission_map[$item['from_type']] ?? '').($capital_type_map[$item['capital_type']]
  113. ?'-'.$capital_type_map[$item['capital_type']]:'');
  114. }
  115. }
  116. return $this->success('操作成功', $list);
  117. }
  118. public function actionCustomer()
  119. {
  120. $get = Yii::$app->request->get();
  121. $ret = WechatVideoShopOrder::listPage([
  122. 'where' => [
  123. ['mall_id' => $this->mall_id],
  124. ['parent_id' => $this->user_id],
  125. ['is_pay' => StatusEnum::ENABLED]
  126. ],
  127. 'group' => 'user_id',
  128. 'order' => 'id desc',
  129. 'select'=> 'user_id',
  130. 'with' => ['user'],
  131. 'limit' => $get['limit'] ?? 5
  132. ]);
  133. if (!empty($ret['list'])) {
  134. foreach ($ret['list'] as &$item) {
  135. $item = $item['user'];
  136. }
  137. }
  138. return $this->success('获取成功', $ret);
  139. }
  140. public function actionOrderList()
  141. {
  142. $get = Yii::$app->request->get();
  143. $reqModel = new SharerOrderListModel();
  144. $reqModel->openid = $get['openid'] ?? '';
  145. $reqModel->page = $get['page'] ?? 1;
  146. $reqModel->page_size = $get['limit'] ?? 20;
  147. try {
  148. $resp = WechatRequestHelper::accessTokenAndToArray(new SharerOrderListRequest((new ShopService())->getShop($get['shop_id'])), $reqModel);
  149. return $this->success('获取成功', $resp);
  150. } catch (\Exception $e) {
  151. return $this->error($e->getMessage());
  152. }
  153. }
  154. /**
  155. * 获取邀请二维码
  156. * @return mixed|null
  157. */
  158. public function actionApply()
  159. {
  160. $data = $this->request->get();
  161. $valid = \Yii::$app->services->validate->validate($data, [
  162. [['wx_username'], 'required'],
  163. [['wx_username'], 'string'],
  164. ]);
  165. if ($valid === false) $this->error(Yii::$app->services->validate->getErrorMessage());
  166. $sharer_default_apply_shop_id = intval(\Yii::$app->services->setting->addons->get($this->mall_id, WechatVideoShopEnums::ADDONS_NAME,
  167. 'basic.sharer_default_apply_shop_id'));
  168. $sharer_default_apply_shop_id = !empty($sharer_default_apply_shop_id) ? $sharer_default_apply_shop_id :
  169. (new ShopService())->firstShopId($this->mall_id);
  170. if (empty($sharer_default_apply_shop_id)) return $this->error("当前系统还未绑定视频号小店");
  171. $req = new SharerInviteRequest((new ShopService())->getShop($sharer_default_apply_shop_id));
  172. $params = new SharerInviteModel();
  173. $params->username = $data['wx_username'];
  174. try {
  175. $resp = WechatRequestHelper::accessTokenAndToArray($req, $params);
  176. if (empty($resp['qrcode_img_base64'])) throw new \Exception("未获取到邀请二维码");
  177. return $this->success('获取成功', [
  178. 'qrcode_img_base64' => $resp['qrcode_img_base64']
  179. ]);
  180. } catch (\Exception $e) {
  181. return $this->error($e->getMessage());
  182. }
  183. }
  184. }