Files
liche/api/controllers/wxapp/liche/User.php
T
2021-07-22 15:46:18 +08:00

421 lines
17 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
require_once APPPATH.'controllers/wxapp/Wxapp.php';
/**
* Created by Vim
* User: lcc
* Date: 2021.06.23
* Time: 14:08
*/
class User extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->login_white = array('get_ukey');//登录白名单
$this->majia_white = array('get_ukey', 'get', 'get_my');//超级管理员披上马甲可操作权限
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg =array();//授权微信信息
$this->load->model('auto/auto_series_model');
$this->load->model('auto/auto_brand_model');
$this->load->model('auto/auto_attr_model');
}
/**
* 获取ukey
* @return array
* @throws Exception
*/
protected function get_ukey(){
$code = $this->input_param('code');
if(!$code){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$session = $this->wx_session($code);
if(!$session['session_key']){
throw new Exception('登录失败', API_CODE_FAIL);
}
$where = array('openid' => $session['openid'], "status <>-1" => null);
$user = $this->app_user_model->get($where);
if($user){
$upd = array();
!$user['unionid'] && $session['unionid'] && $upd['unionid'] = $session['unionid'];
$uid = $user['id'];
if($upd){
$ret = $this->app_user_model->update($upd, array('id' => $uid));
if(!$ret){
debug_log("[error]# code:{$code}; ".$this->app_user_model->db->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('授权用户信息失败', API_CODE_FAIL);
}
}
} else {
$add = array(
'openid' => $session['openid'],
'unionid' => strval($session['unionid']),
'c_time' => time(),
);
$uid = $this->app_user_model->add($add);
if(!$uid){
debug_log("[error]# code:{$code}; ".$this->app_user_model->db->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('授权用户信息失败', API_CODE_FAIL);
}
}
$udata = array('uid' => $uid, 'session_key' => $session['session_key']);
$ukey = $this->refresh_login($udata);
return array('ukey' => $ukey);
}
/**
* 用户信息
* @return array
*/
protected function get(){
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
$data = array(
'uid' => $uid,
'mobile' => $user['mobile'],
'nickname' => $user['nickname'],
'headimg' => $user['headimg'],
);
return $data;
}
/**
* 更新用户信息
* @return array
* @throws Exception
*/
protected function put(){
$encrypted = $this->input_param('encryptedData');
$iv = $this->input_param('iv');
$intro = $this->input_param('intro');
//获取用户信息
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
$upd = array();
$mobile = '';
//授权
if(!is_null($encrypted)){
if(!$encrypted || !$iv){
throw new Exception('授权失败', API_CODE_INVILD_PARAM);
}
$wxdata = $this->wx_data($encrypted, $iv);
if(!$wxdata){
throw new Exception('授权失败', API_CODE_FAIL);
}
$openid = $wxdata['openId'];
$nickname = $wxdata['nickName'];
$sex = $wxdata['gender'];
$headimg = $wxdata['avatarUrl'];
$unionid = $wxdata['unionId'];
$mobile = $wxdata['phoneNumber'];
if(!$mobile && isset($wxdata['phoneNumber'])){
throw new Exception('微信未绑定手机号', API_CODE_FAIL);
}
$mobile && !$user['mobile'] && $upd['mobile'] = $mobile;
$nickname && $upd['nickname'] = $nickname;
$headimg && $upd['headimg'] = $headimg;
if($mobile){//判断手机号是否重复
if(!mobile_valid($mobile)){
throw new Exception("请输入正确的手机号", API_CODE_FAIL);
}
$where = array('mobile' => $mobile, 'id <>' => $uid, "status<>" => -1);
$count = $this->app_user_model->count($where);
if($count > 0){
throw new Exception("{$mobile}已被绑", API_CODE_FAIL);
}
}
} else {//编辑其他信息
$userInfo = $this->input_param('userInfo');
$userInfo['nickName'] && $upd['nickname'] = $userInfo['nickName'];
$userInfo['avatarUrl'] && $upd['headimg'] = $userInfo['avatarUrl'];
if($intro){
if(mb_strlen($intro, 'utf8') > 20){
throw new Exception('介绍自己20个字够啦', API_CODE_FAIL);
}
$upd['signature'] = $intro;
}
}
if($upd){
$ret = $this->app_user_model->update($upd, array('id' => $uid));
if(!$ret){
debug_log("[error]# ".$this->app_user_model->db->last_query(), __FUNCTION__, $this->log_dir);
throw new Exception('请求失败', API_CODE_FAIL);
}
}
$data = array(
'mobile' => $mobile,
'nickname' => $upd['nickname'] ? $upd['nickname'] : $user['nickname'],
'headimg' => $upd['headimg'] ? $upd['headimg'] : $user['headimg'],
);
return $data;
}
/**
* 我的
* @return array
*/
protected function get_my(){
$mobile = $this->session['mobile'];
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$this->load->model('receiver/order/receiver_order_signs_model');
$this->load->model('receiver/order/receiver_order_contracts_model');
$this->load->model('receiver/order/receiver_order_loans_model');
$this->load->model('receiver/order/receiver_order_ckcars_model');
$this->load->model('receiver/order/receiver_order_deliverys_model');
$row = $this->orders_model->get_step($mobile);
$progressOpt = $progress = [];
if($row){
$progressOpt = [];
$progress_arr = [
[
"id" => 1,
"title"=>"已签合同",
"icon"=>"icon-qiandinghetong",
"key" => 0,
"state"=>2//0未开启/1已开启/2当前开启
],
[
"id" => 2,
"title"=>"已交定金",
"icon"=>"icon-yijiaodingjin",
"key" => 0,
"state"=>0
],
[
"id" => 3,
"title"=>"办理按揭",
"icon"=>"icon-banlianjie",
"key" => 1,
"state"=>0
],
[
"id" => 4,
"title"=>"配车准备",
"icon"=>"icon-peichezhunbei",
"key" => 2,
"state"=>0
],
[
"id" => 5,
"title"=>"车辆确认",
"icon"=>"icon-cheliangqueren",
"key" => 2,
"state"=>0
],
[
"id" => 6,
"title"=>"已开发票",
"icon"=>"icon-yikaifapiao",
"key" => 3,
"state"=>0
],
[
"id" => 7,
"title"=>"保险上牌",
"icon"=>"icon-baoxianshangpai",
"key" => 4,
"state"=>0
],
[
"id" => 8,
"title"=>"恭喜提车",
"icon"=>"icon-gongxitiche",
"key" => 5,
"state"=>0
],
];
if($row['payway']){ //全款
unset($progress_arr[2]);
}
$progress = [];
$sign_row = $this->receiver_order_signs_model->get(['o_id'=>$row['id']],'status');
$ckcar_row = $this->receiver_order_ckcars_model->get(['o_id'=>$row['id']],'status');
$delivery_row = $this->receiver_order_deliverys_model->get(['o_id'=>$row['id']],'status');
$this->load->library('receiver/orders_entity');
foreach($progress_arr as $key=>$val){
$imgs = [];
if($row['status']>$val['key']){
if($val['key']==0 && $val['id']==1){
$imgs = $this->orders_entity->get_sign_imgs($row['id'],[0,1]);
}
if($val['key']==2 && $val['id']==5){
$imgs = $this->orders_entity->get_sign_imgs($row['id'],[2]);
}
$state = 1;
}elseif($row['status']==$val['key']){
switch($val['key']){
case 0:
$state = 0;
if(!$sign_row['status']){//未签名
$count = $this->receiver_order_contracts_model->count(['o_id'=>$row['id'],'type in (0,1)','status'=>1]);
if($count>0){
$url = '/pages/mine/signContract/daiLi?id='.$row['id'];
}else{
$url = '/pages/mine/signContract/zhengChe?id='.$row['id'];
}
$progressOpt = ['title'=> '合同签订','url'=>$url];
$val['id']== 1 && $state = 2;
}else{ //已签约
if($sign_row['status']==1){//未交定金
if($val['id']==1){
$imgs = $this->orders_entity->get_sign_imgs($row['id'],['0,1']);
$state = 1;
}else{
$state = 2;
}
$progressOpt = ['title'=> '交定金','url'=>'/pages/order/index?typeId=1'];
}else{ //已交定金
$state = 1;
}
}
break;
case 2: //车辆确认
if($ckcar_row['status']){
if($val['id']==5){
$state = 2;
$progressOpt = ['title'=> '合同签订','url'=>'/pages/mine/signContract/queRen?id='.$row['id']];
}else{
$state = 1;
}
}else{
$state = $val['id'] == 4 ? 2:0;
}
break;
case 5: //交付确认
$state = 2;
if($delivery_row['status']){
$imgs = $this->orders_entity->get_sign_imgs($row['id'],[3]);
}else{
$progressOpt = ['title'=> '合同签订','url'=>'/pages/mine/signContract/jiaoJie?id='.$row['id']];
}
break;
default:
$state = 2;
}
}else{
$state = 0;
}
$progress[] = [
'title' => $val['title'],
'icon' => $val['icon'],
'state' => $state,
'imgs' => $imgs
];
}
}
$data = [
'progressOpt' => $progressOpt,
'progress' => $progress,
'about' => [
['title'=>'我的爱车','icon'=>'https://qs.haodian.cn/wechat_app/liche/mine/list-icon-1.png','url'=>'/pages/mine/myCar/index'],
['title'=>'联系阿狸','icon'=>'https://qs.haodian.cn/wechat_app/liche/mine/list-icon-3.png']
],
'order' => [
['title' => '全部订单', 'icon' => 'https://qs.haodian.cn/wechat_app/liche/mine/order-icon-1.png', 'url' => '/pages/order/index?typeId=0'],
['title' => '待付款', 'icon' => 'https://qs.haodian.cn/wechat_app/liche/mine/order-icon-2.png', 'url' => "/pages/order/index?typeId=1"],
['title' => '进行中', 'icon' => 'https://qs.haodian.cn/wechat_app/liche/mine/order-icon-3.png', 'url' => "/pages/order/index?typeId=2"],
['title' => '已完成', 'icon' => 'https://qs.haodian.cn/wechat_app/liche/mine/order-icon-4.png', 'url' => "/pages/order/index?typeId=3"],
],
];
return $data;
}
//我的爱车
protected function get_mycar(){
$this->load->model('receiver/order/receiver_orders_model','orders_model');
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 10;
$count = $this->orders_model->get_finsh($this->session['mobile'],'','',1);
$lists = [];
if($count){
$this->load->model("items/items_model");
$this->load->model('receiver/order/receiver_order_agents_model','agents_model');
$rows = $this->orders_model->get_finsh($this->session['mobile'],$page,$size);
//品牌车型
$brand_arr = array_unique(array_column($rows,'brand_id'));
$brands = $this->auto_brand_model->get_map_by_ids($brand_arr,'id,name');
//车系车型
$series_arr = array_unique(array_column($rows,'s_id'));
$series = $this->auto_series_model->get_map_by_ids($series_arr,'id,name');
foreach($rows as $key=>$val){
$item = $this->items_model->get(['id'=>$val['item_id']],'vin');
$agent = $this->agents_model->get(['o_id'=>$val['id']]);
$agent['ins_time'] = $agent['ins_time'] ? date('Y.m.d',strtotime($agent['ins_time'])):'';
$car_json = json_decode($val['car_json'],true);
$color = isset($car_json['color']) ? $car_json['color'] : '';
$brand_name = isset($brands[$val['brand_id']]) ? $brands[$val['brand_id']][0]['name'] : '';
$serie_name = isset($series[$val['s_id']]) ? $series[$val['s_id']][0]['name'] : '';
$car_img = [];
if($agent['car_img']){
$car_img[] = build_qiniu_image_url($agent['car_img']);
}
$ins_img = [];
if($agent['ins_img']){
$ins_imgs = json_decode($agent['ins_img']);
foreach($ins_imgs as $key =>$val){
$ins_img[] = build_qiniu_image_url($val);
}
}
$other[] = [
'icon'=>'icon-hangshizheng',
'title'=>'行驶证',
'img'=> $car_img
];
$other[] = [
'icon'=>'icon-baodan',
'title'=>'保单',
'img'=> $ins_img
];
$lists[] = [
'title' => "{$brand_name}{$serie_name}",
'model' => [
'title' => $color['title'],
'img' => $color['jsondata']['img']? build_qiniu_image_url($color['jsondata']['img']):'',
'color' => $color['jsondata']['code']
],
'vincode' => "车架号 {$item['vin']}",
'platenumber' => $agent['car_num'],
'insure' => "{$agent['ins_time']}到期",
'other' => $other
];
}
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
}