123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- namespace app\admin\model\wechat;
- use app\admin\model\User;
- use liuniu\BaseModel;
- class Relation extends BaseModel
- {
-
- // 表名
- protected $name = 'user_relation';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'int';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = false;
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'sex_text',
- 'subscribe_text',
- 'subscribe_time_text',
- 'user_type_text'
- ];
- public function getSexList()
- {
- return ['1' => __('Sex 1'), '2' => __('Sex 2'), '0' => __('Sex 0')];
- }
- public function getSubscribeList()
- {
- return ['0' => __('Subscribe 0'), '1' => __('Subscribe 1')];
- }
- public function getUserTypeList()
- {
- return ['0' => __('User_type 0'), '1' => __('User_type 1'), '2' => __('User_type 2'), '3' => __('User_type 3')];
- }
- public function getSexTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
- $list = $this->getSexList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getSubscribeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['subscribe']) ? $data['subscribe'] : '');
- $list = $this->getSubscribeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getSubscribeTimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['subscribe_time']) ? $data['subscribe_time'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- public function getUserTypeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
- $list = $this->getUserTypeList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- protected function setSubscribeTimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- /**
- * 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'];
- // 判断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);
- } 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);
- } else {
- $routineInfo['add_time'] = time();//用户添加时间
- $routineInfo = self::create($routineInfo);
- $res = User::setRoutineUser($routineInfo, $spid);
- $uid = $res->uid;
- }
- 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);
- return $wechatUser;
- }
- /**
- * 更新用户信息
- * @param $openid
- * @return bool
- */
- public static function updateUser($openid)
- {
- $userInfo = WechatService::getUserInfo($openid);
- $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
- return self::edit($userInfo->toArray(), $openid, 'openid');
- }
- /**
- * 用户存在就更新 不存在就添加
- * @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();
- }
- }
|