xhUserService.php 8.0 KB

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