model = new \app\admin\model\oauth\Log; parent::initialize(); } //获取token public function getTokens() { return $this->auth->getToken(); } /** * 微信小程序获一键登录 * @param Request $request * @return void * @author wb */ public function miniprogramLogin(Request $request): void { if ($request->isPost()) { // code 前端获取,传递给接口 $params = $request->only(['code']); $result = MpService::getInstance()->getOpenidAndUnionid($params); $user_id= Log::where('uuid', $result['unionid'])->Value('user_id'); if (empty($user_id)) { $this->error(__('当前微信未绑定')); } $user_info=Admin::where('id',$user_id)->find(); $username=$user_info['username']; $password=$user_info['password']; $res = $this->auth->wxlogin($username, $password); AdminLog::setTitle(__('Login')); if ($res) { // 这里获取到Openid、Unionid $this->success('success', ['userInfo' => $this->auth->getInfo()] ); } else { $this->error('error'); } } } /** * 微信小程序账号绑定 * @param Request $request * @return void * @author wb */ public function miniprogramBind(Request $request): void { if ($request->isPost()) { // code 前端获取,传递给接口 $params['code'] = $request->only(['code']); $params['uid'] = $request->only(['uid']); $result = MpService::getInstance()->getOpenidAndUnionid($params['code']); $res=Log::create([ 'user_id' => $params['uid']['uid'], 'source' => 'wechat_scan', 'uuid' => $result['unionid'], 'extend' => 'null', ]); if ($res) { $this->success('success', $res ); } else { $this->error('error'); } } } /** * 微信小程序获取手机号示例 * @param Request $request * @return void * @author wb * 3 */ public function getPhoneNumber(Request $request): void { if ($request->isPost()) { // code 前端获取,传递给接口 $params = $request->only(['code']); $result = MpService::getInstance()->getPhoneNumber($params); if (!empty($result)) { // 这里获取到手机号 $this->success('success', $result); } else { $this->error('error'); } } } }