UserClass.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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\services\ImageService;
  7. use common\services\xhMerchantExtendService;
  8. use common\services\xhUserCapitalService;
  9. use common\services\xhUserIntegralService;
  10. use common\services\xhUserMergeService;
  11. use Yii;
  12. use biz\base\classes\BaseClass;
  13. class UserClass extends BaseClass
  14. {
  15. public static $baseFile = '\biz\user\models\User';
  16. //用户和收入的来源
  17. public static $sourceType = [
  18. 2 => '朋友圈',
  19. 1 => '门店',
  20. 0 => '公众号',
  21. 3 => '淘宝',
  22. ];
  23. //用户来源分类
  24. public static $userSource = [
  25. 0 => ['name' => 'official', 'title' => '公众号'],
  26. 1 => ['name' => 'alipay', 'title' => '支付宝'],
  27. 2 => ['name' => 'mini', 'title' => '小程序'],
  28. ];
  29. //用户来源分类ID
  30. public static $userSourceId = [
  31. 'official' => ['id' => 0, 'name' => 'official'],
  32. 'alipay' => ['id' => 1, 'name' => 'alipay'],
  33. 'mini' => ['id' => 2, 'name' => 'mini'],
  34. ];
  35. //组合客户基本和资产信息
  36. public static function groupUserBaseInfo($list)
  37. {
  38. //取客户资产
  39. $ids = array_column($list, 'id');
  40. $ids = array_unique($ids);
  41. $asset = UserAssetClass::getByUserIds($ids, 'userId');
  42. $imgUrl = Yii::$app->params['imgUrl'];
  43. foreach ($list as $key => $val) {
  44. $userId = $val['id'];
  45. //访问时间
  46. $visitTime = date("m-d H:i", $val['visitTime']);
  47. if (date("m-d") == date("m-d", $val['visitTime'])) {
  48. $visitTime = '今天 ' . date("H:i", $val['visitTime']);
  49. }
  50. $list[$key]['visitTimeFormat'] = $visitTime;
  51. $list[$key]['avatarUrl'] = $imgUrl . $val['avatar'];
  52. $list[$key]['userAsset'] = isset($asset[$userId]) ? $asset[$userId] : [];
  53. //客户发来新消息数
  54. $list[$key]['messageNum'] = rand(0, 19);
  55. }
  56. return $list;
  57. }
  58. //获取客户基本和资产信息 shish 2019.11.30
  59. public static function getUserInfo($id)
  60. {
  61. $list = self::getByIds([$id]);
  62. $data = self::groupUserBaseInfo($list);
  63. return current($data);
  64. }
  65. //根据手机号查询客户 shish 2019.12.1
  66. public static function getByMobile($mobile)
  67. {
  68. $merchantId = Yii::$app->params['merchantId'];
  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. $random = Yii::$app->getSecurity()->generateRandomString();//yii2 里生成32位唯一随机数
  104. $uniqueCode = $userId . $random;
  105. $assetData = ['userId' => $userId, 'merchantId' => $merchantId, 'createTime' => $date, 'uniqueCode' => $uniqueCode];
  106. //生成用户资产信息
  107. UserAssetClass::add($assetData);
  108. //保存头像
  109. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  110. if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) {
  111. $imgUrl = $getUserInfo['headImgUrl'];
  112. $name = ImageService::generateAvatar($merchantId, $userId, $imgUrl);
  113. self::updateById($userId, ['avatar' => $name]);
  114. }
  115. }
  116. //商家客户粉丝 +1
  117. $type = $subscribe == 1 ? 3 : 1;
  118. MerchantAssetClass::addCustomerFasNum($merchantId, $type);
  119. return $user;
  120. }
  121. public static function updateUser($originalUser, $getUserInfo, $merchantId)
  122. {
  123. $data = [];
  124. $assetData = [];
  125. $userId = $originalUser['id'];
  126. //替换掉微信不支持的图片
  127. $userName = isset($getUserInfo['userName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['userName']) : '';
  128. if (!empty($userName)) {
  129. $data['userName'] = $userName;
  130. }
  131. if (isset($getUserInfo['openId']) && !empty($getUserInfo['openId'])) {
  132. $openId = $getUserInfo['openId'];
  133. $data['openId'] = $openId;
  134. }
  135. if (isset($getUserInfo['miniOpenId']) && !empty($getUserInfo['miniOpenId'])) {
  136. $data['miniOpenId'] = $getUserInfo['miniOpenId'];
  137. }
  138. if (isset($getUserInfo['unionId']) && !empty($getUserInfo['unionId'])) {
  139. $data['unionId'] = $getUserInfo['unionId'];
  140. }
  141. if (isset($getUserInfo['sex'])) {
  142. $data['sex'] = $getUserInfo['sex'];
  143. }
  144. if (isset($getUserInfo['subscribe'])) {
  145. $data['subscribe'] = $getUserInfo['subscribe'];
  146. }
  147. if (isset($getUserInfo['subscribeTime'])) {
  148. $data['subscribeTime'] = $getUserInfo['subscribeTime'];
  149. }
  150. $data['merchantId'] = $merchantId;
  151. if (isset($getUserInfo['alipayId']) && !empty($getUserInfo['alipayId'])) {
  152. $data['alipayId'] = $getUserInfo['alipayId'];
  153. }
  154. if (isset($getUserInfo['isFull'])) {
  155. $data['isFull'] = $getUserInfo['isFull'];
  156. }
  157. if (isset($getUserInfo['headImgUrl']) && !empty($getUserInfo['headImgUrl'])) {
  158. $data['isFull'] = 1;
  159. }
  160. if (isset($getUserInfo['isMember'])) {
  161. $data['isMember'] = $getUserInfo['isMember'];
  162. $assetData['isMember'] = $getUserInfo['isMember'];
  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::addCustomerFasNum($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. 'isMember',
  226. 'subscribe',
  227. 'hasSubscribe',
  228. 'subscribeTime',
  229. 'alipayId',
  230. 'openId',
  231. 'miniOpenId',
  232. 'unionId',
  233. ];
  234. foreach ($updateKey as $key) {
  235. if (isset($delUser[$key]) && !empty($delUser[$key])) {
  236. $updateData[$key] = $delUser[$key];
  237. }
  238. }
  239. if (!empty($updateData)) {
  240. $updateData['visitTime'] = time();
  241. self::updateById($retainUserId, $updateData);
  242. }
  243. //资产累加
  244. $addData = [];
  245. $delUserAsset = UserAssetClass::getByUserId($delUserId);
  246. $retainUserAsset = UserAssetClass::getByUserId($retainUserId);
  247. if (!empty($delUserAsset)) {
  248. //累计消费
  249. $delTotalExpend = isset($delUserAsset['totalExpend']) ? $delUserAsset['totalExpend'] : 0;
  250. if (!empty($delTotalExpend)) {
  251. $addData['totalExpend'] = $retainUserAsset['totalExpend'] + $delTotalExpend;
  252. //变化记录要增加
  253. }
  254. //累计收入
  255. $delTotalIncome = isset($delUserAsset['totalIncome']) ? $delUserAsset['totalIncome'] : 0;
  256. if (!empty($delTotalIncome)) {
  257. $addData['totalIncome'] = $retainUserAsset['totalIncome'] + $delTotalIncome;
  258. //变化记录要增加
  259. }
  260. //累计充值
  261. $delTotalRecharge = isset($delUserAsset['totalRecharge']) ? $delUserAsset['totalRecharge'] : 0;
  262. if (!empty($delTotalRecharge)) {
  263. $addData['totalRecharge'] = $retainUserAsset['totalRecharge'] + $delTotalRecharge;
  264. //变化记录要增加
  265. }
  266. //累计购买数量
  267. $delTotalBuyNum = isset($delUserAsset['totalBuyNum']) ? $delUserAsset['totalBuyNum'] : 0;
  268. if (!empty($delTotalBuyNum)) {
  269. $addData['totalBuyNum'] = $retainUserAsset['totalBuyNum'] + $delTotalBuyNum;
  270. //变化记录要增加
  271. }
  272. //余额
  273. $delBalance = isset($delUserAsset['balance']) ? $delUserAsset['balance'] : 0;
  274. if (!empty($delBalance)) {
  275. $addData['balance'] = $retainUserAsset['balance'] + $delBalance;
  276. //变化记录要增加
  277. $userCapitalData = [
  278. 'relateId' => (string)$targetId,
  279. 'balance' => $addData['balance'],
  280. 'totalIncome' => $retainUserAsset['totalIncome'] + $delBalance,
  281. 'totalExpend' => $retainUserAsset['totalExpend'],
  282. 'amount' => $delBalance,
  283. 'io' => 1,
  284. 'payWay' => 0,
  285. 'event' => '帐号合并',
  286. 'merchantId' => $merchantId,
  287. 'userId' => $retainUserId,
  288. 'alipayId' => '',
  289. 'userName' => $userName,
  290. 'operateId' => 0,
  291. 'createTime' => $date,
  292. 'addTime' => $now,
  293. 'capitalType' => $capitalType,
  294. ];
  295. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  296. }
  297. //积分
  298. $delIntegral = isset($delUserAsset['integral']) ? $delUserAsset['integral'] : 0;
  299. if (!empty($delIntegral)) {
  300. $addData['integral'] = $retainUserAsset['integral'] + $delIntegral;
  301. //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
  302. $integralData = [
  303. 'userId' => $retainUserId,
  304. 'userName' => $userName,
  305. 'merchantId' => $merchantId,
  306. 'relateId' => (string)$targetId,
  307. 'integral' => $addData['integral'],
  308. 'num' => $delIntegral,
  309. 'io' => 1,
  310. 'payWay' => 0,
  311. 'alipayId' => '',
  312. 'event' => '帐号合并',
  313. 'operatorId' => 0,
  314. 'createTime' => $date,
  315. 'capitalType' => $capitalType,
  316. 'addTime' => $now,
  317. ];
  318. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  319. $preLevel = $retainUserAsset['memberLevel'];
  320. $preIntegral = $retainUserAsset['integral'];//可抵用型积分
  321. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  322. UserIntegralService::increaseToUpgrade($preIntegral, $addData['integral'], $preLevel, $retainUserId, $merchantId, $merchantExtend);
  323. }
  324. //积分
  325. $delPoint = isset($delUserAsset['point']) ? $delUserAsset['point'] : 0;
  326. if (!empty($delPoint)) {
  327. $addData['point'] = $retainUserAsset['point'] + $delPoint;
  328. //注意会员等级要进行重新计算!!!!!还有积分变化记录要增加
  329. }
  330. if (!empty($addData)) {
  331. UserAssetClass::updateByUserId($retainUserId, $addData);
  332. }
  333. }
  334. //清除不需要的用户
  335. $clearData = ['openId' => '', 'miniOpenId' => '', 'unionId' => '', 'alipayId' => '', 'merchantId' => 0];
  336. self::updateById($delUserId, $clearData);
  337. UserAssetClass::updateByUserId($delUserId, ['merchantId' => 0]);
  338. //清除支付宝用户
  339. $clearAlipayData = ['alipayId' => '', 'alipayAccount' => '', 'merchantId' => 0];
  340. UserAlipayClass::updateByUserId($delUserId, $clearAlipayData);
  341. $transaction->commit();
  342. return self::getById($retainUserId);
  343. } catch (Exception $e) {
  344. $transaction->rollBack();
  345. Yii::info("合并userid $delUserId $retainUserId 时事务回滚,原因:" . $e->getMessage());
  346. return [];
  347. }
  348. }
  349. //找出相同unionId的用户
  350. public static function getSameUnionId($merchantId, $unionId)
  351. {
  352. return self::getAllByCondition(['merchantId' => $merchantId, 'unionId' => $unionId], 'addTime desc', '*');
  353. }
  354. }