hasOne('user', 'id', 'user_id')->field('id,avatar,mobile,username'); } /** * 通过微信小程序openid获取用户id */ public function getUserInfoByOpenid($openid) { $userExtend = $this ->with('user') ->where(['openid' => $openid]) ->find(); if ($userExtend) { $user = $userExtend->user; } else { Db::startTrans(); try { $params = [ 'level' => 1, 'score' => 0, 'jointime' => time(), 'joinip' => $_SERVER['REMOTE_ADDR'], 'logintime' => time(), 'loginip' => $_SERVER['REMOTE_ADDR'], 'prevtime' => time(), 'status' => 'normal', 'avatar' => '', 'username' => __('Tourist') ]; $user = User::create($params, true); self::create([ 'user_id' => $user->id, 'openid' => $openid ], true); Db::commit(); } catch (Exception $e) { Db::rollback(); return false; } } $user = $user->toArray(); return $user; } }