Skip to content

Commit

Permalink
Discuz! api
Browse files Browse the repository at this point in the history
  • Loading branch information
leiyu committed Aug 5, 2019
1 parent 4ee5ef4 commit 3f20f46
Show file tree
Hide file tree
Showing 37 changed files with 3,415 additions and 2 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# Discuz-Api
DiscuzAPI,适用于Discuz app 小程序
## 使用方法

- 修改配置文件,config_minapp.php, 将申请好的小程序 appkey appsecret 修改好
- 增加新表,SQL如下
- 把mobile目录覆盖discuz source/plugin/mobile目录
- 修改小程序配置文件,将域名换成您网站域名
- 上传小程序测试
- 该接口也适用于App调用

## 增加小程序登录绑定表
```
CREATE TABLE `pre_weixin_minapp_user` (
`uid` int(11) unsigned NOT NULL,
`openid` char(35) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '0',
`session_key` char(255) NOT NULL DEFAULT '',
`unionid` char(255) NOT NULL DEFAULT '',
PRIMARY KEY (`openid`),
KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
```
30 changes: 30 additions & 0 deletions mobile/api/5/activityapplies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: activityapplies.php 34314 2014-02-20 01:04:24Z nemohou $
*/

if(!defined('IN_MOBILE_API')) {
exit('Access Denied');
}

$_GET['mod'] = 'misc';
$_GET['action'] = 'activityapplies';
include_once 'forum.php';

class mobile_api {

function common() {
}

function output() {
$variable = array();
mobile_core::result(mobile_core::variable($variable));
}

}

?>
67 changes: 67 additions & 0 deletions mobile/api/5/check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: check.php 36332 2016-12-30 01:44:19Z nemohou $
*/

if(!defined('IN_MOBILE_API')) {
exit('Access Denied');
}

require './source/class/class_core.php';

$discuz = C::app();
$discuz->init();

if(!defined('DISCUZ_VERSION')) {
require './source/discuz_version.php';
}

if(in_array('mobile', $_G['setting']['plugins']['available'])) {
loadcache('api_checkinfo');
if (!$_G['cache']['api_checkinfo'] || TIMESTAMP - $_G['cache']['api_checkinfo']['expiration'] > 600) {
$_G['wechat']['setting'] = unserialize($_G['setting']['mobilewechat']);
$forums = C::t('forum_forum')->fetch_all_for_grouplist('', array(), 0);
$countforums = $posts = $threads = 0;
foreach ($forums as $forum) {
$posts += $forum['posts'];
$threads += $forum['threads'];
$countforums++;
}
loadcache('userstats');
$array = array(
'discuzversion' => DISCUZ_VERSION,
'charset' => CHARSET,
'version' => MOBILE_PLUGIN_VERSION,
'pluginversion' => $_G['setting']['plugins']['version']['mobile'],
'regname' => $_G['setting']['regname'],
'sitename' => $_G['setting']['bbname'],
'ucenterurl' => $_G['setting']['ucenterurl'],
'totalthreads' => $threads,
'totalposts' => $posts,
'totalforums' => $countforums,
'totalmembers' => $_G['cache']['userstats']['totalmembers'],
);
savecache('api_checkinfo', array('variable' => $array, 'expiration' => TIMESTAMP));
} else {
$array = $_G['cache']['api_checkinfo']['variable'];
}
} else {
$array = array();
}

$array['formhash'] = formhash();
$array['setting'] = array(
'repliesrank' => $_G['setting']['repliesrank'],
'allowpostcomment' => $_G['setting']['allowpostcomment'],
);

$data = mobile_core::json($array);
mobile_core::make_cors($_SERVER['REQUEST_METHOD'], REQUEST_METHOD_DOMAIN);

echo $data;

?>
151 changes: 151 additions & 0 deletions mobile/api/5/code2session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: login.php 34314 2014-02-20 01:04:24Z nemohou $
*/

if(!defined('IN_MOBILE_API')) {
exit('Access Denied');
}


require './source/class/class_core.php';
require_once libfile('function/member');

$discuz = C::app();
$discuz->init();

$_G['config'] = array_merge($_G['config'], require './source/plugin/mobile/config_minapp.php');

$appid = $_G['config']['minapp']['appid'];
$secret = $_G['config']['minapp']['secret'];
$code = isset($_GET['code']) ? trim($_GET['code']) : '';

if(!$code || !$appid || !$secret) {
$result['code'] = -1;
$result['message'] = 'param_error';
outjson($result);
}

$wxApiurl = sprintf('https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code', $appid, $secret, $code);

$response = dfsockopen($wxApiurl);
$wxinfo = json_decode($response);
if(isset($wxinfo->errcode)) {
$result['code'] = -2;
$result['message'] = 'weixin_api_error';
outjson($result);
}


$userinfo = C::t('#mobile#weixin_minapp_user')->fetch_by_openid($wxinfo->openid);
if(!empty($userinfo)) {
C::t('#mobile#weixin_minapp_user')->update($openid, array('session_key' => $wxinfo->session_key));
$member = getuserbyuid($userinfo['uid']);
setloginstatus($member, 1296000);
} else {
// $username = 'minapp_'.strtolower(random(8));
// $uid = register($username);

$result['code'] = -3;
$result['openid'] = $wxinfo->openid;
$result['message'] = 'no_user';
outjson($result);


// C::t('#mobile#weixin_minapp_user')->insert(array('uid' => $uid, 'openid' => $wxinfo->openid, 'session_key' => $wxinfo->session_key, 'unionid' => $wxinfo->unionid));

// if(C::memory()->enable && strtolower(C::memory()->type) === 'redis') {

// $redis = new memory_driver_redis();
// $redis = $redis->instance();

// $data = json_encode(array(
// 'type' => 'newuser',
// 'username' => $username,
// 'uid' => $uid
// ));
// $redis->obj->publish('sitefeed', $data);
// }

}

$result['code'] = 0;
$result['data'] = array(
'auth' => $_G['cookie']['auth'],
'saltkey' => $_G['cookie']['saltkey'],
'formhash' => FORMHASH,
'uid' => $_G['member']['uid']
);
$result['message'] = 'login_success';
outjson($result);


function outjson($result) {
echo json_encode($result);
exit;
}

function register($username, $groupid = 0) {
global $_G;
if(!$username) {
return false;
}

loaducenter();
$groupid = !$groupid ? $_G['setting']['newusergroupid'] : $groupid;

$password = md5(random(10));
$email = 'minapp_'.strtolower(random(10)).'@null.null';
$uid = uc_user_register(addslashes($username), $password, $email, '', '', $_G['clientip']);
if($uid <= 0) {
if($uid == -3) {
$username = 'minapp_'.strtolower(random(5));
register($username);
}
return false;
}

$init_arr = array('credits' => explode(',', $_G['setting']['initcredits']));
C::t('common_member')->insert($uid, $username, $password, $email, $_G['clientip'], $groupid, $init_arr);

if($_G['setting']['regctrl'] || $_G['setting']['regfloodctrl']) {
C::t('common_regip')->delete_by_dateline($_G['timestamp']-($_G['setting']['regctrl'] > 72 ? $_G['setting']['regctrl'] : 72)*3600);
if($_G['setting']['regctrl']) {
C::t('common_regip')->insert(array('ip' => $_G['clientip'], 'count' => -1, 'dateline' => $_G['timestamp']));
}
}

if($_G['setting']['regverify'] == 2) {
C::t('common_member_validate')->insert(array(
'uid' => $uid,
'submitdate' => $_G['timestamp'],
'moddate' => 0,
'admin' => '',
'submittimes' => 1,
'status' => 0,
'message' => '',
'remark' => '',
), false, true);
manage_addnotify('verifyuser');
}

setloginstatus(array(
'uid' => $uid,
'username' => $username,
'password' => $password,
'groupid' => $groupid,
), 1296000);

include_once libfile('function/stat');
updatestat('register');


require_once libfile('cache/userstats', 'function');
build_cache_userstats();
return $uid;
}
?>
43 changes: 43 additions & 0 deletions mobile/api/5/favorite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: favorite.php 34314 2014-02-20 01:04:24Z nemohou $
*/

if(!defined('IN_MOBILE_API')) {
exit('Access Denied');
}

$_GET['mod'] = 'spacecp';
$_GET['ac'] = 'favorite';
include_once 'home.php';

class mobile_api {

function common() {
global $_G;
//删除收藏
if($_GET['op'] == 'delete') {
if($_GET['type'] == 'thread') {
$_GET['type'] = 'tid';
} elseif($_GET['type'] == 'forum') {
$_GET['type'] = 'fid';
}

$fav = C::t('home_favorite')->fetch_by_id_idtype(intval($_GET['id']), $_GET['type'], $_G['uid']);
unset($_GET['id'], $_GET['type']);
$_GET['favid'] = $fav['favid'];
}
}

function output() {
$variable = array();
mobile_core::result(mobile_core::variable($variable));
}

}

?>
59 changes: 59 additions & 0 deletions mobile/api/5/forum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: forumupload.php 35181 2015-01-08 01:51:31Z nemohou $
*/
if (!defined('IN_MOBILE_API')) {
exit('Access Denied');
}

$_GET['mod'] = 'group';
include_once 'forum.php';

class mobile_api {

function common() {
}

function output() {
global $_G;
$variable = array();
$m = isset($_GET['m']) ? trim($_GET['m']) : '';
if($m === 'grouptype') {
$variable['grouptype'] = $_G['cache']['grouptype'];
}

if(isset($_GET['createsubmit'])) {
$_G['forum'] = is_array($_G['forum']) ? $_G['forum'] : array();
$_G['forum']['fid'] = $GLOBALS['newfid'];
} elseif($_GET['op'] == 'checkuser') {
$uids = array();
foreach($GLOBALS['checkusers'] as $key => $value) {
$value['avatar'] = avatar($value['uid'], 'middle', true);
$uids[] = $value['uid'];
$GLOBALS['checkusers'][$key] = $value;
}

$usernicknames = array();
foreach(C::t('common_member_profile')->fetch_all($uids) as $user) {
$usernicknames[$user['uid']] = $user['realname'];
}
$variable['checkusers'] = $GLOBALS['checkusers'];
$variable['usernicknames'] = $usernicknames;
}


$_G['forum']['icon'] = $_G['siteurl'].$_G['forum']['icon'];

$variable['forum'] = mobile_core::getvalues($_G['forum'], array('fid', 'fup', 'name', 'description', 'icon', 'jointype', 'gviewperm'));

mobile_core::result(mobile_core::variable($variable));
}


}

?>
Loading

0 comments on commit 3f20f46

Please sign in to comment.