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 = "

尊敬的$row[firstname],您好!
本次验证码为:

" . $code . "


有效期为30分钟,请及时做出处理。
本邮件由系统自动发出,请勿直接回复!"; $res = $this->sendemail($data['email'], $row['firstname'], $subject, $body, 'oricogroup@orico.com.cn'); if ($res['code'] == 200) { return $this->json(200, "邮件已发送,请您及时确认。"); } else { return $this->json(-4, $res['msg']); } } public function sendemail($to, $to_name, $subject, $body, $from_email = '', $from_name = 'ORICO') { $email_host = (string) Config::get('email_host'); $email_tls = (string) Config::get('email_tls'); $email_port = (string) Config::get('email_port'); $email_user = (string) Config::get('email_user'); $email_pass = (string) Config::get('email_pass'); $email_code = (string) Config::get('email_code'); $email_replyaddr = (string) Config::get('email_replyaddr'); $website_email = (string) Config::get('website_email'); // Passing `true` enables exceptions $mail = new \mail\PHPMailer\PHPMailer(true); try { //Tell PHPMailer to use SMTP $mail->isSMTP(); //$mail->setLanguage('en'); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 0; $mail->Host = $email_host; // if your network does not support SMTP over IPv6 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = $email_port; $mail->CharSet = strtolower($email_code); $mail->Encoding = 'base64'; $mail->SMTPKeepAlive = true; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = strtolower($email_tls); //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = $email_user; //Password to use for SMTP authentication $mail->Password = $email_pass; //Set who the message is to be sent from if ($from_email) { $mail->setFrom($from_email, $from_name); } else { $mail->setFrom($email_replyaddr, 'Sender'); } //Set an alternative reply-to address if ($website_email) { $mail->addReplyTo($website_email, 'Reply'); } //Set who the message is to be sent to $mail->addAddress($to, $to_name); //$mail->addAddress($website_email, 'Recipient'); //Set the subject line $mail->Subject = $subject; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->msgHTML($body); //$mail->Body = 'This is the HTML message body in bold!'; //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; $mail->WordWrap = 60; //send the message, check for errors if (!$mail->send()) { $result = ['code' => -1, 'msg' => '邮箱发送失败,稍后再试']; } else { $result = ['code' => 200, 'msg' => '邮箱发送成功']; } } catch (\mail\PHPMailer\Exception $e) { $result = ['code' => -2, 'msg' => '邮箱发送失败,稍后再试']; } return $result; } public function save(){ // exit('暂未开放'); $client_id = 101544135; $client_secret = 'ef8a9c590667e0aa226cfa5ae5372aa0'; $redirect_uri = 'http://www.orico.com.cn/index/customer/save'; $code = input("code"); $url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=$client_id&client_secret=$client_secret&code=$code&state=123&redirect_uri=$redirect_uri"; $response = file_get_contents($url); if(strpos($response ,"callback")!== false){ $lpos = strpos($response ,"("); $rpos = strpos($response ,")"); $response = substr($response ,$lpos+1,$rpos-$lpos-1); $msg = json_decode($response ); if(isset($msg->error)){ echo "

error:

".$msg->error; echo "

msg:

".$msg->error_description; exit; } } $params = array(); parse_str($response,$params); $url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token']; $str = file_get_contents($url); if(strpos($str ,"callback")!== false){ $lpos = strpos($str ,"("); $rpos = strpos($str ,")"); $str = substr($str ,$lpos+1,$rpos-$lpos-1); $user = json_decode($str ); if(isset($user->error)){ echo "

error:

".$user->error; echo "

msg:

".$user->error_description; exit; } } $user_url = "https://graph.qq.com/user/get_user_info?access_token=".$params['access_token']."&oauth_consumer_key=".$client_id."&openid=".$user->openid; $userinfo = file_get_contents($user_url); $userinfo = json_decode($userinfo, true); $where = [ 'openid' => $user->openid, 'stat' => 0 ]; $result = model('customer')->where($where)->find(); if(empty($result) || $result['telephone'] == '') { $this->assign('openid', $user->openid); $this->assign('access_token', $params['access_token']); $this->assign('userinfo', $userinfo); return view('bind_phone'); } else { $customer_info = model('customer')->where($where)->field('id, firstname, picture, sex, email, telephone, qq, birthday, password')->find(); $this->set_login_token($customer_info); $this->redirect(url('/index/customer/personal')); } } public function qq_register() { $data = $this->request->post(); if (empty($data) || $data['openid'] == '' || $data['access_token'] == '' || empty($data['userinfo'])) { return $this->json(-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, '验证码错误'); } $customer_info = model('customer')->getBasicInfoByTelephone($data['telephone']); if (!empty($customer_info)) { // 手机注册过 $update_data = [ 'openid' => $data['openid'], 'token' => $data['access_token'] ]; $result = model('customer')->where(['telephone' => $data['telephone']])->update($update_data); if (!$result) { return $this->json(-4, '绑定失败'); } } else { // 手机未注册 $userinfo = json_decode($data['userinfo'], true);//dump($userinfo);die; $insert_data = [ 'telephone' => $data['telephone'], 'openid' => $data['openid'], 'token' => $data['access_token'], 'firstname' => $userinfo['nickname'], 'sex' => $userinfo['gender'], 'birthday' => $userinfo['year'], 'picture' => $userinfo['figureurl_qq_2'], 'stat' => 0 ]; $customer_id = model('customer')->insertGetId($insert_data); if (!$customer_id) { return $this->json(-5, '绑定失败'); } $customer_info = model('customer')->getBasicInfo($customer_id); } $this->set_login_token($customer_info); return $this->json(200, '绑定成功'); } public function bind_phone() { return view(); } private function autoLogin($row) { /* 更新登录信息 */ // $data = [ // 'id' => $row['id'], // 'login' => \think\Db::raw('`login`+1'), // //'last_login_time' => Request::instance()->time(), // //'ip' => Request::instance()->ip() // ]; // $this::update($data); /* 记录登录SESSION和COOKIES */ Session::set('customer_auth', $row); Session::set('customer_auth_sign', data_auth_sign($row)); unset($row); } public function wx_save(){ $appdi = 'wx979d391fdfb583d1'; $secret= '69613bae6537f7425b62c9632c63fa16'; $code = $_GET['code']; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appdi&secret=$secret&code=$code&grant_type=authorization_code"; $userinfo = file_get_contents($url); $userinfo = json_decode($userinfo, true); $access_token = $userinfo['access_token']; $openid = $userinfo['openid']; $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid"; $userinfo = file_get_contents($url); $userinfo = json_decode($userinfo, true); $unionid = $userinfo['unionid']; $where = [ 'openid' => $unionid, 'stat' => 0 ]; $result = model('customer')->where($where)->find(); $picture = $userinfo['headimgurl']; $gender = $userinfo['sex']; $addtime = time(); $country_code = $userinfo['city']; $userinfo['figureurl_qq_2'] = $picture; $userinfo['addtime'] = $addtime; $userinfo['gender'] = $gender; $userinfo['year'] = ''; if(empty($result) || $result['telephone'] == '') { $this->assign('openid', $unionid); $this->assign('access_token', $access_token); $this->assign('userinfo', $userinfo); return view('bind_phone'); } else { $customer_info = model('customer')->where($where)->field('id, firstname, picture, sex, email, telephone, qq, birthday, password')->find(); $this->set_login_token($customer_info); $this->redirect(url('/index/customer/personal')); } } }