UserClass.php 14 KB

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