| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace addons\WechatVideoShop\api\modules\v1\controllers;
- use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\sharer\SharerInviteModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\sharer\SharerListModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\model\sharer\SharerOrderListModel;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\sharer\SharerInviteRequest;
- use addons\WechatVideoShop\common\expand\sdk\weixin\src\request\sharer\SharerOrderListRequest;
- use addons\WechatVideoShop\common\helper\WechatRequestHelper;
- use addons\WechatVideoShop\common\models\WechatVideoShopOrder;
- use addons\WechatVideoShop\common\models\WechatVideoShopSharer;
- use addons\WechatVideoShop\common\service\ShopService;
- use common\enums\StatusEnum;
- use common\models\common\CapitalLog;
- use common\models\common\GlobalVarMap;
- use common\models\user\User;
- use Yii;
- use api\controllers\OnAuthController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\WechatVideoShop\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- return 'Hello world';
- }
- public function actionHead()
- {
- // 等级
- // 佣金
- // 头像
- $default_level_name = '默认等级';
- $data['user'] = [
- 'avatar_url' => $this->user->avatar_url,
- 'nickname' => $this->user->nickname,
- 'mobile' => $this->user->mobile,
- 'level_name' => $default_level_name
- ];
- $data['total'] = [
- 'order_cnt' => 0,
- 'sale_amount' => 0,
- 'cumulative_income' => 0,
- ];
- $sharer = WechatVideoShopSharer::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $this->user_id],
- ['status' => StatusEnum::ENABLED],
- ], true, ['level' => function($query) {
- $query->andWhere(['mall_id' => $this->mall_id]);
- }]);
- if (!empty($sharer)) {
- $data['user']['level_name'] = $sharer['level']['name'] ?? $default_level_name;
- $data['total'] = [
- 'order_cnt' => $sharer['total_order'],
- 'sale_amount' => $sharer['total_sales'],
- 'cumulative_income' => $sharer['total_commission'],
- ];
- }
- return $this->success('获取成功', $data);
- }
- public function actionCommission()
- {
- $params = \Yii::$app->request->get();
- if($params['list_type'] ==1){//未结算
- $wallet_type = 'commission_frozen';
- $params['status'] = StatusEnum::DISABLED;
- }else{
- $wallet_type = 'commission';
- }
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- if (!empty($params['keyword'])) { // 贡献者检索
- $uids = User::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['or', ['like', 'id', $params['keyword']], ['like', 'nickname', $params['keyword']]])
- ->select('id')
- ->column();
- if (empty($uids)) $this->success();
- $params['contributor_id'] = $uids;
- }
- if (!empty($params['month'])) { // 按月筛选
- $params['month'] = [
- strtotime($params['month']),
- strtotime("+1 months", strtotime($params['month'])),
- ];
- }
- $list = CapitalLog::getCapitalLogList($params, $wallet_type, WechatVideoShopEnums::ADDONS_NAME);
- if (!empty(CapitalLog::getStaticError())) return $this->error(CapitalLog::getStaticError());
- if (!empty($list['list'])) {
- $commission_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
- $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
- foreach ($list['list'] as &$item) {
- $item['source_text'] = ($commission_map[$item['from_type']] ?? '').($capital_type_map[$item['capital_type']]
- ?'-'.$capital_type_map[$item['capital_type']]:'');
- }
- }
- return $this->success('操作成功', $list);
- }
- public function actionCustomer()
- {
- $get = Yii::$app->request->get();
- $ret = WechatVideoShopOrder::listPage([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['parent_id' => $this->user_id],
- ['is_pay' => StatusEnum::ENABLED]
- ],
- 'group' => 'user_id',
- 'order' => 'id desc',
- 'select'=> 'user_id',
- 'with' => ['user'],
- 'limit' => $get['limit'] ?? 5
- ]);
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- $item = $item['user'];
- }
- }
- return $this->success('获取成功', $ret);
- }
- public function actionOrderList()
- {
- $get = Yii::$app->request->get();
- $reqModel = new SharerOrderListModel();
- $reqModel->openid = $get['openid'] ?? '';
- $reqModel->page = $get['page'] ?? 1;
- $reqModel->page_size = $get['limit'] ?? 20;
- try {
- $resp = WechatRequestHelper::accessTokenAndToArray(new SharerOrderListRequest((new ShopService())->getShop($get['shop_id'])), $reqModel);
- return $this->success('获取成功', $resp);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 获取邀请二维码
- * @return mixed|null
- */
- public function actionApply()
- {
- $data = $this->request->get();
- $valid = \Yii::$app->services->validate->validate($data, [
- [['wx_username'], 'required'],
- [['wx_username'], 'string'],
- ]);
- if ($valid === false) $this->error(Yii::$app->services->validate->getErrorMessage());
- $sharer_default_apply_shop_id = intval(\Yii::$app->services->setting->addons->get($this->mall_id, WechatVideoShopEnums::ADDONS_NAME,
- 'basic.sharer_default_apply_shop_id'));
- $sharer_default_apply_shop_id = !empty($sharer_default_apply_shop_id) ? $sharer_default_apply_shop_id :
- (new ShopService())->firstShopId($this->mall_id);
- if (empty($sharer_default_apply_shop_id)) return $this->error("当前系统还未绑定视频号小店");
- $req = new SharerInviteRequest((new ShopService())->getShop($sharer_default_apply_shop_id));
- $params = new SharerInviteModel();
- $params->username = $data['wx_username'];
- try {
- $resp = WechatRequestHelper::accessTokenAndToArray($req, $params);
- if (empty($resp['qrcode_img_base64'])) throw new \Exception("未获取到邀请二维码");
- return $this->success('获取成功', [
- 'qrcode_img_base64' => $resp['qrcode_img_base64']
- ]);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|