|
@@ -16,13 +16,6 @@ class Wechat
|
|
|
* @var array
|
|
|
*/
|
|
|
private $config;
|
|
|
-
|
|
|
- /**
|
|
|
- * config
|
|
|
- *
|
|
|
- * @var array
|
|
|
- */
|
|
|
- private $token;
|
|
|
|
|
|
/**
|
|
|
* project
|
|
@@ -36,9 +29,10 @@ class Wechat
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function __construct($type = 1, $project = false)
|
|
|
+ public function __construct($project = false, $type = '')
|
|
|
{
|
|
|
- $this->config = Dever::config('wechat')->cAll;
|
|
|
+ $this->config = Dever::config('wechat', $type)->cAll;
|
|
|
+ $type = $this->config['type'];
|
|
|
|
|
|
if (!$project) {
|
|
|
$appid = Dever::input('appid');
|
|
@@ -86,15 +80,32 @@ class Wechat
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- private function save($type = 'ticket', $value, $expires = false, $interval = 2000)
|
|
|
+ private function save($type = 'ticket', $value, $expires = false, $interval = 2000, $data = false)
|
|
|
{
|
|
|
if (strpos($type, '.')) {
|
|
|
$temp = explode('.', $type);
|
|
|
+ if ($temp[1] == 'refresh') {
|
|
|
+ $temp[1] = 'oauth';
|
|
|
+ }
|
|
|
$table = 'main/' . $temp[1];
|
|
|
} else {
|
|
|
$table = 'main/' . $type;
|
|
|
}
|
|
|
$db = Dever::db($table);
|
|
|
+
|
|
|
+ if ($data) {
|
|
|
+ if (isset($data['id'])) {
|
|
|
+ $where['option_id'] = $data['id'];
|
|
|
+ unset($data['id']);
|
|
|
+ }
|
|
|
+ if (isset($data['openid'])) {
|
|
|
+ $where['option_openid'] = $data['openid'];
|
|
|
+ }
|
|
|
+ if (isset($data['unionid'])) {
|
|
|
+ $where['option_unionid'] = $data['unionid'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
$where['option_project_id'] = $this->project['id'];
|
|
|
$info = $db->one($where);
|
|
|
|
|
@@ -103,17 +114,15 @@ class Wechat
|
|
|
if ($info && time() - $info['mdate'] >= $info['expires']) {
|
|
|
$update = true;
|
|
|
} elseif($info) {
|
|
|
- return $info['value'];
|
|
|
+ return $info;
|
|
|
}
|
|
|
if (!$info) {
|
|
|
$update = false;
|
|
|
}
|
|
|
-
|
|
|
if (!$value) {
|
|
|
- $value = $this->param($type);
|
|
|
+ $value = $this->param($type, $info);
|
|
|
}
|
|
|
|
|
|
- $data = array();
|
|
|
if (is_array($value) || (is_string($value) && strstr($value, 'http://'))) {
|
|
|
$result = $this->curl(false, $value);
|
|
|
|
|
@@ -138,15 +147,22 @@ class Wechat
|
|
|
}
|
|
|
if ($update == true) {
|
|
|
$data['where_id'] = $info['id'];
|
|
|
+ $id = $info['id'];
|
|
|
$db->update($data);
|
|
|
} else {
|
|
|
- $db->insert($data);
|
|
|
+ $id = $db->insert($data);
|
|
|
+ }
|
|
|
+ $data['id'] = $id;
|
|
|
+ if ($id > 0 && isset($result['callback'])) {
|
|
|
+ foreach ($result['callback'] as $v) {
|
|
|
+ Dever::load($v[0], $id, $v[1], $this->project['id']);
|
|
|
+ }
|
|
|
}
|
|
|
} elseif($info && $info['value']) {
|
|
|
$data['value'] = $info['value'];
|
|
|
}
|
|
|
|
|
|
- return $data['value'];
|
|
|
+ return $data;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -156,7 +172,8 @@ class Wechat
|
|
|
*/
|
|
|
public function token($value = false, $expires = false, $interval = 2000)
|
|
|
{
|
|
|
- return $this->save('token', $value, $expires, $interval);
|
|
|
+ $result = $this->save('token', $value, $expires, $interval);
|
|
|
+ return $result['value'];
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -166,7 +183,8 @@ class Wechat
|
|
|
*/
|
|
|
public function ticket($value = false, $expires = false, $interval = 200)
|
|
|
{
|
|
|
- return $this->save('ticket', $value, $expires, $interval);
|
|
|
+ $result = $this->save('ticket', $value, $expires, $interval);
|
|
|
+ return $result['value'];
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -174,9 +192,23 @@ class Wechat
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function oauth($value = false, $expires = false, $interval = 200)
|
|
|
+ public function oauth($param, $interval = 2000)
|
|
|
{
|
|
|
- return $this->save('oauth.oauth', $value, $expires, $interval);
|
|
|
+ if (is_array($param) && isset($param['auth_code'])) {
|
|
|
+ $result = $this->curl('oauth.oauth', $param);
|
|
|
+ if (isset($result['oauth.oauth'])) {
|
|
|
+ return $this->save('oauth.oauth', $result['oauth.oauth'], $result['expires_in'], $interval, $result);
|
|
|
+ } else {
|
|
|
+ Dever::alert('oauth error');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (is_numeric($param)) {
|
|
|
+ $id = $param;
|
|
|
+ $param = array();
|
|
|
+ $param['id'] = $id;
|
|
|
+ }
|
|
|
+ return $this->save('oauth.refresh', false, false, $interval, $param);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -186,7 +218,19 @@ class Wechat
|
|
|
*/
|
|
|
public function code($value = false, $expires = false, $interval = 200)
|
|
|
{
|
|
|
- return $this->save('oauth.code', $value, $expires, $interval);
|
|
|
+ $result = $this->save('oauth.code', $value, $expires, $interval);
|
|
|
+ return $result['value'];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取最新的openid
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function openid($id, $key = 'openid')
|
|
|
+ {
|
|
|
+ $info = Dever::db('main/oauth')->one($id);
|
|
|
+ return $info[$key];
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -213,7 +257,7 @@ class Wechat
|
|
|
*
|
|
|
* @return mixed
|
|
|
*/
|
|
|
- public function curl($method, $param = array())
|
|
|
+ public function curl($method, $param = array(), $alert = true)
|
|
|
{
|
|
|
if (is_string($param)) {
|
|
|
$result = json_decode(Dever::curl($param), true);
|
|
@@ -223,11 +267,15 @@ class Wechat
|
|
|
}
|
|
|
|
|
|
$result = json_decode(Dever::curl($param['url'], $param['param'], $param['method'], $param['json']), true);
|
|
|
+ $this->log($result, $param['name'], $param['url'], $param['param']);
|
|
|
}
|
|
|
+ //print_r($param);die;
|
|
|
if (isset($result['errcode']) && $result['errcode'] != 0) {
|
|
|
$result = $param + $result;
|
|
|
Dever::log($result);
|
|
|
- Dever::alert(json_encode($result));
|
|
|
+ if ($alert) {
|
|
|
+ Dever::alert(json_encode($result, JSON_UNESCAPED_UNICODE));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (isset($param['response'])) {
|
|
@@ -240,11 +288,25 @@ class Wechat
|
|
|
} elseif (isset($result[$k])) {
|
|
|
$result[$v] = $result[$k];
|
|
|
}
|
|
|
+
|
|
|
+ if (strpos($v, 'callback.') !== false) {
|
|
|
+ $temp = explode('callback.', $v);
|
|
|
+ $result['callback'][] = array($temp[1], $result[$v]);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
|
|
|
+ public function log($result, $name, $url, $param)
|
|
|
+ {
|
|
|
+ $insert['name'] = $name;
|
|
|
+ $insert['url'] = $url;
|
|
|
+ $insert['result'] = json_encode($result, JSON_UNESCAPED_UNICODE);
|
|
|
+ $insert['param'] = json_encode($param, JSON_UNESCAPED_UNICODE);
|
|
|
+ Dever::db('main/log')->insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 拼装wechat需要的参数
|
|
|
*
|
|
@@ -261,9 +323,14 @@ class Wechat
|
|
|
}
|
|
|
$config = $this->config[$method];
|
|
|
}
|
|
|
+
|
|
|
+ if (!$param) {
|
|
|
+ $param = array();
|
|
|
+ }
|
|
|
|
|
|
$param = $this->project + $param;
|
|
|
|
|
|
+ //print_r($param);die;
|
|
|
foreach ($config['param'] as $k => $v) {
|
|
|
if ($v == 'token') {
|
|
|
$config['url'] .= $k . '=' . $this->token();
|
|
@@ -272,12 +339,19 @@ class Wechat
|
|
|
$config['param'][$k] = $this->ticket();
|
|
|
} elseif ($v == 'code') {
|
|
|
$config['param'][$k] = $this->code();
|
|
|
- }elseif ($v == 'oauth') {
|
|
|
- $config['param'][$k] = $this->oauth();
|
|
|
- } elseif (isset($param[$v]) && $param[$v]) {
|
|
|
+ } elseif ($v == 'oauth') {
|
|
|
+ if (!isset($param['oauth'])) {
|
|
|
+ Dever::alert('oauth erorr');
|
|
|
+ } elseif (is_numeric($param['oauth'])) {
|
|
|
+ $oauth = $this->oauth($param['oauth']);
|
|
|
+ $param['oauth'] = $oauth['value'];
|
|
|
+ }
|
|
|
+ $config['url'] .= $k . '=' . $param['oauth'];
|
|
|
+ unset($config['param'][$k]);
|
|
|
+ } elseif (!is_array($v) && isset($param[$v])) {
|
|
|
$config['param'][$k] = $param[$v];
|
|
|
} elseif($v) {
|
|
|
- $config['param'][$k] = $v;
|
|
|
+ $config['param'][$k] = $this->replace($v, $param);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -286,6 +360,23 @@ class Wechat
|
|
|
return $config;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 替换{}
|
|
|
+ *
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ public function replace($value, $param)
|
|
|
+ {
|
|
|
+ if (isset($param['domain']) && strpos($value, '{domain}') !== false) {
|
|
|
+ foreach ($param['domain'] as $k => $v) {
|
|
|
+ $param['domain'][$k] = str_replace('{domain}', $v, $value);
|
|
|
+ }
|
|
|
+ return $param['domain'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 消息解密
|
|
|
*
|