Files
orico-official-website-old/app/mobile/controller/Customer20191104.php
2024-10-29 14:04:59 +08:00

582 lines
20 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\mobile\controller;
use think\Cookie;
use think\Lang;
use think\Loader;
use think\Config;
use think\Session;
use think\Validate;
use think\Cache;
class Customer extends BaseController {
public function login() {
if ($this->customer_id > 0)
$this->redirect(url('mobile/customer/personal'));
return view();
}
public function quick_login() {
if ($this->customer_id > 0)
$this->redirect(url('mobile/customer/personal'));
return view();
}
public function register() {
if ($this->customer_id > 0)
$this->redirect(url('mobile/customer/personal'));
return view();
}
public function personal() {
if ($this->customer_id <= 0)
$this->redirect(url('mobile/customer/login'));
return view();
}
public function forgetpwd()
{
return view();
}
# 用旧密码改新密码
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("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{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();
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, '旧手机号错误');
}
if (!preg_match("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/", $data['new_telephone']))
{
return $this->json(-3, '手机号格式错误');
}
$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();
tiaoshi($data);die;
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, '登录成功');
}
public function new_logout()
{
$this->_logout();
return $this->json(200, 'ok');
}
/**
* 前台用户登录
*/
public function check_login() {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->json(-1, '未知错误');
}
// $code = $this->cacheGet('regtel' . $data['telephone']);
// if ($code != $data['code']) {
// return $this->json(-1, '短信验证码不正确');
// }
$validate = Loader::validate('customer');
if (!$validate->scene('login')->check($data)) {
return $this->json(-1, $validate->getError());
}
// 登录成功
$user = model('customer')->getBasicInfoByTelephone($data['telephone']);
$curr_time = time();
$token = $this->set_token($user['id'], $curr_time);
$data = [
'user_info' => $user,
'curr_time' => $curr_time,
'token' => $token,
];
return $this->json(1, '登录成功', $data);
}
public function check_register() {
$data = $this->request->post();
if (empty($data) || !is_array($data)) {
return $this->json(-1, '未知错误');
}
// $code = $this->cacheGet('regtel' . $data['telephone']);
// if ($code != $data['code']) {
// return $this->json(-1, '短信验证码不正确');
// }
$validate = Loader::validate('customer');
if (!$validate->scene('register')->check($data)) {
return $this->json(-1, $validate->getError());
}
// tiaoshi($data);die;
$addtime = time();
$set = [
'group_id' => 1,
'telephone' => $data['telephone'],
'firstname' => isset($data['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([]),
];
$result = Loader::model('Customer')->insertRow($set);
if ($result && $customer_id = $result->getData('id')) {
$user = model('customer')->getBasicInfo($customer_id);
$curr_time = time();
$token = $this->set_token($customer_id, $curr_time);
$data = [
'user_info' => $user,
'curr_time' => $curr_time,
'token' => $token,
];
return $this->json(1, '注册成功', $data);
} else {
return $this->json(-1, '注册失败');
}
}
public function sendsms() {
$data = $this->request->post();
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}$'],
];
$validatemsg = [
'telephone.regex' => '手机格式错误',
];
$valid_result = $this->validate($data, $validaterule, $validatemsg);
if (true !== $valid_result) {
return $this->json(-2, $valid_result);
}
$mobile = $data['telephone'];
$curr_date = date('Y-m-d');
// 当前手机号今天是否发送过验证码
$is_send = Cache::get($mobile . $curr_date, '');
if ($is_send) {
// 如果发过,自增一次
Cache::inc($mobile . $curr_date);
} else {
// 如果今天第一次发短信
$expire = 86400;
Cache::set($mobile . $curr_date, 1, $expire);
}
// 当天发送数量
$today_send_count = Cache::get($mobile . $curr_date);
if ($today_send_count > 10) {
return $this->json(-102, '每天发送验证码不能超过10次');
}
//$mobile = $data['telephone'];
//$code = mt_rand(10000, 99999);
//$this->cacheSet('regtel' . $mobile, $code, 300);
//return $this->success($code);
//获取对象,如果上面没有引入命名空间,可以这样实例化:$sms = new \alisms\SendSms()
$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');
//模板参数自定义了随机数你可以在这里保存在缓存或者cookie等设置有效期以便逻辑发送后用户使用后的逻辑处理
$code = mt_rand(10000, 99999);
$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(-1, $m['Message']);
}
}
/*个人中心*/
public function my_collection()
{
if ($this->customer_id <= 0)
{
$this->redirect('mobile/customer/login.html');
}
$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);
// echo model('collection')->getLastSql();die;
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();
}
}