UserService.php 59.1 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 13
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;

use think\Db;
14
use think\facade\Hook;
15
use app\service\RegionService;
D
devil_gong 已提交
16
use app\service\SafetyService;
D
v1.2.0  
devil_gong 已提交
17 18 19 20 21 22 23 24 25 26 27

/**
 * 用户服务层
 * @author   Devil
 * @blog     http://gong.gg/
 * @version  0.0.1
 * @datetime 2016-12-01T21:51:08+0800
 */
class UserService
{
    /**
D
devil_gong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
     * 获取用户登录信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-02-27
     * @desc    description
     */
    public static function LoginUserInfo()
    {
        if(APPLICATION == 'web')
        {
            return session('user');
        } else {
            $params = input();
            return empty($params['user_id']) ? null : self::UserLoginRecord($params['user_id'], true);
        }
    }
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

    /**
     * 用户状态校验
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-02-27
     * @desc    description
     * @param   [string]          $field [条件字段]
     * @param   [string]          $value [条件值]
     */
    public static function UserStatusCheck($field, $value)
    {
        // 查询用户状态是否正常
        $user = self::UserInfo($field, $value);
        if(empty($user))
        {
            return DataReturn('用户不存在或已删除', -110);
        }
        if(!in_array($user['status'], [0,1]))
        {
            $common_user_status_list = lang('common_user_status_list');
            if(isset($common_user_status_list[$user['status']]))
            {
                return DataReturn($common_user_status_list[$user['status']]['tips'], -110);
            } else {
                return DataReturn('用户状态有误', -110);
            }
        }
        return DataReturn('正常', 0);
    }

D
devil_gong 已提交
77
    /**
D
v1.2.0  
devil_gong 已提交
78 79 80 81 82 83 84
     * 用户列表
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-06T21:31:53+0800
     * @param    [array]          $params [输入参数]
     */
85
    public static function UserList($params = [])
D
v1.2.0  
devil_gong 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
    {
        $where = empty($params['where']) ? [] : $params['where'];
        $field = empty($params['field']) ? '*' : $params['field'];
        $order_by = empty($params['order_by']) ? 'id desc' : trim($params['order_by']);

        $m = isset($params['m']) ? intval($params['m']) : 0;
        $n = isset($params['n']) ? intval($params['n']) : 10;

        // 获取管理员列表
        $data = Db::name('User')->where($where)->order($order_by)->limit($m, $n)->select();
        if(!empty($data))
        {
            $common_gender_list = lang('common_gender_list');
D
devil_gong 已提交
99
            $common_user_status_list = lang('common_user_status_list');
D
v1.2.0  
devil_gong 已提交
100 101 102 103 104 105 106 107
            foreach($data as &$v)
            {
                // 生日
                $v['birthday_text'] = empty($v['birthday']) ? '' : date('Y-m-d', $v['birthday']);

                // 头像
                if(!empty($v['avatar']))
                {
108
                    $v['avatar'] = ResourcesService::AttachmentPathViewHandle($v['avatar']);
D
v1.2.0  
devil_gong 已提交
109
                } else {
110
                    $v['avatar'] = config('shopxo.attachment_host').'/static/index/'.strtolower(MyC('common_default_theme', 'default', true)).'/images/default-user-avatar.jpg';
D
v1.2.0  
devil_gong 已提交
111 112 113 114 115 116 117 118 119 120
                }

                // 注册时间
                $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);

                // 更新时间
                $v['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);

                // 性别
                $v['gender_text'] = $common_gender_list[$v['gender']]['name'];
D
devil_gong 已提交
121 122 123

                // 状态
                $v['status_text'] = $common_user_status_list[$v['status']]['name'];
D
v1.2.0  
devil_gong 已提交
124 125
            }
        }
126
        return DataReturn('处理成功', 0, $data);
D
v1.2.0  
devil_gong 已提交
127 128 129 130 131 132 133 134 135 136
    }

    /**
     * 用户列表条件
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-10T22:16:29+0800
     * @param    [array]          $params [输入参数]
     */
137
    public static function UserListWhere($params = [])
D
v1.2.0  
devil_gong 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    {
        $where = [];
        if(!empty($params['keywords']))
        {
            $where[] =['username|nickname|mobile', 'like', '%'.$params['keywords'].'%'];
        }

        // 是否更多条件
        if(isset($params['is_more']) && $params['is_more'] == 1)
        {
            // 性别
            if(isset($params['gender']) && $params['gender'] > -1)
            {
                $where[] = ['gender', '=', intval($params['gender'])];
            }

D
devil_gong 已提交
154 155 156 157 158 159
            // 状态
            if(isset($params['status']) && $params['status'] > -1)
            {
                $where[] = ['status', '=', intval($params['status'])];
            }

D
v1.2.0  
devil_gong 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            // 时间
            if(!empty($params['time_start']))
            {
                $where[] = ['add_time', '>', strtotime($params['time_start'])];
            }
            if(!empty($params['time_end']))
            {
                $where[] = ['add_time', '<', strtotime($params['time_end'])];
            }
        }
        return $where;
    }

    /**
     * 用户总数
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-10T22:16:29+0800
     * @param    [array]          $where [条件]
     */
181
    public static function UserTotal($where)
D
v1.2.0  
devil_gong 已提交
182 183 184 185 186 187 188 189 190 191 192 193
    {
        return (int) Db::name('User')->where($where)->count();
    }

    /**
     * 用户信息保存
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-10T22:16:29+0800
     * @param    [array]          $params [输入参数]
     */
194
    public static function UserSave($params = [])
D
v1.2.0  
devil_gong 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'admin',
                'error_msg'         => '用户信息有误',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'username',
                'checked_data'      => '30',
                'is_checked'        => 1,
                'error_msg'         => '用户名格式最多 30 个字符之间',
            ],
            [
                'checked_type'      => 'length',
                'key_name'          => 'nickname',
                'checked_data'      => '30',
                'is_checked'        => 1,
                'error_msg'         => '用户昵称格式最多 30 个字符之间',
            ],
            [
                'checked_type'      => 'fun',
                'key_name'          => 'mobile',
                'checked_data'      => 'CheckMobile',
                'is_checked'        => 1,
                'error_msg'         => '手机号码格式错误',
            ],
            [
                'checked_type'      => 'fun',
                'key_name'          => 'email',
                'checked_data'      => 'CheckEmail',
                'is_checked'        => 1,
                'error_msg'         => '邮箱格式错误',
            ],
            [
                'checked_type'      => 'in',
                'key_name'          => 'gender',
D
devil_gong 已提交
234
                'checked_data'      => array_column(lang('common_gender_list'), 'id'),
D
v1.2.0  
devil_gong 已提交
235 236
                'error_msg'         => '性别值范围不正确',
            ],
D
devil_gong 已提交
237 238 239 240 241 242
            [
                'checked_type'      => 'in',
                'key_name'          => 'status',
                'checked_data'      => array_column(lang('common_user_status_list'), 'id'),
                'error_msg'         => '状态值范围不正确',
            ],
D
v1.2.0  
devil_gong 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
            [
                'checked_type'      => 'length',
                'key_name'          => 'address',
                'checked_data'      => '80',
                'is_checked'        => 1,
                'error_msg'         => '地址格式最多 80 个字符之间',
            ],
            [
                'checked_type'      => 'fun',
                'key_name'          => 'pwd',
                'checked_data'      => 'CheckLoginPwd',
                'is_checked'        => 1,
                'error_msg'         => '密码格式 6~18 个字符之间',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 更新数据
        $data = [
            'username'              => isset($params['username']) ? $params['username'] :  '',
            'nickname'              => isset($params['nickname']) ? $params['nickname'] :  '',
            'mobile'                => isset($params['mobile']) ? $params['mobile'] :  '',
            'email'                 => isset($params['email']) ? $params['email'] :  '',
            'address'               => isset($params['address']) ? $params['address'] :  '',
            'gender'                => intval($params['gender']),
            'integral'              => intval($params['integral']),
D
devil_gong 已提交
273 274 275 276
            'status'                => intval($params['status']),
            'alipay_openid'         => isset($params['alipay_openid']) ? $params['alipay_openid'] :  '',
            'weixin_openid'         => isset($params['weixin_openid']) ? $params['weixin_openid'] :  '',
            'baidu_openid'          => isset($params['baidu_openid']) ? $params['baidu_openid'] :  '',
D
v1.2.0  
devil_gong 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 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 323 324 325 326 327 328 329 330 331 332 333 334
            'birthday'              => empty($params['birthday']) ? 0 : strtotime($params['birthday']),
            'upd_time'              => time(),
        ];

        // 密码
        if(!empty($params['pwd']))
        {
            $data['salt'] = GetNumberCode(6);
            $data['pwd'] = LoginPwdEncryption(trim($params['pwd']), $data['salt']);
        }

        // 更新/添加
        if(!empty($params['id']))
        {
            // 获取用户信息
            $user = Db::name('User')->field('id,integral')->find($params['id']);
            if(empty($user))
            {
                return DataReturn('用户信息不存在', -10);
            }

            $data['upd_time'] = time();
            if(Db::name('User')->where(['id'=>$params['id']])->update($data))
            {
                $user_id = $params['id'];
            }
        } else {
            $data['add_time'] = time();
            $user_id = Db::name('User')->insertGetId($data);
        }

        // 状态
        if(isset($user_id))
        {
            if(($data['integral'] > 0 && empty($user)) || (isset($user['integral']) && $user['integral'] != $data['integral']))
            {
                $integral_type = 1;
                $integral = 0;
                if(isset($user['integral']))
                {
                    $integral_type = ($user['integral'] > $data['integral']) ? 0 : 1;
                    $integral = $user['integral'];
                }
                IntegralService::UserIntegralLogAdd($user_id, $integral, $data['integral'], '管理员操作', $integral_type, $params['admin']['id']);
            }
            return DataReturn('操作成功', 0);
        }
        return DataReturn('操作失败', -100);
    }

    /**
     * 用户删除
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-10T22:16:29+0800
     * @param    [array]          $params [输入参数]
     */
335
    public static function UserDelete($params = [])
D
v1.2.0  
devil_gong 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '删除id有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }
           
        // 删除操作
        if(Db::name('User')->delete(intval($params['id'])))
        {
            return DataReturn('删除成功');
        }
        return DataReturn('删除失败或资源不存在', -100);
    }

    /**
     * 用户地址列表列表
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
368
    public static function UserAddressList($params = [])
D
v1.2.0  
devil_gong 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        $where = (!empty($params['where']) && is_array($params['where'])) ? $params['where'] : [];
        $where['user_id'] = $params['user']['id'];
        $where['is_delete_time'] = 0;

        // 获取用户地址
        $field = 'id,alias,name,tel,province,city,county,address,lng,lat,is_default';
        $data = Db::name('UserAddress')->where($where)->field($field)->order('id desc')->select();
        if(!empty($data))
        {
D
devil_gong 已提交
393
            $is_default = false;
D
v1.2.0  
devil_gong 已提交
394 395 396 397 398
            foreach($data as &$v)
            {
                $v['province_name'] = RegionService::RegionName($v['province']);
                $v['city_name'] = RegionService::RegionName($v['city']);
                $v['county_name'] = RegionService::RegionName($v['county']);
D
devil_gong 已提交
399 400 401 402 403 404 405 406

                // 是否有默认地址
                if($is_default === false && $v['is_default'] == 1)
                {
                    $is_default = true;
                }
            }

G
gongfuxiang 已提交
407 408 409
            // 是否处理默认地址,没有默认地址将第一个设置为默认地址
            $is_default_handle = isset($params['is_default_handle']) ? intval($params['is_default_handle']) : 1;
            if($is_default === false && $is_default_handle == 1)
D
devil_gong 已提交
410 411
            {
                $data[0]['is_default'] = true;
D
v1.2.0  
devil_gong 已提交
412 413 414 415 416 417 418 419 420 421 422 423 424
            }
        }
        return DataReturn('操作成功', 0, $data);
    }

    /**
     * [UserAddressRow 获取地址详情]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-23T23:19:25+0800
     * @param   [array]          $params [输入参数]
     */
425
    public static function UserAddressRow($params = [])
D
v1.2.0  
devil_gong 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '地址id不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 获取用户地址
        $params['where'] = [
            'id'    => intval($params['id']),
        ];
G
gongfuxiang 已提交
450
        $params['is_default_handle'] = 0;
451
        $ret = self::UserAddressList($params);
D
v1.2.0  
devil_gong 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
        if(!empty($ret['data'][0]))
        {
            $ret['data'] = $ret['data'][0];
        }
        return $ret;
    }

    /**
     * 用户默认地址
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-08-29
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
468
    public static function UserDefaultAddress($params = [])
D
v1.2.0  
devil_gong 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 获取用户地址
D
devil_gong 已提交
485
        $params['where'] = empty($params['where']) ? ['is_default'=>1] : $params['where'];
486
        $ret = self::UserAddressList($params);
D
v1.2.0  
devil_gong 已提交
487 488 489
        if(!empty($ret['data'][0]))
        {
            $ret['data'] = $ret['data'][0];
D
devil_gong 已提交
490 491 492 493 494 495 496 497
        } else {
            // 没有默认地址则读取第一条作为默认地址
            unset($params['where']);
            $ret = self::UserAddressList($params);
            if(!empty($ret['data'][0]))
            {
                $ret['data'] = $ret['data'][0];
            }
D
v1.2.0  
devil_gong 已提交
498 499 500 501 502 503 504 505 506 507 508 509
        }
        return $ret;
    }

    /**
     * [UserAddressSave 用户地址保存]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-23T22:28:31+0800
     * @param   [array]          $params [输入参数]
     */
510
    public static function UserAddressSave($params = [])
D
v1.2.0  
devil_gong 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'name',
                'error_msg'         => '姓名不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'tel',
                'error_msg'         => '联系电话不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'province',
                'error_msg'         => '省不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'city',
                'error_msg'         => '城市不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'county',
                'error_msg'         => '区/县不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'address',
                'error_msg'         => '详细地址不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        if(!empty($params['id']))
        {
            $where = ['user_id' => $params['user']['id'], 'id'=>$params['id']];
            $temp = Db::name('UserAddress')->where($where)->find();
        }
        
        // 操作数据
        $is_default = isset($params['is_default']) ? intval($params['is_default']) : 0;
        $data = [
            'name'          => $params['name'],
            'tel'           => $params['tel'],
            'province'      => $params['province'],
            'city'          => $params['city'],
            'county'        => $params['county'],
            'address'       => $params['address'],
            'is_default'    => $is_default,
        ];
        if(!empty($params['alias']))
        {
G
gongfuxiang 已提交
575
            $data['alias'] = $params['alias'];
D
v1.2.0  
devil_gong 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
        }
        if(!empty($params['lng']))
        {
            $data['lng'] = floatval($params['lng']);
        }
        if(!empty($params['lat']))
        {
            $data['lat'] = floatval($params['lat']);
        }

        Db::startTrans();

        // 默认地址处理
        if($is_default == 1)
        {
            Db::name('UserAddress')->where(['user_id'=>$params['user']['id'], 'is_default'=>1])->update(['is_default'=>0]);
        }

        // 添加/更新数据
        if(empty($temp))
        {
            $data['user_id'] = $params['user']['id'];
            $data['add_time'] = time();
            if(Db::name('UserAddress')->insertGetId($data) > 0)
            {
                Db::commit();
                return DataReturn('新增成功', 0);
            } else {
                Db::rollback();
                return DataReturn('新增失败');
            }
        } else {
            $data['upd_time'] = time();
            if(Db::name('UserAddress')->where($where)->update($data))
            {
                Db::commit();
                return DataReturn('更新成功', 0);
            } else {
                Db::rollback();
                return DataReturn('更新失败');
            }
        }
    }

    /**
     * [UserAddressDelete 用户地址删除]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  1.0.0
     * @datetime 2018-09-23T23:55:51+0800
     * @param   [array]          $params [输入参数]
     */
628
    public static function UserAddressDelete($params = [])
D
v1.2.0  
devil_gong 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '地址id不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 软删除数据
        $where = ['user_id' => $params['user']['id'], 'id'=>$params['id']];
        $data = ['is_delete_time' => time()];
        if(Db::name('UserAddress')->where($where)->update($data))
        {
            return DataReturn('删除成功', 0);
        } else {
            return DataReturn('删除失败或资源不存在', -100);
        }
    }

    /**
     * 用户地址设置默认地址
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-09-25
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
669
    public static function UserAddressDefault($params = [])
D
v1.2.0  
devil_gong 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'id',
                'error_msg'         => '地址id不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 开启事务
        Db::startTrans();

        // 先全部设置为0 再将当前设置为1
        $all_status = Db::name('UserAddress')->where(['user_id' => $params['user']['id']])->update(['is_default'=>0]);
        $my_status = Db::name('UserAddress')->where(['user_id' => $params['user']['id'], 'id'=>$params['id']])->update(['is_default'=>1]);
        if($all_status !== false && $my_status)
        {
            // 提交事务
            Db::commit();
            return DataReturn('设置成功', 0);
        } else {
            // 回滚事务
            Db::rollback();
            return DataReturn('设置失败', -100);
        }
    }

    /**
     * [UserLoginRecord 用户登录记录]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-09T11:37:43+0800
     * @param    [int]     $user_id [用户id]
     * @param    [boolean] $is_app  [是否为app]
     * @return   [boolean]          [记录成功true, 失败false]
     */
718
    public static function UserLoginRecord($user_id = 0, $is_app = false)
D
v1.2.0  
devil_gong 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    {
        if(!empty($user_id))
        {
            $user = Db::name('User')->field('*')->find($user_id);
            if(!empty($user))
            {
                // 基础数据处理
                $user['add_time_text']  =   date('Y-m-d H:i:s', $user['add_time']);
                $user['upd_time_text']  =   date('Y-m-d H:i:s', $user['upd_time']);
                $user['gender_text']    =   lang('common_gender_list')[$user['gender']]['name'];
                $user['birthday_text']  =   empty($user['birthday']) ? '' : date('Y-m-d', $user['birthday']);
                $user['mobile_security']=   empty($user['mobile']) ? '' : substr($user['mobile'], 0, 3).'***'.substr($user['mobile'], -3);
                $user['email_security'] =   empty($user['email']) ? '' : substr($user['email'], 0, 3).'***'.substr($user['email'], -3);

                // 显示名称,根据规则优先展示
                $user['user_name_view'] = $user['username'];
                if(empty($user['user_name_view']))
                {
                    $user['user_name_view'] = $user['nickname'];
                }
                if(empty($user['user_name_view']))
                {
                    $user['user_name_view'] = $user['mobile_security'];
                }
                if(empty($user['user_name_view']))
                {
                    $user['user_name_view'] = $user['email_security'];
                }

                // 头像
                if(!empty($user['avatar']))
                {
751
                    $user['avatar'] = ResourcesService::AttachmentPathViewHandle($user['avatar']);
D
v1.2.0  
devil_gong 已提交
752
                } else {
753
                    $user['avatar'] = config('shopxo.attachment_host').'/static/index/'.strtolower(config('DEFAULT_THEME', 'default')).'/images/default-user-avatar.jpg';
D
v1.2.0  
devil_gong 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
                }

                if($is_app == true)
                {
                    return $user;
                } else {
                    // 存储session
                    session('user', $user);
                    return (session('user') !== null);
                }
            }
        }
        return false;
    }

    /**
     * 用户头像更新
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-10-16
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
778
    public static function UserAvatarUpload($params = [])
D
v1.2.0  
devil_gong 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
    {
        // 请求参数
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'img_width',
                'error_msg'         => '图片宽度不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'img_height',
                'error_msg'         => '图片高度不能为空',
            ],
            [
                'checked_type'      => 'isset',
                'key_name'          => 'img_x',
                'error_msg'         => '图片裁剪x坐标有误',
            ],
            [
                'checked_type'      => 'isset',
                'key_name'          => 'img_y',
                'error_msg'         => '图片裁剪y坐标有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'img_field',
                'error_msg'         => '图片name字段值不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 开始处理图片存储
        // 定义图片目录
G
gongfuxiang 已提交
821
        $root_path = ROOT.'public'.DS;
G
gongfuxiang 已提交
822
        $img_path = 'static'.DS.'upload'.DS.'images'.DS.'user_avatar'.DS;
D
v1.2.0  
devil_gong 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
        $date = DS.date('Y').DS.date('m').DS.date('d').DS;

        // 图像类库
        $images_obj = \base\Images::Instance(['is_new_name'=>false]);

        // 文件上传校验
        $error = FileUploadError($params['img_field']);
        if($error !== true)
        {
            return DataReturn($error, -2);
        }

        $original = $images_obj->GetCompressCut($_FILES[$params['img_field']], $root_path.$img_path.'original'.$date, 800, 800, $params['img_x'], $params['img_y'], $params['img_width'], $params['img_height']);
        if(!empty($original))
        {
            $compr = $images_obj->GetBinaryCompress($root_path.$img_path.'original'.$date.$original, $root_path.$img_path.'compr'.$date, 200, 200);
            $small = $images_obj->GetBinaryCompress($root_path.$img_path.'original'.$date.$original, $root_path.$img_path.'small'.$date, 50, 50);
        }
        if(empty($compr) || empty($small))
        {
            return DataReturn('图片有误,请换一张', -3);
        }

        // 更新用户头像
        $data = [
            'avatar'    => DS.$img_path.'compr'.$date.$compr,
            'upd_time'  => time(),
        ];
        if(Db::name('User')->where(['id'=>$params['user']['id']])->update($data))
        {
853
            self::UserLoginRecord($params['user']['id']);
D
v1.2.0  
devil_gong 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867
            return DataReturn('上传成功', 0);
        }
        return DataReturn('上传失败', -100);
    }

    /**
     * 用户登录
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-03
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
868
    public static function Login($params = [])
D
v1.2.0  
devil_gong 已提交
869 870 871 872 873 874 875 876
    {
        // 是否开启用户登录
        if(MyC('home_user_login_state') != 1)
        {
            return DataReturn('暂时关闭用户登录', -1);
        }

        // 登录帐号格式校验
D
devil_gong 已提交
877
        if(empty($params['accounts']))
D
v1.2.0  
devil_gong 已提交
878
        {
D
devil_gong 已提交
879
            return DataReturn('登录账号有误', -1);
D
v1.2.0  
devil_gong 已提交
880 881 882 883 884 885 886 887 888 889
        }

        // 密码
        $pwd = trim($params['pwd']);
        if(!CheckLoginPwd($pwd))
        {
            return DataReturn('密码格式 6~18 个字符之间', -2);
        }

        // 获取用户账户信息
D
devil_gong 已提交
890
        $where = array('username|mobile|email' => $params['accounts'], 'is_delete_time'=>0);
891
        $user = Db::name('User')->field('id,pwd,salt,status')->where($where)->find();
D
v1.2.0  
devil_gong 已提交
892 893 894 895
        if(empty($user))
        {
            return DataReturn('帐号不存在', -3);
        }
D
devil_gong 已提交
896

D
v1.2.0  
devil_gong 已提交
897
        // 用户状态
D
devil_gong 已提交
898
        if(in_array($user['status'], [2,3]))
D
v1.2.0  
devil_gong 已提交
899 900 901 902 903 904 905 906 907 908
        {
            return DataReturn(lang('common_user_status_list')[$user['status']]['tips'], -10);
        }

        // 密码校验
        if(LoginPwdEncryption($pwd, $user['salt']) != $user['pwd'])
        {
            return DataReturn('密码错误', -4);
        }

909
        // 用户登录前钩子
910 911 912 913 914 915 916
        $hook_name = 'plugins_service_user_login_begin';
        $ret = Hook::listen($hook_name, [
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'params'        => &$params,
            'user_id'       => $user['id']
        ]);
917 918 919 920 921
        if(isset($ret['code']) && $ret['code'] != 0)
        {
            return $ret;
        }

D
v1.2.0  
devil_gong 已提交
922 923 924 925 926 927 928
        // 更新用户密码
        $salt = GetNumberCode(6);
        $data = array(
                'pwd'       =>  LoginPwdEncryption($pwd, $salt),
                'salt'      =>  $salt,
                'upd_time'  =>  time(),
            );
D
devil_gong 已提交
929
        if(Db::name('User')->where(['id'=>$user['id']])->update($data) !== false)
D
v1.2.0  
devil_gong 已提交
930 931
        {
            // 登录记录
932
            if(self::UserLoginRecord($user['id']))
D
v1.2.0  
devil_gong 已提交
933
            {
D
devil_gong 已提交
934 935 936
                // 返回前端html代码
                $body_html = [];

937
                // 用户登录后钩子
938 939 940 941 942
                $hook_name = 'plugins_service_user_login_end';
                $ret = Hook::listen($hook_name, [
                    'hook_name'     => $hook_name,
                    'is_backend'    => true,
                    'params'        => &$params,
D
devil_gong 已提交
943 944 945
                    'user_id'       => $user['id'],
                    'user'          => Db::name('User')->field('id,username,nickname,mobile,email,gender,avatar,province,city,birthday')->where(['id'=>$user['id']])->find(),
                    'body_html'     => &$body_html,
946
                ]);
947 948 949 950 951
                if(isset($ret['code']) && $ret['code'] != 0)
                {
                    return $ret;
                }

D
devil_gong 已提交
952 953 954 955 956
                // 登录返回
                $result = [
                    'body_html'    => is_array($body_html) ? implode(' ', $body_html) : $body_html,
                ];
                return DataReturn('登录成功', 0, $result);
D
v1.2.0  
devil_gong 已提交
957 958 959 960 961 962 963 964 965 966 967 968 969 970
            }
        }
        return DataReturn('登录失效,请重新登录', -100);
    }

    /**
     * 用户注册
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-03
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
971
    public static function Reg($params = [])
D
v1.2.0  
devil_gong 已提交
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    {
        // 数据验证
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'accounts',
                'error_msg'         => '账号不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'pwd',
                'error_msg'         => '密码不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'type',
                'error_msg'         => '注册类型有误',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'verify',
                'error_msg'         => '验证码不能为空',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 是否开启用户注册
        if(!in_array($params['type'], MyC('home_user_reg_state')))
        {
            return DataReturn('暂时关闭用户注册', -1);
        }

        // 账户校验
1009
        $ret = self::UserRegAccountsCheck($params);
D
v1.2.0  
devil_gong 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 验证码校验
        $verify_param = array(
                'key_prefix' => 'reg',
                'expire_time' => MyC('common_verify_expire_time')
            );
        if($params['type'] == 'sms')
        {
            $obj = new \base\Sms($verify_param);
        } else {
            $obj = new \base\Email($verify_param);
        }
        // 是否已过期
        if(!$obj->CheckExpire())
        {
            return DataReturn('验证码已过期', -10);
        }
        // 是否正确
        if(!$obj->CheckCorrect($params['verify']))
        {
            return DataReturn('验证码错误', -11);
        }

D
devil_gong 已提交
1037 1038 1039 1040
        // 是否需要审核
        $common_register_is_enable_audit = MyC('common_register_is_enable_audit', 0);

        // 用户数据
D
v1.2.0  
devil_gong 已提交
1041 1042 1043 1044 1045 1046
        $salt = GetNumberCode(6);
        $data = [
            'add_time'      => time(),
            'upd_time'      => time(),
            'salt'          => $salt,
            'pwd'           => LoginPwdEncryption($params['pwd'], $salt),
D
devil_gong 已提交
1047
            'status'        => ($common_register_is_enable_audit == 1) ? 3 : 0,
D
v1.2.0  
devil_gong 已提交
1048 1049 1050 1051 1052 1053 1054 1055 1056
        ];
        if($params['type'] == 'sms')
        {
            $data['mobile'] = $params['accounts'];
        } else {
            $data['email'] = $params['accounts'];
        }

        // 数据添加
D
devil_gong 已提交
1057 1058
        $user_ret = self::UserInsert($data, $params);
        if($user_ret['code'] == 0)
D
v1.2.0  
devil_gong 已提交
1059 1060 1061 1062
        {
            // 清除验证码
            $obj->Remove();

D
devil_gong 已提交
1063 1064 1065 1066 1067 1068 1069
            // 是否需要审核
            if($common_register_is_enable_audit == 1)
            {
                return DataReturn('注册成功,请等待审核');
            }

            // 用户登录session纪录
D
devil_gong 已提交
1070
            if(self::UserLoginRecord($user_ret['data']['user_id']))
D
v1.2.0  
devil_gong 已提交
1071
            {
D
devil_gong 已提交
1072
                return DataReturn('注册成功', 0, $user_ret);
D
v1.2.0  
devil_gong 已提交
1073 1074
            }
            return DataReturn('注册成功,请到登录页面登录帐号');
D
devil_gong 已提交
1075 1076
        } else {
            return $user_ret;
D
v1.2.0  
devil_gong 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
        }
        return DataReturn('注册失败', -100);
    }

    /**
     * 用户注册账户校验
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-10T10:06:29+0800
     * @param   [array]          $params [输入参数]
     */
1089
    private static function UserRegAccountsCheck($params = [])
D
v1.2.0  
devil_gong 已提交
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
    {
        // 参数
        $type = $params['type'];
        $accounts = $params['accounts'];
        if(empty($accounts) || empty($type) || !in_array($type, array('sms', 'email')))
        {
             return DataReturn('参数错误', -1);
        }

        // 手机号码
        if($type == 'sms')
        {
            // 手机号码格式
            if(!CheckMobile($accounts))
            {
                 return DataReturn('手机号码格式错误', -2);
            }

            // 手机号码是否已存在
1109
            if(self::IsExistAccounts($accounts, 'mobile'))
D
v1.2.0  
devil_gong 已提交
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
            {
                 return DataReturn('手机号码已存在', -3);
            }

        // 电子邮箱
        } else {
            // 电子邮箱格式
            if(!CheckEmail($accounts))
            {
                 return DataReturn('电子邮箱格式错误', -2);
            }

            // 电子邮箱是否已存在
1123
            if(self::IsExistAccounts($accounts, 'email'))
D
v1.2.0  
devil_gong 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
            {
                 return DataReturn('电子邮箱已存在', -3);
            }
        }
        return DataReturn('操作成功', 0);
    }

    /**
     * 账户是否存在
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-08T10:27:14+0800
     * @param    [string] $accounts     [账户名称]
     * @param    [string] $field        [字段名称]
     * @return   [boolean]              [存在true, 不存在false]
     */
1141
    private static function IsExistAccounts($accounts, $field = 'mobile')
D
v1.2.0  
devil_gong 已提交
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    {
        $id = Db::name('User')->where(array($field=>$accounts))->value('id');
        return !empty($id);
    }

    /**
     * 是否开启图片验证码校验
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-22T15:48:31+0800
     * @param    [array]    $params         [输入参数]
     * @param    [array]    $verify_params  [配置参数]
     * @return   [object]                   [图片验证码类对象]
     */
1157
    private static function IsImaVerify($params, $verify_params)
D
v1.2.0  
devil_gong 已提交
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    {
        if(MyC('home_img_verify_state') == 1)
        {
            if(empty($params['verify']))
            {
                return DataReturn('参数错误', -10);
            }
            $verify = new \base\Verify($verify_params);
            if(!$verify->CheckExpire())
            {
                return DataReturn('验证码已过期', -11);
            }
            if(!$verify->CheckCorrect($params['verify']))
            {
                return DataReturn('验证码错误', -12);
            }
            return DataReturn('操作成功', 0, $verify);
        }
        return DataReturn('操作成功', 0);
    }

    /**
     * 用户注册-验证码发送
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-10T10:06:29+0800
     * @param   [array]          $params [输入参数]
     */
1187
    public static function RegVerifySend($params = [])
D
v1.2.0  
devil_gong 已提交
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
    {
        // 数据验证
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'accounts',
                'error_msg'         => '账号不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'type',
                'error_msg'         => '注册类型有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 是否开启用户注册
        if(!in_array($params['type'], MyC('home_user_reg_state')))
        {
            return DataReturn('暂时关闭用户注册');
        }

        // 账户校验
1215
        $ret = self::UserRegAccountsCheck($params);
D
v1.2.0  
devil_gong 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 验证码公共基础参数
        $verify_params = array(
                'key_prefix' => 'reg',
                'expire_time' => MyC('common_verify_expire_time'),
                'time_interval' =>  MyC('common_verify_time_interval'),
            );

        // 是否开启图片验证码
1229
        $verify = self::IsImaVerify($params, $verify_params);
D
v1.2.0  
devil_gong 已提交
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
        if($verify['code'] != 0)
        {
            return $verify;
        }

        // 发送验证码
        $code = GetNumberCode(6);
        if($params['type'] == 'sms')
        {
            $obj = new \base\Sms($verify_params);
            $status = $obj->SendCode($params['accounts'], $code, MyC('home_sms_user_reg'));
        } else {
            $obj = new \base\Email($verify_params);
            $email_param = array(
                    'email'     =>  $params['accounts'],
                    'content'   =>  MyC('home_email_user_reg'),
                    'title'     =>  MyC('home_site_name').' - 用户注册',
                    'code'      =>  $code,
                );
            $status = $obj->SendHtml($email_param);
        }
        
        // 状态
        if($status)
        {
            // 清除验证码
            if(isset($verify['data']) && is_object($verify['data']))
            {
                $verify['data']->Remove();
            }

            return DataReturn('发送成功', 0);
        } else {
            return DataReturn('发送失败'.'['.$obj->error.']', -100);
        }
    }

    /**
     * 密码找回验证码发送
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-10T17:35:03+0800
     * @param   [array]          $params [输入参数]
     */
1275
    public static function ForgetPwdVerifySend($params = [])
D
v1.2.0  
devil_gong 已提交
1276 1277 1278 1279 1280 1281 1282 1283
    {
        // 参数
        if(empty($params['accounts']))
        {
            return DataReturn('参数错误', -10);
        }

        // 账户是否存在
1284
        $ret = self::UserForgetAccountsCheck($params['accounts']);
D
v1.2.0  
devil_gong 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 验证码公共基础参数
        $verify_params = array(
                'key_prefix' => 'forget',
                'expire_time' => MyC('common_verify_expire_time'),
                'time_interval' =>  MyC('common_verify_time_interval'),
            );

        // 是否开启图片验证码
1298
        $verify = self::IsImaVerify($params, $verify_params);
D
v1.2.0  
devil_gong 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
        if($verify['code'] != 0)
        {
            return $verify;
        }

        // 验证码
        $code = GetNumberCode(6);

        // 手机
        if($ret['data'] == 'mobile')
        {
            $obj = new \base\Sms($verify_params);
            $status = $obj->SendCode($params['accounts'], $code, MyC('home_sms_user_forget_pwd'));

        // 邮箱
        } else if($ret['data'] == 'email')
        {
            $obj = new \base\Email($verify_params);
            $email_param = array(
                    'email'     =>  $params['accounts'],
                    'content'   =>  MyC('home_email_user_forget_pwd'),
                    'title'     =>  MyC('home_site_name').' - '.'密码找回',
                    'code'      =>  $code,
                );
            $status = $obj->SendHtml($email_param);
        } else {
            return DataReturn('手机/邮箱格式有误', -1);
        }

        // 状态
        if($status)
        {
            // 清除验证码
            if(isset($verify['data']) && is_object($verify['data']))
            {
                $verify['data']->Remove();
            }

            return DataReturn('发送成功', 0);
        } else {
            return DataReturn('发送失败'.'['.$obj->error.']', -100);
        }
    }

    /**
     * [UserForgetAccountsCheck 帐号校验]
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-10T17:59:53+0800
     * @param    [string]     $accounts [账户名称]
     * @return   [string]               [账户字段 mobile, email]
     */
1352
    private static function UserForgetAccountsCheck($accounts)
D
v1.2.0  
devil_gong 已提交
1353 1354 1355
    {
        if(CheckMobile($accounts))
        {
1356
            if(!self::IsExistAccounts($accounts, 'mobile'))
D
v1.2.0  
devil_gong 已提交
1357 1358 1359 1360 1361 1362
            {
                return DataReturn('手机号码不存在', -3);
            }
            return DataReturn('操作成功', 0, 'mobile');
        } else if(CheckEmail($accounts))
        {
1363
            if(!self::IsExistAccounts($accounts, 'email'))
D
v1.2.0  
devil_gong 已提交
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
            {
                return DataReturn('电子邮箱不存在', -3);
            }
            return DataReturn('操作成功', 0, 'email');
        }
        return DataReturn('手机/邮箱格式有误', -4);
    }

    /**
     * 密码找回
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2017-03-10T17:35:03+0800
     * @param   [array]          $params [输入参数]
     */
1380
    public static function ForgetPwd($params = [])
D
v1.2.0  
devil_gong 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
    {
        // 数据验证
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'accounts',
                'error_msg'         => '账号不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'pwd',
                'error_msg'         => '密码不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'verify',
                'error_msg'         => '验证码不能为空',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 账户是否存在
1407
        $ret = self::UserForgetAccountsCheck($params['accounts']);
D
v1.2.0  
devil_gong 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
        if($ret['code'] != 0)
        {
            return $ret;
        }

        // 验证码校验
        $verify_params = array(
                'key_prefix' => 'forget',
                'expire_time' => MyC('common_verify_expire_time'),
                'time_interval' =>  MyC('common_verify_time_interval'),
            );
        if($ret['data'] == 'mobile')
        {
            $obj = new \base\Sms($verify_params);
        } else if($ret['data'] == 'email')
        {
            $obj = new \base\Email($verify_params);
        }
        // 是否已过期
        if(!$obj->CheckExpire())
        {
            return DataReturn('验证码已过期', -10);
        }
        // 是否正确
        if(!$obj->CheckCorrect($params['verify']))
        {
            return DataReturn('验证码错误', -11);
        }

D
devil_gong 已提交
1437 1438 1439
        // 获取用户信息
        $user = Db::name('User')->where([$ret['data']=>$params['accounts']])->find();
        if(empty($user))
D
v1.2.0  
devil_gong 已提交
1440
        {
D
devil_gong 已提交
1441
            return DataReturn('用户信息不存在', -12);
D
v1.2.0  
devil_gong 已提交
1442
        }
D
devil_gong 已提交
1443 1444 1445 1446 1447 1448 1449 1450

        // 密码修改
        $ret = SafetyService::UserLoginPwdUpdate($params['accounts'], $user['id'], $params['pwd']);
        if($ret['code'] != 0)
        {
            return DataReturn('操作成功', 0);
        }
        return $ret;
D
v1.2.0  
devil_gong 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
    }

    /**
     * 用户资料保存
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-04
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
1462
    public static function PersonalSave($params = [])
D
v1.2.0  
devil_gong 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
    {
        // 数据验证
        $p = [
            [
                'checked_type'      => 'length',
                'checked_data'      => '2,16',
                'key_name'          => 'nickname',
                'error_msg'         => '昵称 2~16 个字符之间',
            ],
            [
1473
                'checked_type'      => 'isset',
D
v1.2.0  
devil_gong 已提交
1474 1475 1476 1477
                'key_name'          => 'birthday',
                'error_msg'         => '请填写生日',
            ],
            [
1478 1479
                'checked_type'      => 'in',
                'checked_data'      => [0,1,2],
D
v1.2.0  
devil_gong 已提交
1480
                'key_name'          => 'gender',
1481
                'error_msg'         => '性别选择有误',
D
v1.2.0  
devil_gong 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'user',
                'error_msg'         => '用户信息有误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 更新数据库
        $data = [
1497
            'birthday'      => empty($params['birthday']) ? '' : strtotime($params['birthday']),
D
v1.2.0  
devil_gong 已提交
1498 1499 1500 1501 1502 1503 1504
            'nickname'      => $params['nickname'],
            'gender'        => intval($params['gender']),
            'upd_time'      => time(),
        ];
        if(Db::name('User')->where(array('id'=>$params['user']['id']))->update($data))
        {
            // 更新用户session数据
1505
            self::UserLoginRecord($params['user']['id']);
D
v1.2.0  
devil_gong 已提交
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521

            return DataReturn('编辑成功', 0);
        }
        return DataReturn('编辑失败或数据未改变', -100);
    }

    /**
     * 用户授权数据
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-11-06
     * @desc    description
     * @param   [array]          $params    [用户数据]
     * @param   [string]         $field     [平台字段名称]
     */
1522
    public static function AuthUserProgram($params, $field)
D
v1.2.0  
devil_gong 已提交
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
    {
        $data = [
            $field              => $params['openid'],
            'nickname'          => empty($params['nick_name']) ? '' : $params['nick_name'],
            'avatar'            => empty($params['avatar']) ? '' : $params['avatar'],
            'gender'            => empty($params['gender']) ? 0 : ($params['gender'] == 'm') ? 2 : 1,
            'province'          => empty($params['province']) ? '' : $params['province'],
            'city'              => empty($params['city']) ? '' : $params['city'],
            'referrer'          => isset($params['referrer']) ? intval($params['referrer']) : 0,
        ];
D
devil_gong 已提交
1533
        $user = self::UserInfo($field, $params['openid']);
D
v1.2.0  
devil_gong 已提交
1534 1535 1536 1537 1538 1539 1540 1541 1542
        if(!empty($user))
        {
            $data = $user;
        }

        // 返回成功
        return DataReturn('授权成功', 0, $data);
    }

D
devil_gong 已提交
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
    /**
     * 根据字段获取用户信息
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-01-25
     * @desc    description
     * @param   [string]          $field [字段名称]
     * @param   [string]          $value [字段值]
     */
    public static function UserInfo($field, $value)
    {
G
gongfuxiang 已提交
1555 1556 1557 1558 1559
        if(empty($field) || empty($value))
        {
            return '';
        }
        
D
devil_gong 已提交
1560 1561 1562
        return Db::name('User')->where([$field=>$value, 'is_delete_time'=>0])->find();
    }

D
devil_gong 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    /**
     * 用户添加
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2019-04-03
     * @desc    description
     * @param   [array]          $data   [用户添加数据]
     * @param   [array]          $params [输入参数]
     */
    public static function UserInsert($data, $params = [])
    {
        // 账号是否存在,以用户名 手机 邮箱 作为唯一
        if(!empty($data['username']))
        {
            $temp = Db::name('User')->where(['username'=>$data['username'], 'is_delete_time'=>0])->find();
        } else if(!empty($data['mobile']))
        {
            $temp = Db::name('User')->where(['mobile'=>$data['mobile'], 'is_delete_time'=>0])->find();
        } else if(!empty($data['email']))
        {
            $temp = Db::name('User')->where(['email'=>$data['email'], 'is_delete_time'=>0])->find();
        }
        if(!empty($temp))
        {
            return DataReturn('账号已存在', -10);
        }

        $user_id = Db::name('User')->insertGetId($data);
        if($user_id > 0)
        {
            // 返回前端html代码
            $body_html = [];

            // 注册成功后钩子
            $hook_name = 'plugins_service_user_register_end';
            $ret = Hook::listen($hook_name, [
                'hook_name'     => $hook_name,
                'is_backend'    => true,
                'params'        => &$params,
                'user_id'       => $user_id,
                'user'          => Db::name('User')->field('id,username,nickname,mobile,email,gender,avatar,province,city,birthday')->where(['id'=>$user_id])->find(),
                'body_html'     => &$body_html,
            ]);
            if(isset($ret['code']) && $ret['code'] != 0)
            {
                return $ret;
            }

            // 登录返回
            $result = [
                'body_html'     => is_array($body_html) ? implode(' ', $body_html) : $body_html,
                'user_id'       => $user_id,
            ];

            return DataReturn('添加成功', 0, $result);
        }
        return DataReturn('添加失败', -100);
    }

D
v1.2.0  
devil_gong 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631
    /**
     * app用户注册
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-27
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
1632
    public static function AppReg($params = [])
D
v1.2.0  
devil_gong 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    {
        // 数据验证
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'mobile',
                'error_msg'         => '手机号码不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'verify',
                'error_msg'         => '验证码不能为空',
            ],
            [
                'checked_type'      => 'empty',
                'key_name'          => 'app_type',
                'error_msg'         => '终端用户类型不能为空',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 手机号码格式
        if(!CheckMobile($params['mobile']))
        {
             return DataReturn('手机号码格式错误', -2);
        }

        // 验证码校验
        $verify_param = array(
                'key_prefix' => 'bind',
                'expire_time' => MyC('common_verify_expire_time')
            );
        $obj = new \base\Sms($verify_param);

        // 是否已过期
        if(!$obj->CheckExpire())
        {
            return DataReturn('验证码已过期', -10);
        }
        // 是否正确
        if(!$obj->CheckCorrect($params['verify']))
        {
            return DataReturn('验证码错误', -11);
        }

        // 用户信息
        $accounts_field = $params['app_type'].'_openid';
        if(empty($params[$accounts_field]))
        {
            return DataReturn('用户openid不能为空', -20);
        }
D
devil_gong 已提交
1688 1689 1690 1691 1692

        // 是否需要审核
        $common_register_is_enable_audit = MyC('common_register_is_enable_audit', 0);

        // 用户数据
D
v1.2.0  
devil_gong 已提交
1693 1694 1695
        $data = array(
            $accounts_field     => $params[$accounts_field],
            'mobile'            => $params['mobile'],
D
devil_gong 已提交
1696
            'status'            => ($common_register_is_enable_audit == 1) ? 3 : 0,
D
v1.2.0  
devil_gong 已提交
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
        );

        // 获取用户信息
        $where = ['mobile'=>$data['mobile'], 'is_delete_time'=>0];
        $temp_user = Db::name('User')->where($where)->find();

        // 额外信息
        if(empty($temp_user['nickname']) && !empty($params['nickname']))
        {
            $data['nickname'] = $params['nickname'];
        }
        if(empty($temp_user['avatar']) && !empty($params['avatar']))
        {
            $data['avatar'] = $params['avatar'];
        }
        if(empty($temp_user['province']) && !empty($params['province']))
        {
            $data['province'] = $params['province'];
        }
        if(empty($temp_user['city']) && !empty($params['city']))
        {
            $data['city'] = $params['city'];
        }
        if(empty($temp_user) && isset($params['gender']))
        {
            $data['gender'] = intval($params['gender']);
        }

        // 不存在添加/则更新
        if(empty($temp_user))
        {
            $data['referrer'] = isset($params['referrer']) ? intval($params['referrer']) : 0;
            $data['add_time'] = time();
D
devil_gong 已提交
1730 1731 1732 1733 1734 1735 1736
            $user_ret = self::UserInsert($data, $params);
            if($user_ret['code'] == 0)
            {
                $user_id = $user_ret['data']['user_id'];
            } else {
                return $user_ret;
            }
D
v1.2.0  
devil_gong 已提交
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749
        } else {
            $data['upd_time'] = time();
            if(Db::name('User')->where($where)->update($data))
            {
                $user_id = $temp_user['id'];
            }
        }
        
        if(isset($user_id) && $user_id > 0)
        {
            // 清除验证码
            $obj->Remove();

1750
            return DataReturn('绑定成功', 0, self::UserLoginRecord($user_id, true));
D
v1.2.0  
devil_gong 已提交
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
        } else {
            return DataReturn('绑定失败', -100);
        }
    }

    /**
     * app用户绑定验证码发送
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-12-27
     * @desc    description
     * @param   [array]          $params [输入参数]
     */
1765
    public static function AppUserBindVerifySend($params = [])
D
v1.2.0  
devil_gong 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
    {
        // 数据验证
        $p = [
            [
                'checked_type'      => 'empty',
                'key_name'          => 'mobile',
                'error_msg'         => '手机号码不能为空',
            ],
            [
                'checked_type'      => 'fun',
                'key_name'          => 'mobile',
                'checked_data'      => 'CheckMobile',
                'error_msg'         => '手机号码格式错误',
            ],
        ];
        $ret = ParamsChecked($params, $p);
        if($ret !== true)
        {
            return DataReturn($ret, -1);
        }

        // 验证码公共基础参数
        $verify_param = array(
                'key_prefix' => 'bind',
                'expire_time' => MyC('common_verify_expire_time'),
                'time_interval' => MyC('common_verify_time_interval'),
            );

        // 发送验证码
        $obj = new \base\Sms($verify_param);
        $code = GetNumberCode(6);
        $status = $obj->SendCode($params['mobile'], $code, MyC('home_sms_user_mobile_binding'));
        
        // 状态
        if($status)
        {
            return DataReturn('发送成功', 0);
        } else {
            return DataReturn('发送失败'.'['.$obj->error.']', -100);
        }
    }

D
devil_gong 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
    /**
     * 用户退出
     * @author   Devil
     * @blog     http://gong.gg/
     * @version  0.0.1
     * @datetime 2016-12-05T14:31:23+0800
     * @param   [array]          $params [输入参数]
     */
    public static function Logout($params = [])
    {
        // 用户信息
        $user = self::LoginUserInfo();

        // 清除session
        session('user', null);

        // html代码
        $body_html = [];

        // 用户退出钩子
        $hook_name = 'plugins_service_user_logout_handle';
        $ret = Hook::listen($hook_name, [
            'hook_name'     => $hook_name,
            'is_backend'    => true,
            'params'        => [],
            'user_id'       => isset($user['id']) ? $user['id'] : 0,
            'user'          => $user,
            'body_html'     => &$body_html,
        ]);

        // 数据返回
        $result = [
            'body_html'    => is_array($body_html) ? implode(' ', $body_html) : $body_html,
        ];

        return DataReturn('退出成功', 0, $result);
    }

D
v1.2.0  
devil_gong 已提交
1846 1847
}
?>