v2ex 大神多,我代码写的不好。但是这样写我感觉很舒服。让后期维护人员可能更加方便一些。尽可能的不给别人挖坑。 培训班出来的人只要严格要求自己,也会把代码写好。 对这段代码进行了简单的重构,方法还是这样,
/**
* @author coffey
*
* 文档说明 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) {
$openid = $message->FromUserName;
$userService = $app->user;
if (self::MSGTYPE_EVENT == $message->MsgType) {
$eventKeys = explode('qrscene_', $message['EventKey']);
$keyword = $eventKeys[1] ?? $message['EventKey'];
$material = Material::where('custom_name', $keyword)->first();
// 如果关注 注册用户信息
$userInfo['openid'] = $openid;
$user = $userService->get($userInfo['openid']);
switch ($message->Event) {
case self::EVENT_SUBSCRIBE:
$this->sbuscribe($user, $material, $keyword);
break;
// 取消关注事件
case self::EVENT_UNSBUSCRIBE:
return $this->unsbuscribe($userInfo);
break;
// 扫码事件
case self::EVENT_SCAN:
$this->scan($material, $openid, $keyword);
break;
// 其他事件
default:
return 'hello';
break;
}
} else {
return '你在说什么';
}
});
$response = $server->serve();
return $response;
}
/**
* @author coffey
*
* 生成微信用户数据
* @param $data
* @return mixed
*/
protected function generateWechatUserData($data)
{
$userInfo['openid'] = $data['openid'] ?? '';
$userInfo['subscribe_time'] = $data['subscribe_time'] ?? '';
$userInfo['unionid'] = $data['unionid'] ?? '';
$userInfo['nickname'] = filterEmoji($data['nickname']) ?? '';
$userInfo['avatar'] = $data['headimgurl'] ?? '';
$userInfo['sex'] = $data['sex'] ?? '';
$userInfo['province'] = $data['province'] ?? '';
$userInfo['city'] = $data['city'] ?? '';
$userInfo['country'] = $data['country'] ?? '';
$userInfo['is_subscribe'] = 1;
return $userInfo;
}
/**
* @author coffey
*
* 关注事件
*
* @param $wechatUser
* @param Material|null $material
* @param string $keyword
*
* @return bool|void
*/
protected function sbuscribe($wechatUser, Material $material = null, $keyword = '')
{
// 生成微信用户数据
$userInfo = $this->generateWechatUserData($wechatUser);
// 统一关注回复
$this->wechatMessageService->sendText($wechatUser['openid'] ?? '', env('FOLLOW_WECHAT_WELCOME_MESSAGE', '欢迎关注答题通小学版的公众号!'));
if (!empty($material)) {
$userInfo['source'] = 2;
$this->weixin_model->addDate($userInfo);
$this->materialService->pushMessage($wechatUser['openid'] ?? '', $material);
} else {
$user_info['source'] = 3;
$this->weixin_model->addDate($userInfo);
$homework = Homework::query()->find($keyword);
if (empty($homework)) {
Log::error('该作业不存在---'.$keyword);
return false;
}
$this->wechatMessageService->sendNewsItem($wechatUser['openid'] ?? '', $this->generateHomeworkData($homework));
$this->materialService->sendScanHomeworkQrCode($wechatUser['openid'] ?? '');
}
$this->materialService->sendFollow($wechatUser['openid'] ?? '', $material);
return;
}
/**
* @author coffey
*
* 扫码事件
*
* @param Material|null $material
* @param string $openId
* @param $keyword
*
* @return bool|void
*/
protected function scan(Material $material = null, string $openId = '', $keyword)
{
//带参二维码中的数值
if (!empty($material)) {
$this->materialService->pushMessage($openId, $material);
} else {
$homework = Homework::query()->find($keyword);
if (empty($homework)) {
Log::error('该作业不存在---'.$keyword);
return false;
}
$this->wechatMessageService->sendNewsItem($openId, $this->generateHomeworkData($homework));
$this->materialService->sendScanHomeworkQrCode($openId);
}
return;
}
/**
* @author coffey
*
* 取消关注事件
*
* @param array $userInfo
*
* @return string
*/
protected function unsbuscribe(array $userInfo = [])
{
$userInfo['is_subscribe'] = 0;
$this->weixin_model->addDate($userInfo);
return 'bye';
}
/**
* @author coffey
*
* 生成习题数据
*
* @param Homework $homework
*
* @return array
*/
protected function generateHomeworkData(Homework $homework = null)
{
$title = $this->newsName($homework->term, $homework->grade_id, $homework->lesson);
$url = $this->newsUrl($homework->homework_version_id, $homework->grade_id, $homework->day);
return [
'title' => $title,
'url' => $url,
'image' => getScheme('/[email protected]'),
];
}
1
jowan 2018-09-30 11:50:31 +08:00
朋友你这里是不是写错了
``` $user_info['source'] = 3; $this->weixin_model->addDate($userInfo); ``` 一会 Camel 一会 UnderScore |
4
jowan 2018-09-30 11:56:56 +08:00
sbuscribe 方法里面的 Line:10
另外 subscribe 拼写错了 |
10
msg7086 2018-09-30 12:07:52 +08:00 1
在你上一个帖子的时候我就想说了。
公司的代码是公司的财产,除非公司同意,否则任何把公司代码(即使是你为公司写的代码)就这样放出来是有严重法律问题的。公司可以以此为理由开除你并起诉你泄露公司机密(即使代码里并没有机密)。 如果只是贴出三五行代码,也就算了。你这样整段整段的贴代码,实在是…… 再还有,代码的质量还要看测试覆盖。你这些个拼写错误,没有集成测试去检查,就直接放上生产了? |
12
hexiaolu 2018-09-30 16:47:16 +08:00
代码写得再烂能跑起来, 改的再好不能跑又有什么用呢.
从来不会闲着蛋疼去改这改那,严格要求好自己的代码就行,有这时间还不如刷多几个帖子。 |
13
CuminLo 2018-09-30 17:44:47 +08:00
其实吧,我觉得没啥必要。
如果真的完全遵循框架建议的话那么楼主就不应该用: Log::error('该作业不存在---'.$keyword); Log::info 会更适合 |