审查视图

extend/fast/service/Wechat.php 7.8 KB
Karson authored
1 2 3 4 5 6 7 8
<?php

namespace fast\service;

use app\common\model\Page;
use app\common\model\User;
use app\common\model\UserSignin;
use app\common\model\UserThird;
9
use app\common\model\WechatConfig;
Karson authored
10 11 12
use EasyWeChat\Message\News;
use EasyWeChat\Message\Transfer;
use fast\Date;
13
use think\Config;
Karson authored
14 15 16 17 18 19 20 21 22

/**
 * Wechat服务类
 */
class Wechat
{

    public function __construct()
    {
23
        
Karson authored
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    }

    public static function appConfig()
    {
        return array(
            'signin'  => array(
                'name'   => '签到送积分',
                'config' => array(
                )
            ),
            'article' => array(
                'name'   => '关联文章',
                'config' => array(
                    array(
                        'type'    => 'text',
                        'caption' => '文章ID',
                        'field'   => 'id',
                        'options' => ''
                    )
                )
            ),
            'page'    => array(
                'name'   => '关联单页',
                'config' => array(
                    array(
                        'type'    => 'text',
                        'caption' => '单页ID',
                        'field'   => 'id',
                        'options' => ''
                    )
                )
            ),
            'service' => array(
                'name'   => '在线客服',
                'config' => array(
                )
            ),
        );
    }

    // 微信输入交互内容指令
    public function command($obj, $openid, $content, $context)
    {
        $response = FALSE;
        if (isset($content['app']))
        {
            switch ($content['app'])
            {
                case 'signin':
                case 'article':
                case 'page':
                    break;
                case 'service':
77
                    $service = (array) json_decode(WechatConfig::value('service'), true);
Karson authored
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                    list($begintime, $endtime) = explode('-', $service['onlinetime']);
                    $session = $obj->app->staff_session;
                    $staff = $obj->app->staff;

                    $kf_account = $session->get($openid)->kf_account;
                    $time = time();
                    if (!$kf_account && ($time < strtotime(date("Y-m-d {$begintime}")) || $time > strtotime(date("Y-m-d {$endtime}"))))
                    {
                        return $service['offlinemsg'];
                    }
                    if (!$kf_account)
                    {
                        $kf_list = $staff->onlines()->kf_online_list;
                        if ($kf_list)
                        {
                            $kfarr = [];
                            foreach ($kf_list as $k => $v)
                            {
                                $kfarr[$v['kf_account']] = $v['accepted_case'];
                            }
                            $kfkeys = array_keys($kfarr, min($kfarr));
                            $kf_account = reset($kfkeys);
                            $session->create($kf_account, $openid);
                            $response = $service['waitformsg'];
                        }
                        else
                        {
                            $response = $service['nosessionmsg'];
                        }
                    }
                    else
                    {
                        $server = $obj->app->server;
111
                        $server->setMessageHandler(function($message) {
Karson authored
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
                            return new Transfer();
                        });
                        $response = $server->serve();
                        $response->send();
                        exit;
                    }

                    break;
                default:
                    break;
            }
        }
        else
        {
            $response = isset($content['content']) ? $content['content'] : $response;
        }
        return $response;
    }

    // 微信点击菜单event指令
    public function response($obj, $openid, $content, $context)
    {
134
        $upload = Config::get('upload');
Karson authored
135 136 137 138 139 140 141 142 143
        $response = FALSE;
        if (isset($content['app']))
        {
            switch ($content['app'])
            {
                case 'signin':
                    $thirdinfo = UserThird::get(['platform' => 'wechat', 'openid' => $openid]);
                    if (!$thirdinfo)
                    {
144
                        $response = '您还没有<a href="' . url('index/user/third', 'action=redirect&platform=wechat', true, true) . '">绑定用户</a>还不能签到!';
Karson authored
145 146 147 148 149
                    }
                    else
                    {
                        $user_id = $thirdinfo->user_id;
                        $usersign = new UserSignin;
150
                        $signdata = $usersign->where('user_id', '=', $user_id)->where('createtime', '>=', Date::unixtime())->find();
Karson authored
151 152 153 154 155 156
                        if ($signdata)
                        {
                            $response = '今天已签到,请明天再来!';
                        }
                        else
                        {
157
                            $signdata = (array) json_decode(WechatConfig::value('signin'), TRUE);
Karson authored
158
159
                            $lastdata = $usersign->where('user_id', $user_id)->order('id', 'desc')->limit(1)->find();
Karson authored
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
                            $successions = $lastdata && $lastdata['createtime'] > Date::unixtime('day', -1) ? $lastdata['successions'] + 1 : 1;
                            $usersign->save(['user_id' => $thirdinfo['user_id'], 'successions' => $successions, 'createtime' => time()]);
                            $score = isset($signdata['s' . $successions]) ? $signdata['s' . $successions] : $signdata['sn'];
                            User::where(['id' => $user_id])->setInc('score', $score);
                            $response = '签到成功!连续签到' . $successions . '天!获得' . $score . '积分';
                        }
                    }

                    break;
                case 'article':
                    $id = explode(',', $content['id']);
                    $pagelist = Page::all($id);
                    $response = [];
                    foreach ($pagelist as $k => $pageinfo)
                    {
                        if ($pageinfo)
                        {
                            $news = new News();
                            $news->title = $pageinfo['title'];
179 180
                            $news->url = $pageinfo['url'] ? $pageinfo['url'] : url('index/page/show', ['id' => $pageinfo['id']], true, true);
                            $news->image = $upload['cdnurl'] . $pageinfo['image'];
Karson authored
181 182 183 184 185 186 187 188 189 190 191 192
                            $news->description = $pageinfo['description'];
                            $response[] = $news;
                        }
                    }

                case 'page':
                    $id = isset($content['id']) ? $content['id'] : 0;
                    $pageinfo = Page::get($id);
                    if ($pageinfo)
                    {
                        $news = new News();
                        $news->title = $pageinfo['title'];
193 194
                        $news->url = $pageinfo['url'] ? $pageinfo['url'] : url('index/page/show', ['id' => $pageinfo['id']], true, true);
                        $news->image = $upload['cdnurl'] . $pageinfo['image'];
Karson authored
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
                        $news->description = $pageinfo['description'];
                        return $news;
                    }
                    break;
                case 'service':
                    $response = $this->command($obj, $openid, $content, $context);
                    break;
                default:
                    break;
            }
        }
        else
        {
            $response = isset($content['content']) ? $content['content'] : $response;
        }
        return $response;
    }

}