checkSet($param); if ($setResult['status'] != 1) { return $setResult; } $set = $setResult['data']; //商户的公众号配置 if (strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger")) { switch ($set['type']) { case 1: //商户自定义配置的公众号 //判断code是否存在 if (!empty($param['code'])) { if ($param['code'] != 'authdeny') { //用户授权 $options['appid'] = $set['appid']; $options['appsecret'] = $set['appsercet']; $wx = new \Util\Wx\Wechat($options); $wx_call = $wx->getOauthAccessToken($param['code']);//获取access_token和openid if (!$wx_call) { return callBack(statusCode()['error'], "授权失败"); }else{ return callBack(statusCode()['success'], "获取成功", $wx_call['openid']); } } else { return callBack(statusCode()['error'], "请授权"); } } else { $this->getCode([ 'appid' => $set['appid'], 'appsecret' => $set['appsercet'], 'type' => 1 ]); } break; case 2: //商户授权的公众号 $options = [ 'appid'=>$set['authorizer_appid'] ]; $appinfo = M('appweixin')->where(['id' => 1])->find(); //判断code是否存在 if (!empty($param['code'])) { if ($param['code'] != 'authdeny') { //用户授权 $wx = new \Util\Wx\Wechat($options); $token = get_acc_token(); $wx_call = $wx->getOauthAccessTokens($param['code'], $appinfo['appid'], $token);//获取access_token和openid if (!$wx_call) { return callBack(statusCode()['error'], "获取失败"); }else{ return callBack(statusCode()['success'], "获取成功", $wx_call['openid']); } } else { return callBack(statusCode()['error'], "请授权"); } } else { $this->getCode([ 'appid' => $options['appid'], 'third_appid' => $appinfo['appid'], 'type' => 2 ]); } break; default: return callBack(statusCode()['error'], "微信配置参数有误"); break; } } else { return callBack(statusCode()['error'], "请使用微信浏览器访问本应用!"); } } /** * 获取code * @param $param * [ * type 1 自定义公众号 2 授权的公众号 * appid 公众号app_id * appsecret 公众号 secret * third_appid 第三方appid * ] * @return mixed */ public function getCode($param) { $options['appid'] = $param['appid']; if ($param['type'] == 1) { $options['appsecret'] = $param['appsecret']; } if ($_SERVER["HTTPS"] == "on") { $title = 'https'; } else { $title = 'http'; } $url = $title . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $wx = new \Util\Wx\Wechat($options); if ($param['type'] == 1) { $_url = $wx->getOauthRedirect($url, '1', 'snsapi_userinfo'); } else { $_url = $wx->getOauthRedirects($url, '1', 'snsapi_userinfo', $param['third_appid']); } header("Location:" . $_url); exit; } /** * 设置全局变量 全局缓存SET * @param $param * @return mixed */ public function checkSet($param) { $set = M('wxconfig')->where(['merchantid' => $param['merchant_id']])->find(); if (!empty($set)) { return callBack(statusCode()['success'], "系统全局设置已定义", $set); } else { return callBack(statusCode()['error'], "系统全局设置未定义"); } } }