xhUserService.php 9.2 KB

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