| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace app\api\model\user;
- use app\api\model\plus\agent\Referee as RefereeModel;
- use app\api\model\plus\invitationgift\Partake;
- use app\common\model\user\Grade as GradeModel;
- use think\facade\Cache;
- use app\common\exception\BaseException;
- use app\common\model\user\User as UserModel;
- /**
- * 公众号用户模型类
- */
- class UserMp extends UserModel
- {
- private $token;
- /**
- * 隐藏字段
- */
- protected $hidden = [
- 'open_id',
- 'is_delete',
- 'app_id',
- 'create_time',
- 'update_time'
- ];
- /**
- * 用户登录
- */
- public function login($userInfo, $referee_id = null)
- {
- $userInfo = $userInfo['original'];
- return [
- // 'nickName' => authcode($userInfo['nickname'], 'ENCODE'),
- // 'avatarUrl' => authcode($userInfo['headimgurl'], 'ENCODE'),
- // 'gender' => $userInfo['sex'],
- // 'province' => $userInfo['province'],
- // 'country' => $userInfo['country'],
- // 'city' => $userInfo['city'],
- 'openid' => authcode($userInfo['openid'], 'ENCODE'),
- // 'referee_id' => $referee_id,
- ];
- return $data;
- }
- // /**
- // * 用户登录
- // */
- // public function login($userInfo, $referee_id = null)
- // {
- // $userInfo = $userInfo['original'];
- // // 邀请好友
- // $invitation_id = isset($post['invitation_id']) ? $post['invitation_id'] : 0;
- // // 自动注册用户
- // $user_id = $this->register($userInfo, $referee_id, $invitation_id);
- // // 生成token (session3rd)
- // $this->token = $this->token($userInfo['openid']);
- // // 记录缓存, 7天
- // Cache::set($this->token, $user_id, 86400 * 7);
- // return $user_id;
- // }
- /**
- * 获取token
- */
- public function getToken()
- {
- return $this->token;
- }
- /**
- * 生成用户认证的token
- */
- private function token($openid)
- {
- return md5($openid . 'token_salt');
- }
- /**
- * 自动注册用户
- */
- private function register($userInfo, $referee_id, $invitation_id)
- {
- $data = [];
- //通过unionid查询用户是否存在
- $user = null;
- if(isset($userInfo['unionid']) && !empty($userInfo['unionid'])){
- $data['union_id'] = $userInfo['unionid'];
- $user = self::detailByUnionid($userInfo['unionid']);
- }
- // 查询用户是否已存在
- if(!$user){
- $user = self::detail(['mpopen_id' => $userInfo['openid']]);
- }
- if($user){
- $model = $user;
- if (isset($data['union_id'])) {
- // 只修改union_id
- $data = [
- 'union_id' => $data['union_id'],
- ];
- }else {
- return $user['user_id'];
- }
- }else{
- if($referee_id > 0){
- if(UserModel::detail($referee_id) == null){
- $referee_id = 0;
- }
- }
- $model = $this;
- $data['referee_id'] = $referee_id;
- $data['reg_source'] = 'mp';
- //默认等级
- $data['grade_id'] = GradeModel::getDefaultGradeId();
- }
- $data['mpopen_id'] = $userInfo['openid'];
- // 用户昵称
- if(!$user){
- $data['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $userInfo['nickname']);
- }
- $data['avatarUrl'] = $userInfo['headimgurl'];
- $data['gender'] = $userInfo['sex'];
- $data['province'] = $userInfo['province'];
- $data['country'] = $userInfo['country'];
- $data['city'] = $userInfo['city'];
- try {
- $this->startTrans();
- // 保存/更新用户记录
- if (!$model->save(array_merge($data, [
- 'app_id' => self::$app_id
- ]))
- ) {
- throw new BaseException(['msg' => '用户注册失败']);
- }
- if (!$user && $referee_id > 0) {
- // 记录推荐人关系,
- RefereeModel::createRelation($model['user_id'], $referee_id);
- //更新用户邀请数量
- (new UserModel())->setIncInvite($referee_id);
- //邀请好友送好礼
- $invitation_id > 0 && (new Partake())->addPartake($invitation_id, $referee_id, $model['user_id']);
- }
- $this->commit();
- } catch (\Exception $e) {
- $this->rollback();
- throw new BaseException(['msg' => $e->getMessage()]);
- }
- return $model['user_id'];
- }
- }
|