123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace app\models\user;
- use app\models\store\StoreCouponIssue;
- use crmeb\basic\BaseModel;
- use crmeb\services\WechatService;
- use crmeb\traits\ModelTrait;
- use crmeb\services\SystemConfigService;
- use app\models\routine\RoutineQrcode;
- use app\models\store\StoreCouponUser;
- class WechatUser extends BaseModel
- {
-
- protected $pk = 'uid';
-
- protected $name = 'wechat_user';
- use ModelTrait;
-
- public static function getOpenId($uid = '')
- {
- if ($uid == '') return false;
- return self::where('uid', $uid)->value('routine_openid');
- }
-
- public static function uidToOpenid($uid, $openidType = 'routine_openid')
- {
- $openid = self::where('uid', $uid)->value($openidType);
- return $openid;
- }
-
- public static function openidTouid($openid, $openidType = 'openid')
- {
- return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
- }
-
- public static function routineOauth($routine)
- {
- $routineInfo['nickname'] = filter_emoji($routine['nickName']);
- $routineInfo['sex'] = $routine['gender'];
- $routineInfo['language'] = $routine['language'];
- $routineInfo['city'] = $routine['city'];
- $routineInfo['province'] = $routine['province'];
- $routineInfo['country'] = $routine['country'];
- $routineInfo['headimgurl'] = $routine['avatarUrl'];
- $routineInfo['routine_openid'] = $routine['openId'];
- $routineInfo['session_key'] = $routine['session_key'];
- $routineInfo['unionid'] = $routine['unionId'];
- $routineInfo['user_type'] = 'routine';
- $spid = 0;
- $isCOde = false;
-
- if ($routine['code']) {
- if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
- $spid = $info['third_id'];
- $isCOde = true;
- } else
- $spid = $routine['spid'];
- } else if ($routine['spid'])
- $spid = $routine['spid'];
-
- if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
- self::edit($routineInfo, $uid, 'uid');
- $routineInfo['code'] = $spid;
- $routineInfo['isPromoter'] = $isCOde;
- if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
- User::updateWechatUser($routineInfo, $uid);
- } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) {
- self::edit($routineInfo, $uid, 'uid');
- $routineInfo['code'] = $spid;
- $routineInfo['isPromoter'] = $isCOde;
- if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
- User::updateWechatUser($routineInfo, $uid);
- } else {
- $routineInfo['add_time'] = time();
- $routineInfo = self::create($routineInfo);
- $res = User::setRoutineUser($routineInfo, $spid);
- $uid = $res->uid;
- }
- return $uid;
- }
-
- public static function isRoutineUser($uid = 0)
- {
- if (!$uid) return false;
- return self::where('uid', $uid)->where('user_type', 'routine')->count();
- }
-
- public static function isUserStatus($uid = 0)
- {
- if (!$uid) return 0;
- $user = User::getUserInfo($uid);
- return $user['status'];
- }
-
- public static function setNewUser($openid)
- {
- $userInfo = WechatService::getUserInfo($openid);
- if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
- exception('请关注公众号!');
- $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
-
- if (isset($userInfo['unionid'])) {
- $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
- unset($userInfo['qr_scene'],$userInfo['qr_scene_str'],$userInfo['qr_scene_str'],$userInfo['subscribe_scene']);
- if ($wechatInfo) {
- return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
- }
- }
- self::beginTrans();
- $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
- if (!$wechatUser) {
- self::rollbackTrans();
- exception('用户储存失败!');
- }
- if (!User::setWechatUser($wechatUser)) {
- self::rollbackTrans();
- exception('用户信息储存失败!');
- }
- self::commitTrans();
- self::userFirstSubGiveCoupon($openid);
- return $wechatUser;
- }
-
- public static function userFirstSubGiveCoupon($openid)
- {
- $couponIds = StoreCouponIssue::where('status',1)
- ->where('is_give_subscribe',1)
- ->where('is_del',0)
- ->column('cid');
- if ($couponIds)
- foreach ($couponIds as $couponId)
- if ($couponId)
- StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
- }
-
- public static function userTakeOrderGiveCoupon($uid,$total_price)
- {
- $couponIds = StoreCouponIssue::where('status',1)
- ->where('is_full_give',1)
- ->where('is_del',0)
- ->column('cid,full_reduction');
- if ($couponIds)
- foreach ($couponIds as $couponId)
- if ($couponId)
- if ($total_price >= $couponId['full_reduction'])
- StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
- }
-
- public static function updateUser($openid)
- {
- $userInfo = WechatService::getUserInfo($openid);
- $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
- return self::edit($userInfo->toArray(), $openid, 'openid');
- }
-
- public static function saveUser($openid)
- {
- self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
- }
-
- public static function unSubscribe($openid)
- {
- return self::edit(['subscribe' => 0], $openid, 'openid');
- }
-
- public static function uidToUnionid($uid)
- {
- return self::where('uid', $uid)->value('unionid');
- }
-
- public static function getWechatInfo($openid, $openidType)
- {
- if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
- $wechatInfo = self::where($openidType, $openid)->find();
- if (!$wechatInfo) {
- self::setNewUser($openid);
- $wechatInfo = self::where($openidType, $openid)->find();
- }
- if (!$wechatInfo) exception('获取用户信息失败!');
- return $wechatInfo->toArray();
- }
- }
|