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('/640@320.png'),
];
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.