来一波"laravel 黑",其实是个人问题。并非为框架本身

2018-09-29 16:29:00 +08:00
 coffeygao

这几天领导让我增加一个公众号推送消息的功能,看了下同事以前写的一套,惨不忍睹,这并非优雅的 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;
    }

如果卡死在客服身上,为何要后台添加自定义参数????真是日了狗了。发发牢骚,然后自己改吧。。。。。。。

6117 次点击
所在节点    PHP
70 条回复
xiqingongzi
2018-09-29 16:36:34 +08:00
这种程序员,感觉把 rails 给他,也是写出和 shit 一样的代码....
coffeygao
2018-09-29 16:38:43 +08:00
@xiqingongzi 是的,跟他去讲这些东西也不是很清楚。自己做的东西自己都不清楚逻辑,让找个方法半天也找不到。我跟他说什么????
anyforever
2018-09-29 16:39:17 +08:00
这是人的问题,给他什么也拯救不了。。
GTim
2018-09-29 16:40:15 +08:00
楼主,第一个你吐槽的是 $_GET 吧? 最后一个,吐槽的是代码太长?
qqjt
2018-09-29 16:40:17 +08:00
easywechat 了解一下
coffeygao
2018-09-29 16:40:20 +08:00
@anyforever 对啊。框架的东西其实也没用。get post 也是用的原生
GTim
2018-09-29 16:41:12 +08:00
错了,最后一个是可以把不同类型划分为小函数的
coffeygao
2018-09-29 16:42:07 +08:00
@GTim 是的。但并非吐槽代码长。开发过程中难免会遇到长代码。但这么写,把啥功能都塞进一个方法里。着实让人头疼。
coffeygao
2018-09-29 16:42:58 +08:00
@qqjt 他已经用户了 easywechat........但是污染了 laravel & easywechat!
mokeyjay
2018-09-29 16:50:27 +08:00
想知道你出于什么想法非要把 Laravel 扯上,明明你也知道不是框架的问题
coffeygao
2018-09-29 16:51:51 +08:00
@mokeyjay 没有什么框架问题。只因他用了 laravel,所以有一层意思是说他不善用框架本身方法
097ecom
2018-09-29 16:58:07 +08:00
这锅 laravel 不背
way2create
2018-09-29 17:04:29 +08:00
问他:你在干什么?
way2create
2018-09-29 17:06:38 +08:00
个人对 laravel 比较没什么感觉,但是不太喜欢动不动写个什么都要扯优雅,也反感个别 php 基础都没学好就跟风学 laravel 的,对人, 不对框架
skdyk
2018-09-29 17:11:17 +08:00
起码还知道用 service,见过所有代码全部放在 Controller 里的吗?毫无封装的意识,上千行代码一个 action 全部撸下来,见到这种代码,入职第一天就像跑路
jowan
2018-09-29 17:11:24 +08:00
我觉得 laravel 很好用 但从来不会跟别人说:来 看我这代码 优雅不!
coffeygao
2018-09-29 17:13:05 +08:00
@097ecom 总要有个背锅侠😂
coffeygao
2018-09-29 17:14:40 +08:00
@way2create 优不优雅咱不说,哪怕代码写一万行写在一个方法里。能不能给方法说明。。。注释之类的。。
coffeygao
2018-09-29 17:15:18 +08:00
@skdyk 没办法。我爱这家公司😂。我还是耐下性子大概改动下。
coffeygao
2018-09-29 17:15:35 +08:00
@jowan 这样会被打是不

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/493900

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX