UserService.php 8.6 KB

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