在联想工作一年的联想

今天公司组织了应届生 Future Leader 活动,上图是我们 TEAM 的奖品,从公交到宿舍,一直看一些人 POST 的工作感言,我也随笔用几个故事来表达我在联想工作的感悟,不喜莫喷。

1. 视野

罗老师在节目上说,一个用木叉捡粪的乞丐坐坐路边说:“他妈的,要是有一天老子当了皇帝,我这捡粪的木叉要换成金子做的。”
粉碎四人帮后,开控诉大会,一位陕北老农控诉江青:江青那臭娘们儿,床头还放俩糖罐,一个白糖罐,一个红糖罐,晚上睡觉之前还要吃一个白面馍,想蘸白糖就蘸白糖,想吃红糖就蘸红糖。
视野不是说说就能明白的,但联想的有些前辈能让你把水泼了顺便连盆子都不要。

2. 明智

春秋鲁国,有一个跟曾参同名的人杀了人,好心的领居急忙跑到曾参的母亲那里说:“曾参杀了人,你快跑吧。” 曾母回答说:“我儿不会杀人。”照常织布。没多久又有人跑来误传曾参杀了人,这时曾母害怕,就投杼逾墙逃跑了。本来没事儿,一关心,就有事儿了。

3. 大气

乐毅伐齐,站站大捷,仅剩数城难以攻克。有人向燕王告密:“乐毅这厮如此表现可能是要不听话了。” 燕王于是大摆宴席,找个好时候当着贵族百官的面把这个告密的人杀了,又告诉所有人:“燕国能壮大如此,乐毅功不可没,即便乐毅要建立国家,也是情理之中,而且燕国从此能有一个兄弟之邦照应岂不美事?” 遂下令把太子的衣服送给乐毅的孩子穿,把王后的衣服送给乐毅夫人穿,乐毅很感动….

4. 圣人行不言之教

这句话用过好几次,难以忘怀是某次 PPT 批评了别的同事,被领导猛卷。所谓圣人行不言之教就是,牛逼的人不讲话,把事做好了来影响别人,而不是通过批评职责来证明自己的牛逼。人们批评别人的动机往往不是想证明自己牛逼就是想证明自己牛逼。

5. 我没生过一个鸡蛋,但我仍然想说,这个鸡蛋不好吃

很多时候没有这个勇气,如果对方总是和稀泥:“你行你上”。我就想用这句话告诉你,我确实没办法,但是仍然要找机会告诉你,这样不好,请大哥们带我一起想想办法。

6. 我想你们

此处省略…

Headers and client library minor version mismatch.

mysqli_real_connect(): Headers and client library minor version mismatch. Headers:50547 Library:50630

If this this warning really annoys you, feel free to install php5-mysqlnd instead of php5-mysql :
it’s a drop-in replacementit’s not linked against any libmysqlclient libraryit won’t throw any irrelevant warning about version mismatchit has a lot of benefits. See http://www.php.net/mysqlnd for more info.

PHP的排错思路

结合工作经验,总结一下排错思路。

习惯

1、大部分是书写错误,要有良好的代码规范,严谨的书写风格,使用严格对比而不是松散对比。
2、注释和编写高质量的文档。

IDE

1、IDE设置提示,高亮提示等语法检查

配置

1、开发、测试环境显示所有错误 display_error
2、断点追踪 trace

扩展

3、xdebug 扩展

日志

error log

工具

1、debuger
2、debugbar
3、linter-php 语法检查
4、PHP -L $filename
5、版本控制 git, git diff

设计

1、模块化、合理解耦
2、自动化测试
3、设计模式很重要

最后

打印断点,输出语句。

Get Array from .env

/**
 * @param $name
 *
 * @return array
 */
private function getEnvArray($name)
{
    $env = env($name, null);

    if ($env === true) {
        $env = 'true';
    }

    if ($env === false) {
        $env = 'false';
    }


    if ($env === null) {
        return [];
    }

    if ($env === '') {
        return [];
    }

    return explode(',', $env);
}

Correct Way to Delete All the Records in a MySQL Table

A few days ago, I tried to delete all the records in a MySQL table, The statement caused the master-slave database to be out of sync because it works with millions of database records.

The following is the correct way to delete all records.

\DB ::statement("TRUNCATE TABLE $this->table");
// Do not use `DELETE FROM table_name`
// Do not use chunk() which selects first

$total = $request_log -> count();

$chunk = 1000;

$times = $total / $chunk;

for ($time = 0; $time <= $times; ++$time) {
    \DB ::statement("DELETE FROM $this->table LIMIT $chunk");
}

Enhanced Qiniu SDK

Installation

Require this package with composer:

composer require songshenzong/qiniu

After updating composer, add the ServiceProvider to the providers array in config/app.php

Laravel 5.x:

Songshenzong\Qiniu\ServiceProvider::class,

Put File:

$file = request() -> file('file');

return qiniu() -> auth('accessKey', 'secretKey')
               -> bucket('bucketName')
               -> domain('http://my.com/')
               -> prefix('project_name')
               -> addPrefix('dev')
               -> putFile($file);

Success Return

{
  "hash": "FrNDCStnjFNdyTZSOj0MlcW7vW4Y",
  "key": "project_name/dev/b4937112-288c-3a98-a630-f33034a28650.jpg",
  "url": "http://my.com/project_name/dev/b4937112-288c-3a98-a630-f33034a28650.jpg"
}

Use Qiniu Key

putFile($file,true);

Success Return

{
  "hash": "FrNDCStnjFNdyTZSOj0MlcW7vW4Y",
  "key": "FrNDCStnjFNdyTZSOj0MlcW7vW4Y",
  "url": "http://my.com/FrNDCStnjFNdyTZSOj0MlcW7vW4Y"
}

Put Data:

return qiniu() -> auth('accessKey', 'secretKey')
               -> bucket('bucketName')
               -> domain('http://my.com/')
               -> prefix('project_name')
               -> addPrefix('dev')
               -> put('love.txt','I Love U');

Delete Data:

return qiniu() -> auth('accessKey', 'secretKey')
               -> bucket('bucketName')
               -> delete('a8ac8401-aab8-3380-ad99-292ae4fc3236.png');

Get More…

[L5] Simple Methods for Captcha

/**
 * Get Captcha Image
 *
 * @param string $prefix_name Prefix Name
 * @param int    $width       Image Width
 * @param int    $height      Image Height
 * @param null   $font        Image Font
 */
public function getCaptchaImage($prefix_name = 'common', $width = 100, $height = 40, $font = null)
{
    $key     = $this->generateCaptchaKey($prefix_name);
    $builder = new CaptchaBuilder;
    $builder->build($width, $height, $font);
    $phrase = $builder->getPhrase();
    Redis::set($key, $phrase);
    Redis::expire($key, 100);
    header("Cache-Control: no-cache, must-revalidate");
    header('Content-Type: image/jpeg');
    return $builder->output();
}

/**
 * Get Captcha Phrase Text
 *
 * @param string $prefix_name Prefix Name
 *
 * @return mixed
 */
public function getCaptchaPhrase($prefix_name = 'common')
{
    $key = $this->generateCaptchaKey($prefix_name);
    return Redis::get($key);
}

/**
 * Delete Captcha
 *
 * @param string $prefix_name Prefix Name
 */
public function deleteCaptcha($prefix_name = 'common')
{
    $key = $this->generateCaptchaKey($prefix_name);
    return Redis::set($key, '');
}

/**
 * Generate Redis Key Name for Captcha
 *
 * @param string $prefix_name Prefix Name
 *
 * @return mixed
 */
private function generateCaptchaKey($prefix_name)
{
    return $prefix_name . ':' . request()->token ?: request()->imei_id;
}