审查视图

app/portal/controller/IndexController.php 6.0 KB
lihan authored
1 2 3 4 5 6 7 8 9 10 11
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
12 13
use think\Db;
use app\activity\model\ActivityModel;
lihan authored
14 15
use EasyWeChat\Foundation\Application;
16 17 18 19
/**
 * @title 欢迎页
 * @description 欢迎使用在线接口文档
 */
20 21
class IndexController extends HomeBaseController
{
22
lihan authored
23 24
    function _initialize()
    {
lihan authored
25 26 27 28 29 30 31 32 33 34
        parent::_initialize(); // TODO: Change the autogenerated stub
        if (cmf_is_wechat()) {
            if (empty(session('user.id'))) {
                require_once EXTEND_PATH . '/WeChatCommon.php';
                $wx = new \WeChatCommon();
                if (request()->param('code') == NULL) {
                    $wx->code();
                } else {
                    $code = request()->param('code');
                    $info = $wx->getOpenid($code);
lihan authored
35
                    if (Db::name('user')->where(['openid' => $info['openid']])->count() == 0) {
lihan authored
36 37 38 39 40 41 42 43 44 45 46
                        //注册新用户
                        //拉去用户信息
                        $return = $wx->getUserInfo($info);
                        $data = [
                            'user_type' => 2,
                            'create_time' => time(),
                            'user_nickname' => $return['nickname'],
                            'sex' => $return['sex'],
                            'avatar' => $return['headimgurl'],
                            'openid' => $info['openid']
                        ];
lihan authored
47
                        if (Db::name('user')->insert($data)) {
lihan authored
48 49 50 51
                            $userId = Db::name('user')->getLastInsID();
                            session('user.id', $userId);
                            session('user.openid', $info['openid']);
                        }
lihan authored
52 53
                    } else {
                        $userId = Db::name('user')->where(['openid' => $info['openid']])->value('id');
lihan authored
54 55 56 57 58
                        session('user.id', $userId);
                        session('user.openid', $info['openid']);
                    }
                }
            }
lihan authored
59
        } else {
lihan authored
60
            $this->error('请从微信浏览器打开');
lihan authored
61
        }
lihan authored
62 63
    }
64
    /**
lihan authored
65
     * @title 接口返回参数说明
66
     * @description 默认访问接口
lihan authored
67
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
68
     * @url /portal/index/api
69 70 71 72 73
     * @method GET
     *
     * @return version:版本号
     * @return code:错误码
     */
74
    public function api()
75
    {
76
        $data = [
77
            'version' => '1.0.0',
78
            'code' => [
79 80 81 82 83 84 85 86
                '20000' => '默认成功返回码',
                '40000' => '默认错误返回码',
                '40001' => '未登录或登录失效',
                '40002' => '签名验证失败',
                '40003' => '缺少必要参数或参数格式错误',
                '40004' => '登录失败',
            ]
        ];
87
        echo json_encode(['msg' => $data]);
lihan authored
88
        exit();
lihan authored
89
    }
90
91
lihan authored
92 93 94 95 96
    /**
     * @title 首页
     * @description 默认访问接口
     * @author sᴏᴜʟ ᴏғ ᴄɪɴᴅᴇʀ
     * @url /portal/Index/index
97
     * @method POST
lihan authored
98 99 100 101
     *
     * @return version:版本号
     * @return code:错误码
     */
102 103
    public function index()
    {
lihan authored
104 105
        //首页轮播图
        $banner = Db::name('slide_item')->field('image,url')->where(['slide_id' => 1])->order('list_order')->select();
106 107 108 109
        foreach ($banner as $k => $v) {
            $v['image'] = cmf_get_image_url($v['image']);
            $banner[$k] = $v;
        }
lihan authored
110
        //分类
111
        $type = Db::name('type')->field('id as t_id,type_name,type_url,type_icon')->order('listorder')->select();
lihan authored
112 113 114 115 116 117 118 119 120 121 122 123
        //活动
        $model = new ActivityModel;
        $t_id = request()->param('t_id');
        $keyword = request()->param('keyword');
        $is_new = $model->activityList($t_id, time(), session('user.id'), $keyword, 1, '');
        $is_hot = $model->activityList($t_id, time(), session('user.id'), $keyword, '', 1);
        $result = [
            'banner' => $banner,
            'type' => $type,
            'is_new' => $is_new,
            'is_hot' => $is_hot
        ];
124 125 126 127 128 129 130 131
        if (request()->isPost()) {
            echo json_encode(['data' => $result, 'code' => 20000]);
            exit();
        } else {
            return $this->fetch('', [
                'result' => $result
            ]);
        }
132 133
    }
134 135 136 137 138 139
    public function bindMobile()
    {
        $request = request();
        if ($request->isAjax()) {
            $user_id = session('user.id');
            $tel = $request->param('tel');
lihan authored
140 141 142
            $is_first = Db::name('user')->where(['id' => session('user.id')])->value('is_first');
            if ($is_first == 1) {
                give_as_a_present($user_id, '首次下单赠送');
143
                Db::name('user')->update(['id' => $user_id, 'tel' => $tel, 'is_first' => 0]);
lihan authored
144
                echo json_encode(['msg' => '绑定成功']);
145 146 147 148 149 150
                //领取优惠券
                exit();
            }
        }
    }
lihan authored
151
    //更新用户经纬度
lihan authored
152 153
    public function updatePosition()
    {
lihan authored
154 155 156 157 158
        $position = [
            'id' => session('user.id'),
            'lat' => request()->param('lat'),
            'lng' => request()->param('lng')
        ];
lihan authored
159
        if (Db::name('user')->update($position)) {
lihan authored
160 161
            $this->success('', '', true);
        }
lihan authored
162 163
    }
lihan authored
164 165
    public function alipay()
    {
lihan authored
166
        $params = [
lihan authored
167 168 169 170
            'subject' => '购买',
            'out_trade_no' => '20181017256210',
            'total_amount' => 0.01,
        ];
lihan authored
171
        $return = EXTEND_PATH . \alipaymaster\extend\alipay\Wappay::pay($params);
lihan authored
172
        print_r($return);
lihan authored
173 174
    }
175
}