UserService.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. //合并店长后台添加的客户
  138. $merchantId = $merchant['id'];
  139. $mobile = isset($data['mobile']) ? $data['mobile'] : '';
  140. if (!empty($mobile)) {
  141. $hasUser = UserClass::getByCondition(['merchantId' => $merchantId, 'mobile' => $mobile]);
  142. $userSource = UserClass::$userSourceId;
  143. $systemId = $userSource['system']['id'];
  144. Yii::info($hasUser['sourceType'] . ' -- ' . $systemId);
  145. if (!empty($hasUser) && $hasUser['sourceType'] == $systemId) {
  146. UserClass::mergeUser($hasUser, $user);
  147. }
  148. }
  149. //合并之后再更新信息
  150. $userId = $user['id'];
  151. UserClass::updateById($userId, $data);
  152. //注意成为会员和关注公众号时要去检查是否符合发优惠券条件
  153. $userAsset = UserAssetClass::getByUserId($userId);
  154. $user['mobile'] = $mobile;
  155. CouponAssetClass::giveCoupon($user, $merchant, $userAsset);
  156. //增加会员数
  157. MerchantAssetService::addMemberNum($merchantId);
  158. }
  159. //取消关注 shish 2019.9.8
  160. public static function unFocus($userId, $merchantId)
  161. {
  162. self::updateById($userId, ['subscribe' => 0, 'hasSubscribe' => 1]);
  163. //商家粉丝减少
  164. MerchantAssetClass::reduceFans($merchantId);
  165. }
  166. //关注公众号的后续 shish 2020.5.5
  167. public static function focus($user, $merchant)
  168. {
  169. $userId = $user['id'];
  170. UserClass::updateById($userId, ['subscribe' => 1]);
  171. $userAsset = UserAssetClass::getByUserId($userId);
  172. CouponAssetClass::giveCoupon($user, $merchant, $userAsset);
  173. }
  174. //批量获取用户信息,图片等信息处理好了 shish 2019.11.28
  175. public static function getUserByIds($ids)
  176. {
  177. return UserClass::getUserByIds($ids);
  178. }
  179. //验证支付密码是否正确 shish 2019.12.6
  180. public static function validPayPassword($password, $user)
  181. {
  182. if (isset($user['hasPayPwd']) == false || $user['hasPayPwd'] == 0) {
  183. util::fail('您还没有设置支付密码');
  184. }
  185. $payPassword = $user['payPassword'];
  186. if (password_verify($password, $payPassword) == false) {
  187. util::fail('密码错误');
  188. }
  189. }
  190. //验证用户权限
  191. public static function valid($info, $merchantId)
  192. {
  193. return UserClass::valid($info, $merchantId);
  194. }
  195. //根据消费次数和有没领优惠券来判断是不是新人
  196. public static function newUser($userInfo)
  197. {
  198. return UserClass::newUser($userInfo);
  199. }
  200. //添加新客户 shish 2020.1.10
  201. public static function addUser($data)
  202. {
  203. //merchantId $source $realName $member $growth $balance $mobile
  204. //必传参数
  205. validate::easyCheck(['merchantId', 'mobile'], $data);
  206. $merchantId = $data['merchantId'];
  207. $source = 'system';
  208. $mobile = $data['mobile'];
  209. $realName = isset($data['realName']) ? $data['realName'] : $mobile;
  210. $member = isset($data['member']) ? $data['member'] : -1;
  211. $balance = isset($data['balance']) ? $data['balance'] : 0;
  212. $growth = isset($data['growth']) ? $data['growth'] : 0;
  213. $originalInfo = [];
  214. $originalInfo['merchantId'] = $merchantId;
  215. $originalInfo['mobile'] = $mobile;
  216. $originalInfo['realName'] = $realName;
  217. $originalInfo['userName'] = $realName;
  218. $user = UserClass::replaceUser($originalInfo, $source, $merchantId);
  219. $extend = MerchantExtendService::getByMerchantId($merchantId);
  220. //等级
  221. if ($member > 0) {
  222. $gradeList = xhMerchantExtendService::getGradeList($extend, 'desc');
  223. $growth = isset($gradeList[$member]['growth']) ? $gradeList[$member]['growth'] : 0;
  224. }
  225. //余额成长值更新
  226. $userId = $user['id'];
  227. $data = [];
  228. $data['growth'] = $growth;
  229. $data['balance'] = $balance > 0 ? $balance : 0;
  230. UserAssetService::updateByUserId($userId, $data);
  231. //会员升级
  232. UserIntegralService::increaseToUpgrade(0, $growth, 0, $userId, $merchantId, $extend);
  233. return $user;
  234. }
  235. //合并用户 shish 2020.5.12
  236. public static function mergeUser($delUser, $retainUser)
  237. {
  238. return UserClass::mergeUser($delUser, $retainUser);
  239. }
  240. }