InstallController.class.php 3.44 KB
<?php
/**
 * 套件应用安装初始化数据
 * 同事圈
 * @author zhoutao
 * @date 2017-09-27 10:05:30
 */

namespace Rpc\Controller\Common;

use Common\Service\AttachmentService;
use Common\Service\CircleService;
use Common\Service\SettingService;
use VcySDK\Attach;
use VcySDK\Exception;
use VcySDK\Service;

class InstallController extends AbstractController
{
    public function Index()
    {
        $settingServ = new SettingService();
        $count = $settingServ->count();
        if (!$count) {
            // 默认配置信息
            $defaultData = \Common\Sql\DefaultData::installData();
            $settingServ->insert_all($defaultData['workmate_config_default']);

            // 安装默认数据
            $this->default_circle();
        }

        return true;
    }

    /**
     * 安装默认数据
     *
     * @return bool
     */
    private function default_circle()
    {

        $circle_serv = new CircleService();
        $attach_serv = new AttachmentService();

        // 是否已经有默认数据
        $default_data = $circle_serv->get_by_conds([]);
        if (!$default_data) {

            $at_ids = [];
            // 上传默认图片
            $attach = new Attach(Service::instance());
            try {

                $picFiles = [
                    CODE_ROOT . DIRECTORY_SEPARATOR . "www/static/img/workmate/default_1.png",
                    CODE_ROOT . DIRECTORY_SEPARATOR . "www/static/img/workmate/default_2.png",
                    CODE_ROOT . DIRECTORY_SEPARATOR . "www/static/img/workmate/default_3.png",
                ];

                foreach ($picFiles as $key => $pic) {
                    $fileData = [
                        'file' => [
                            'name' => QY_DOMAIN . '_default_'. $key .'.png',
                            'type' => mime_content_type($pic),
                            'size' => filesize($pic),
                            'tmp_name' => $pic,
                        ],
                    ];

                    $result = $attach->upload([
                        'atMediatype' => Attach::TYPE_IMG,
                    ], $fileData);

                    $at_ids[] = $result['atId'];
                }

            } catch (Exception $e) {
                \Think\Log::record('setDefaultMedal 方法内错误:::' . $e->getMessage());
            } catch (\Exception $e) {
                \Think\Log::record('setDefaultMedal 方法内错误:::' . $e->getMessage());
            }

            if (!empty($at_ids)) {
                // 写入默认数据
                $cid = $circle_serv->insert([
                    'username' => '员圈小助手',
                    'content' => '欢迎来到「员圈」社区,这是企业内部的朋友圈,企业专属的内部论坛,支持发表话题、评论、点赞;在这里可以畅所欲言,碰撞思维,交流工作心得,组织公司活动等。',
                    'is_attach' => 1,
                    'audit_state' => 1,
                    'audit_type' => 1,
                    'audit_time' => MILLI_TIME,
                ]);

                // 写入附件数据
                $attach_data = [];
                foreach ($at_ids as $at_id) {
                    $attach_data[] = [
                        'cid' => $cid,
                        'atid' => $at_id,
                    ];
                }
                $attach_serv->insert_all($attach_data);
            }
        }

        return true;
    }

}