UserClass.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. namespace biz\user\classes;
  3. use biz\merchant\classes\MerchantAssetClass;
  4. use biz\merchant\classes\MerchantClass;
  5. use biz\merchant\services\MerchantService;
  6. use biz\message\classes\ChatClass;
  7. use biz\user\services\UserIntegralService;
  8. use common\components\business;
  9. use common\components\configDict;
  10. use common\components\util;
  11. use common\components\wxUtil;
  12. use common\services\ImageService;
  13. use common\services\xhMerchantExtendService;
  14. use common\services\xhUserCapitalService;
  15. use common\services\xhUserIntegralService;
  16. use common\services\xhUserMergeService;
  17. use Yii;
  18. use biz\base\classes\BaseClass;
  19. class UserClass extends BaseClass
  20. {
  21. public static $baseFile = '\biz\user\models\User';
  22. //用户和收入的来源
  23. public static $sourceType = [
  24. 2 => '朋友圈',
  25. 1 => '门店',
  26. 0 => '公众号',
  27. 3 => '淘宝',
  28. ];
  29. //用户来源分类
  30. public static $userSource = [
  31. 0 => ['name' => 'official', 'title' => '公众号'],
  32. 1 => ['name' => 'alipay', 'title' => '支付宝'],
  33. 2 => ['name' => 'mini', 'title' => '小程序'],
  34. 3 => ['name' => 'friend', 'title' => '朋友圈'],
  35. 4 => ['name' => 'meituan', 'title' => '美团'],
  36. 5 => ['name' => 'system', 'title' => '系统'],
  37. ];
  38. //用户来源分类ID
  39. public static $userSourceId = [
  40. 'official' => ['id' => 0, 'name' => 'official'],
  41. 'alipay' => ['id' => 1, 'name' => 'alipay'],
  42. 'mini' => ['id' => 2, 'name' => 'mini'],
  43. 'friend' => ['id' => 3, 'name' => 'friend'],
  44. 'meituan' => ['id' => 4, 'name' => 'meituan'],
  45. 'system' => ['id' => 5, 'name' => 'system'],
  46. ];
  47. //组合客户基本和资产信息
  48. public static function groupUserBaseInfo($list, $sensitive = true)
  49. {
  50. //取客户资产
  51. $ids = array_column($list, 'id');
  52. $ids = array_unique($ids);
  53. $asset = UserAssetClass::getByUserIds($ids, 'userId');
  54. $merchantIdList = array_column($list, 'merchantId');
  55. $merchantIdList = array_filter(array_unique($merchantIdList));
  56. $merchantList = MerchantService::getByIds($merchantIdList, null, 'id');
  57. foreach ($list as $key => $val) {
  58. $userId = $val['id'];
  59. //访问时间
  60. $visitTime = date("m-d H:i", $val['visitTime']);
  61. if (date("m-d") == date("m-d", $val['visitTime'])) {
  62. $visitTime = '今天 ' . date("H:i", $val['visitTime']);
  63. }
  64. $list[$key]['visitTimeFormat'] = $visitTime;
  65. $list[$key]['userAsset'] = isset($asset[$userId]) ? $asset[$userId] : [];
  66. //客户发来新消息数
  67. $list[$key]['unReadNum'] = ChatClass::getUnreadNum($userId);
  68. $list[$key] = business::formatUserAvatar($list[$key]);
  69. $merchantId = $val['merchantId'];
  70. $list[$key]['merchantName'] = isset($merchantList[$merchantId]['merchantName']) ? $merchantList[$merchantId]['merchantName'] : '';
  71. //敏感信息不要输出
  72. if ($sensitive) {
  73. unset($list[$key]['password']);
  74. unset($list[$key]['payPassword']);
  75. }
  76. }
  77. return $list;
  78. }
  79. //获取客户基本和资产信息 shish 2019.11.30
  80. public static function getUserInfo($id, $sensitive = true)
  81. {
  82. $list = self::getByIds([$id]);
  83. $data = self::groupUserBaseInfo($list, $sensitive);
  84. return current($data);
  85. }
  86. //根据手机号查询客户 shish 2019.12.1
  87. public static function getByMobile($mobile, $merchantId)
  88. {
  89. return self::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
  90. }
  91. public static function generateUser($getUserInfo, $merchantId)
  92. {
  93. $date = date("Y-m-d H:i:s");
  94. $time = time();
  95. //替换掉微信名称不支持的图片
  96. $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
  97. $getUserInfo['userName'] = $userName;
  98. $getUserInfo['nickName'] = $userName;
  99. $getUserInfo['realName'] = isset($getUserInfo['realName']) && !empty($getUserInfo['realName']) ? $getUserInfo['realName'] : '';
  100. $getUserInfo['sex'] = isset($getUserInfo['sex']) ? $getUserInfo['sex'] : 0;//未知
  101. $getUserInfo['identity'] = 0;
  102. $subscribe = isset($getUserInfo['subscribe']) ? $getUserInfo['subscribe'] : 0;
  103. $getUserInfo['subscribe'] = $subscribe;
  104. $getUserInfo['merchantId'] = $merchantId;
  105. $getUserInfo['status'] = 0;
  106. //默认没有完整用户信息
  107. $isFull = isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl']) ? 1 : 0;
  108. $getUserInfo['isFull'] = $isFull;
  109. $getUserInfo['createTime'] = $date;
  110. $getUserInfo['avatar'] = '';
  111. $getUserInfo['password'] = '';
  112. $getUserInfo['payPassword'] = '';
  113. $getUserInfo['mobile'] = isset($getUserInfo['mobile']) ? $getUserInfo['mobile'] : '';
  114. $getUserInfo['addTime'] = $time;
  115. $getUserInfo['visitTime'] = $time;
  116. $getUserInfo['source'] = isset($getUserInfo['source']) ? $getUserInfo['source'] : 0;
  117. $getUserInfo['hasSubscribe'] = 0;
  118. //生成用户基础信息
  119. $user = self::add($getUserInfo);
  120. $userId = $user['id'];
  121. if (empty($user['userName'])) {
  122. self::updateById($userId, ['userName' => $userId, 'nickName' => $userId]);
  123. }
  124. $assetData = ['userId' => $userId, 'merchantId' => $merchantId, 'createTime' => $date];
  125. //生成用户资产信息
  126. UserAssetClass::add($assetData);
  127. //保存头像
  128. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  129. if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) {
  130. $imgUrl = $getUserInfo['headImgUrl'];
  131. $currentImg = ImageService::generateAvatar($merchantId, $imgUrl);
  132. if (!empty($currentImg)) {
  133. self::updateById($userId, ['avatar' => $currentImg]);
  134. }
  135. }
  136. }
  137. //客户数粉丝数增加
  138. MerchantAssetClass::addUser($merchantId, $subscribe, true);
  139. return $user;
  140. }
  141. public static function updateUser($originalUser, $getUserInfo, $merchantId)
  142. {
  143. $data = [];
  144. $assetData = [];
  145. $userId = $originalUser['id'];
  146. //替换掉微信不支持的图片
  147. $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
  148. if (!empty($userName)) {
  149. $data['userName'] = $userName;
  150. $data['nickName'] = $userName;
  151. }
  152. if (isset($getUserInfo['openId']) && !empty($getUserInfo['openId'])) {
  153. $openId = $getUserInfo['openId'];
  154. $data['openId'] = $openId;
  155. }
  156. if (isset($getUserInfo['miniOpenId']) && !empty($getUserInfo['miniOpenId'])) {
  157. $data['miniOpenId'] = $getUserInfo['miniOpenId'];
  158. }
  159. if (isset($getUserInfo['unionId']) && !empty($getUserInfo['unionId'])) {
  160. $data['unionId'] = $getUserInfo['unionId'];
  161. }
  162. if (isset($getUserInfo['sex'])) {
  163. $data['sex'] = $getUserInfo['sex'];
  164. }
  165. if (isset($getUserInfo['subscribe'])) {
  166. $data['subscribe'] = $getUserInfo['subscribe'];
  167. }
  168. if (isset($getUserInfo['subscribeTime'])) {
  169. $data['subscribeTime'] = $getUserInfo['subscribeTime'];
  170. }
  171. $data['merchantId'] = $merchantId;
  172. if (isset($getUserInfo['alipayId']) && !empty($getUserInfo['alipayId'])) {
  173. $data['alipayId'] = $getUserInfo['alipayId'];
  174. }
  175. if (isset($getUserInfo['isFull'])) {
  176. $data['isFull'] = $getUserInfo['isFull'];
  177. }
  178. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  179. $data['isFull'] = 1;
  180. }
  181. //更新头像
  182. if (isset($originalUser['avatar']) == false || empty($originalUser['avatar'])) {
  183. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  184. $imgUrl = $getUserInfo['headImgUrl'];
  185. $name = ImageService::generateAvatar($merchantId, $imgUrl);
  186. if (!empty($name)) {
  187. $data['avatar'] = $name;
  188. }
  189. }
  190. }
  191. //增加粉丝数
  192. if (isset($originalUser['subscribe']) && $originalUser['subscribe'] == 0 && isset($getUserInfo['subscribe']) && $getUserInfo['subscribe'] == 1) {
  193. MerchantAssetClass::addUser($merchantId, 1, false);
  194. }
  195. self::updateById($userId, $data);
  196. if (!empty($assetData)) {
  197. UserAssetClass::updateByUserId($userId, $assetData);
  198. }
  199. $user = self::getById($userId);
  200. return $user;
  201. }
  202. public static function getByMiniOpenId($miniOpenId)
  203. {
  204. return self::getByCondition(['miniOpenId' => $miniOpenId]);
  205. }
  206. public static function getByAlipayId($alipayId, $merchantId)
  207. {
  208. return self::getByCondition(['merchantId' => $merchantId, 'alipayId' => $alipayId]);
  209. }
  210. public static function getByOpenId($openId, $merchantId)
  211. {
  212. return self::getByCondition(['merchantId' => $merchantId, 'openId' => $openId]);
  213. }
  214. public static function getByUnionId($unionId)
  215. {
  216. return self::getByCondition(['unionId' => $unionId]);
  217. }
  218. //合并用户
  219. public static function mergeUser($delUser, $retainUser)
  220. {
  221. $connection = Yii::$app->db;//事务处理
  222. $transaction = $connection->beginTransaction();
  223. try {
  224. $delUserId = $delUser['id'];
  225. $retainUserId = $retainUser['id'];
  226. $userName = $retainUser['userName'];
  227. $merchantId = $retainUser['merchantId'];
  228. //合并记录
  229. $return = UserMergeClass::add([
  230. 'delUserId' => $delUserId,
  231. 'merchantId' => $merchantId,
  232. 'retainUserId' => $retainUserId,
  233. 'addTime' => time(),
  234. 'createTime' => date("Y-m-d H:i:s")
  235. ]);
  236. $targetId = $return['id'];
  237. $typeList = configDict::getConfig('capitalType');
  238. $capitalType = $typeList['xhMergeUser']['id'];
  239. $now = time();
  240. $date = date("Y-m-d H:i:s");
  241. //数据迁移
  242. $updateData = [];
  243. $updateKey = [
  244. 'mobile',
  245. 'password',
  246. 'payPassword',
  247. 'subscribe',
  248. 'hasSubscribe',
  249. 'subscribeTime',
  250. 'alipayId',
  251. 'openId',
  252. 'miniOpenId',
  253. 'unionId',
  254. ];
  255. foreach ($updateKey as $key) {
  256. if (isset($delUser[$key]) && !empty($delUser[$key])) {
  257. $updateData[$key] = $delUser[$key];
  258. }
  259. }
  260. if (!empty($updateData)) {
  261. $updateData['visitTime'] = time();
  262. self::updateById($retainUserId, $updateData);
  263. }
  264. //资产累加
  265. $addData = [];
  266. $delUserAsset = UserAssetClass::getByUserId($delUserId);
  267. $retainUserAsset = UserAssetClass::getByUserId($retainUserId);
  268. if (!empty($delUserAsset)) {
  269. //累计消费
  270. $delTotalExpend = isset($delUserAsset['totalExpend']) ? $delUserAsset['totalExpend'] : 0;
  271. if (!empty($delTotalExpend)) {
  272. $addData['totalExpend'] = $retainUserAsset['totalExpend'] + $delTotalExpend;
  273. //变化记录要增加
  274. }
  275. //累计收入
  276. $delTotalIncome = isset($delUserAsset['totalIncome']) ? $delUserAsset['totalIncome'] : 0;
  277. if (!empty($delTotalIncome)) {
  278. $addData['totalIncome'] = $retainUserAsset['totalIncome'] + $delTotalIncome;
  279. //变化记录要增加
  280. }
  281. //累计充值
  282. $delTotalRecharge = isset($delUserAsset['totalRecharge']) ? $delUserAsset['totalRecharge'] : 0;
  283. if (!empty($delTotalRecharge)) {
  284. $addData['totalRecharge'] = $retainUserAsset['totalRecharge'] + $delTotalRecharge;
  285. //变化记录要增加
  286. }
  287. //累计购买数量
  288. $delTotalBuyNum = isset($delUserAsset['totalBuyNum']) ? $delUserAsset['totalBuyNum'] : 0;
  289. if (!empty($delTotalBuyNum)) {
  290. $addData['totalBuyNum'] = $retainUserAsset['totalBuyNum'] + $delTotalBuyNum;
  291. //变化记录要增加
  292. }
  293. //余额
  294. $delBalance = isset($delUserAsset['balance']) ? $delUserAsset['balance'] : 0;
  295. if (!empty($delBalance)) {
  296. $addData['balance'] = $retainUserAsset['balance'] + $delBalance;
  297. //变化记录要增加
  298. $userCapitalData = [
  299. 'relateId' => $targetId,
  300. 'balance' => $addData['balance'],
  301. 'totalIncome' => $retainUserAsset['totalIncome'] + $delBalance,
  302. 'totalExpend' => $retainUserAsset['totalExpend'],
  303. 'amount' => $delBalance,
  304. 'io' => 1,
  305. 'payWay' => 0,
  306. 'event' => '帐号合并',
  307. 'merchantId' => $merchantId,
  308. 'userId' => $retainUserId,
  309. 'alipayId' => '',
  310. 'userName' => $userName,
  311. 'operateId' => 0,
  312. 'createTime' => $date,
  313. 'addTime' => $now,
  314. 'capitalType' => $capitalType,
  315. ];
  316. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  317. }
  318. //成长值
  319. $delGrowth = isset($delUserAsset['growth']) ? $delUserAsset['growth'] : 0;
  320. if (!empty($delGrowth)) {
  321. $addData['growth'] = $retainUserAsset['growth'] + $delGrowth;
  322. //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
  323. $growthData = [
  324. 'userId' => $retainUserId,
  325. 'userName' => $userName,
  326. 'merchantId' => $merchantId,
  327. 'relateId' => $targetId,
  328. 'integral' => $addData['growth'],
  329. 'num' => $delGrowth,
  330. 'io' => 1,
  331. 'payWay' => 0,
  332. 'alipayId' => '',
  333. 'event' => '帐号合并',
  334. 'operatorId' => 0,
  335. 'createTime' => $date,
  336. 'capitalType' => $capitalType,
  337. 'addTime' => $now,
  338. ];
  339. UserGrowthClass::add($growthData);
  340. //成长值增加升级
  341. $preLevel = $retainUserAsset['member'];
  342. $preGrowth = $retainUserAsset['growth'];
  343. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  344. UserIntegralService::increaseToUpgrade($preGrowth, $addData['growth'], $preLevel, $retainUserId, $merchantId, $merchantExtend);
  345. }
  346. if (!empty($addData)) {
  347. UserAssetClass::updateByUserId($retainUserId, $addData);
  348. }
  349. }
  350. //删除用户
  351. $clearData = ['openId' => '', 'miniOpenId' => '', 'unionId' => '', 'alipayId' => '', 'merchantId' => 0, 'mobile' => ''];
  352. self::updateById($delUserId, $clearData);
  353. UserAssetClass::updateByUserId($delUserId, ['merchantId' => 0]);
  354. //如果有支付宝信息则清除支付宝用户
  355. UserAlipayClass::updateByUserId($delUserId, ['alipayId' => '', 'alipayAccount' => '', 'merchantId' => 0]);
  356. //合并帐号减少客户数 shish 2020.5.13
  357. MerchantAssetClass::mergeToReduceUser($merchantId);
  358. $transaction->commit();
  359. return self::getById($retainUserId);
  360. } catch (Exception $e) {
  361. $transaction->rollBack();
  362. Yii::info("合并userid $delUserId $retainUserId 时事务回滚,原因:" . $e->getMessage());
  363. return [];
  364. }
  365. }
  366. //找出相同unionId的用户
  367. public static function getSameUnionId($unionId)
  368. {
  369. return self::getAllByCondition(['unionId' => $unionId], 'addTime desc', '*', 'id');
  370. }
  371. public static function valid($info, $merchantId)
  372. {
  373. if (empty($info)) {
  374. util::fail('找不到客户');
  375. }
  376. if ($info['merchantId'] != $merchantId) {
  377. util::fail('这个客户您没有权限操作');
  378. }
  379. }
  380. //根据消费次数和有没领优惠券来判断是不是新人
  381. public static function newUser($userInfo)
  382. {
  383. $asset = isset($userInfo['userAsset']) ? $userInfo['userAsset'] : [];
  384. $totalBuyNum = isset($asset['totalBuyNum']) ? $asset['totalBuyNum'] : 0;
  385. if ($totalBuyNum > 0) {
  386. return false;
  387. }
  388. $getCouponNum = isset($asset['getCouponNum']) ? $asset['getCouponNum'] : 0;
  389. if ($getCouponNum) {
  390. return false;
  391. }
  392. return true;
  393. }
  394. public static function getUserByIds($ids)
  395. {
  396. $info = self::getByIds($ids, null, 'id');
  397. if (empty($info)) {
  398. return [];
  399. }
  400. $data = self::groupUserBaseInfo($info);
  401. return $data;
  402. }
  403. //将微信和小程序获取的用户信息转成可以保存的用户信息
  404. public static function switchUserInfo($info, $source)
  405. {
  406. $openId = '';
  407. if (isset($info['openid']) && !empty($info['openid'])) {
  408. $openId = $info['openid'];
  409. unset($info['openid']);
  410. }
  411. if (isset($info['openId']) && !empty($info['openId'])) {
  412. $openId = $info['openId'];
  413. unset($info['openId']);
  414. }
  415. if (!empty($openId)) {
  416. $userSource = self::$userSourceId;
  417. switch ($source) {
  418. case $userSource['mini']['name']:
  419. $info['miniOpenId'] = $openId;
  420. break;
  421. case $userSource['official']['name']:
  422. $info['openId'] = $openId;
  423. break;
  424. default:
  425. }
  426. }
  427. if (isset($info['miniOpenId'])) {
  428. $info['miniOpenId'] = $info['miniOpenId'];
  429. }
  430. if (isset($info['nickName'])) {
  431. $info['userName'] = $info['nickName'];
  432. $info['nickName'] = $info['nickName'];
  433. }
  434. if (isset($info['unionid'])) {
  435. $info['unionId'] = $info['unionid'];
  436. }
  437. if (isset($info['unionId'])) {
  438. $info['unionId'] = $info['unionId'];
  439. }
  440. if (isset($info['headimgurl'])) {
  441. $info['headImgUrl'] = $info['headimgurl'];
  442. }
  443. if (isset($info['avatarUrl'])) {
  444. $info['headImgUrl'] = $info['avatarUrl'];
  445. }
  446. if (isset($info['subscribe_time'])) {
  447. $info['subscribeTime'] = $info['subscribe_time'];
  448. }
  449. if (isset($info['subscribe'])) {
  450. $info['subscribe'] = $info['subscribe'];
  451. }
  452. return $info;
  453. }
  454. //创建以及更新用户 shish 2020.2.7
  455. public static function replaceUser($userInfo, $source, $merchantId)
  456. {
  457. $userInfo = self::switchUserInfo($userInfo, $source);
  458. $unionId = isset($userInfo['unionId']) && !empty($userInfo['unionId']) ? $userInfo['unionId'] : '';
  459. $unionUser = [];
  460. if (!empty($unionId)) {
  461. $unionUser = self::getByUnionId($unionId);
  462. }
  463. $userSource = self::$userSourceId;
  464. switch ($source) {
  465. case $userSource['mini']['name']:
  466. $sourceId = $userSource['mini']['id'];
  467. $miniOpenId = $userInfo['miniOpenId'];
  468. $user = self::getByMiniOpenId($miniOpenId);
  469. break;
  470. case $userSource['alipay']['name']:
  471. $sourceId = $userSource['alipay']['id'];
  472. $alipayId = $userInfo['alipayId'];
  473. $user = self::getByAlipayId($alipayId, $merchantId);
  474. break;
  475. case $userSource['official']['name']:
  476. $sourceId = $userSource['official']['id'];
  477. //后台添加的客户没有openId
  478. $openId = isset($userInfo['openId']) ? $userInfo['openId'] : '';
  479. $user = [];
  480. if (!empty($openId)) {
  481. $user = self::getByOpenId($openId, $merchantId);
  482. }
  483. break;
  484. case $userSource['system']['name']:
  485. $sourceId = $userSource['system']['id'];
  486. $mobile = isset($userInfo['mobile']) && !empty($userInfo['mobile']) ? $userInfo['mobile'] : '';
  487. $user = [];
  488. if (!empty($mobile)) {
  489. $user = self::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
  490. }
  491. break;
  492. default:
  493. throw new \Exception('未知来源的用户');
  494. }
  495. //支付宝暂时获取不到很多客户信息不进行更新
  496. if ($source == $userSource['alipay']['name']) {
  497. if (!empty($user)) {
  498. return $user;
  499. }
  500. }
  501. if (empty($user)) {
  502. if (!empty($unionUser)) {
  503. //1、在小程序端已经授权用户信息和unionId之后,关注公众号时走这里
  504. //2、关注了公众号或者H5已经授权时,并首次访问小程序
  505. $user = self::updateUser($unionUser, $userInfo, $merchantId);
  506. } else {
  507. $userInfo['sourceType'] = $sourceId;
  508. $userInfo['merchantId'] = $merchantId;
  509. $user = self::generateUser($userInfo, $merchantId);
  510. }
  511. } else {
  512. if (!empty($unionUser)) {
  513. if (isset($unionUser['id']) && isset($user['id']) && $user['id'] != $unionUser['id']) {
  514. //合并相同客户
  515. $user = self::mergeUser($unionUser, $user);
  516. }
  517. }
  518. $user = self::updateUser($user, $userInfo, $merchantId);
  519. }
  520. return $user;
  521. }
  522. //拉取粉丝 shish 2020.2.7
  523. public static function syncWxUser($merchantId, $nextOpenId = '')
  524. {
  525. $merchant = MerchantClass::getMerchantById($merchantId);
  526. //一次拉取调用最多拉取10000个关注者的OpenID
  527. $result = wxUtil::getSubscribeUserList($merchant, $nextOpenId);
  528. $total = isset($result['total']) ? $result['total'] : 0;
  529. $next_openid = isset($result['next_openid']) ? $result['next_openid'] : '';
  530. $count = isset($result['count']) ? $result['count'] : 0;
  531. $openIdList = isset($result['data']['openid']) ? $result['data']['openid'] : [];
  532. $currentTotal = count($openIdList);
  533. if ($count == 0 || $currentTotal == 0) {
  534. return ['total' => $total, 'count' => $count, 'nextOpenId' => $next_openid];
  535. }
  536. $currentTotalPage = $currentTotal > 100 ? ceil($currentTotal / 100) : 1;
  537. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  538. $source = $userSource['name'];
  539. for ($m = 1; $m <= $currentTotalPage; $m++) {
  540. $offset = ($m - 1) * 100;
  541. $subIdList = array_slice($openIdList, $offset, 100);
  542. $info = wxUtil::batchGetUserInfo($merchant, $subIdList);
  543. if (empty($info) || isset($info['user_info_list']) == false || empty($info['user_info_list'])) {
  544. continue;
  545. }
  546. $user_info_list = $info['user_info_list'];
  547. foreach ($user_info_list as $val) {
  548. $val['isFull'] = 1;
  549. $val['unionId'] = $val['unionid'];
  550. $val['headImgUrl'] = $val['headimgurl'];
  551. $val['openId'] = $val['openid'];
  552. $val['nickName'] = $val['nickName'];
  553. $val['merchantId'] = $merchantId;
  554. self::replaceUser($val, $source, $merchantId);
  555. }
  556. }
  557. return ['total' => $total, 'count' => $count, 'nextOpenId' => $next_openid];
  558. }
  559. }