UserClass.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace biz\user\classes;
  3. use biz\merchant\classes\MerchantAssetClass;
  4. use biz\merchant\services\MerchantService;
  5. use biz\message\classes\ChatClass;
  6. use biz\user\services\UserIntegralService;
  7. use common\components\business;
  8. use common\components\configDict;
  9. use common\components\util;
  10. use common\services\ImageService;
  11. use common\services\xhMerchantExtendService;
  12. use common\services\xhUserCapitalService;
  13. use common\services\xhUserIntegralService;
  14. use common\services\xhUserMergeService;
  15. use Yii;
  16. use biz\base\classes\BaseClass;
  17. class UserClass extends BaseClass
  18. {
  19. public static $baseFile = '\biz\user\models\User';
  20. //用户和收入的来源
  21. public static $sourceType = [
  22. 2 => '朋友圈',
  23. 1 => '门店',
  24. 0 => '公众号',
  25. 3 => '淘宝',
  26. ];
  27. //用户来源分类
  28. public static $userSource = [
  29. 0 => ['name' => 'official', 'title' => '公众号'],
  30. 1 => ['name' => 'alipay', 'title' => '支付宝'],
  31. 2 => ['name' => 'mini', 'title' => '小程序'],
  32. ];
  33. //用户来源分类ID
  34. public static $userSourceId = [
  35. 'official' => ['id' => 0, 'name' => 'official'],
  36. 'alipay' => ['id' => 1, 'name' => 'alipay'],
  37. 'mini' => ['id' => 2, 'name' => 'mini'],
  38. ];
  39. //组合客户基本和资产信息
  40. public static function groupUserBaseInfo($list)
  41. {
  42. //取客户资产
  43. $ids = array_column($list, 'id');
  44. $ids = array_unique($ids);
  45. $asset = UserAssetClass::getByUserIds($ids, 'userId');
  46. $merchantIdList = array_column($list, 'merchantId');
  47. $merchantIdList = array_filter(array_unique($merchantIdList));
  48. $merchantList = MerchantService::getByIds($merchantIdList, null, 'id');
  49. foreach ($list as $key => $val) {
  50. $userId = $val['id'];
  51. //访问时间
  52. $visitTime = date("m-d H:i", $val['visitTime']);
  53. if (date("m-d") == date("m-d", $val['visitTime'])) {
  54. $visitTime = '今天 ' . date("H:i", $val['visitTime']);
  55. }
  56. $list[$key]['visitTimeFormat'] = $visitTime;
  57. $list[$key]['userAsset'] = isset($asset[$userId]) ? $asset[$userId] : [];
  58. //客户发来新消息数
  59. $list[$key]['unReadNum'] = ChatClass::getUnreadNum($userId);
  60. $list[$key] = business::formatUserAvatar($list[$key]);
  61. $merchantId = $val['merchantId'];
  62. $list[$key]['merchantName'] = isset($merchantList[$merchantId]['merchantName']) ? $merchantList[$merchantId]['merchantName'] : '';
  63. }
  64. return $list;
  65. }
  66. //获取客户基本和资产信息 shish 2019.11.30
  67. public static function getUserInfo($id)
  68. {
  69. $list = self::getByIds([$id]);
  70. $data = self::groupUserBaseInfo($list);
  71. return current($data);
  72. }
  73. //根据手机号查询客户 shish 2019.12.1
  74. public static function getByMobile($mobile, $merchantId)
  75. {
  76. return self::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
  77. }
  78. public static function generateUser($getUserInfo, $merchantId)
  79. {
  80. //替换掉微信不支持的图片
  81. $date = date("Y-m-d H:i:s");
  82. $time = time();
  83. $getUserInfo['userName'] = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
  84. $getUserInfo['realName'] = isset($getUserInfo['realName']) && !empty($getUserInfo['realName']) ? $getUserInfo['realName'] : '';
  85. $getUserInfo['sex'] = isset($getUserInfo['sex']) ? $getUserInfo['sex'] : 0;//未知
  86. $getUserInfo['identity'] = 0;
  87. $subscribe = isset($getUserInfo['subscribe']) ? $getUserInfo['subscribe'] : 0;
  88. $getUserInfo['subscribe'] = $subscribe;
  89. $getUserInfo['merchantId'] = $merchantId;
  90. $getUserInfo['status'] = 0;
  91. //默认没有完整用户信息
  92. $isFull = isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl']) ? 1 : 0;
  93. $getUserInfo['isFull'] = $isFull;
  94. $getUserInfo['createTime'] = $date;
  95. $getUserInfo['avatar'] = '';
  96. $getUserInfo['password'] = '';
  97. $getUserInfo['payPassword'] = '';
  98. $getUserInfo['mobile'] = isset($getUserInfo['mobile']) ? $getUserInfo['mobile'] : 0;
  99. $getUserInfo['addTime'] = $time;
  100. $getUserInfo['visitTime'] = $time;
  101. $getUserInfo['source'] = isset($getUserInfo['source']) ? $getUserInfo['source'] : 0;
  102. $getUserInfo['hasSubscribe'] = 0;
  103. //生成用户基础信息
  104. $user = self::add($getUserInfo);
  105. $userId = $user['id'];
  106. if (empty($user['userName'])) {
  107. self::updateById($userId, ['userName' => $userId]);
  108. }
  109. $assetData = ['userId' => $userId, 'merchantId' => $merchantId, 'createTime' => $date];
  110. //生成用户资产信息
  111. UserAssetClass::add($assetData);
  112. //保存头像
  113. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  114. if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) {
  115. $imgUrl = $getUserInfo['headImgUrl'];
  116. $name = ImageService::generateAvatar($merchantId, $userId, $imgUrl);
  117. self::updateById($userId, ['avatar' => $name]);
  118. }
  119. }
  120. //商家客户粉丝 +1
  121. $type = $subscribe == 1 ? 3 : 1;
  122. MerchantAssetClass::addUserNum($merchantId, $type);
  123. return $user;
  124. }
  125. public static function updateUser($originalUser, $getUserInfo, $merchantId)
  126. {
  127. $data = [];
  128. $assetData = [];
  129. $userId = $originalUser['id'];
  130. //替换掉微信不支持的图片
  131. $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
  132. if (!empty($userName)) {
  133. $data['userName'] = $userName;
  134. }
  135. if (isset($getUserInfo['openId']) && !empty($getUserInfo['openId'])) {
  136. $openId = $getUserInfo['openId'];
  137. $data['openId'] = $openId;
  138. }
  139. if (isset($getUserInfo['miniOpenId']) && !empty($getUserInfo['miniOpenId'])) {
  140. $data['miniOpenId'] = $getUserInfo['miniOpenId'];
  141. }
  142. if (isset($getUserInfo['unionId']) && !empty($getUserInfo['unionId'])) {
  143. $data['unionId'] = $getUserInfo['unionId'];
  144. }
  145. if (isset($getUserInfo['sex'])) {
  146. $data['sex'] = $getUserInfo['sex'];
  147. }
  148. if (isset($getUserInfo['subscribe'])) {
  149. $data['subscribe'] = $getUserInfo['subscribe'];
  150. }
  151. if (isset($getUserInfo['subscribeTime'])) {
  152. $data['subscribeTime'] = $getUserInfo['subscribeTime'];
  153. }
  154. $data['merchantId'] = $merchantId;
  155. if (isset($getUserInfo['alipayId']) && !empty($getUserInfo['alipayId'])) {
  156. $data['alipayId'] = $getUserInfo['alipayId'];
  157. }
  158. if (isset($getUserInfo['isFull'])) {
  159. $data['isFull'] = $getUserInfo['isFull'];
  160. }
  161. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  162. $data['isFull'] = 1;
  163. }
  164. /**更新头像**/
  165. if (isset($originalUser['avatar']) == false || empty($originalUser['avatar'])) {
  166. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  167. $imgUrl = $getUserInfo['headImgUrl'];
  168. $name = ImageService::generateAvatar($merchantId, $userId, $imgUrl);
  169. $data['avatar'] = $name;
  170. }
  171. }
  172. /**商家增加粉丝**/
  173. if (isset($originalUser['subscribe']) && $originalUser['subscribe'] == 0 && isset($getUserInfo['subscribe']) && $getUserInfo['subscribe'] == 1) {
  174. MerchantAssetClass::addUserNum($merchantId, 2);
  175. }
  176. //更新访问时间
  177. self::updateById($userId, $data);
  178. if (!empty($assetData)) {
  179. UserAssetClass::updateByUserId($userId, $assetData);
  180. }
  181. $user = self::getById($userId);
  182. return $user;
  183. }
  184. public static function getByMiniOpenId($miniOpenId, $merchantId)
  185. {
  186. return self::getByCondition(['merchantId' => $merchantId, 'miniOpenId' => $miniOpenId]);
  187. }
  188. public static function getByAlipayId($alipayId, $merchantId)
  189. {
  190. return self::getByCondition(['merchantId' => $merchantId, 'alipayId' => $alipayId]);
  191. }
  192. public static function getByOpenId($openId, $merchantId)
  193. {
  194. return self::getByCondition(['merchantId' => $merchantId, 'openId' => $openId]);
  195. }
  196. public static function getByUnionId($unionId, $merchantId)
  197. {
  198. return self::getByCondition(['merchantId' => $merchantId, 'unionId' => $unionId]);
  199. }
  200. /**
  201. * 合并用户
  202. */
  203. public static function mergeUser($delUser, $retainUser)
  204. {
  205. $connection = Yii::$app->db;//事务处理
  206. $transaction = $connection->beginTransaction();
  207. try {
  208. $delUserId = $delUser['id'];
  209. $retainUserId = $retainUser['id'];
  210. $userName = $retainUser['userName'];
  211. $merchantId = $retainUser['merchantId'];
  212. //合并记录
  213. $return = xhUserMergeService::add(['delUserId' => $delUserId, 'merchantId' => $merchantId, 'retainUserId' => $retainUserId, 'addTime' => time(), 'createTime' => date("Y-m-d H:i:s")]);
  214. $targetId = $return['id'];
  215. $typeList = configDict::getConfig('capitalType');
  216. $capitalType = $typeList['xhMergeUser']['id'];
  217. $now = time();
  218. $date = date("Y-m-d H:i:s");
  219. //数据迁移
  220. $updateData = [];
  221. $updateKey = [
  222. 'mobile',
  223. 'password',
  224. 'payPassword',
  225. 'subscribe',
  226. 'hasSubscribe',
  227. 'subscribeTime',
  228. 'alipayId',
  229. 'openId',
  230. 'miniOpenId',
  231. 'unionId',
  232. ];
  233. foreach ($updateKey as $key) {
  234. if (isset($delUser[$key]) && !empty($delUser[$key])) {
  235. $updateData[$key] = $delUser[$key];
  236. }
  237. }
  238. if (!empty($updateData)) {
  239. $updateData['visitTime'] = time();
  240. self::updateById($retainUserId, $updateData);
  241. }
  242. //资产累加
  243. $addData = [];
  244. $delUserAsset = UserAssetClass::getByUserId($delUserId);
  245. $retainUserAsset = UserAssetClass::getByUserId($retainUserId);
  246. if (!empty($delUserAsset)) {
  247. //累计消费
  248. $delTotalExpend = isset($delUserAsset['totalExpend']) ? $delUserAsset['totalExpend'] : 0;
  249. if (!empty($delTotalExpend)) {
  250. $addData['totalExpend'] = $retainUserAsset['totalExpend'] + $delTotalExpend;
  251. //变化记录要增加
  252. }
  253. //累计收入
  254. $delTotalIncome = isset($delUserAsset['totalIncome']) ? $delUserAsset['totalIncome'] : 0;
  255. if (!empty($delTotalIncome)) {
  256. $addData['totalIncome'] = $retainUserAsset['totalIncome'] + $delTotalIncome;
  257. //变化记录要增加
  258. }
  259. //累计充值
  260. $delTotalRecharge = isset($delUserAsset['totalRecharge']) ? $delUserAsset['totalRecharge'] : 0;
  261. if (!empty($delTotalRecharge)) {
  262. $addData['totalRecharge'] = $retainUserAsset['totalRecharge'] + $delTotalRecharge;
  263. //变化记录要增加
  264. }
  265. //累计购买数量
  266. $delTotalBuyNum = isset($delUserAsset['totalBuyNum']) ? $delUserAsset['totalBuyNum'] : 0;
  267. if (!empty($delTotalBuyNum)) {
  268. $addData['totalBuyNum'] = $retainUserAsset['totalBuyNum'] + $delTotalBuyNum;
  269. //变化记录要增加
  270. }
  271. //余额
  272. $delBalance = isset($delUserAsset['balance']) ? $delUserAsset['balance'] : 0;
  273. if (!empty($delBalance)) {
  274. $addData['balance'] = $retainUserAsset['balance'] + $delBalance;
  275. //变化记录要增加
  276. $userCapitalData = [
  277. 'relateId' => (string)$targetId,
  278. 'balance' => $addData['balance'],
  279. 'totalIncome' => $retainUserAsset['totalIncome'] + $delBalance,
  280. 'totalExpend' => $retainUserAsset['totalExpend'],
  281. 'amount' => $delBalance,
  282. 'io' => 1,
  283. 'payWay' => 0,
  284. 'event' => '帐号合并',
  285. 'merchantId' => $merchantId,
  286. 'userId' => $retainUserId,
  287. 'alipayId' => '',
  288. 'userName' => $userName,
  289. 'operateId' => 0,
  290. 'createTime' => $date,
  291. 'addTime' => $now,
  292. 'capitalType' => $capitalType,
  293. ];
  294. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  295. }
  296. //积分
  297. $delIntegral = isset($delUserAsset['integral']) ? $delUserAsset['integral'] : 0;
  298. if (!empty($delIntegral)) {
  299. $addData['integral'] = $retainUserAsset['integral'] + $delIntegral;
  300. //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
  301. $integralData = [
  302. 'userId' => $retainUserId,
  303. 'userName' => $userName,
  304. 'merchantId' => $merchantId,
  305. 'relateId' => (string)$targetId,
  306. 'integral' => $addData['integral'],
  307. 'num' => $delIntegral,
  308. 'io' => 1,
  309. 'payWay' => 0,
  310. 'alipayId' => '',
  311. 'event' => '帐号合并',
  312. 'operatorId' => 0,
  313. 'createTime' => $date,
  314. 'capitalType' => $capitalType,
  315. 'addTime' => $now,
  316. ];
  317. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  318. $preLevel = $retainUserAsset['member'];
  319. $preIntegral = $retainUserAsset['integral'];//可抵用型积分
  320. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  321. UserIntegralService::increaseToUpgrade($preIntegral, $addData['integral'], $preLevel, $retainUserId, $merchantId, $merchantExtend);
  322. }
  323. //积分
  324. $delPoint = isset($delUserAsset['point']) ? $delUserAsset['point'] : 0;
  325. if (!empty($delPoint)) {
  326. $addData['point'] = $retainUserAsset['point'] + $delPoint;
  327. //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
  328. }
  329. if (!empty($addData)) {
  330. UserAssetClass::updateByUserId($retainUserId, $addData);
  331. }
  332. }
  333. //清除不需要的用户
  334. $clearData = ['openId' => '', 'miniOpenId' => '', 'unionId' => '', 'alipayId' => '', 'merchantId' => 0];
  335. self::updateById($delUserId, $clearData);
  336. UserAssetClass::updateByUserId($delUserId, ['merchantId' => 0]);
  337. //清除支付宝用户
  338. $clearAlipayData = ['alipayId' => '', 'alipayAccount' => '', 'merchantId' => 0];
  339. UserAlipayClass::updateByUserId($delUserId, $clearAlipayData);
  340. $transaction->commit();
  341. return self::getById($retainUserId);
  342. } catch (Exception $e) {
  343. $transaction->rollBack();
  344. Yii::info("合并userid $delUserId $retainUserId 时事务回滚,原因:" . $e->getMessage());
  345. return [];
  346. }
  347. }
  348. //找出相同unionId的用户
  349. public static function getSameUnionId($merchantId, $unionId)
  350. {
  351. return self::getAllByCondition(['merchantId' => $merchantId, 'unionId' => $unionId], 'addTime desc', '*');
  352. }
  353. public static function valid($info, $merchantId)
  354. {
  355. if (empty($info)) {
  356. util::fail('找不到客户');
  357. }
  358. if ($info['merchantId'] != $merchantId) {
  359. util::fail('这个客户您没有权限操作');
  360. }
  361. }
  362. //根据消费次数和有没领优惠券来判断是不是新人
  363. public static function newUser($userInfo)
  364. {
  365. $asset = isset($userInfo['userAsset']) ? $userInfo['userAsset'] : [];
  366. $totalBuyNum = isset($asset['totalBuyNum']) ? $asset['totalBuyNum'] : 0;
  367. if ($totalBuyNum > 0) {
  368. return false;
  369. }
  370. $getCouponNum = isset($asset['getCouponNum']) ? $asset['getCouponNum'] : 0;
  371. if ($getCouponNum) {
  372. return false;
  373. }
  374. return true;
  375. }
  376. public static function getUserByIds($ids)
  377. {
  378. $info = self::getByIds($ids, null, 'id');
  379. if (empty($info)) {
  380. return [];
  381. }
  382. $data = self::groupUserBaseInfo($info);
  383. return $data;
  384. }
  385. }