User.php 10.7 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        $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);




        // // 参数
        // if(empty($this->data_post['authcode']))
        // {
        //     return DataReturn('授权码不能为空', -1);
        // }

        // // 授权
        // $ret = (new \base\AlipayAuth())->GetAuthCode($this->data_post['authcode'], MyC('common_app_mini_alipay_appid'));
        // if($ret['status'] != 0)
        // {
        //     return DataReturn($ret['msg'], -10);
        // } else {
        //     $data = $ret['data'];
        //     $data['gender'] = empty($data['gender']) ? 0 : ($data['gender'] == 'm') ? 2 : 1;
        //     $data['openid'] = $data['user_id'];
        //     $data['referrer']= isset($this->data_post['referrer']) ? $this->data_post['referrer'] : 0;
        //     return UserService::AuthUserProgram($data, 'alipay_openid');
        // }
    }

    /**
     * 支付宝小程序获取用户信息
     * @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为空',
            ],
D
1.6  
devil_gong 已提交
141 142 143 144 145
            // [
            //     'checked_type'      => 'empty',
            //     'key_name'          => 'userinfo',
            //     'error_msg'         => '用户数据为空',
            // ],
D
1.6  
devil_gong 已提交
146 147 148
        ];
        $ret = ParamsChecked($this->data_post, $p);
        if($ret !== true)
D
v1.2.0  
devil_gong 已提交
149
        {
D
1.6  
devil_gong 已提交
150 151 152 153 154 155 156 157 158 159 160
            return DataReturn($ret, -1);
        }

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

G
微信  
gongfuxiang 已提交
168 169 170 171 172 173 174 175 176 177
    /**
     * 微信小程序获取用户授权
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     */
    public function WechatUserAuth()
    {
G
gongfuxiang 已提交
178 179 180 181 182 183 184 185
        // 参数
        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 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        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 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        // 参数校验
        $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 已提交
226

D
devil_gong 已提交
227
        // 先从数据库获取用户信息
G
gongfuxiang 已提交
228
        $user = UserService::UserInfo('weixin_openid', $this->data_post['openid']);
D
devil_gong 已提交
229
        if(empty($user))
G
微信  
gongfuxiang 已提交
230
        {
G
gongfuxiang 已提交
231
            $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 已提交
232 233 234 235 236 237 238 239 240 241 242

            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 已提交
243
            $user['is_mandatory_bind_mobile'] = intval(MyC('common_user_is_mandatory_bind_mobile'));
D
devil_gong 已提交
244
            return DataReturn('授权成功', 0, $user);
G
微信  
gongfuxiang 已提交
245
        }
G
gongfuxiang 已提交
246
        return DataReturn(empty($result) ? '获取用户信息失败' : $result, -100);
G
微信  
gongfuxiang 已提交
247 248
    }

D
v1.2.0  
devil_gong 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    /**
     * 百度小程序获取用户信息
     * @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 已提交
265
            return UserService::AuthUserProgram($result, 'baidu_openid');
D
v1.2.0  
devil_gong 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279
        }
        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 已提交
280
        $this->IsLogin();
D
v1.2.0  
devil_gong 已提交
281 282

        // 订单总数
D
devil_gong 已提交
283
        $where = ['user_id'=>$this->user['id'], 'is_delete_time'=>0, 'user_is_delete_time'=>0];
D
v1.2.0  
devil_gong 已提交
284 285 286 287 288 289 290 291 292 293 294
        $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 已提交
295
        $params = ['user'=>$this->user, 'is_more'=>1, 'is_read'=>0];
D
v1.2.0  
devil_gong 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        $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);
    }
}
?>