UserClass.php 14 KB

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