| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?php
- namespace platform\controllers;
- use Yii;
- use common\models\xhShop;
- use yii\data\Pagination;
- use common\models\xhUser;
- use common\models\xhMerchant;
- use common\services\xhShopService;
- use common\services\xhUserTagService;
- use common\services\xhMerchantAssetService;
- use common\services\xhMerchantExtendService;
- use common\services\xhMerchantService;
- use common\services\xhUserService;
- use common\services\xhUserTagClassService;
- use common\services\xhDrawCashService;
- use common\services\xhTMessageService;
- use common\services\xhMerchantCapitalService;
- use common\components\util;
- use common\components\configDict;
- use common\components\weixinUtil;
- use common\components\page;
- use common\components\stringUtil;
- use common\services\xhAdminToMerchantService;
- class MerchantController extends PlatformController
- {
- /**
- * 商家列表
- */
- public function actionList()
- {
- $query = xhMerchant::find();
- $query->orderBy('id desc');
- $countQuery = clone $query;
- $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => 20]);
- $merchant = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
- $merchantStatus = configDict::getConfig('merchantStatus');
- $merchantStatusName = configDict::getConfig('merchantStatusName');
- $accountStyle = configDict::getConfig('accountStyleName');
- $manageStyle = configDict::getConfig('manageStyleName');
- return $this->render('list', ['merchantStatus'=>$merchantStatus,'merchant' => $merchant,'accountStyle' => $accountStyle,'manageStyle' => $manageStyle, 'merchantStatusName'=> $merchantStatusName,'pages' => $pages]);
- }
- /**
- * 商家详细
- */
- public function actionDetail()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $info = xhMerchantService::getById($id);
- $status = configDict::getConfig('merchantStatusName');
- $asset = xhMerchantAssetService::getByMerchantId($id);
- $extend = xhMerchantExtendService::getByMerchantId($id);
- $merchantStatus = configDict::getConfig('merchantStatus');
- $merchantStatusName = configDict::getConfig('merchantStatusName');
- $isDemoMerchant = false;
- $trainDemo = configDict::getConfig('trainDemo');
- $merchantAccount = $trainDemo['merchant']['account'];
- if($merchantAccount == $info['account']){
- $isDemoMerchant = true;
- }
- return $this->render('detail',['isDemoMerchant'=>$isDemoMerchant,'merchantStatus'=>$merchantStatus,'merchantStatusName'=>$merchantStatusName,'asset' => $asset,'info' => $info,'status'=> $status,'extend'=>$extend]);
- }
- /**
- * 审核等操作
- */
- public function actionDeal()
- {
- $get = Yii::$app->request->get();
- $merchantId = isset($get['id']) ? $get['id'] : 0;
- $status = isset($get['status']) ? $get['status'] : 0;
- $merchantStatus = configDict::getConfig('merchantStatus');
- $merchant = xhMerchantService::getById($merchantId);
- $admin = xhAdminToMerchantService::getAdminByMerchantId($merchantId);
- if(empty($admin)){
- util::failInfo('此商家没有绑定管理员');
- }
- switch ($status){
- case $merchantStatus['pass']://审核通过
- xhMerchantService::pass($admin);
- break;
- case $merchantStatus['freeze']:
- break;
- }
- xhMerchantService::updateById($merchantId, ['status' => $status]);
- util::successInfo();
- }
- public function actionSyncUserInfo()
- {
- set_time_limit(0);
- $get = Yii::$app->request->get();
- $nextOpenId = isset($get['nextOpenId']) ? $get['nextOpenId'] : '';
- $merchantId = isset($get['merchantId']) ? $get['merchantId'] : '';
- $merchant = xhMerchantService::getById($merchantId);
- $result = weixinUtil::getSubscribeUserList($merchant,$nextOpenId);
- $total = isset($result['total']) ? $result['total'] : 0;
- $next_openid = isset($result['next_openid']) ? $result['next_openid'] : '';
- $count = isset($result['count']) ? $result['count'] : 0;
- $openIdList = isset($result['data']['openid']) ? $result['data']['openid'] : [];
- if($count == 0){
- util::successInfo('完成更新','A0001',['next_openid' => '','count' => 0]);
- }
- $total = count($openIdList);
- $totalPage = $total > 100 ? ceil($total / 100) : 1;
- for($m=1;$m<=$totalPage;$m++){
- $offset = ($m - 1) * 100;
- $subIdList = array_slice($openIdList,$offset,100);
- $info = weixinUtil::batchGetUserInfo($merchant,$subIdList);
- if(empty($info) || isset($info['user_info_list']) == false || empty($info['user_info_list'])){
- continue;
- }
- $user_info_list = $info['user_info_list'];
- foreach ($user_info_list as $val){
- $openId = $val['openid'];
- $user = xhUserService::getByOpenId($openId,$merchantId);
- if(!empty($user)){
- $upData = [];
- $upData['merchantId'] = $merchantId;
- $upData['sex'] = isset($val['sex']) ? $val['sex'] : 0;
- $upData['subscribe'] = $val['subscribe'];
- $upData['subscribeTime'] = $val['subscribe_time'];
- $upData['hasSubscribe'] = $val['subscribe'];
- if(isset($val['unionid']) && !empty($val['unionid'])){
- $upData['unionId'] = $val['unionid'];
- }
- if(isset($user['id']) == false){
- util::failInfo('出错了');
- }
- xhUserService::updateById($user['id'], $upData);
- }
- }
- }
- xhMerchantAssetService::updateByMerchantId($merchantId, ['totalFans'=>$count]);
- util::successInfo('获取成功','A0001',['next_openid' => $next_openid,'count' => $count]);
- }
- /**
- * 微信门店拉取
- */
- public function actionSyncShop()
- {
- $get = Yii::$app->request->get();
- $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
- $merchant = xhMerchantService::getById($merchantId);
- $xhShopService = new xhShopService();
- $re = $xhShopService->saveShopList($merchant);//查看门店申请数据
- $mcInfo = xhMerchantService::getById($merchantId);
- if($re === false){//无门店,自动建门店
- $re = xhShop::getByCondition(['businessName' => $mcInfo['merchantName']]);
- if(!$re){
- $return = $xhShopService->platformInitStore($mcInfo, $merchant);
- if($return === true){
- util::successInfo('系统自动创建门店成功');
- }
- util::failInfo($re);
- }else{
- util::failInfo('已创建好门店,正在审批中...');
- }
- }
- //无申请成功的门店,自动再申请一家
- //$re = ['total' => $num, 'success' => $successApply, 'applying' => $applying, 'fail' => $faileApply];
- if($re['success'] == 0 && $re['applying'] == 0){
- $return = $xhShopService->platformInitStore($mcInfo, $merchant);
- if(!$return){
- util::failInfo('暂无门店申请成功,系统自动创建门店失败');
- }
- util::successInfo('暂无门店申请成功,系统自动创建门店成功');
- exit();
- }
- $msg = '门店总数:'.$re['total'].'家,' . '申请成功:' . $re['success'] . '家,'.'审批中:' . $re['applying'] . '家,'.'未通过:' . $re['fail'] . '家。';
- util::successInfo($msg);
- }
- /**
- * 同步微信用户标签
- */
- public function actionSyncTag()
- {
- set_time_limit(0);
- $get = Yii::$app->request->get();
- $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
- $merchant = xhMerchantService::getById($merchantId);
- $allTag = weixinUtil::getTag($merchant);//取微信里保存的用户标签
- if(empty($allTag)){
- util::failInfo('没有用户标签信息');
- }
- $tagList = $allTag['tags'];
- $wxTag = [];
- if(!empty($tagList)){
- foreach($tagList as $key => $val){
- $wxTag[$val['id']] = $val['name'];
- }
- }
- $reWxTag = array_flip($wxTag);
- $dbTag = xhUserTagClassService::getMerchantTag($merchantId);
- $reDbTag = array_flip($dbTag);
- $delTag = array_diff($reDbTag,$reWxTag);//删除商家微信里没有的标签
- if(!empty($delTag)){
- foreach($delTag as $key => $val){
- xhUserTagClassService::deleteByCondition(['tagId' => $val,'merchantId' => $merchantId],$merchantId);
- $delTagRelation = xhUserTagService::getAllByCondition(['tagId' => $val,'merchantId' => $merchantId]);
- if(!empty($delTagRelation)){
- foreach($delTagRelation as $delTagVal){
- $userId = $delTagVal['userId'];
- xhUserService::refreshById($userId);
- $tId = $delTagVal['id'];
- xhUserTagService::delById($tId);
- }
- }
- }
- }
- $saveTag = array_diff($reWxTag,$reDbTag);//保存商家的标签
- if(!empty($saveTag)){
- foreach($saveTag as $key => $val){
- $data = ['merchantId' => $merchantId,'tagId' => $val,'name' => $key];
- xhUserTagClassService::add($data,$merchantId);
- }
- }
- $updateTag = array_intersect($reWxTag,$reDbTag);//更新商家的标签
- if(!empty($updateTag)){
- foreach($updateTag as $key => $val){
- xhUserTagClassService::updateByCondition(['merchantId' => $merchantId,'tagId' => $val], ['name' => $key],$merchantId);
- }
- }
- util::successInfo('同步成功');
- }
- public function actionSyncUserTag()
- {
- set_time_limit(0);
- $get = Yii::$app->request->get();
- $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
- $merchant = xhMerchantService::getById($merchantId);
- $tagList = xhUserTagClassService::getMerchantTag($merchantId);
- if(empty($tagList)){
- util::failInfo('请先同步商家标签');
- }
- $wx = new weixinUtil($merchant);
- foreach($tagList as $tagId => $tag){
- $nextOpenId = '';
- $this->__updateCategory($wx, $tagId, $nextOpenId,$merchantId);
- }
- util::successInfo('同步成功');
- }
- public function __updateCategory($wx,$tagId,$nextOpenId,$merchantId)
- {
- $return = $wx->getUserTag($tagId,$nextOpenId);
- $count = isset($return['count']) ? $return['count'] : 0;
- $nextOpenId = isset($return['next_openid']) ? $return['next_openid'] : '';
- $openIdList = isset($return['data']['openid']) ? $return['data']['openid'] : [];
- if(!empty($openIdList)){
- foreach($openIdList as $openId){
- $user = xhUserService::getByOpenId($openId,$merchantId);
- if(empty($user)){
- continue;
- }
- $userId = $user['id'];
- $tagData = ['merchantId'=>$merchantId,'tagId'=>$tagId,'userId'=>$userId];
- $userTag = xhUserTagService::getAllByCondition(['tagId'=>$tagId,'userId'=>$userId]);
- if(empty($userTag)){
- xhUserTagService::add($tagData);
- }
- }
- if($count > 0){
- $this->__updateCategory($wx, $tagId, $nextOpenId,$merchantId);
- }
- }
- }
- public function actionFans()
- {
- $get = Yii::$app->request->get();
- $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
- $get = Yii::$app->request->get();
- $query = xhUser::find();
- $query->where(['merchantId' =>$merchantId]);
- if(isset($get['content']) && !empty($get['content'])){
- $content = $get['content'];
- if(stringUtil::isMobieNumber($content)){
- $query->andWhere(['mobile' => $content]);
- }else{
- $query->andWhere(['like', 'userName', $content]);
- }
- }
- if(isset($get['class']) && $get['class'] == 1){
- $query->andWhere(['isMember' => 1]);
- }
- $countQuery = clone $query;
- $count = $countQuery->count();
- $page = isset($get['page']) ? $get['page'] : 1;
- $pageSize = 20;
- $offset = ($page - 1) * $pageSize;
- $models = $query->offset($offset)->orderBy('id desc')->limit($pageSize)->asArray()->all();
- $page = new page($count,$page,$pageSize);
- $paging = $page->fpage();
- $auth = xhMerchantService::isAuth($merchantId);
- $merchantTag = xhUserTagClassService::getMerchantTag($merchantId);
- $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
- $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend,'desc');
- $merchant = xhMerchantService::getById($merchantId);
- return $this->render('fans', [
- 'merchantTag' => $merchantTag,
- 'models' => $models,
- 'pages' => $paging,
- 'merchant' => $merchant,
- 'memberIntegral' => $memberIntegral,
- 'merchantAsset' => $merchantAsset,
- 'merchantExtend' => $merchantExtend,
- ]);
-
- }
- /**
- * 进行提现操作,全额提现
- */
- public function actionDrawCash()
- {
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- $get = Yii::$app->request->get();
- $merchantId = isset($get['id']) ? $get['id'] : 0;
- $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
- try {
- $amount = $merchantAsset['balance'];
- $mBalance = stringUtil::calcSub($merchantAsset['balance'], $amount);
- $date = date("Y-m-d H:i:s");
- $id = 'D'.stringUtil::buildOrderNo();//订单号
- $cashData = ['amount'=>$amount,'merchantId'=>$merchantId,'balance'=>$mBalance,'addTime'=>time(),'id'=>$id,'createTime'=>$date];
- $order = xhDrawCashService::add($cashData);
- $orderId = $order['id'];
- xhMerchantAssetService::updateByMerchantId($merchantId, ['balance'=>$mBalance,'freezeBalance'=>$amount]);
- $now = time();
- $capitalTypeArray = configDict::getConfig('capitalType','xhDrawCash');
- $capitalType = $capitalTypeArray['id'];
- $mIncome = $merchantAsset['totalIncome'];
- $mExpend = $merchantAsset['totalExpend'];
- $payWay = configDict::getConfig('payWay','other');
- $capitalData = [
- 'relateId' => (string)$orderId,
- 'balance' => $mBalance,
- 'totalIncome' => $mIncome,
- 'totalExpend' => $mExpend,
- 'amount' => $amount,
- 'io' => 0,
- 'payWay' => $payWay,
- 'event' => '申请提现',
- 'merchantId' => $merchantId,
- 'userId' => 0,
- 'userName' => '',
- 'alipayId' => '',
- 'affectBalance' => 1,//会影响商家的余额
- 'createTime' => $date,
- 'addTime' => $now,
- 'capitalType' => $capitalType,
- 'operateId' => $this->adminId,
- ];
- xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
- $transaction->commit();
- util::successInfo();
- }catch (Exception $e) {
- $transaction->rollBack();
- util::failInfo();
- }
- }
- public function actionInit()
- {
- $get = Yii::$app->request->get();
- $merchantId = isset($get['id']) ? $get['id'] : 0;
- $merchant = xhMerchantService::getById($merchantId);
- $extend = xhMerchantExtendService::getByMerchantId($merchantId);
- $gradeList = xhMerchantExtendService::getGradeList($extend);
- $wxPayMerchantId = '';
- $wxPayKey = '';
- $trainDemo = configDict::getConfig('trainDemo');
- if($merchant['id'] == $trainDemo['merchant']['account']){
- $wxPayMerchantId = $trainDemo['merchant']['wxPayMerchantId'];
- $wxPayKey = $trainDemo['merchant']['wxPayKey'];
- }
- if($merchant['id'] == $trainDemo['platform']['account']){
- $wxPayMerchantId = $trainDemo['platform']['wxPayMerchantId'];
- $wxPayKey = $trainDemo['platform']['wxPayKey'];
- }
- return $this->render('init',['extend' => $extend,'gradeList' => $gradeList,'merchantId'=>$merchantId,'wxPayMerchantId'=>$wxPayMerchantId,'wxPayKey'=>$wxPayKey]);
- }
- public function actionInitSet()
- {
- $post = Yii::$app->request->post();
- $merchantId = isset($post['merchantId']) ? $post['merchantId'] : 0;
- $extend = xhMerchantExtendService::getByMerchantId($merchantId);
- $merchant = xhMerchantService::getById($merchantId);
- if(empty($extend['wxTMessageInit'])){//开通创建模板消息
- xhTMessageService::init($merchant, $extend);
- }else{//补充新增加的模板消息
- xhTMessageService::supplement($merchant);
- }
- unset($post['_csrf']);
- $post['init'] = 1;//完成初始化
- xhMerchantExtendService::updateByMerchantId($merchantId, $post);//上面要先初始化,再执行这边的更新
- util::successInfo();
- }
- public function actionSetAgent()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $data['agentLevel'] = 1;
- xhMerchantService::updateById($id, $data);
- util::successInfo();
- }
- }
|