xhUserService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace common\services;
  3. use bizHd\user\services\UserService;
  4. use common\components\util;
  5. use Yii;
  6. use linslin\yii2\curl;
  7. use common\components\stringUtil;
  8. use common\components\dict;
  9. use common\components\wxUtil;
  10. use common\models\xhUser;
  11. class xhUserService
  12. {
  13. public static function getByUnionId($unionId)
  14. {
  15. return xhUser::getByCondition(['unionId' => $unionId]);
  16. }
  17. public static function logout()
  18. {
  19. Yii::$app->session->destroy();//销毁session中所有已注册的数据
  20. }
  21. /**
  22. * 支付宝ID创建或获取用户
  23. */
  24. public static function generateUserByAlipay($alipayId, $sjId)
  25. {
  26. $getUserInfo = ['alipayId' => $alipayId, 'source' => 1];
  27. $user = self::replaceUserByWeixin($getUserInfo, $sjId);
  28. return $user;
  29. }
  30. public static function getByAlipayId($sjId, $alipayId)
  31. {
  32. $user = xhUser::getByCondition(['sjId' => $sjId, 'alipayId' => $alipayId]);
  33. return $user;
  34. }
  35. public static function replaceUserByMini($openId, $unionid, $sjId)
  36. {
  37. $preUser = xhUser::getByCondition(['sjId' => $sjId, 'unionId' => $unionid]);
  38. $getUserInfo = ['miniOpenId' => $openId, 'unionid' => $unionid, 'source' => 2];
  39. return self::replaceUserByWeixin($getUserInfo, $sjId, $preUser);
  40. }
  41. /**
  42. * 微信仅有openid创建用户
  43. */
  44. public static function replaceUserByOpenID($openId, $sjId, $preUser)
  45. {
  46. $getUserInfo = ['openid' => $openId];
  47. return self::replaceUserByWeixin($getUserInfo, $sjId, $preUser);
  48. }
  49. /**
  50. * 通过微信生成更新用户信息
  51. */
  52. public static function replaceUserByWeixin($getUserInfo, $sjId, $preUser = [])
  53. {
  54. //替换掉微信不支持的图片
  55. $nickName = isset($getUserInfo['nickName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getUserInfo['nickName']) : '';
  56. $openId = isset($getUserInfo['openid']) ? $getUserInfo['openid'] : '';
  57. $date = date("Y-m-d H:i:s");
  58. $data = [];
  59. $data['userName'] = $nickName;
  60. $data['sex'] = isset($getUserInfo['sex']) ? $getUserInfo['sex'] : 0;//未知
  61. $data['openId'] = $openId;
  62. $data['miniOpenId'] = isset($getUserInfo['miniOpenId']) ? $getUserInfo['miniOpenId'] : '';
  63. $data['unionId'] = isset($getUserInfo['unionid']) ? $getUserInfo['unionid'] : '';
  64. $data['identity'] = 0;
  65. $subscribe = isset($getUserInfo['subscribe']) ? $getUserInfo['subscribe'] : 0;
  66. $data['subscribe'] = $subscribe;
  67. $data['subscribeTime'] = isset($getUserInfo['subscribe_time']) ? $getUserInfo['subscribe_time'] : 0;
  68. $data['sjId'] = $sjId;
  69. $data['status'] = 0;
  70. $data['alipayId'] = isset($getUserInfo['alipayId']) ? $getUserInfo['alipayId'] : '';
  71. //默认完整的用户信息
  72. $data['isFull'] = isset($getUserInfo['isFull']) ? $getUserInfo['isFull'] : 0;
  73. if (!empty($preUser)) {
  74. $userId = $preUser['id'];
  75. self::updateById($userId, $data);
  76. $user = self::getById($userId);
  77. } else {
  78. $data['createTime'] = $date;
  79. $data['avatar'] = '';
  80. $data['password'] = '';
  81. $data['payPassword'] = '';
  82. $data['mobile'] = '';
  83. $data['source'] = isset($getUserInfo['source']) ? $getUserInfo['source'] : 0;
  84. $data['hasSubscribe'] = 0;
  85. $user = self::add($data);//生成用户基础信息
  86. $userId = $user['id'];
  87. if (empty($nickName)) {
  88. self::updateById($userId, ['userName' => $userId]);
  89. }
  90. $random = Yii::$app->getSecurity()->generateRandomString();//yii2 里生成32位唯一随机数
  91. $uniqueCode = $userId . $random;
  92. $assetData = ['userId' => $userId, 'sjId' => $sjId, 'createTime' => $date, 'uniqueCode' => $uniqueCode];
  93. xhUserAssetService::add($userId, $assetData);
  94. }
  95. if (isset($getUserInfo['unionid']) && !empty($getUserInfo['unionid'])) {
  96. $unionId = $getUserInfo['unionid'];
  97. $now = time();
  98. $unionData = ['unionId' => $unionId, 'createTime' => $now];
  99. xhUnionUserService::add($unionData);
  100. }
  101. //保存头像
  102. if (isset($getUserInfo['headimgurl']) && !empty($getUserInfo['headimgurl'])) {
  103. if (empty($preUser) || (isset($preUser['avatar']) && empty($preUser['avatar']))) {
  104. $imgUrl = $getUserInfo['headimgurl'];
  105. $name = self::avatarSave($userId, $imgUrl);
  106. self::updateById($userId, ['avatar' => $name]);
  107. }
  108. }
  109. //商家粉丝增加1个
  110. if ($subscribe == 1) {
  111. $mAsset = xhMerchantAssetService::getBySjId($sjId);
  112. $totalFans = $mAsset['totalFans'] + 1;
  113. $asset = xhMerchantAssetService::updateBySjId($sjId, ['totalFans' => $totalFans]);
  114. xhUserByMonthService::replace($sjId, 1, $asset);//月粉丝量 +1
  115. xhUserByDayService::replace($sjId, 1, $asset);//每天增加粉丝 +1
  116. }
  117. return $user;
  118. }
  119. /**
  120. * 保存头像
  121. * @return string
  122. */
  123. public static function avatarSave($userId, $url)
  124. {
  125. $directory = dict::getDict('imgSavePath', 'wxUserAvatar');
  126. $path = Yii::getAlias('@webroot') . '/../../../resource/images';
  127. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  128. if (!file_exists($path . $pre)) {
  129. mkdir($path . $pre, 0777, true);
  130. }
  131. $randString = stringUtil::buildOrderNo();
  132. $name = $userId . '_' . $randString . '.jpg';
  133. $fullFileName = $path . $pre . $name;
  134. $fileName = $pre . $name;
  135. $curl = new curl\Curl();
  136. $result = $curl->get($url);
  137. $fp2 = @fopen($fullFileName, 'a');//文件大小
  138. fwrite($fp2, $result);
  139. fclose($fp2);
  140. $dstImg200 = $path . $pre . $userId . '_' . $randString . '_200.jpg';//缩略宽 200px 中
  141. util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
  142. $dstImg50 = $path . $pre . $userId . '_' . $randString . '_50.jpg';//缩略成宽50px 小
  143. util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
  144. return $fileName;//默认大小 大
  145. }
  146. /**
  147. * 传入最大的头像,输出三种头像:大 中 小
  148. */
  149. public static function getAvatarList($largeImg)
  150. {
  151. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  152. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  153. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  154. }
  155. public static function add($data)
  156. {
  157. $user = xhUser::add($data);
  158. $userId = $user['id'];
  159. self::refreshById($userId);
  160. return $user;
  161. }
  162. /**
  163. * 是否关注了公众号
  164. */
  165. public static function isSubscribe($userId)
  166. {
  167. $user = self::getById($userId);
  168. return empty($user['subscribe']) ? false : true;
  169. }
  170. /**
  171. * 检查手机号在平台下没有被使用
  172. */
  173. public static function mobileNotUsed($mobile, $sjId)
  174. {
  175. $user = xhUser::getByCondition(['sjId' => $sjId, 'mobile' => $mobile]);//每个公众号下有唯一的手机号
  176. if (!empty($user)) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. //获取用户信息
  182. public static function getById($id, $sjId = 0)
  183. {
  184. return xhUser::find()->where(['id' => $id])->asArray()->one();
  185. }
  186. /**
  187. * 获取用户信息
  188. */
  189. public static function getByOpenId($openId, $sjId)
  190. {
  191. $user = xhUser::find()->where(['sjId' => $sjId, 'openId' => $openId])->asArray()->one();
  192. return $user;
  193. }
  194. public static function refreshById($userId)
  195. {
  196. $preKey = dict::getCacheKey('xhUser');
  197. $key = $preKey . $userId;
  198. Yii::$app->redis->executeCommand('DEL', [$key]);
  199. $user = self::getById($userId);
  200. $openId = $user['openId'];
  201. $preKey = dict::getCacheKey('xhUserOpenId');
  202. $key = $preKey . $openId;
  203. Yii::$app->redis->executeCommand('DEL', [$key]);
  204. self::getByOpenId($openId);
  205. }
  206. public static function updateById($id, $data)
  207. {
  208. xhUser::updateById($id, $data);
  209. }
  210. /**
  211. * 更新用户的标签
  212. */
  213. public static function updateTag($userId)
  214. {
  215. $ids = xhUserTagService::refreshUserTag($userId);
  216. $string = implode(',', $ids);
  217. self::updateById($userId, ['tag' => $string]);
  218. }
  219. /**
  220. * 通过微信获取用户是否关注公众号
  221. */
  222. public static function checkUserFocus($user, $merchant)
  223. {
  224. $openId = $user['openId'];
  225. $info = wxUtil::batchGetUserInfo($merchant, [$openId]);
  226. $wxUserInfo = isset($info['user_info_list'][0]) ? $info['user_info_list'][0] : [];
  227. return isset($wxUserInfo['subscribe']) && !empty($wxUserInfo['subscribe']) ? true : false;
  228. }
  229. /**
  230. * 根据手机号搜索
  231. */
  232. public static function searchByMobile($mobile, $sjId)
  233. {
  234. $query = xhUser::find();
  235. $query->where(['sjId' => $sjId]);
  236. $query->andWhere(['like', 'mobile', $mobile]);
  237. $respond = $query->orderBy('id desc')->limit(5)->asArray()->all();
  238. return $respond;
  239. }
  240. public static function getAlipay($sjId, $alipayId)
  241. {
  242. return xhUser::find()->where(['sjId' => $sjId, 'alipayId' => $alipayId])->asArray()->one();
  243. }
  244. }