| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/12/21
- */
- 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;
- use think\facade\Db;
- use think\facade\Log;
- /**
- * TODO 微信用户Model
- * Class WechatUser
- * @package app\models\user
- */
- class WechatUser extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'uid';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'wechat_user';
- use ModelTrait;
- /**
- * uid获取小程序Openid
- * @param string $uid
- * @return bool|mixed
- */
- public static function getOpenId($uid = '')
- {
- if ($uid == '') return false;
- return self::where('uid', $uid)->value('routine_openid');
- }
- /**
- * TODO 用uid获得openid
- * @param $uid
- * @param string $openidType
- * @return mixed
- * @throws \Exception
- */
- public static function uidToOpenid($uid, $openidType = 'routine_openid')
- {
- $openid = self::where('uid', $uid)->value($openidType);
- return $openid;
- }
- /**
- * TODO 用openid获得uid
- * @param $openid
- * @param string $openidType
- * @return mixed
- */
- public static function openidTouid($openid, $openidType = 'openid')
- {
- return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
- }
- /**
- * 小程序创建用户后返回uid
- * @param $routineInfo
- * @return mixed
- */
- 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'];//openid
- $routineInfo['session_key'] = $routine['session_key'];//会话密匙
- $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
- $routineInfo['user_type'] = 'routine';//用户类型
- $spid = 0;//绑定关系uid
- $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 (sys_config('spread_look') == 1) {
- $spid = 0;
- }
- // 判断unionid 存在根据unionid判断
- 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);
- if (sys_config('spread_look') == 0) {
- if (!User::where('uid', $uid)->value('spread_uid') && $spid && $uid > $spid) { //绑定关系
- Db::name('user')->where('uid', $spid)->inc('spread_count', 1)->update();
- User::where('uid', $uid)->update([
- 'spread_uid' => $spid,
- 'spread_time' => time(),
- ]);
- }
- }
- } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
- 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);
- if (sys_config('spread_look') == 0) {
- if (!User::where('uid', $uid)->value('spread_uid') && $spid && $uid > $spid) { //绑定关系
- Db::name('user')->where('uid', $spid)->inc('spread_count', 1)->update();
- User::where('uid', $uid)->update([
- 'spread_uid' => $spid,
- 'spread_time' => time(),
- ]);
- }
- }
- } else {
- $routineInfo['add_time'] = time();//用户添加时间
- $routineInfo = self::create($routineInfo);
- $res = User::setRoutineUser($routineInfo, $spid);
- $uid = $res->uid;
- }
- if (intval(User::where('uid', $uid)->value('level')) == 0) {
- UserLevel::setUserLevel($uid, 1);
- }
- return $uid;
- }
- /**
- * 判断是否是小程序用户
- * @param int $uid
- * @return bool|int|string
- */
- public static function isRoutineUser($uid = 0)
- {
- if (!$uid) return false;
- return self::where('uid', $uid)->where('user_type', 'routine')->count();
- }
- /**
- * @param int $uid
- * @return int
- */
- public static function isUserStatus($uid = 0)
- {
- if (!$uid) return 0;
- $user = User::getUserInfo($uid);
- return $user['status'];
- }
- /**
- * .添加新用户
- * @param $openid
- * @return object
- */
- 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']);
- //判断 unionid 是否存在
- 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);
- if (intval(User::where('uid', $userInfo['uid'])->value('level')) == 0) {
- UserLevel::setUserLevel($userInfo['uid'], 1);
- }
- if (!UserBill::be(['uid' => $wechatUser['uid'], 'type' => 'focus_wechat', 'status' => 1]) && $wechatUser['subscribe'] == 1) {
- self::focusWechatGiveIntegral($wechatUser['uid']); //关注送积分
- }
- return $wechatUser;
- }
- /**
- * TODO 关注送优惠券
- * @param $openid
- */
- 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);
- }
- /**
- * 订单金额达到预设金额赠送优惠卷
- * @param $uid
- */
- 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']);
- }
- /**
- * 首次注册送积分
- * @param $uid
- */
- public static function firstRegGiveIntegral($uid)
- {
- $integral = sys_config('first_reg_user_give_integral');
- if ($integral > 0) {
- BaseModel::beginTrans();
- $balance = User::where('uid', $uid)->value('integral');
- $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
- $res2 = UserBill::income('首次注册', $uid, 'integral', 'first_reg', $integral, 0, $balance, '首次注册送贡献值');
- $res = $res1 && $res2;
- BaseModel::checkTrans($res);
- }
- }
- /**
- * 拼团成功送积分
- * @param $uid
- */
- public static function buyProductGiveIntegral($uid, $integral = 0)
- {
- // $integral = sys_config('buy_product_give_integral');
- // if ($integral > 0) {
- // BaseModel::beginTrans();
- // $balance = User::where('uid', $uid)->value('integral');
- // $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
- // $res2 = UserBill::income('购物成功', $uid, 'integral', 'buy_product', $integral, 0, $balance, '购物成功送积分');
- // $res = $res1 && $res2;
- // BaseModel::checkTrans($res);
- // }
- return true;
- }
- /**
- * 中奖失败奖励WDC
- * @param array $pinkRegimental 成功的团长编号
- * @return bool
- * @throws \Exception
- */
- public static function failGiveWdc($uid, $wdc = 0)
- {
- BaseModel::beginTrans();
- $userInfo = User::where('uid', $uid)->find();
- $sum_wdc = bcadd($userInfo['wdc'], $wdc, 2);
- $res1 = false != User::where('uid', $userInfo['uid'])->update(['wdc' => $sum_wdc]);
- $res3 = false != User::where('uid', $userInfo['uid'])->update(['wdc_all' => $sum_wdc]);
- $res2 = false != UserBill::income('未中奖赠送酒币', $uid, 'wdc', 'fail_give', $wdc, 0, bcadd($userInfo['wdc'], $wdc, 2), '拼团未中奖赠送' . floatval($wdc) . 'wdc');
- $res = $res1 && $res2 && $res3;
- BaseModel::checkTrans($res);
- return $res;
- }
- //拼中上级增加一次排队结束机会
- public static function OverLikeNum($uid)
- {
- BaseModel::beginTrans();
- $res = true;
- $userInfo = User::where('uid', $uid)->find();
- if ($userInfo['spread_uid ']) {
- $res1 = User::bcInc($userInfo['spread_uid '], 'over_like_num', 1, 'uid');
- $spreadInfo = User::where('uid', $userInfo['spread_uid '])->find();
- if ($spreadInfo['over_like_num'] == sys_config('invite_num_pd')) {
- $res2 = User::bcInc($userInfo['spread_uid '], ' over_num', 1, 'uid');
- $res3 = User::bcDec($userInfo['spread_uid '], ' over_like_num', sys_config('invite_num_pd'), 'uid');
- $res = $res1 && $res2 && $res3;
- } else {
- $res = $res1;
- }
- }
- BaseModel::checkTrans($res);
- return $res;
- }
- /**
- * 中奖奖励WDC
- * @param array $pinkRegimental 成功的团长编号
- * @return bool
- * @throws \Exception
- */
- public static function successGiveWdc($uid, $wdc = 0)
- {
- BaseModel::beginTrans();
- $userInfo = User::where('uid', $uid)->find();
- $sum_wdc = bcadd($userInfo['wdc'], $wdc, 2);
- $res1 = false != User::where('uid', $userInfo['uid'])->update(['wdc' => $sum_wdc]);
- $res3 = false != User::where('uid', $userInfo['uid'])->update(['wdc_all' => $sum_wdc]);
- $res2 = false != UserBill::income('中奖赠送酒币', $uid, 'wdc', 'success_give', $wdc, 0, bcadd($userInfo['wdc'], $wdc, 2), '拼团中奖赠送' . floatval($wdc) . 'wdc');
- $res = $res1 && $res2 && $res3;
- BaseModel::checkTrans($res);
- return $res;
- }
- /**
- * 成功发展下线送积分
- * @param $uid
- */
- public static function spreadSuccessGiveIntegral($uid)
- {
- $integral = sys_config('spread_success_give_integral');
- if ($integral > 0) {
- BaseModel::beginTrans();
- $balance = User::where('uid', $uid)->value('integral');
- $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
- $res2 = UserBill::income('推广成功', $uid, 'integral', 'spread_success', $integral, 0, $balance, '推广成功送积分');
- $res = $res1 && $res2;
- BaseModel::checkTrans($res);
- }
- }
- /**
- * 关注公众号送积分
- * @param $uid
- */
- public static function focusWechatGiveIntegral($uid)
- {
- $integral = sys_config('focus_wechat_give_integral');
- if ($integral > 0) {
- BaseModel::beginTrans();
- $balance = User::where('uid', $uid)->value('integral');
- $res1 = User::bcInc($uid, 'integral', $integral, 'uid');
- $res2 = UserBill::income('关注公众号', $uid, 'integral', 'focus_wechat', $integral, 0, $balance, '关注公众号送积分');
- $res = $res1 && $res2;
- BaseModel::checkTrans($res);
- }
- }
- /**
- * 更新用户信息
- * @param $openid
- * @return bool
- */
- public static function updateUser($openid)
- {
- $userInfo = WechatService::getUserInfo($openid);
- $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
- self::edit($userInfo->toArray(), $openid, 'openid');
- $user = WechatUser::where("openid", $openid)->find();
- if (!UserBill::be(['uid' => $user['uid'], 'type' => 'focus_wechat', 'status' => 1]) && $user['subscribe'] == 1) {
- self::focusWechatGiveIntegral($user['uid']); //关注送积分
- }
- }
- /**
- * 用户存在就更新 不存在就添加
- * @param $openid
- */
- public static function saveUser($openid)
- {
- self::be(['openid' => $openid]) == true ? self::updateUser($openid) : self::setNewUser($openid);
- }
- /**
- * 用户取消关注
- * @param $openid
- * @return bool
- */
- public static function unSubscribe($openid)
- {
- return self::edit(['subscribe' => 0], $openid, 'openid');
- }
- /**
- * TODO 用uid获得Unionid
- * @param $uid
- * @return mixed
- */
- public static function uidToUnionid($uid)
- {
- return self::where('uid', $uid)->value('unionid');
- }
- /**
- * TODO 获取用户信息
- * @param $openid
- * @param $openidType
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- 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();
- }
- }
|