mirror of
				https://gitee.com/zoujingli/ThinkAdmin.git
				synced 2025-10-27 02:12:08 +08:00 
			
		
		
		
	[更新]微信模块开发中
This commit is contained in:
		
							parent
							
								
									c0c119f365
								
							
						
					
					
						commit
						eac1022a3e
					
				| @ -14,7 +14,7 @@ | |||||||
| 
 | 
 | ||||||
| namespace app\admin\controller; | namespace app\admin\controller; | ||||||
| 
 | 
 | ||||||
| use app\admin\model\Node; | use app\admin\model\Node as NodeModal; | ||||||
| use controller\BasicAdmin; | use controller\BasicAdmin; | ||||||
| use service\LogService; | use service\LogService; | ||||||
| use think\Db; | use think\Db; | ||||||
| @ -55,7 +55,7 @@ class Login extends BasicAdmin { | |||||||
|             ($user['password'] !== md5($password)) && $this->error('登录密码与账号不匹配,请重新输入!'); |             ($user['password'] !== md5($password)) && $this->error('登录密码与账号不匹配,请重新输入!'); | ||||||
|             Db::name('SystemUser')->where('id', $user['id'])->update(['login_at' => ['exp', 'now()'], 'login_num' => ['exp', 'login_num+1']]); |             Db::name('SystemUser')->where('id', $user['id'])->update(['login_at' => ['exp', 'now()'], 'login_num' => ['exp', 'login_num+1']]); | ||||||
|             session('user', $user); |             session('user', $user); | ||||||
|             !empty($user['authorize']) && Node::applyAuthNode(); |             !empty($user['authorize']) && NodeModal::applyAuthNode(); | ||||||
|             LogService::write('登录系统', '用户登录系统成功!'); |             LogService::write('登录系统', '用户登录系统成功!'); | ||||||
|             $this->success('登录成功,正在进入系统...', '@admin'); |             $this->success('登录成功,正在进入系统...', '@admin'); | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -26,7 +26,7 @@ | |||||||
|                     <b style="color:#fff">推荐谷歌浏览器</b> |                     <b style="color:#fff">推荐谷歌浏览器</b> | ||||||
|                 </a> |                 </a> | ||||||
|             </li> |             </li> | ||||||
|             <li><a href="http://www.cuci.cc">楚才官网</a></li> |             <li><a target="_blank" href="http://www.cuci.cc">楚才官网</a></li> | ||||||
|         </ul> |         </ul> | ||||||
|     </div> |     </div> | ||||||
| 
 | 
 | ||||||
| @ -40,40 +40,44 @@ | |||||||
|             </div> |             </div> | ||||||
|             <ul> |             <ul> | ||||||
|                 <li> |                 <li> | ||||||
|                     <input name='username' style='display:none' /> |                     <input name='username' style='display:none'/> | ||||||
|                     <input required="required" |                     <input required="required" | ||||||
|                            pattern="^\S{4,}$" |                            pattern="^\S{4,}$" | ||||||
|                            title="请输入4位及以上的字符" |                            title="请输入4位及以上的字符" | ||||||
|                            type="text" |                            type="text" | ||||||
|                            name="username" |                            name="username" | ||||||
|                            class="login-input username" |                            value="admin" | ||||||
|                            autocomplete="off" |                            autocomplete="off" | ||||||
|                            autofocus="autofocus" |                            autofocus="autofocus" | ||||||
|                            value="admin" |                            class="login-input username" | ||||||
|                            placeholder="请输入用户名/手机号码"/> |                            placeholder="请输入用户名/手机号码"/> | ||||||
|                 </li> |                 </li> | ||||||
|                 <li> |                 <li> | ||||||
|                     <input name='password' style='display:none' /> |                     <input name='password' style='display:none'/> | ||||||
|                     <input required="required" |                     <input required="required" | ||||||
|                            pattern="^\S{4,}$" |                            pattern="^\S{4,}$" | ||||||
|                            title="请输入4位及以上的字符" |                            title="请输入4位及以上的字符" | ||||||
|                            type="password" |                            type="password" | ||||||
|                            name="password" |                            name="password" | ||||||
|                            class="login-input password" |  | ||||||
|                            autocomplete="off" |  | ||||||
|                            value="admin" |                            value="admin" | ||||||
|  |                            autocomplete="off" | ||||||
|  |                            class="login-input password" | ||||||
|                            placeholder="请输入密码"/> |                            placeholder="请输入密码"/> | ||||||
|                 </li> |                 </li> | ||||||
|                 <li class="text-center"> |                 <li class="text-center"> | ||||||
|                     <button type="submit" class="layui-btn layui-btn">立 即 登 入</button> |                     <button type="submit" class="layui-btn layui-disabled" data-loaded="立 即 登 入">正 在 载 入</button> | ||||||
|                     <a style="position:absolute;display:block;right:0" href="javascript:void(0)">忘记密码?</a> |                     <a style="position:absolute;display:block;right:0" href="javascript:void(0)">忘记密码?</a> | ||||||
|                 </li> |                 </li> | ||||||
|             </ul> |             </ul> | ||||||
|         </form> |         </form> | ||||||
|     </div> |     </div> | ||||||
|  | 
 | ||||||
|  |     <!-- 底部版权信息 开始 --> | ||||||
|     {if sysconf('site_copy')} |     {if sysconf('site_copy')} | ||||||
|     <div class="footer">{:sysconf('site_copy')}</div> |     <div class="footer">{:sysconf('site_copy')}</div> | ||||||
|     {/if} |     {/if} | ||||||
|  |     <!-- 底部版本信息 结束 --> | ||||||
|  | 
 | ||||||
| </div> | </div> | ||||||
| {/block} | {/block} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -17,7 +17,6 @@ use app\admin\model\Node as NodeModal; | |||||||
| use think\Config; | use think\Config; | ||||||
| use think\Db; | use think\Db; | ||||||
| use Wechat\Loader; | use Wechat\Loader; | ||||||
| use Wechat\WechatReceive; |  | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 打印输出数据到文件 |  * 打印输出数据到文件 | ||||||
| @ -27,14 +26,14 @@ use Wechat\WechatReceive; | |||||||
|  */ |  */ | ||||||
| function p($data, $replace = false, $pathname = NULL) { | function p($data, $replace = false, $pathname = NULL) { | ||||||
|     is_null($pathname) && $pathname = RUNTIME_PATH . date('Ymd') . '.txt'; |     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); |     $replace ? file_put_contents($pathname, $str) : file_put_contents($pathname, $str, FILE_APPEND); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 获取微信操作对象 |  * 获取微信操作对象 | ||||||
|  * @param string $type |  * @param string $type | ||||||
|  * @return WechatReceive |  * @return \Wechat\WechatReceive|\Wechat\WechatUser | ||||||
|  */ |  */ | ||||||
| function & load_wechat($type = '') { | function & load_wechat($type = '') { | ||||||
|     static $wechat = array(); |     static $wechat = array(); | ||||||
|  | |||||||
| @ -1,22 +0,0 @@ | |||||||
| <!DOCTYPE html> |  | ||||||
| <html lang="en"> |  | ||||||
| <head> |  | ||||||
|     <meta charset="UTF-8"> |  | ||||||
|     <title>后台首页</title> |  | ||||||
| </head> |  | ||||||
| <body> |  | ||||||
| 框架版本:{$Think.const.THINK_VERSION} |  | ||||||
| <br/> |  | ||||||
| 系统类型:{:php_uname('s')} |  | ||||||
| <br/> |  | ||||||
| 运行环境:{:php_sapi_name()} |  | ||||||
| <br/> |  | ||||||
| PHP版本:{:phpversion()} |  | ||||||
| <br/> |  | ||||||
| MySQL版本:{$mysql_ver} |  | ||||||
| <br/> |  | ||||||
| 上传限制:{:ini_get('upload_max_filesize')} |  | ||||||
| <br/> |  | ||||||
| POST限制:{:ini_get('post_max_size')} |  | ||||||
| </body> |  | ||||||
| </html> |  | ||||||
							
								
								
									
										245
									
								
								application/wechat/controller/Api.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										245
									
								
								application/wechat/controller/Api.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,245 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | // +----------------------------------------------------------------------
 | ||||||
|  | // | Think.Admin
 | ||||||
|  | // +----------------------------------------------------------------------
 | ||||||
|  | // | 版权所有 2016~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
 | ||||||
|  | // +----------------------------------------------------------------------
 | ||||||
|  | // | 官方网站: http://think.ctolog.com
 | ||||||
|  | // +----------------------------------------------------------------------
 | ||||||
|  | // | 开源协议 ( https://mit-license.org )
 | ||||||
|  | // +----------------------------------------------------------------------
 | ||||||
|  | // | github开源项目:https://github.com/zoujingli/Think.Admin
 | ||||||
|  | // +----------------------------------------------------------------------
 | ||||||
|  | 
 | ||||||
|  | namespace app\wechat\controller; | ||||||
|  | 
 | ||||||
|  | use service\DataService; | ||||||
|  | use service\WechatService; | ||||||
|  | use Wechat\WechatReceive; | ||||||
|  | use think\Controller; | ||||||
|  | use think\Db; | ||||||
|  | use think\Log; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 微信接口控制器 | ||||||
|  |  * Class Api | ||||||
|  |  * @package app\wechat\controller | ||||||
|  |  * @author Anyon <zoujingli@qq.com> | ||||||
|  |  */ | ||||||
|  | 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']); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -75,6 +75,10 @@ require(['pace', 'jquery', 'layui', 'laydate', 'bootstrap', 'template', 'ueditor | |||||||
|     layui.use(['layer', 'form', 'element'], function () { |     layui.use(['layer', 'form', 'element'], function () { | ||||||
|         window.layer = layui.layer; |         window.layer = layui.layer; | ||||||
|         window.form = layui.form(); |         window.form = layui.form(); | ||||||
|         require(['admin.listen']); |         require(['admin.listen'], function () { | ||||||
|  |             $('[data-loaded]').map(function () { | ||||||
|  |                 $(this).html($(this).attr('data-loaded')).removeClass('layui-disabled'); | ||||||
|  |             }); | ||||||
|  |         }); | ||||||
|     }); |     }); | ||||||
| }); | }); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user