/** * 创建平台特定的公众号操作对象 * @param int $id 公众号编号 * @return Platform|null */ public static function create($id) { $p = new Account(); $platform = $p->getAccount($id); if (!empty($platform)) { if ($platform['type'] == Account::ACCOUNT_ALIPAY) { return new Alipay($platform); } if ($platform['type'] == Account::ACCOUNT_WEIXIN) { return new WeiXin($platform); } } return null; }
public function weixinAction() { $code = I('get.code'); if (!empty($code)) { Member::loadSettings(); $setting = C('MS'); $auth = $setting[Member::OPT_AUTH_WEIXIN]; if ($auth == '0') { exit('request error'); } else { $a = new Account(); $account = $a->getAccount($auth, Account::ACCOUNT_WEIXIN); $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$account['appid']}&secret={$account['secret']}&code={$code}&grant_type=authorization_code"; $ret = Net::httpGet($url); if (!is_error($ret)) { $auth = @json_decode($ret, true); if (is_array($auth) && !empty($auth['openid'])) { $condition = '`platformid`=:platform AND `openid`=:openid'; $pars = array(); $pars[':platform'] = $account['id']; $pars[':openid'] = $auth['openid']; $fan = $a->table('__MMB_MAPPING_FANS__')->where($condition)->bind($pars)->find(); if (empty($fan)) { $platform = Platform::create($account['id']); $info = $platform->fansQueryInfo($auth['openid'], true); $fan = array(); $fan['platformid'] = $account['id']; $fan['uid'] = 0; $fan['openid'] = $auth['openid']; if (!is_error($info)) { if (!empty($info['original']['unionid'])) { $fan['unionid'] = $info['original']['unionid']; $uid = $a->table('__MMB_MAPPING_FANS__')->field('uid')->where("`unionid`=`{$info['original']['unionid']}`")->find(); if (!empty($uid)) { $fan['uid'] = $uid; } } $fan['subscribe'] = $info['original']['subscribe']; $fan['subscribetime'] = $info['original']['subscribe_time']; unset($info['original']); $fan['info'] = serialize($info); } $fan['unsubscribetime'] = 0; $a->table('__MMB_MAPPING_FANS__')->data($fan)->add(); } $stateKey = I('get.state'); $state = session('auth:forward'); session('auth:forward', null); if ($state[0] == $stateKey) { $forward = $state[1]; } else { $forward = U('/'); } if (stripos($forward, '?') !== false) { $forward .= '&wxref=mp.weixin.qq.com#wechat_redirect'; } else { $forward .= '?wxref=mp.weixin.qq.com#wechat_redirect'; } if (!empty($fan['uid'])) { //登陆 $m = new Member(); $member = $m->profile($uid); if (!empty($member)) { $m->login($fan['uid']); redirect($forward); } } if ($setting[Member::OPT_POLICY] == Member::OPT_POLICY_CLASSICAL) { //兼容模式, 创建新用户 $this->error('兼容模式暂未支持'); redirect($forward); } else { //统一模式, 注册新用户 session('fan:weixin', $fan); if (!empty($fan['info'])) { $fan['info'] = unserialize($fan['info']); } $this->assign('fan', $fan); $this->assign('forward', $forward); $this->display('weixin'); } return; } else { $this->error('微信授权失败错误信息为: ' . $ret); } } $this->error('微信授权失败错误信息为: ' . $ret['message']); } } exit('访问错误'); }
/** * @param $log * @return js payment object */ public function payWeixin($log) { self::loadSettings(); $setting = C('PAY'); $pay = $setting[self::OPT_WEIXIN]; $a = new Account(); $account = $a->getAccount($pay['account']); $pay['appid'] = $account['appid']; $pay['secret'] = $account['secret']; $wOpt = array(); $m = new Member(); $fan = $m->fetchFan($log['uid'], $pay['account']); $package = array(); $package['appid'] = $pay['appid']; $package['mch_id'] = $pay['mchid']; $package['nonce_str'] = util_random(8); $package['body'] = $log['title']; $package['attach'] = $log['plid']; $package['out_trade_no'] = md5($log['plid']); $package['total_fee'] = $log['fee'] * 100; $package['spbill_create_ip'] = get_client_ip(); $package['time_start'] = date('YmdHis', TIMESTAMP); $package['time_expire'] = date('YmdHis', TIMESTAMP + 600); $package['notify_url'] = __HOST__ . U('wander/payment/weixin/t/notify'); $package['trade_type'] = 'JSAPI'; $package['openid'] = $fan['openid']; ksort($package, SORT_STRING); $string1 = ''; foreach ($package as $key => $v) { $string1 .= "{$key}={$v}&"; } $string1 .= "key={$pay['key']}"; $package['sign'] = strtoupper(md5($string1)); $dat = util_2xml($package); $response = Net::httpPost('https://api.mch.weixin.qq.com/pay/unifiedorder', $dat); if (is_error($response)) { return $response; } $xml = '<?xml version="1.0" encoding="utf-8"?>' . $response; $dom = new \DOMDocument(); if (!$dom->loadXML($xml)) { return error(-1, 'error response'); } $xpath = new \DOMXPath($dom); if ($xpath->evaluate("string(//xml/return_code)") == 'FAIL') { return error(-2, $xpath->evaluate("string(//xml/return_msg)")); } if ($xpath->evaluate("string(//xml/result_code)") == 'FAIL') { return error(-3, $xpath->evaluate("string(//xml/err_code_des)")); } $prepayid = $xpath->evaluate("string(//xml/prepay_id)"); $wOpt['appId'] = $pay['appid']; $wOpt['timeStamp'] = TIMESTAMP; $wOpt['nonceStr'] = util_random(8); $wOpt['package'] = 'prepay_id=' . $prepayid; $wOpt['signType'] = 'MD5'; ksort($wOpt, SORT_STRING); $string = ''; foreach ($wOpt as $key => $v) { $string .= "{$key}={$v}&"; } $string .= "key={$pay['key']}"; $wOpt['paySign'] = strtoupper(md5($string)); return $wOpt; }
/** * 获取当前会员身份 */ public function auth() { $uid = session('__:uid'); if (!empty($uid)) { $profile = $this->profile($uid); if (!empty($profile)) { C('MEMBER', $profile); return true; } } if (IN_CONTAINER_WEIXIN) { Member::loadSettings(); $setting = C('MS'); $auth = $setting[Member::OPT_AUTH_WEIXIN]; if ($auth == '0') { $account = null; } else { $a = new Account(); $account = $a->getAccount($auth, Account::ACCOUNT_WEIXIN); $callback = urlencode(__HOST__ . U('/auth/weixin')); } if (empty($account)) { $account = array(); $account['appid'] = 'wx2f9d7b7b086d0fd8'; $callback = urlencode('http://cloud.microb.cn/proxy/auth/weixin?site=' . C('')); } $state = $_SERVER['REQUEST_URI']; $stateKey = substr(md5($state), 0, 8); session('auth:forward', array($stateKey, $state)); $forward = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$account['appid']}&redirect_uri={$callback}&response_type=code&scope=snsapi_base&state={$stateKey}#wechat_redirect"; redirect($forward); } elseif (IN_CONTAINER_ALIPAY) { } else { $state = $_SERVER['REQUEST_URI']; $stateKey = substr(md5($state), 0, 8); session('auth:forward', array($stateKey, $state)); $url = U('/auth/basic?forward=' . $stateKey); redirect($url); } return false; }
public function weixinDelete() { $id = intval(I('get.id')); if (empty($id)) { $this->error('访问错误'); } $a = new Account(); $account = $a->getAccount($id, Account::ACCOUNT_ALIPAY); if (empty($account)) { $this->error('访问错误'); } $a->remove($id); $this->success('删除成功'); exit; }