这几天领导让我增加一个公众号推送消息的功能,看了下同事以前写的一套,惨不忍睹,这并非优雅的 laravel,我是个 symfonyer。见到这种代码,很想打人。所有的方法统一写在了 wechatService 里。不管有无关联
这是控制器,功能是得到微信的种种信息,然后针对恢复回复消息。自己体会吧。框架毫无意义
/**
* 微信初始化接口.
*/
public function serve(Request $request)
{
if (config('wechat.wechat_signature')) {
// 获取到微信请求里包含的几项内容
$echoStr = $_GET['echostr'];
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
// 是我在微信后台手工添加的 token 的值
$token = config('wechat.token');
// 加工出自己的 signature
$our_signature = array($token, $timestamp, $nonce);
sort($our_signature, SORT_STRING);
$our_signature = implode($our_signature);
$our_signature = sha1($our_signature);
// 用自己的 signature 去跟请求里的 signature 对比
if ($our_signature != $signature) {
return false;
} else {
echo $echoStr;
exit;
}
} else {
$response = $this->wechatService->serve();
$response->send();
}
}
这是后台对应页面,目的是根据匹配用户自定义参数针对回复
这是处理的方法:这个倒还 OK,但是为啥要写在一个好无关联的方法内???
/**
* @param $data
*
* @return object
*/
public function customQrCode($data)
{
$fileName = time();
$fileName = $this->createImgName('custom_', $fileName);
$directory = $this->createDir();
$this->checkDirectory($directory);
$url = $this->getImgContent($data['custom_name']);
$img = $directory.DIRECTORY_SEPARATOR.$fileName;
file_put_contents($img, $url);
$result = $this->uploadMaterial($img);
$data['media_id'] = $result['media_id'];
$data['remote_url'] = $result['url'];
$data['local_url'] = $img;
$data['type'] = 0;
$obj = Material::query()->create($data);
return $obj;
}
这是微信推送的方法,为啥不把素材跟消息单独封装在一个类的。写入用户信息为何不拿出来封装一下。
/**
* @author hepengfei
*
* 文档说明 3.x 版 https://www.easywechat.com/docs/3.x/zh-CN/server
*
* 微信初始化入口
*
* @return response
*/
public function serve()
{
$app = $this->app;
$server = $app->server;
$server->setMessageHandler(function($message) use($app){
// 注意,这里的 $message 不仅仅是用户发来的消息,也可能是事件
// 当 $message->MsgType 为 event 时为事件
$user_openid = $message->FromUserName ;
if ($message->MsgType == self::MSGTYPE_EVENT) {
switch ($message->Event) {
case self::EVENT_SUBSCRIBE:
# 如果关注 注册用户信息
$user_info['openid'] = $user_openid;
$userService = $app->user;
$user = $userService->get($user_info['openid']);
$user_info['subscribe_time'] = $user['subscribe_time'];
$user_info['unionid'] = $user['unionid'];
$user_info['nickname'] = filterEmoji($user['nickname']);
$user_info['avatar'] = $user['headimgurl'];
$user_info['sex'] = $user['sex'];
$user_info['province'] = $user['province'];
$user_info['city'] = $user['city'];
$user_info['country'] = $user['country'];
$user_info['is_subscribe'] = 1;
# 微信自带前缀 qrscene_ 拆分获取 homeworkid
if (empty($message['EventKey'])) {
# 搜索关注用户来源
$user_info['source'] = 1;
if ($this->weixin_model->addDate($user_info)) {
Log::info('添加用户信息成功');
} else {
Log::error('添加用户信息失败');
}
return '欢迎关注答题通小学版的公众号!';
} else if($message['EventKey'] == 'qrscene_客服') {
# 客服二维码用户来源
$user_info['source'] = 2;
if ($this->weixin_model->addDate($user_info)) {
Log::info('添加用户信息成功');
} else {
Log::error('添加用户信息失败');
}
return '答题通人工客服的工作时间为 08:30 至 20:30,如需帮助或反馈意见,欢迎在此发送消息给我们';
} else {
# 习题二维码用户来源
$user_info['source'] = 3;
$result = explode('qrscene_',$message['EventKey']);
$homework = Homework::query()->find($result[1]);
$title = $this->newsName($homework->volume,$homework->grade_id,$homework->lesson);
$url = $this->newsUrl($homework->homework_version_id,$homework->grade_id,$homework->day);
$news = new News([
'title' => $title,
'url' => $url,
'image' => getScheme($path = '/640@320.png'),
// ...
]);
if ($this->weixin_model->addDate($user_info)) {
Log::info('添加用户信息成功');
} else {
Log::error('添加用户信息失败');
}
return $news;
}
break;
case self::EVENT_UNSBUSCRIBE:
# 取消关注更改关注状态 1 关注 0 未关注
$user_info['openid'] = $user_openid;
$user_info['is_subscribe'] = 0;
$this->weixin_model->addDate($user_info);
return 'bye';
break;
case self::EVENT_SCAN:
#带参二维码中的数值
if ($message['EventKey'] == '客服') {
return '答题通人工客服的工作时间为 08:30 至 20:30,如需帮助或反馈意见,欢迎在此发送消息给我们';
} else {
$homework = Homework::query()->find($message['EventKey']);
$title = $this->newsName($homework->term,$homework->grade_id,$homework->lesson);
$url = $this->newsUrl($homework->homework_version_id,$homework->grade_id,$homework->day);
$news = new News([
'title' => $title,
'url' => $url,
'image' => getScheme($path = '/640@320.png'),
// ...
]);
return $news;
}
break;
default:
# code...
return "hello";
break;
}
} elseif ($message->MsgType == self::MSGTYPE_TEXT) {
# 发送客服消息必须实例化响应的类 Text Image News Article
$img = new Image(['media_id' => 'abIQFxsaJtMdCSbMwa_Ntj9fFvF39rl2Osa2KwiTVqI']);
$text = new Text(['content' => '我在写代码']);
# 客服发送小程序卡片
$miniprogrampage = new Raw('{"touser":"'.$user_openid.'",
"msgtype":"miniprogrampage",
"miniprogrampage":
{
"title":"小程序卡片测试",
"appid":"wx1e078baea0622aa4",
"pagepath":"pages/index/index",
"thumb_media_id":"abIQFxsaJtMdCSbMwa_Ntj9fFvF39rl2Osa2KwiTVqI"
}
}');
$app->staff->message($miniprogrampage)->to($user_openid)->send();
$app->staff->message($img)->to($user_openid)->send();
$app->staff->message($text)->to($user_openid)->send();
$news = new News([
'title' => '测试',
'description' => '...',
'url' => 'http://www.baidu.com',
'image' => getScheme($path = '/640@320.png'),
// ...
]);
$news1 = new News([
'title' => '测试 1',
'description' => '...',
'url' => 'http://www.baidu.com',
'image' => getScheme($path = '/640@320.png'),
// ...
]);
return [$news,$news1];
} elseif ($message->MsgType == self::MSGTYPE_IMAGE) {
return '图片信息';
} elseif ($message->MsgType == self::MSGTYPE_VOICE){
return '声音信息';
} elseif ($message->MsgType == self::MSGTYPE_VIDEO){
return '视频信息';
} elseif ($message->MsgType == self::MSGTYPE_LINK) {
return '链接信息';
} elseif ($message->MsgType == self::MSGTYPE_LOCATION) {
return '位置信息';
} else {
return '你在干什么';
}
});
$response = $server->serve();
return $response;
}
如果卡死在客服身上,为何要后台添加自定义参数????真是日了狗了。发发牢骚,然后自己改吧。。。。。。。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.