diff --git a/application/admin/controller/Login.php b/application/admin/controller/Login.php index 024c311d8..9342820bb 100644 --- a/application/admin/controller/Login.php +++ b/application/admin/controller/Login.php @@ -14,7 +14,7 @@ namespace app\admin\controller; -use app\admin\model\Node; +use app\admin\model\Node as NodeModal; use controller\BasicAdmin; use service\LogService; use think\Db; @@ -55,7 +55,7 @@ class Login extends BasicAdmin { ($user['password'] !== md5($password)) && $this->error('登录密码与账号不匹配,请重新输入!'); Db::name('SystemUser')->where('id', $user['id'])->update(['login_at' => ['exp', 'now()'], 'login_num' => ['exp', 'login_num+1']]); session('user', $user); - !empty($user['authorize']) && Node::applyAuthNode(); + !empty($user['authorize']) && NodeModal::applyAuthNode(); LogService::write('登录系统', '用户登录系统成功!'); $this->success('登录成功,正在进入系统...', '@admin'); } diff --git a/application/admin/view/login.index.html b/application/admin/view/login.index.html index cf55ef314..b8a9ff4bf 100644 --- a/application/admin/view/login.index.html +++ b/application/admin/view/login.index.html @@ -26,7 +26,7 @@ 推荐谷歌浏览器 -
  • 楚才官网
  • +
  • 楚才官网
  • @@ -40,40 +40,44 @@ + + {if sysconf('site_copy')} {/if} + + {/block} diff --git a/application/common.php b/application/common.php index a343aa6dd..41ebb1417 100644 --- a/application/common.php +++ b/application/common.php @@ -17,7 +17,6 @@ use app\admin\model\Node as NodeModal; use think\Config; use think\Db; use Wechat\Loader; -use Wechat\WechatReceive; /** * 打印输出数据到文件 @@ -27,14 +26,14 @@ use Wechat\WechatReceive; */ function p($data, $replace = false, $pathname = NULL) { is_null($pathname) && $pathname = RUNTIME_PATH . date('Ymd') . '.txt'; - $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, TRUE) : var_export($data, TRUE)) . "\n"; + $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . "\n"; $replace ? file_put_contents($pathname, $str) : file_put_contents($pathname, $str, FILE_APPEND); } /** * 获取微信操作对象 * @param string $type - * @return WechatReceive + * @return \Wechat\WechatReceive|\Wechat\WechatUser */ function & load_wechat($type = '') { static $wechat = array(); diff --git a/application/index/view/index.index.html b/application/index/view/index.index.html deleted file mode 100644 index 205879448..000000000 --- a/application/index/view/index.index.html +++ /dev/null @@ -1,22 +0,0 @@ - - - - - 后台首页 - - -框架版本:{$Think.const.THINK_VERSION} -
    -系统类型:{:php_uname('s')} -
    -运行环境:{:php_sapi_name()} -
    -PHP版本:{:phpversion()} -
    -MySQL版本:{$mysql_ver} -
    -上传限制:{:ini_get('upload_max_filesize')} -
    -POST限制:{:ini_get('post_max_size')} - - \ No newline at end of file diff --git a/application/wechat/controller/Api.php b/application/wechat/controller/Api.php new file mode 100644 index 000000000..f8f0d5444 --- /dev/null +++ b/application/wechat/controller/Api.php @@ -0,0 +1,245 @@ + + */ +class Api extends Controller { + + /** + * 微信消息对象 + * @var \Wechat\WechatReceive + */ + protected $wechat; + + /** + * 微信openid + * @var string + */ + protected $openid; + + public function index() { + /* 实例接口对象 */ + $this->wechat = &load_wechat('Receive'); + /* 验证接口请求 */ + if ($this->wechat->valid() === FALSE) { + Log::error("微信被动接口验证失败,{$this->wechat->errMsg}[{$this->wechat->errCode}]"); + exit($this->wechat->errMsg); + } + /* 获取openid */ + $this->openid = $this->wechat->getRev()->getRevFrom(); + /* 同步粉丝数据 */ + $this->_syncFans(true); + /* 分别执行对应类型的操作 */ + switch ($this->wechat->getRev()->getRevType()) { + case WechatReceive::MSGTYPE_TEXT: + $keys = $this->wechat->getRevContent(); + exit($this->_keys("wechat_keys#keys#{$keys}")); + case WechatReceive::MSGTYPE_EVENT: + exit($this->_event()); + case WechatReceive::MSGTYPE_IMAGE: + exit($this->_image()); + case WechatReceive::MSGTYPE_LOCATION: + exit($this->_location()); + default: + exit($this->_default()); + } + } + + /** + * 关键字处理 + * @param string $keys 关键字(常规或规格关键字) + * @param bool $default 是否启用默认模式 + * @return string + */ + protected function _keys($keys, $default = FALSE) { + list($table, $field, $value) = explode('#', $keys . '##'); + $info = Db::table($table)->where('appid', $this->appid)->where($field, $value)->find(); + if ($info) { + # 转发给多客服 + if (!empty($info['type']) && $info['type'] === 'customservice') { + $this->wechat->sendCustomMessage(array('touser' => $this->openid, 'msgtype' => 'text', 'text' => array('content' => $info['content']))); + return $this->wechat->transfer_customer_service()->reply(); + } + # 无法给出回复时调用默认回复机制 + array_key_exists('status', $info) && empty($info['status']) && exit('success'); + switch ($info['type']) { + case 'keys':/* 关键字 */ + empty($info['content']) && empty($info['name']) && exit('success'); + return $this->_keys('wechat_keys#keys#' . (empty($info['content']) ? $info['name'] : $info['content'])); + case 'text':/* 文本消息 */ + empty($info['content']) && exit('success'); + return $this->wechat->text($info['content'])->reply(); + case 'news':/* 图文消息 */ + empty($info['news_id']) && exit('success'); + return $this->_news($info['news_id']); + case 'music':/* 音频消息 */ + (empty($info['music_url']) || empty($info['music_title']) || empty($info['music_desc'])) && exit('success'); + $media_id = empty($info['music_image']) ? '' : WechatService::uploadForeverMedia($info['music_image'], 'image'); + empty($media_id) && exit('success'); + return $this->wechat->music($info['music_title'], $info['music_desc'], $info['music_url'], $info['music_url'], $media_id)->reply(); + case 'voice':/* 语音消息 */ + empty($info['voice_url']) && exit('success'); + $media_id = WechatService::uploadForeverMedia($info['voice_url'], 'voice')->reply(); + empty($media_id) && exit('success'); + return $this->wechat->voice($media_id)->reply(); + case 'image':/* 图文消息 */ + empty($info['image_url']) && exit('success'); + $media_id = WechatService::uploadForeverMedia($info['image_url'], 'image'); + empty($media_id) && exit('success'); + return $this->wechat->image($media_id)->reply(); + case 'video':/* 视频消息 */ + (empty($info['video_url']) || empty($info['video_desc']) || empty($info['video_title'])) && exit('success'); + $data = array('title' => $info['video_title'], 'introduction' => $info['video_desc']); + $media_id = WechatService::uploadForeverMedia($info['video_url'], 'video', TRUE, $data); + return $this->wechat->video($media_id, $info['video_title'], $info['video_desc'])->reply(); + } + } + + $default && exit('success'); + return $this->_keys('wechat_keys#keys#default', TRUE); + } + + + /** + * 回复图文 + * @param int $news_id + * @return bool|string + */ + protected function _news($news_id = 0) { + if (is_array($newsinfo = WechatService::getNewsById($news_id))) { + $newsdata = array(); + foreach ($newsinfo['articles'] as $vo) { + $newsdata[] = [ + 'Title' => $vo['title'], + 'Description' => $vo['digest'], + 'PicUrl' => $vo['local_url'], + 'Url' => url("@wechat/view/news/id/{$vo['id']}", '', true, true) + ]; + } + return $this->wechat->news($newsdata)->reply(); + } + exit('success'); + } + + /** + * 事件处理 + */ + protected function _event() { + $event = $this->wechat->getRevEvent(); + switch (strtolower($event['event'])) { + case 'subscribe': + /* 关注事件 */ + $this->_syncFans(true); + if (!empty($event['key']) && stripos($event['key'], 'qrscene_') !== false) { + $this->_spread(preg_replace('|^.*?(\d+).*?$|', '$1', $event['key'])); + } + return $this->_keys('wechat_keys#keys#subscribe'); + case 'unsubscribe': + /* 取消关注 */ + $this->_syncFans(false); + exit('success'); + case 'click': + /* 点击链接 */ + return $this->_keys($event['key']); + case 'scancode_push': + case 'scancode_waitmsg': + /* 扫码推事件 */ + $scanInfo = $this->wechat->getRev()->getRevScanInfo(); + if (isset($scanInfo['ScanResult'])) { + return $this->_keys($scanInfo['ScanResult']); + } + exit('success'); + case 'scan': + if (!empty($event['key'])) { + return $this->_spread($event['key']); + } + exit('success'); + } + } + + /** + * 推荐好友扫码关注 + * @param string $key + * @return mixed + */ + protected function _spread($key) { + // 检测推荐是否有效 + $fans = Db::name('WechatFans')->where('id', $key)->find(); + if (!is_array($fans) || !isset($fans['openid']) || $fans['openid'] === $this->openid) { + return false; + } + // 标识推荐关系 + Db::name('WechatFans') + ->where('appid', $this->appid) + ->where('(spread_openid is null or spread_openid="")') + ->setField(['spread_by' => $fans['openid'], 'spread_at' => date('Y-m-d H:i:s')]); + // @todo 推荐成功的奖励 + } + + /** + * 位置事情回复 + * @return string + */ + protected function _location() { + exit('success'); + } + + /** + * 图片事件处理 + */ + protected function _image() { + exit('success'); + } + + /** + * 默认事件处理 + * @return string + */ + protected function _default() { + exit('success'); + } + + /** + * 同步粉丝状态 + * @param bool $subscribe 关注状态 + */ + protected function _syncFans($subscribe = true) { + if ($subscribe) { + $fans = WechatService::getFansInfo($this->openid); + if (empty($fans) || empty($fans['subscribe'])) { + $wechat = &load_wechat('User'); + $userInfo = $wechat->getUserInfo($this->openid); + $userInfo['subscribe'] = intval($subscribe); + WechatService::setFansInfo($userInfo, $this->appid); + } + } else { + $data = ['subscribe' => '0', 'appid' => $this->appid, 'openid' => $this->openid]; + DataService::save('wechat_fans', $data, ['appid', 'openid']); + } + } + +} diff --git a/public/static/admin/app.js b/public/static/admin/app.js index dac2d73e2..a3ad39904 100644 --- a/public/static/admin/app.js +++ b/public/static/admin/app.js @@ -75,6 +75,10 @@ require(['pace', 'jquery', 'layui', 'laydate', 'bootstrap', 'template', 'ueditor layui.use(['layer', 'form', 'element'], function () { window.layer = layui.layer; window.form = layui.form(); - require(['admin.listen']); + require(['admin.listen'], function () { + $('[data-loaded]').map(function () { + $(this).html($(this).attr('data-loaded')).removeClass('layui-disabled'); + }); + }); }); });