MemberModel.php 2.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/8/9
 * Time: 11:01
 */

namespace app\member\model;

use think\Model;
use think\Db;
use think\Session;

class MemberModel extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * 编辑用户
     */
    public function editMember($data) {
        $this->allowField(true)->data($data,true)->isUpdate(true)->save();
        return $this;
    }

    public function doMobile($user)
    {
        $userQuery = Db::name("Member");

        $result = $userQuery->where('mobile', $user['mobile'])->find();

        if (!empty($result)) {
            $hookParam =[
                'user'=>$user
            ];
            hook_one("user_login_start",$hookParam);
            $code_where = [
                'mobile'  => $user['mobile'],
                'code'    => $user['code'],
                'is_used' => 0
            ];
            $code_check = Db::name('Code')->where($code_where)->find();
            if($code_check) {
                Db::name('Code')->where($code_where)->update(['is_used'=>1]);
                session('user', $result);
                session('baby_age','');
                $data = [
                    'last_login_time' => time(),
                    'last_login_ip'   => get_client_ip(0, true),
                ];
                $userQuery->where('id', $result["id"])->update($data);
                return 0;
            }
            return 1;
        }
        $hookParam =[
            'user'=>$user
        ];
        hook_one("user_login_start",$hookParam);
        return 2;
    }

    public function registerMobile($user)
    {
        $result = Db::name("Member")->where('mobile', $user['mobile'])->find();

        if (empty($result)) {
            $data   = [
                'mobile'            => $user['mobile'],
                'baby_name'         => $user['baby_name'],
                'baby_birth'        => $user['baby_birth'],
                'parent_name'       => $user['parent_name'],
                'parent_status'     => $user['parent_status'],
                'is_member'         => $user['is_member'],
//                'user_pass'       => cmf_password($user['user_pass']),
                'last_login_ip'     => get_client_ip(0, true),
                'create_time'       => time(),
                'last_login_time'   => time(),
//                'user_status'     => $userStatus,
//                "user_type"       => 2,//会员
            ];
            // 是否为渠道注册
            $channel_id = Session::pull('channel');
            if(!empty($channel_id)) {
                $data['channel_id'] = $channel_id;
            }
            $userId = Db::name("Member")->insertGetId($data);
            $data   = Db::name("Member")->where('id', $userId)->find();
            cmf_update_current_user($data);
            session('baby_age',null);
            return 0;
        }
        return 1;
    }
}