customer_id > 0) { $this->redirect(url('index/customer/personal')); } $url = $this->request->get('url'); $url = $url != '' ? $url : ''; $this->assign('url', $url); return $this->fetch(); } public function update_headimg() { if ($this->customer_id <= 0) { return $this->json(-1, '数据错误'); } $result = upload_headimg('headimg'); if ($result['code'] < 0) { return $this->json(-2, $result['msg']); } $headimg = $result['data']; $result = model('customer')->where(['id' => $this->customer_id])->update(['picture' => $headimg]); if (!$result) { return $this->json(-3, '修改失败'); } $customer_info = model('customer')->getBasicInfo($this->customer_id); $this->set_login_token($customer_info); return $this->json(200, '修改成功'); } # 用旧密码改新密码 public function update_pwd() { $data = $this->request->post(); if (empty($data) || $this->customer_id <= 0) { return $this->json(-1, '数据错误'); } if ($this->customer_info['have_pwd']) { $customer_info = model('customer')->where(['id' => $this->customer_id])->find(); if (md5($data['old_password']) != $customer_info['password']) { return $this->json(-2, '旧密码错误'); } } $update_data = [ 'password' => md5($data['password']), 'salt' => $data['password'] ]; $result = model('customer')->where(['id' => $this->customer_id])->update($update_data); if (!$result) { return $this->json(-4, '修改密码失败'); } $customer_info = model('customer')->getBasicInfo($this->customer_id); $this->set_login_token($customer_info); return $this->json(200, '修改密码成功'); } # 用手机号/邮箱改密码 public function update_forget_pwd() { $data = $this->request->post(); if (empty($data)) { return $this->json(-1, '数据错误'); } if (isset($data['telephone'])) { $flag = 1; if (!preg_match("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/", $data['telephone'])) { return $this->json(-2, '手机号格式错误'); } $captcha = $this->cacheGet('regtel' . $data['telephone'], 'error'); if ($captcha != $data['captcha']) { return $this->json(-3, '验证码错误'); } } else { return $this->json(-100, '邮箱暂不可用'); $flag = 2; if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $data['email'])) { return $this->json(-4, '邮箱格式错误'); } $captcha = $this->cacheGet('regemail' . $data['email'], 'error'); if ($captcha != $data['captcha']) { return $this->json(-5, '验证码错误'); } } if (!preg_match("/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&*-.]+$)[a-zA-Z\d!@#$%^&*-.]{8,20}$/", $data['password'])) { return $this->json(-6, '密码必须包含8-20个字符,且至少包含两种类型字符'); } if ($flag == 1) { $customer_info = model('customer')->getBasicInfoByTelephone($data['telephone']); if (empty($customer_info)) { return $this->json(-7, '该手机号未注册'); } $update_data = [ 'password' => md5($data['password']), 'salt' => $data['password'] ]; $result = model('customer')->where(['telephone' => $data['telephone']])->update($update_data); } else { $customer_info = model('customer')->getBasicInfoByEmail($data['email']); if (empty($customer_info)) { return $this->json(-8, '该邮箱未注册'); } $update_data = [ 'password' => md5($data['password']), 'salt' => $data['password'] ]; $result = model('customer')->where(['email' => $data['email']])->update($update_data); } if (!$result) { return $this->json(-9, '修改密码失败,稍后再试'); } $this->_logout(); return $this->json(200, '修改密码成功'); } public function bind_email() { $data = $this->request->post(); // tiaoshi($data);die; if (empty($data) || $this->customer_id <= 0) { return $this->json(-1, '数据错误'); } if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $data['email'])) { return $this->json(-2, '邮箱格式错误'); } $customer_info = model('customer')->where(['email' => $data['email']])->find(); if (!empty($customer_info)) { return $this->json(-3, '邮箱已被使用'); } $update_data = [ 'email' => $data['email'] ]; $result = model('customer')->where(['id' => $this->customer_id])->update($update_data); if (!$result) { return $this->json(-3, '绑定邮箱失败,请稍后再试'); } $customer_info = model('customer')->getBasicInfo($this->customer_id); $this->set_login_token($customer_info); return $this->json(200, '绑定邮箱成功'); } # 旧手机号改新手机号 public function update_tel() { $data = $this->request->post(); // tiaoshi($data);die; if (empty($data) || $this->customer_id <= 0) { return $this->json(-1, '数据错误'); } $customer_info = model('customer')->where(['id' => $this->customer_id])->find(); if ($customer_info['telephone'] != $data['old_telephone']) { return $this->json(-2, '旧手机号错误'); } $validate = Loader::validate('customer'); if (!$validate->scene('update_tel')->check($data)) { return $this->json(-3, $validate->getError()); } $captcha = $this->cacheGet('regtel' . $data['new_telephone'], 'error'); if ($captcha != $data['captcha']) { return $this->json(-4, '验证码错误'); } $update_data = [ 'telephone' => $data['new_telephone'] ]; $result = model('customer')->where(['id' => $this->customer_id])->update($update_data); if (!$result) { return $this->json(-5, '修改失败'); } $new_customer_info = model('customer')->getBasicInfo($this->customer_id); $this->set_login_token($new_customer_info); return $this->json(200, '修改成功'); } public function new_register() { if ($this->customer_id > 0) return $this->json(-10001, '已经登录过'); $data = $this->request->post(); if (empty($data)) { return $this->json(-1, '数据错误'); } $validate = Loader::validate('customer'); if (!$validate->scene('register_by_telephone')->check($data)) { return $this->json(-2, $validate->getError()); } $captcha = $this->cacheGet('regtel' . $data['telephone'], 'error'); if ($captcha != $data['captcha']) { return $this->json(-4, '验证码错误'); } $firstname = 'ORICO' . rand(10000000, 99999999); $insert_data = [ 'firstname' => $firstname, 'telephone' => $data['telephone'], 'addtime' => time() ]; $customer_id = model('customer')->insertGetId($insert_data); if (!$customer_id) { return $this->json(-100, '注册失败'); } $customer_info = model('customer')->getBasicInfo($customer_id); $this->set_login_token($customer_info); return $this->json(200, '注册成功'); } public function new_login() { if ($this->customer_id > 0) return $this->json(-10001, '已经登录过'); $data = $this->request->post(); if (empty($data)) { return $this->json(-1, '数据错误'); } if (isset($data['password'])) { // 密码登录 if (preg_match("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/", $data['username'])) { $where = ['telephone' => $data['username']]; } elseif (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $data['username'])) { $where = ['email' => $data['username']]; } else { return $this->json(-2, '账号格式错误'); } $where['stat'] = 0; $customer_info = model('customer')->where($where)->field('id, firstname, picture, sex, email, telephone, qq, birthday, password')->find(); if (empty($customer_info)) { return $this->json(-3, '账号未注册'); } if ($customer_info['password'] != md5($data['password'])) { return $this->json(-4, '账号或密码错误'); } } else { // 短信验证码登录 $captcha = $this->cacheGet('regtel' . $data['telephone'], 'error'); if ($captcha != $data['captcha']) { return $this->json(-5, '验证码错误'); } $customer_info = model('customer')->getBasicInfoByTelephone($data['telephone']); if (empty($customer_info)) { // 未注册直接入库 $firstname = 'ORICO' . rand(10000000, 99999999); $insert_data = [ 'firstname' => $firstname, 'telephone' => $data['telephone'], ]; $customer_id = model('customer')->insertGetId($insert_data); if (!$customer_id) { return $this->json(-6, '登录失败'); } $customer_info = model('customer')->getBasicInfoByTelephone($data['telephone']); } } $this->set_login_token($customer_info); return $this->json(200, '登录成功'); } /** * 前台用户登录 * @param string $username 前台用户名 * @param string $password 密码 * @param string $verify 验证码 */ public function login() { if ($this->customer_id) { return $this->redirect(url('index/customer/index')); } $this->request->isPost() || $this->error(Lang::get('illegal request')); //判断是否ajax登录 $data = $this->request->post(); if (empty($data) || !is_array($data)) { return $this->error('未知错误'); } $this->verify_check($data['authcode'], 'yanzhengma') || $this->error('验证码 ' . Lang::get('error'), url('/login')); $validaterule = [ //会员登陆字段验证 'firstname|' . Lang::get('user name') => 'require|min:2', 'password|' . Lang::get('user password') => 'require|min:6', ]; // 数据验证 $valid_result = $this->validate($data, $validaterule); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $result = Loader::model('Customer')->login($data['firstname'], $data['password']); $result['status'] !== true && $this->error($result['msg'], url('/login')); //登录失败 if ($this->request->isAjax()) { $result['id'] ? $this->success('登录成功', url('index/customer/index')) : $this->error(Lang::get('unknown error'), url('/login')); } return $result['id'] ? $this->redirect(url('index/customer/index')) : $this->error(Lang::get('unknown error'), url('/login')); } /** * 退出登录 */ public function logout() { if (!$this->customer_id) { return $this->redirect(url('/login')); } Session::delete('customer_auth', null); Session::delete('customer_auth_sign', null); return $this->redirect(url('/login')); } public function register() { if ($this->customer_id > 0) { return $this->redirect(url('index/customer/index')); } if ($this->request->isPost()) { $data = $this->request->post(); if (empty($data) || !is_array($data)) { return $this->error(Lang::get('incorrect operation')); } $this->verify_check($data['authcode'], 'yanzhengma') || $this->error('验证码 ' . Lang::get('error'), url('/login')); //验证规则 $validaterule = [ 'firstname' => 'require|length:2,32|unique:customer,firstname', 'email' => 'email|unique:customer,email', 'telephone' => ['regex' => '^1[345789]\d{9}$', 'unique' => 'customer,telephone',], 'password' => 'require|min:6|max:32', 'repassword' => 'require|confirm:password', //'group_id' => 'require|between:0,2147483647', 'item' => 'accepted', ]; //验证提示信息 $validatemsg = [ 'firstname.require' => '用户名不能为空', 'firstname.unique' => '用户名已经被使用', 'firstname.length' => '用户名在2-32个字符之间', 'email.email' => '邮箱格式错误', 'email.unique' => '邮箱已经被使用', 'telephone.regex' => '电话格式错误', 'telephone.unique' => '电话已经被使用', 'password.require' => '密码不能为空', 'password.min' => '密码不少于6个字符', 'password.max' => '密码不多于32个字符', 'repassword.require' => '确认密码不能为空', 'repassword.confirm' => '两次密码不相符', 'group_id.require' => '用户组不能为空', 'item' => '请确认阅读服务条款', ]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $code = $this->cacheGet('regtel' . $data['telephone']); if ($code != $data['code']) { return $this->error('短信验证码不正确,请输入正确验证码'); } $addtime = time(); $set = [ 'group_id' => 1, 'email' => isset($data['email']) ? $data['email'] : '', 'telephone' => isset($data['telephone']) ? $data['telephone'] : '', 'firstname' => $data['firstname'], 'lastname' => isset($data['lastname']) ? $data['lastname'] : '', 'newsletter' => isset($data['newsletter']) ? $data['newsletter'] : 0, 'salt' => $data['password'], 'password' => md5($data['password']), 'stat' => 0, 'safe' => 1, 'code' => '', 'item' => isset($data['item']) ? $data['item'] : 0, 'token' => isset($data['token']) ? $data['token'] : '', 'wishlist' => isset($data['wishlist']) ? $data['wishlist'] : '', 'ip' => isset($data['ip']) ? $data['ip'] : '', 'fenxiang' => isset($data['fenxiang']) ? $data['fenxiang'] : 0, 'guanzhu' => isset($data['guanzhu']) ? $data['guanzhu'] : 0, 'hangye' => isset($data['hangye']) ? $data['hangye'] : '', 'zhiye' => isset($data['zhiye']) ? $data['zhiye'] : '', 'sex' => isset($data['sex']) ? $data['sex'] : '', 'birthday' => isset($data['birthday']) ? $data['birthday'] : '', 'qq' => isset($data['qq']) ? $data['qq'] : '', 'addtime' => $addtime, 'custom_field' => json_encode([]), ]; $model = Loader::model('Customer')->insertRow($set); if ($model && $customer_id = $model->getData('id')) { return $this->success('注册成功', url('/index/customer/information', ['key' => 'regsuccess'])); } return $this->error(Lang::get('operation failed')); } return $this->fetch(); } public function personal() { if ($this->customer_id <= 0) { $this->redirect(url('index/customer/index')); } return $this->fetch(); } public function my_collection() { if ($this->customer_id <= 0) { $this->redirect(url('index/customer/index')); } $param = $this->request->param(); // tiaoshi($param);die; $where = ['a.stat' => 0, 'b.stat' => 0, 'a.customer_id' => $this->customer_id]; if (isset($param['cid'])) { $cid_arr = model('product_category')->getChildIDArray($param['cid']); $where['b.cid'] = ['in', $cid_arr]; $cid = $param['cid']; } else { $cid = 0; } $field = ['b.id', 'b.cid', 'b.name', 'b.shortname', 'b.isnew', 'b.ishot', 'b.recommend', 'b.viewcount', 'b.brand_id']; $order = ['a.id' => 'desc']; $list = model('collection')->getList($where, $order, $field, 10); foreach ($list as $key => $value) { $product_two_img = model('product_two_img')->where(['product_id' => $value['id']])->find(); $list[$key]['product_two_img'] = $product_two_img['image_url']; } $data = [ 'list' => $list->isEmpty() ? null : $list->items(), 'page' => $list->render(), 'cid' => $cid ]; $this->assign($data); return $this->fetch(); } public function my_report() { if ($this->customer_id <= 0) $this->redirect(url('index/customer/index')); $param = $this->request->param(); $where = ['stat' => 0, 'customer_id' => $this->customer_id]; if (isset($param['status']) && $param['status'] >= 0) { $where['status'] = $param['status']; $status = $param['status']; } else $status = -1; $list = model('report')->getList($where, ['id' => 'desc'], null, 10); tiaoshi($list);die; $data = [ 'list' => $list->isEmpty() ? null : $list->items(), 'page' => $list->render(), 'status' => $status ]; $this->assign($data); return $this->fetch(); } public function new_logout() { $this->_logout(); return $this->redirect('index/customer/index'); } public function information($key) { $key = (string) $key; //$this->engine->layout(false); $result = [ 'regsuccess' => ['msg' => '注册成功', 'url' => ''], 'getpwdsuccess' => ['msg' => '找回密码完成', 'url' => ''], ]; if ($result[$key]) { $value = $result[$key]; } else { $value = ['msg' => '信息提示', 'url' => '']; } $this->assign($value); return $this->fetch(); } public function forgetpwd() { if ($this->customer_id) { return $this->redirect(url('index/customer/index')); } if ($this->request->isPost()) { $data = $this->request->post(); if (empty($data) || !is_array($data)) { return $this->error(Lang::get('incorrect operation')); } //验证规则 $validaterule = [ 'email' => 'email', 'password' => 'require|min:6|max:32', 'repassword' => 'require|confirm:password', ]; //验证提示信息 $validatemsg = [ 'email.email' => '邮箱格式错误', 'password.require' => '密码不能为空', 'password.min' => '密码不少于6个字符', 'password.max' => '密码不多于32个字符', 'repassword.require' => '确认密码不能为空', 'repassword.confirm' => '两次密码不相符', ]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->error($valid_result); } $row = Loader::model('Customer')->getRow(['email' => $data['email']]); if (empty($row)) { return $this->error('该邮箱尚未注册!'); } $code = $this->cacheGet('regemail' . $data['email']); if ($code != $data['code']) { return $this->error('邮箱验证码不正确,请输入正确验证码'); } // if ($row['password'] != md5($data['password'])) { // return $this->error('原密码不正确'); // } $data['id'] = $row['id']; $model = Loader::model('Customer')->updatePassword($data); if ($model && $model->getData('id')) { return $this->success('找回密码完成', url('/index/customer/information', ['key' => 'getpwdsuccess'])); } } return $this->fetch(); } public function sendsms() { $data = $this->request->param(); // tiaoshi($data);die; if (empty($data) || !is_array($data)) { return $this->json(-1, '数据错误'); } if (isset($data['login']) && $data['login']) { $customer_info = model('customer')->getBasicInfoByTelephone($data['telephone']); if (empty($customer_info)) { return $this->json(-101, '手机号未注册'); } } else if (isset($data['register']) && $data['register']) { $customer_info = model('customer')->getBasicInfoByTelephone($data['telephone']); if (!empty($customer_info)) { return $this->json(-102, '手机号已注册'); } } //验证规则 $validaterule = [ // 'telephone' => ['regex' => '^1[345789]\d{9}$', 'unique' => 'customer,telephone',], 'telephone' => ['regex' => '^1[345789]\d{9}$'], ]; //验证提示信息 $validatemsg = [ 'telephone.regex' => '手机格式错误', // 'telephone.unique' => '手机号已注册', ]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { return $this->json(-2, $valid_result); } $send_count = Cache::get('sendsms_' . $data['telephone'], ''); if ($send_count == '') { Cache::set('sendsms_' . $data['telephone'], 1, 43200); } else { if ($send_count > 10) { return $this->json(-3, '发送次数超过限制'); } Cache::inc('sendsms_' . $data['telephone']); } $sms = new \alisms\SendSms(); //设置关键的四个配置参数,其实配置参数应该写在公共或者模块下的config配置文件中,然后在获取使用,这里我就直接使用了。 $sms->accessKeyId = (string) Config::get('sms_accesskeyid'); $sms->accessKeySecret = (string) Config::get('sms_accesskeysecret'); $sms->signName = (string) Config::get('sms_signname'); $sms->templateCode = (string) Config::get('sms_templatecode'); $mobile = $data['telephone']; //模板参数,自定义了随机数,你可以在这里保存在缓存或者cookie等设置有效期以便逻辑发送后用户使用后的逻辑处理 $code = mt_rand(1000, 9999); $this->cacheSet('regtel' . $mobile, $code, 300); $templateParam = array('code' => $code); $m = $sms->send($mobile, $templateParam); //类中有说明,默认返回的数组格式,如果需要json,在自行修改类,或者在这里将$m转换后在输出 if ($m['Code'] == 'OK') { return $this->json(200, $m['Message']); } else { return $this->json(-4, $m['Message']); } } public function sendresetemail() { $data = $this->request->param(); if (empty($data) || !is_array($data)) { return $this->json(-1, '数据错误'); } //验证规则 $validaterule = ['email' => 'email',]; //验证提示信息 $validatemsg = ['email.email' => '邮箱格式错误',]; $valid_result = $this->validate($data, $validaterule, $validatemsg); if (true !== $valid_result) { // 验证失败 输出错误信息 return $this->json(-2, $valid_result); } $row = Loader::model('Customer')->getRow(['email' => $data['email']]); if (empty($row)) { return $this->json(-3, '该邮箱尚未注册!'); } //$email = $data['email']; //$code = mt_rand(10000, 99999); //$this->cacheSet('regemail' . $email, $code, 1800); //return $this->success($code); //$email为邮箱 $email = $data['email']; //模板参数,自定义了随机数,你可以在这里保存在缓存或者cookie等设置有效期以便逻辑发送后用户使用后的逻辑处理 $code = mt_rand(100000, 999999); $this->cacheSet('regemail' . $email, $code, 1800); //邮件标题 $subject = $this->request->host() . '-激活邮箱'; //邮件内容 $body = "