User.php 9.8 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2 3 4
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
D
devil_gong 已提交
5
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
D
v1.2.0  
devil_gong 已提交
6 7 8 9 10 11 12
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\api\controller;

13 14 15 16
use app\service\UserService;
use app\service\OrderService;
use app\service\GoodsService;
use app\service\MessageService;
D
v1.2.0  
devil_gong 已提交
17 18 19 20 21 22 23 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 77 78

/**
 * 用户
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class User extends Common
{
    /**
     * [__construct 构造方法]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-03T12:39:08+0800
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();
    }

    /**
     * [Reg 用户注册-数据添加]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-07T00:08:36+0800
     */
    public function Reg()
    {
        // 是否ajax请求
        if(!IS_AJAX)
        {
            return $this->error('非法访问');
        }

        // 调用服务层
        return UserService::AppReg(input('post.'));
    }

    /**
     * [RegVerifySend 用户注册-验证码发送]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-05T19:17:10+0800
     */
    public function RegVerifySend()
    {
        // 是否ajax请求
        if(!IS_AJAX)
        {
            return $this->error('非法访问');
        }

        // 调用服务层
        return UserService::AppUserBindVerifySend(input('post.'));
    }

    /**
D
1.6  
devil_gong 已提交
79
     * [GetAlipayUserInfo 支付宝用户授权]
D
v1.2.0  
devil_gong 已提交
80 81 82 83 84 85 86 87 88 89
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2017-09-23T21:52:49+0800
     */
    public function AlipayUserAuth()
    {
        // 参数
        if(empty($this->data_post['authcode']))
        {
D
1.6  
devil_gong 已提交
90
            return DataReturn('授权码为空', -1);
D
v1.2.0  
devil_gong 已提交
91 92 93
        }

        // 授权
D
1.6  
devil_gong 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        $result = (new \base\AlipayAuth())->GetAuthCode(MyC('common_app_mini_alipay_appid'), $this->data_post['authcode']);
        if($result['status'] == 0)
        {
            return DataReturn('授权登录成功', 0, $result['data']['user_id']);
        }
        return DataReturn($result['msg'], -100);
    }

    /**
     * 支付宝小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function AlipayUserInfo()
    {
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
        ];
        $ret = ParamsChecked($this->data_post, $p);
        if($ret !== true)
D
v1.2.0  
devil_gong 已提交
122
        {
D
1.6  
devil_gong 已提交
123 124 125 126 127 128 129
            return DataReturn($ret, -1);
        }

        // 先从数据库获取用户信息
        $user = UserService::UserInfo('alipay_openid', $this->data_post['openid']);
        if(empty($user))
        {
D
1.6  
devil_gong 已提交
130 131 132
            $result['nick_name'] = isset($this->data_post['nickName']) ? $this->data_post['nickName'] : '';
            $this->data_post['gender'] = empty($this->data_post['gender']) ? 0 : ($this->data_post['gender'] == 'f') ? 1 : 2;
            return UserService::AuthUserProgram($this->data_post, 'alipay_openid');
D
v1.2.0  
devil_gong 已提交
133
        } else {
D
1.6  
devil_gong 已提交
134 135
            $user['is_mandatory_bind_mobile'] = intval(MyC('common_user_is_mandatory_bind_mobile'));
            return DataReturn('授权成功', 0, $user);
D
v1.2.0  
devil_gong 已提交
136
        }
D
1.6  
devil_gong 已提交
137
        return DataReturn(empty($this->data_post) ? '获取用户信息失败' : $result, -100);
D
v1.2.0  
devil_gong 已提交
138 139
    }

G
微信  
gongfuxiang 已提交
140 141 142 143 144 145 146 147 148 149
    /**
     * 微信小程序获取用户授权
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function WechatUserAuth()
    {
G
gongfuxiang 已提交
150 151 152 153 154 155 156 157
        // 参数
        if(empty($this->data_post['authcode']))
        {
            return DataReturn('授权码为空', -1);
        }

        // 授权
        $result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->GetAuthSessionKey($this->data_post['authcode']);
G
微信  
gongfuxiang 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        if($result !== false)
        {
            return DataReturn('授权登录成功', 0, $result);
        }
        return DataReturn('授权登录失败', -100);
    }

    /**
     * 微信小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function WechatUserInfo()
    {
G
gongfuxiang 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        // 参数校验
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'openid',
                'error_msg'         => 'openid为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'encrypted_data',
                'error_msg'         => '解密数据为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'iv',
                'error_msg'         => 'iv数据为空',
            ]
        ];
        $ret = ParamsChecked($this->data_post, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }
G
微信  
gongfuxiang 已提交
198

D
devil_gong 已提交
199
        // 先从数据库获取用户信息
G
gongfuxiang 已提交
200
        $user = UserService::UserInfo('weixin_openid', $this->data_post['openid']);
D
devil_gong 已提交
201
        if(empty($user))
G
微信  
gongfuxiang 已提交
202
        {
G
gongfuxiang 已提交
203
            $result = (new \base\Wechat(MyC('common_app_mini_weixin_appid'), MyC('common_app_mini_weixin_appsecret')))->DecryptData($this->data_post['encrypted_data'], $this->data_post['iv'], $this->data_post['openid']);
D
devil_gong 已提交
204 205 206 207 208 209 210 211 212 213 214

            if(is_array($result))
            {
                $result['nick_name'] = isset($result['nickName']) ? $result['nickName'] : '';
                $result['avatar'] = isset($result['avatarUrl']) ? $result['avatarUrl'] : '';
                $result['gender'] = empty($result['gender']) ? 0 : ($result['gender'] == 2) ? 1 : 2;
                $result['openid'] = $result['openId'];
                $result['referrer']= isset($this->data_post['referrer']) ? intval($this->data_post['referrer']) : 0;
                return UserService::AuthUserProgram($result, 'weixin_openid');
            }
        } else {
D
1.6  
devil_gong 已提交
215
            $user['is_mandatory_bind_mobile'] = intval(MyC('common_user_is_mandatory_bind_mobile'));
D
devil_gong 已提交
216
            return DataReturn('授权成功', 0, $user);
G
微信  
gongfuxiang 已提交
217
        }
G
gongfuxiang 已提交
218
        return DataReturn(empty($result) ? '获取用户信息失败' : $result, -100);
G
微信  
gongfuxiang 已提交
219 220
    }

D
v1.2.0  
devil_gong 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    /**
     * 百度小程序获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function BaiduUserAuth()
    {
        return DataReturn('暂未开放', -1);

        $_POST['config'] = MyC('baidu_mini_program_config');
        $result = (new \Library\BaiduAuth())->GetAuthUserInfo($_POST);
        if($result['status'] == 0)
        {
D
1.6  
devil_gong 已提交
237
            return UserService::AuthUserProgram($result, 'baidu_openid');
D
v1.2.0  
devil_gong 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251
        }
        return DataReturn($result['msg'], -10);
    }

    /**
     * [ClientCenter 用户中心]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-05-21T15:21:52+0800
     */
    public function Center()
    {
        // 登录校验
G
gongfuxiang 已提交
252
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
253 254

        // 订单总数
D
devil_gong 已提交
255
        $where = ['user_id'=>$this->user['id'], 'is_delete_time'=>0, 'user_is_delete_time'=>0];
D
v1.2.0  
devil_gong 已提交
256 257 258 259 260 261 262 263 264 265 266
        $user_order_count = OrderService::OrderTotal($where);

        // 商品收藏总数
        $where = ['user_id'=>$this->user['id']];
        $user_goods_favor_count = GoodsService::GoodsFavorTotal($where);

        // 商品浏览总数
        $where = ['user_id'=>$this->user['id']];
        $user_goods_browse_count = GoodsService::GoodsBrowseTotal($where);

        // 未读消息总数
D
devil_gong 已提交
267
        $params = ['user'=>$this->user, 'is_more'=>1, 'is_read'=>0];
D
v1.2.0  
devil_gong 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        $common_message_total = MessageService::UserMessageTotal($params);
        $common_message_total = ($common_message_total > 99) ? '99+' : $common_message_total;

        // 用户订单状态
        $user_order_status = OrderService::OrderStatusStepTotal(['user_type'=>'user', 'user'=>$this->user, 'is_comments'=>1]);

        // 初始化数据
        $result = array(
            'integral'                          => (int) $this->user['integral'],
            'avatar'                            => $this->user['avatar'],
            'nickname'                          => $this->user['nickname'],
            'username'                          => $this->user['username'],
            'customer_service_tel'              => MyC('common_app_customer_service_tel', null, true),
            'common_user_center_notice'         => MyC('common_user_center_notice', null, true),
            'user_order_status'                 => $user_order_status['data'],
            'user_order_count'                  => $user_order_count,
            'user_goods_favor_count'            => $user_goods_favor_count,
            'user_goods_browse_count'           => $user_goods_browse_count,
            'common_message_total'              => $common_message_total,
            'common_app_is_enable_answer'       => (int) MyC('common_app_is_enable_answer', 0),
        );

        // 返回数据
        return DataReturn('success', 0, $result);
    }
}
?>