MerchantController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace platform\controllers;
  3. use Yii;
  4. use common\models\xhShop;
  5. use yii\data\Pagination;
  6. use common\models\xhUser;
  7. use common\models\xhMerchant;
  8. use common\services\xhShopService;
  9. use common\services\xhUserTagService;
  10. use common\services\xhMerchantAssetService;
  11. use common\services\xhMerchantExtendService;
  12. use common\services\xhMerchantService;
  13. use common\services\xhUserService;
  14. use common\services\xhUserTagClassService;
  15. use common\services\xhDrawCashService;
  16. use common\services\xhTMessageService;
  17. use common\services\xhMerchantCapitalService;
  18. use common\components\util;
  19. use common\components\configDict;
  20. use common\components\weixinUtil;
  21. use common\components\page;
  22. use common\components\stringUtil;
  23. use common\services\xhAdminToMerchantService;
  24. class MerchantController extends PlatformController
  25. {
  26. /**
  27. * 商家列表
  28. */
  29. public function actionList()
  30. {
  31. $query = xhMerchant::find();
  32. $query->orderBy('id desc');
  33. $countQuery = clone $query;
  34. $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => 20]);
  35. $merchant = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  36. $merchantStatus = configDict::getConfig('merchantStatus');
  37. $merchantStatusName = configDict::getConfig('merchantStatusName');
  38. $accountStyle = configDict::getConfig('accountStyleName');
  39. $manageStyle = configDict::getConfig('manageStyleName');
  40. return $this->render('list', ['merchantStatus'=>$merchantStatus,'merchant' => $merchant,'accountStyle' => $accountStyle,'manageStyle' => $manageStyle, 'merchantStatusName'=> $merchantStatusName,'pages' => $pages]);
  41. }
  42. /**
  43. * 商家详细
  44. */
  45. public function actionDetail()
  46. {
  47. $get = Yii::$app->request->get();
  48. $id = isset($get['id']) ? $get['id'] : 0;
  49. $info = xhMerchantService::getById($id);
  50. $status = configDict::getConfig('merchantStatusName');
  51. $asset = xhMerchantAssetService::getByMerchantId($id);
  52. $extend = xhMerchantExtendService::getByMerchantId($id);
  53. $merchantStatus = configDict::getConfig('merchantStatus');
  54. $merchantStatusName = configDict::getConfig('merchantStatusName');
  55. $isDemoMerchant = false;
  56. $trainDemo = configDict::getConfig('trainDemo');
  57. $merchantAccount = $trainDemo['merchant']['account'];
  58. if($merchantAccount == $info['account']){
  59. $isDemoMerchant = true;
  60. }
  61. return $this->render('detail',['isDemoMerchant'=>$isDemoMerchant,'merchantStatus'=>$merchantStatus,'merchantStatusName'=>$merchantStatusName,'asset' => $asset,'info' => $info,'status'=> $status,'extend'=>$extend]);
  62. }
  63. /**
  64. * 审核等操作
  65. */
  66. public function actionDeal()
  67. {
  68. $get = Yii::$app->request->get();
  69. $merchantId = isset($get['id']) ? $get['id'] : 0;
  70. $status = isset($get['status']) ? $get['status'] : 0;
  71. $merchantStatus = configDict::getConfig('merchantStatus');
  72. $merchant = xhMerchantService::getById($merchantId);
  73. $admin = xhAdminToMerchantService::getAdminByMerchantId($merchantId);
  74. if(empty($admin)){
  75. util::failInfo('此商家没有绑定管理员');
  76. }
  77. switch ($status){
  78. case $merchantStatus['pass']://审核通过
  79. xhMerchantService::pass($admin);
  80. break;
  81. case $merchantStatus['freeze']:
  82. break;
  83. }
  84. xhMerchantService::updateById($merchantId, ['status' => $status]);
  85. util::successInfo();
  86. }
  87. public function actionSyncUserInfo()
  88. {
  89. set_time_limit(0);
  90. $get = Yii::$app->request->get();
  91. $nextOpenId = isset($get['nextOpenId']) ? $get['nextOpenId'] : '';
  92. $merchantId = isset($get['merchantId']) ? $get['merchantId'] : '';
  93. $merchant = xhMerchantService::getById($merchantId);
  94. $result = weixinUtil::getSubscribeUserList($merchant,$nextOpenId);
  95. $total = isset($result['total']) ? $result['total'] : 0;
  96. $next_openid = isset($result['next_openid']) ? $result['next_openid'] : '';
  97. $count = isset($result['count']) ? $result['count'] : 0;
  98. $openIdList = isset($result['data']['openid']) ? $result['data']['openid'] : [];
  99. if($count == 0){
  100. util::successInfo('完成更新','A0001',['next_openid' => '','count' => 0]);
  101. }
  102. $total = count($openIdList);
  103. $totalPage = $total > 100 ? ceil($total / 100) : 1;
  104. for($m=1;$m<=$totalPage;$m++){
  105. $offset = ($m - 1) * 100;
  106. $subIdList = array_slice($openIdList,$offset,100);
  107. $info = weixinUtil::batchGetUserInfo($merchant,$subIdList);
  108. if(empty($info) || isset($info['user_info_list']) == false || empty($info['user_info_list'])){
  109. continue;
  110. }
  111. $user_info_list = $info['user_info_list'];
  112. foreach ($user_info_list as $val){
  113. $openId = $val['openid'];
  114. $user = xhUserService::getByOpenId($openId,$merchantId);
  115. if(!empty($user)){
  116. $upData = [];
  117. $upData['merchantId'] = $merchantId;
  118. $upData['sex'] = isset($val['sex']) ? $val['sex'] : 0;
  119. $upData['subscribe'] = $val['subscribe'];
  120. $upData['subscribeTime'] = $val['subscribe_time'];
  121. $upData['hasSubscribe'] = $val['subscribe'];
  122. if(isset($val['unionid']) && !empty($val['unionid'])){
  123. $upData['unionId'] = $val['unionid'];
  124. }
  125. if(isset($user['id']) == false){
  126. util::failInfo('出错了');
  127. }
  128. xhUserService::updateById($user['id'], $upData);
  129. }
  130. }
  131. }
  132. xhMerchantAssetService::updateByMerchantId($merchantId, ['totalFans'=>$count]);
  133. util::successInfo('获取成功','A0001',['next_openid' => $next_openid,'count' => $count]);
  134. }
  135. /**
  136. * 微信门店拉取
  137. */
  138. public function actionSyncShop()
  139. {
  140. $get = Yii::$app->request->get();
  141. $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
  142. $merchant = xhMerchantService::getById($merchantId);
  143. $xhShopService = new xhShopService();
  144. $re = $xhShopService->saveShopList($merchant);//查看门店申请数据
  145. $mcInfo = xhMerchantService::getById($merchantId);
  146. if($re === false){//无门店,自动建门店
  147. $re = xhShop::getByCondition(['businessName' => $mcInfo['merchantName']]);
  148. if(!$re){
  149. $return = $xhShopService->platformInitStore($mcInfo, $merchant);
  150. if($return === true){
  151. util::successInfo('系统自动创建门店成功');
  152. }
  153. util::failInfo($re);
  154. }else{
  155. util::failInfo('已创建好门店,正在审批中...');
  156. }
  157. }
  158. //无申请成功的门店,自动再申请一家
  159. //$re = ['total' => $num, 'success' => $successApply, 'applying' => $applying, 'fail' => $faileApply];
  160. if($re['success'] == 0 && $re['applying'] == 0){
  161. $return = $xhShopService->platformInitStore($mcInfo, $merchant);
  162. if(!$return){
  163. util::failInfo('暂无门店申请成功,系统自动创建门店失败');
  164. }
  165. util::successInfo('暂无门店申请成功,系统自动创建门店成功');
  166. exit();
  167. }
  168. $msg = '门店总数:'.$re['total'].'家,' . '申请成功:' . $re['success'] . '家,'.'审批中:' . $re['applying'] . '家,'.'未通过:' . $re['fail'] . '家。';
  169. util::successInfo($msg);
  170. }
  171. /**
  172. * 同步微信用户标签
  173. */
  174. public function actionSyncTag()
  175. {
  176. set_time_limit(0);
  177. $get = Yii::$app->request->get();
  178. $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
  179. $merchant = xhMerchantService::getById($merchantId);
  180. $allTag = weixinUtil::getTag($merchant);//取微信里保存的用户标签
  181. if(empty($allTag)){
  182. util::failInfo('没有用户标签信息');
  183. }
  184. $tagList = $allTag['tags'];
  185. $wxTag = [];
  186. if(!empty($tagList)){
  187. foreach($tagList as $key => $val){
  188. $wxTag[$val['id']] = $val['name'];
  189. }
  190. }
  191. $reWxTag = array_flip($wxTag);
  192. $dbTag = xhUserTagClassService::getMerchantTag($merchantId);
  193. $reDbTag = array_flip($dbTag);
  194. $delTag = array_diff($reDbTag,$reWxTag);//删除商家微信里没有的标签
  195. if(!empty($delTag)){
  196. foreach($delTag as $key => $val){
  197. xhUserTagClassService::deleteByCondition(['tagId' => $val,'merchantId' => $merchantId],$merchantId);
  198. $delTagRelation = xhUserTagService::getAllByCondition(['tagId' => $val,'merchantId' => $merchantId]);
  199. if(!empty($delTagRelation)){
  200. foreach($delTagRelation as $delTagVal){
  201. $userId = $delTagVal['userId'];
  202. xhUserService::refreshById($userId);
  203. $tId = $delTagVal['id'];
  204. xhUserTagService::delById($tId);
  205. }
  206. }
  207. }
  208. }
  209. $saveTag = array_diff($reWxTag,$reDbTag);//保存商家的标签
  210. if(!empty($saveTag)){
  211. foreach($saveTag as $key => $val){
  212. $data = ['merchantId' => $merchantId,'tagId' => $val,'name' => $key];
  213. xhUserTagClassService::add($data,$merchantId);
  214. }
  215. }
  216. $updateTag = array_intersect($reWxTag,$reDbTag);//更新商家的标签
  217. if(!empty($updateTag)){
  218. foreach($updateTag as $key => $val){
  219. xhUserTagClassService::updateByCondition(['merchantId' => $merchantId,'tagId' => $val], ['name' => $key],$merchantId);
  220. }
  221. }
  222. util::successInfo('同步成功');
  223. }
  224. public function actionSyncUserTag()
  225. {
  226. set_time_limit(0);
  227. $get = Yii::$app->request->get();
  228. $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
  229. $merchant = xhMerchantService::getById($merchantId);
  230. $tagList = xhUserTagClassService::getMerchantTag($merchantId);
  231. if(empty($tagList)){
  232. util::failInfo('请先同步商家标签');
  233. }
  234. $wx = new weixinUtil($merchant);
  235. foreach($tagList as $tagId => $tag){
  236. $nextOpenId = '';
  237. $this->__updateCategory($wx, $tagId, $nextOpenId,$merchantId);
  238. }
  239. util::successInfo('同步成功');
  240. }
  241. public function __updateCategory($wx,$tagId,$nextOpenId,$merchantId)
  242. {
  243. $return = $wx->getUserTag($tagId,$nextOpenId);
  244. $count = isset($return['count']) ? $return['count'] : 0;
  245. $nextOpenId = isset($return['next_openid']) ? $return['next_openid'] : '';
  246. $openIdList = isset($return['data']['openid']) ? $return['data']['openid'] : [];
  247. if(!empty($openIdList)){
  248. foreach($openIdList as $openId){
  249. $user = xhUserService::getByOpenId($openId,$merchantId);
  250. if(empty($user)){
  251. continue;
  252. }
  253. $userId = $user['id'];
  254. $tagData = ['merchantId'=>$merchantId,'tagId'=>$tagId,'userId'=>$userId];
  255. $userTag = xhUserTagService::getAllByCondition(['tagId'=>$tagId,'userId'=>$userId]);
  256. if(empty($userTag)){
  257. xhUserTagService::add($tagData);
  258. }
  259. }
  260. if($count > 0){
  261. $this->__updateCategory($wx, $tagId, $nextOpenId,$merchantId);
  262. }
  263. }
  264. }
  265. public function actionFans()
  266. {
  267. $get = Yii::$app->request->get();
  268. $merchantId = isset($get['merchantId']) ? $get['merchantId'] : 0;
  269. $get = Yii::$app->request->get();
  270. $query = xhUser::find();
  271. $query->where(['merchantId' =>$merchantId]);
  272. if(isset($get['content']) && !empty($get['content'])){
  273. $content = $get['content'];
  274. if(stringUtil::isMobieNumber($content)){
  275. $query->andWhere(['mobile' => $content]);
  276. }else{
  277. $query->andWhere(['like', 'userName', $content]);
  278. }
  279. }
  280. if(isset($get['class']) && $get['class'] == 1){
  281. $query->andWhere(['isMember' => 1]);
  282. }
  283. $countQuery = clone $query;
  284. $count = $countQuery->count();
  285. $page = isset($get['page']) ? $get['page'] : 1;
  286. $pageSize = 20;
  287. $offset = ($page - 1) * $pageSize;
  288. $models = $query->offset($offset)->orderBy('id desc')->limit($pageSize)->asArray()->all();
  289. $page = new page($count,$page,$pageSize);
  290. $paging = $page->fpage();
  291. $auth = xhMerchantService::isAuth($merchantId);
  292. $merchantTag = xhUserTagClassService::getMerchantTag($merchantId);
  293. $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  294. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  295. $memberIntegral = xhMerchantExtendService::getGradeList($merchantExtend,'desc');
  296. $merchant = xhMerchantService::getById($merchantId);
  297. return $this->render('fans', [
  298. 'merchantTag' => $merchantTag,
  299. 'models' => $models,
  300. 'pages' => $paging,
  301. 'merchant' => $merchant,
  302. 'memberIntegral' => $memberIntegral,
  303. 'merchantAsset' => $merchantAsset,
  304. 'merchantExtend' => $merchantExtend,
  305. ]);
  306. }
  307. /**
  308. * 进行提现操作,全额提现
  309. */
  310. public function actionDrawCash()
  311. {
  312. $connection = Yii::$app->db;//事务处理
  313. $transaction = $connection->beginTransaction();
  314. $get = Yii::$app->request->get();
  315. $merchantId = isset($get['id']) ? $get['id'] : 0;
  316. $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  317. try {
  318. $amount = $merchantAsset['balance'];
  319. $mBalance = stringUtil::calcSub($merchantAsset['balance'], $amount);
  320. $date = date("Y-m-d H:i:s");
  321. $id = 'D'.stringUtil::buildOrderNo();//订单号
  322. $cashData = ['amount'=>$amount,'merchantId'=>$merchantId,'balance'=>$mBalance,'addTime'=>time(),'id'=>$id,'createTime'=>$date];
  323. $order = xhDrawCashService::add($cashData);
  324. $orderId = $order['id'];
  325. xhMerchantAssetService::updateByMerchantId($merchantId, ['balance'=>$mBalance,'freezeBalance'=>$amount]);
  326. $now = time();
  327. $capitalTypeArray = configDict::getConfig('capitalType','xhDrawCash');
  328. $capitalType = $capitalTypeArray['id'];
  329. $mIncome = $merchantAsset['totalIncome'];
  330. $mExpend = $merchantAsset['totalExpend'];
  331. $payWay = configDict::getConfig('payWay','other');
  332. $capitalData = [
  333. 'relateId' => (string)$orderId,
  334. 'balance' => $mBalance,
  335. 'totalIncome' => $mIncome,
  336. 'totalExpend' => $mExpend,
  337. 'amount' => $amount,
  338. 'io' => 0,
  339. 'payWay' => $payWay,
  340. 'event' => '申请提现',
  341. 'merchantId' => $merchantId,
  342. 'userId' => 0,
  343. 'userName' => '',
  344. 'alipayId' => '',
  345. 'affectBalance' => 1,//会影响商家的余额
  346. 'createTime' => $date,
  347. 'addTime' => $now,
  348. 'capitalType' => $capitalType,
  349. 'operateId' => $this->adminId,
  350. ];
  351. xhMerchantCapitalService::add($capitalData);//商家资金流水记录增加
  352. $transaction->commit();
  353. util::successInfo();
  354. }catch (Exception $e) {
  355. $transaction->rollBack();
  356. util::failInfo();
  357. }
  358. }
  359. public function actionInit()
  360. {
  361. $get = Yii::$app->request->get();
  362. $merchantId = isset($get['id']) ? $get['id'] : 0;
  363. $merchant = xhMerchantService::getById($merchantId);
  364. $extend = xhMerchantExtendService::getByMerchantId($merchantId);
  365. $gradeList = xhMerchantExtendService::getGradeList($extend);
  366. $wxPayMerchantId = '';
  367. $wxPayKey = '';
  368. $trainDemo = configDict::getConfig('trainDemo');
  369. if($merchant['id'] == $trainDemo['merchant']['account']){
  370. $wxPayMerchantId = $trainDemo['merchant']['wxPayMerchantId'];
  371. $wxPayKey = $trainDemo['merchant']['wxPayKey'];
  372. }
  373. if($merchant['id'] == $trainDemo['platform']['account']){
  374. $wxPayMerchantId = $trainDemo['platform']['wxPayMerchantId'];
  375. $wxPayKey = $trainDemo['platform']['wxPayKey'];
  376. }
  377. return $this->render('init',['extend' => $extend,'gradeList' => $gradeList,'merchantId'=>$merchantId,'wxPayMerchantId'=>$wxPayMerchantId,'wxPayKey'=>$wxPayKey]);
  378. }
  379. public function actionInitSet()
  380. {
  381. $post = Yii::$app->request->post();
  382. $merchantId = isset($post['merchantId']) ? $post['merchantId'] : 0;
  383. $extend = xhMerchantExtendService::getByMerchantId($merchantId);
  384. $merchant = xhMerchantService::getById($merchantId);
  385. if(empty($extend['wxTMessageInit'])){//开通创建模板消息
  386. xhTMessageService::init($merchant, $extend);
  387. }else{//补充新增加的模板消息
  388. xhTMessageService::supplement($merchant);
  389. }
  390. unset($post['_csrf']);
  391. $post['init'] = 1;//完成初始化
  392. xhMerchantExtendService::updateByMerchantId($merchantId, $post);//上面要先初始化,再执行这边的更新
  393. util::successInfo();
  394. }
  395. public function actionSetAgent()
  396. {
  397. $get = Yii::$app->request->get();
  398. $id = isset($get['id']) ? $get['id'] : 0;
  399. $data['agentLevel'] = 1;
  400. xhMerchantService::updateById($id, $data);
  401. util::successInfo();
  402. }
  403. }