UserService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace biz\user\services;
  3. use biz\base\services\BaseService;
  4. use biz\merchant\classes\MerchantAssetClass;
  5. use biz\merchant\services\MerchantAssetService;
  6. use biz\merchant\services\MerchantExtendService;
  7. use biz\order\services\OrderService;
  8. use biz\user\classes\UserClass;
  9. use common\components\util;
  10. use common\components\validate;
  11. use common\services\xhMerchantExtendService;
  12. use common\services\xhMerchantService;
  13. use Yii;
  14. use common\components\weixinUtil;
  15. class UserService extends BaseService
  16. {
  17. public static $baseFile = '\biz\user\classes\UserClass';
  18. //获取客户列表 shish 2019.11.30
  19. public static function getUserList($where)
  20. {
  21. $data = UserClass::getList('*', $where, 'visitTime DESC');
  22. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  23. if (empty($list)) {
  24. return $data;
  25. }
  26. $data['list'] = UserClass::groupUserBaseInfo($list);
  27. return $data;
  28. }
  29. //获取客户基本和资产信息 shish 2019.11.30
  30. public static function getUserInfo($id)
  31. {
  32. return UserClass::getUserInfo($id);
  33. }
  34. //获取客户基本、资产和订单信息 shish 2019.11.30
  35. public static function getFullUserInfo($id)
  36. {
  37. $userInfo = UserClass::getUserInfo($id);
  38. $order = OrderService::getUserOrder($id, 10, 'addTime desc');
  39. $userInfo['orderList'] = $order;
  40. return $userInfo;
  41. }
  42. //根据手机号查询客户 shish 2019.12.1
  43. public static function getByMobile($mobile, $merchantId)
  44. {
  45. return UserClass::getByMobile($mobile, $merchantId);
  46. }
  47. /**
  48. * 创建以及更新用户
  49. * @author shish
  50. * @time 20190507
  51. */
  52. public static function replaceUser($userInfo, $source, $merchantId)
  53. {
  54. $userInfo = self::switchUserInfo($userInfo, $source);
  55. $userSource = UserClass::$userSourceId;
  56. switch ($source) {
  57. case $userSource['mini']['name']:
  58. $sourceId = $userSource['mini']['id'];
  59. $miniOpenId = $userInfo['miniOpenId'];
  60. $user = UserClass::getByMiniOpenId($miniOpenId, $merchantId);
  61. break;
  62. case $userSource['alipay']['name']:
  63. $sourceId = $userSource['alipay']['id'];
  64. $alipayId = $userInfo['alipayId'];
  65. $user = UserClass::getByAlipayId($alipayId, $merchantId);
  66. break;
  67. case $userSource['official']['name']:
  68. $sourceId = $userSource['official']['id'];
  69. //老客添加没有openId
  70. $openId = isset($userInfo['openId']) ? $userInfo['openId'] : '';
  71. $user = [];
  72. if (!empty($openId)) {
  73. $user = self::getByOpenId($openId, $merchantId);
  74. }
  75. break;
  76. default:
  77. throw new \Exception('未知来源的用户');
  78. }
  79. if (empty($user)) {
  80. $userInfo['source'] = $sourceId;
  81. $userInfo['merchantId'] = $merchantId;
  82. $user = UserClass::generateUser($userInfo, $merchantId);
  83. } else {
  84. //支付宝付款时不需要每次更新用户信息
  85. if ($source != $userSource['alipay']['name']) {
  86. $user = UserClass::updateUser($user, $userInfo, $merchantId);
  87. }
  88. }
  89. //存在相同unionid则进行合并
  90. $unionId = isset($userInfo['unionId']) ? $userInfo['unionId'] : '';
  91. if (!empty($unionId)) {
  92. $same = UserClass::getSameUnionId($merchantId, $unionId);
  93. if (count($same) == 2) {
  94. $delUser = $same[0];
  95. $retainUser = $same[1];
  96. $user = UserClass::mergeUser($delUser, $retainUser);
  97. }
  98. }
  99. return $user;
  100. }
  101. public static function getByOpenId($openId, $merchantId)
  102. {
  103. return UserClass::getByOpenId($openId, $merchantId);
  104. }
  105. public static function getByMiniOpenId($miniOpenId, $merchantId)
  106. {
  107. return UserClass::getByMiniOpenId($miniOpenId, $merchantId);
  108. }
  109. /**
  110. * 同步微信用户信息
  111. * @author shish <shish@zhhinc.com>
  112. * @time 2019.5.11
  113. */
  114. public static function syncWeixinUser($merchantId, $nextOpenId = '')
  115. {
  116. set_time_limit(0);
  117. $merchant = xhMerchantService::getById($merchantId);
  118. //一次拉取调用最多拉取10000个关注者的OpenID
  119. $result = weixinUtil::getSubscribeUserList($merchant, $nextOpenId);
  120. $total = isset($result['total']) ? $result['total'] : 0;
  121. $next_openid = isset($result['next_openid']) ? $result['next_openid'] : '';
  122. $count = isset($result['count']) ? $result['count'] : 0;
  123. $openIdList = isset($result['data']['openid']) ? $result['data']['openid'] : [];
  124. if ($count == 0) {
  125. return ['code' => 'A0001', 'msg' => '完成更新', 'data' => ['total' => $total]];
  126. }
  127. $total = count($openIdList);
  128. $totalPage = $total > 100 ? ceil($total / 100) : 1;
  129. $userSource = Yii::$app->dict->getValue('userSourceGetId', 'official');
  130. $source = $userSource['name'];
  131. for ($m = 1; $m <= $totalPage; $m++) {
  132. $offset = ($m - 1) * 100;
  133. $subIdList = array_slice($openIdList, $offset, 100);
  134. $info = weixinUtil::batchGetUserInfo($merchant, $subIdList);
  135. if (empty($info) || isset($info['user_info_list']) == false || empty($info['user_info_list'])) {
  136. continue;
  137. }
  138. $user_info_list = $info['user_info_list'];
  139. foreach ($user_info_list as $val) {
  140. $val['isFull'] = 1;
  141. $val['unionId'] = $val['unionid'];
  142. $val['headImgUrl'] = $val['headimgurl'];
  143. $val['openId'] = $val['openid'];
  144. $val['nickName'] = $val['nickname'];
  145. $val['merchantId'] = $merchantId;
  146. self::replaceUser($val, $source, $merchantId);
  147. }
  148. }
  149. return ['code' => 'A0001', 'msg' => '完成更新', 'data' => ['total' => $total]];
  150. }
  151. /**
  152. * 更新用户的访问时间
  153. */
  154. public static function updateVisitTime($merchantId, $userId)
  155. {
  156. $date = date("Ymd");
  157. if (empty($userId)) {
  158. return false;
  159. }
  160. //每5分钟更新一次访问时间
  161. $key = 'UserVisitTime_' . $date . '_' . $merchantId . '_' . $userId;
  162. $respond = Yii::$app->redis->executeCommand('GET', [$key]);
  163. //每过5分钟更新访问时间
  164. if (empty($respond)) {
  165. Yii::$app->redis->executeCommand('SET', [$key, $date]);
  166. Yii::$app->redis->executeCommand('EXPIRE', [$key, 300]);
  167. $time = time();
  168. self::updateById($userId, ['visitTime' => $time]);
  169. }
  170. }
  171. /**
  172. * 将微信和小程序获取的用户信息转成可以保存的用户信息
  173. */
  174. public static function switchUserInfo($info, $source)
  175. {
  176. $openId = '';
  177. if (isset($info['openid']) && !empty($info['openid'])) {
  178. $openId = $info['openid'];
  179. unset($info['openId']);
  180. }
  181. if (isset($info['openId']) && !empty($info['openId'])) {
  182. $openId = $info['openId'];
  183. unset($info['openId']);
  184. }
  185. if (!empty($openId)) {
  186. $userSource = self::$userSourceId;
  187. switch ($source) {
  188. case $userSource['mini']['name']:
  189. $info['miniOpenId'] = $openId;
  190. break;
  191. case $userSource['official']['name']:
  192. $info['openId'] = $openId;
  193. break;
  194. default:
  195. throw new \Exception('未知来源的用户');
  196. }
  197. }
  198. if (isset($info['nickname'])) {
  199. $info['userName'] = $info['nickname'];
  200. }
  201. if (isset($info['nickName'])) {
  202. $info['userName'] = $info['nickName'];
  203. }
  204. if (isset($info['unionid'])) {
  205. $info['unionId'] = $info['unionid'];
  206. }
  207. if (isset($info['unionId'])) {
  208. $info['unionId'] = $info['unionId'];
  209. }
  210. if (isset($info['headimgurl'])) {
  211. $info['headImgUrl'] = $info['headimgurl'];
  212. }
  213. if (isset($info['avatarUrl'])) {
  214. $info['headImgUrl'] = $info['avatarUrl'];
  215. }
  216. if (isset($info['subscribe_time'])) {
  217. $info['subscribeTime'] = $info['subscribe_time'];
  218. }
  219. if (isset($info['subscribe'])) {
  220. $info['subscribe'] = $info['subscribe'];
  221. }
  222. return $info;
  223. }
  224. //升级成为会员,并更新用户相关信息 shish 2019.9.7
  225. public static function upgradeToMember($userId, $merchantId, $data = [])
  226. {
  227. self::updateById($userId, $data);
  228. /**会员数+1**/
  229. MerchantAssetService::addMemberNum($merchantId);
  230. }
  231. //取消关注 shish 2019.9.8
  232. public static function unFocus($userId, $merchantId)
  233. {
  234. self::updateById($userId, ['subscribe' => 0, 'hasSubscribe' => 1]);
  235. /**商家粉丝减少**/
  236. MerchantAssetClass::reduceFans($merchantId);
  237. }
  238. //批量获取用户信息,图片等信息处理好了 shish 2019.11.28
  239. public static function getUserByIds($ids)
  240. {
  241. return UserClass::getUserByIds($ids);
  242. }
  243. //验证支付密码是否正确 shish 2019.12.6
  244. public static function validPayPassword($password, $user)
  245. {
  246. if (isset($user['payPassword']) == false || empty($user['payPassword'])) {
  247. util::fail('您还没有设置支付密码');
  248. }
  249. $payPassword = $user['payPassword'];
  250. if (password_verify($password, $payPassword) == false) {
  251. util::fail('密码错误');
  252. }
  253. }
  254. //验证用户权限
  255. public static function valid($info, $merchantId)
  256. {
  257. return UserClass::valid($info, $merchantId);
  258. }
  259. //根据消费次数和有没领优惠券来判断是不是新人
  260. public static function newUser($userInfo)
  261. {
  262. return UserClass::newUser($userInfo);
  263. }
  264. //添加新客户 shish 2020.1.10
  265. public static function addUser($data)
  266. {
  267. //merchantId $source $realName $member $growth $balance $mobile
  268. //必传参数
  269. validate::easyCheck(['merchantId', 'mobile'], $data);
  270. $merchantId = $data['merchantId'];
  271. $source = isset($data['source']) ? $data['source'] : 'official';
  272. $mobile = $data['mobile'];
  273. $realName = isset($data['realName']) ? $data['realName'] : $mobile;
  274. $member = isset($data['member']) ? $data['member'] : -1;
  275. $balance = isset($data['balance']) ? $data['balance'] : 0;
  276. $originalInfo = [];
  277. $originalInfo['merchantId'] = $merchantId;
  278. $originalInfo['mobile'] = $mobile;
  279. $originalInfo['realName'] = $realName;
  280. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  281. $extend = MerchantExtendService::getByMerchantId($merchantId);
  282. //等级
  283. $growth = 0;
  284. if ($member > 0) {
  285. $gradeList = xhMerchantExtendService::getGradeList($extend, 'desc');
  286. $growth = isset($gradeList[$member]['growth']) ? $gradeList[$member]['growth'] : 0;
  287. }
  288. //余额成长值更新
  289. $userId = $user['id'];
  290. $data = [];
  291. $data['growth'] = $growth;
  292. $data['balance'] = $balance > 0 ? $balance : 0;
  293. UserAssetService::updateByUserId($userId, $data);
  294. //会员升级
  295. UserIntegralService::increaseToUpgrade(0, $growth, 0, $userId, $merchantId, $extend);
  296. }
  297. }