UserService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. namespace biz\user\services;
  3. use biz\base\services\BaseService;
  4. use biz\coupon\classes\CouponAssetClass;
  5. use biz\merchant\classes\MerchantAssetClass;
  6. use biz\merchant\services\MerchantAssetService;
  7. use biz\merchant\services\MerchantExtendService;
  8. use biz\order\services\OrderService;
  9. use biz\user\classes\UserAssetClass;
  10. use biz\user\classes\UserClass;
  11. use common\components\util;
  12. use common\components\validate;
  13. use common\services\ImageService;
  14. use common\services\xhMerchantExtendService;
  15. use common\services\xhMerchantService;
  16. use Yii;
  17. use common\components\wxUtil;
  18. class UserService extends BaseService
  19. {
  20. public static $baseFile = '\biz\user\classes\UserClass';
  21. //获取客户列表 shish 2019.11.30
  22. public static function getUserList($where, $delStatus = 0)
  23. {
  24. //默认取没有删除的用户
  25. $where['delStatus'] = $delStatus;
  26. $data = UserClass::getList('*', $where, 'visitTime DESC');
  27. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  28. if (empty($list)) {
  29. return $data;
  30. }
  31. $data['list'] = UserClass::groupUserBaseInfo($list);
  32. return $data;
  33. }
  34. //获取客户基本和资产信息 shish 2019.11.30
  35. public static function getUserInfo($id, $sensitive = true)
  36. {
  37. return UserClass::getUserInfo($id, $sensitive);
  38. }
  39. //获取客户基本、资产和订单信息 shish 2019.11.30
  40. public static function getFullUserInfo($id)
  41. {
  42. $userInfo = UserClass::getUserInfo($id);
  43. $order = OrderService::getUserOrder($id, 10, 'addTime desc');
  44. $userInfo['orderList'] = $order;
  45. return $userInfo;
  46. }
  47. //根据手机号查询客户 shish 2019.12.1
  48. public static function getByMobile($mobile, $merchantId)
  49. {
  50. return UserClass::getByMobile($mobile, $merchantId);
  51. }
  52. //添加更新客户 shish 2020.2.7
  53. public static function replaceUser($userInfo, $source, $merchantId)
  54. {
  55. return UserClass::replaceUser($userInfo, $source, $merchantId);
  56. }
  57. public static function getByOpenId($openId, $merchantId)
  58. {
  59. return UserClass::getByOpenId($openId, $merchantId);
  60. }
  61. public static function getByMiniOpenId($miniOpenId)
  62. {
  63. return UserClass::getByMiniOpenId($miniOpenId);
  64. }
  65. public static function getByUnionId($id)
  66. {
  67. return UserClass::getByUnionId($id);
  68. }
  69. //同步微信用户 shish 2020.2.8
  70. public static function syncWxAllUser($merchantId)
  71. {
  72. $return = UserClass::syncWxUser($merchantId);
  73. while (isset($return['count']) && $return['count'] > 0 && isset($return['nextOpenId']) && !empty($return['nextOpenId'])) {
  74. $nextOpenId = $return['nextOpenId'];
  75. $return = UserClass::syncWxUser($merchantId, $nextOpenId);
  76. }
  77. return ['total' => $return['total']];
  78. }
  79. //更新微信头像 shish 2020.1.12
  80. public static function syncAvatar($merchantId, $nextOpenId = '')
  81. {
  82. set_time_limit(0);
  83. $merchant = xhMerchantService::getById($merchantId);
  84. //一次拉取调用最多拉取10000个关注者的OpenID
  85. $result = wxUtil::getSubscribeUserList($merchant, $nextOpenId);
  86. $total = isset($result['total']) ? $result['total'] : 0;
  87. $next_openid = isset($result['next_openid']) ? $result['next_openid'] : '';
  88. $count = isset($result['count']) ? $result['count'] : 0;
  89. $openIdList = isset($result['data']['openid']) ? $result['data']['openid'] : [];
  90. if ($count == 0) {
  91. util::stop("完成更新\n");
  92. }
  93. $total = count($openIdList);
  94. $totalPage = $total > 100 ? ceil($total / 100) : 1;
  95. for ($m = 1; $m <= $totalPage; $m++) {
  96. $offset = ($m - 1) * 100;
  97. $subIdList = array_slice($openIdList, $offset, 100);
  98. $info = wxUtil::batchGetUserInfo($merchant, $subIdList);
  99. if (empty($info) || isset($info['user_info_list']) == false || empty($info['user_info_list'])) {
  100. continue;
  101. }
  102. $user_info_list = $info['user_info_list'];
  103. foreach ($user_info_list as $val) {
  104. $headImgUrl = $val['headimgurl'];
  105. $openId = $val['openid'];
  106. $img = ImageService::generateAvatar($merchantId, $headImgUrl);
  107. if (!empty($img)) {
  108. UserClass::updateByCondition(['openId' => $openId], ['avatar' => $img]);
  109. }
  110. }
  111. }
  112. echo "更新成功\n";
  113. }
  114. /**
  115. * 更新用户的访问时间
  116. */
  117. public static function updateVisitTime($merchantId, $userId)
  118. {
  119. $date = date("Ymd");
  120. if (empty($userId)) {
  121. return false;
  122. }
  123. //每5分钟更新一次访问时间
  124. $key = 'UserVisitTime_' . $date . '_' . $merchantId . '_' . $userId;
  125. $respond = Yii::$app->redis->executeCommand('GET', [$key]);
  126. //每过5分钟更新访问时间
  127. if (empty($respond)) {
  128. Yii::$app->redis->executeCommand('SET', [$key, $date]);
  129. Yii::$app->redis->executeCommand('EXPIRE', [$key, 300]);
  130. $time = time();
  131. self::updateById($userId, ['visitTime' => $time]);
  132. }
  133. }
  134. //注册成为普通会员(非VIP),并更新用户相关信息 shish 2019.9.7
  135. public static function becomeMember($user, $merchant, $data)
  136. {
  137. $userId = $user['id'];
  138. $merchantId = $merchant['id'];
  139. UserClass::updateById($userId, $data);
  140. //注意成为会员和关注公众号时要去检查是否符合发优惠券条件
  141. $userAsset = UserAssetClass::getByUserId($userId);
  142. CouponAssetClass::giveCoupon($user, $merchant, $userAsset);
  143. //合并店长后台添加的客户
  144. $mobile = isset($data['mobile']) ? $data['mobile'] : '';
  145. if (!empty($mobile)) {
  146. $hasUser = UserClass::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
  147. $userSource = UserClass::$userSourceId;
  148. $systemId = $userSource['system']['id'];
  149. Yii::info($hasUser['sourceType'].' -- '.$systemId);
  150. if (!empty($hasUser) && $hasUser['sourceType'] == $systemId) {
  151. UserClass::mergeUser($hasUser, $user);
  152. }
  153. }
  154. //增加会员数
  155. MerchantAssetService::addMemberNum($merchantId);
  156. }
  157. //取消关注 shish 2019.9.8
  158. public static function unFocus($userId, $merchantId)
  159. {
  160. self::updateById($userId, ['subscribe' => 0, 'hasSubscribe' => 1]);
  161. //商家粉丝减少
  162. MerchantAssetClass::reduceFans($merchantId);
  163. }
  164. //关注公众号的后续 shish 2020.5.5
  165. public static function focus($user, $merchant)
  166. {
  167. $userId = $user['id'];
  168. UserClass::updateById($userId, ['subscribe' => 1]);
  169. $userAsset = UserAssetClass::getByUserId($userId);
  170. CouponAssetClass::giveCoupon($user, $merchant, $userAsset);
  171. }
  172. //批量获取用户信息,图片等信息处理好了 shish 2019.11.28
  173. public static function getUserByIds($ids)
  174. {
  175. return UserClass::getUserByIds($ids);
  176. }
  177. //验证支付密码是否正确 shish 2019.12.6
  178. public static function validPayPassword($password, $user)
  179. {
  180. if (isset($user['hasPayPwd']) == false || $user['hasPayPwd'] == 0) {
  181. util::fail('您还没有设置支付密码');
  182. }
  183. $payPassword = $user['payPassword'];
  184. if (password_verify($password, $payPassword) == false) {
  185. util::fail('密码错误');
  186. }
  187. }
  188. //验证用户权限
  189. public static function valid($info, $merchantId)
  190. {
  191. return UserClass::valid($info, $merchantId);
  192. }
  193. //根据消费次数和有没领优惠券来判断是不是新人
  194. public static function newUser($userInfo)
  195. {
  196. return UserClass::newUser($userInfo);
  197. }
  198. //添加新客户 shish 2020.1.10
  199. public static function addUser($data)
  200. {
  201. //merchantId $source $realName $member $growth $balance $mobile
  202. //必传参数
  203. validate::easyCheck(['merchantId', 'mobile'], $data);
  204. $merchantId = $data['merchantId'];
  205. $source = 'system';
  206. $mobile = $data['mobile'];
  207. $realName = isset($data['realName']) ? $data['realName'] : $mobile;
  208. $member = isset($data['member']) ? $data['member'] : -1;
  209. $balance = isset($data['balance']) ? $data['balance'] : 0;
  210. $growth = isset($data['growth']) ? $data['growth'] : 0;
  211. $originalInfo = [];
  212. $originalInfo['merchantId'] = $merchantId;
  213. $originalInfo['mobile'] = $mobile;
  214. $originalInfo['realName'] = $realName;
  215. $originalInfo['userName'] = $realName;
  216. $user = UserClass::replaceUser($originalInfo, $source, $merchantId);
  217. $extend = MerchantExtendService::getByMerchantId($merchantId);
  218. //等级
  219. if ($member > 0) {
  220. $gradeList = xhMerchantExtendService::getGradeList($extend, 'desc');
  221. $growth = isset($gradeList[$member]['growth']) ? $gradeList[$member]['growth'] : 0;
  222. }
  223. //余额成长值更新
  224. $userId = $user['id'];
  225. $data = [];
  226. $data['growth'] = $growth;
  227. $data['balance'] = $balance > 0 ? $balance : 0;
  228. UserAssetService::updateByUserId($userId, $data);
  229. //会员升级
  230. UserIntegralService::increaseToUpgrade(0, $growth, 0, $userId, $merchantId, $extend);
  231. return $user;
  232. }
  233. //合并用户 shish 2020.5.12
  234. public static function mergeUser($delUser, $retainUser)
  235. {
  236. return UserClass::mergeUser($delUser, $retainUser);
  237. }
  238. }