Files
liche/api/controllers/wxapp/licheb/User.php
T
2022-06-02 13:41:07 +08:00

501 lines
22 KiB
PHP

<?php
defined('WXAPP_APP') OR exit('No direct script access allowed');
/**
* Created by Vim
* User: lcc
* Date: 2021.06.23
* Time: 14:08
*/
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
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');//超级管理员披上马甲可操作权限
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg = array();//授权微信信息
$this->load->model("biz/biz_model");
$this->load->model('auto/auto_brand_model');
$this->load->model('items/items_model', 'mdItems');
$this->load->model('items/items_oplogs_model', 'mdItemsOplogs');
$this->load->library('receiver/orders_v2_entity');
}
/**
* 获取ukey
* @return array
* @throws Exception
*/
protected function get_ukey()
{
$mobile_white = ['15359333655', '18050017004', '18350451617', '13860199666'];
$code = $this->input_param('code');
$mobile = $this->input_param('mobile');
$sms_code = $this->input_param('sms_code');
if (!$code || !$mobile || !$sms_code) {
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
//判断验证码
if (!in_array($mobile, $mobile_white) && $this->env != 'd') {//测试环境和测试号码
$mc = &load_cache();
$key = "licheb_login_code_" . $mobile;
$cache_code = $mc->get($key);
if ($sms_code != $cache_code) {
throw new Exception('验证码错误', API_CODE_FAIL);
}
}
$user = $this->app_user_model->get(['mobile' => $mobile, 'status>' => -1]);
if (!$user) {
throw new Exception('用户不存在', API_CODE_FAIL);
}
if (!$user['status']) {
throw new Exception('该账号已停用', API_CODE_FAIL);
}
//判断门店是否存在
if ($user['group_id'] < 4) {
$biz_id = intval($user['biz_id']);
$biz = $this->biz_model->get(['id' => $biz_id, 'status' => 1]);
if (!$biz) {
throw new Exception('门店不存在', API_CODE_FAIL);
}
}
$session = $this->wx_session($code);
//print_r($session);
if (!$session['session_key']) {
throw new Exception('登录失败', API_CODE_FAIL);
}
$uid = $user['id'];
if (!$user['openid']) { //未绑定微信
$upd = [
'openid' => $session['openid']
];
$session['unionid'] && $upd['unionid'] = $session['unionid'];
$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);
}
}
$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'];
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$user = $this->app_user_model->get(array('id' => $uid));
//获取所属店铺字段
$biz = $this->biz_model->get(['id' => $biz_id, 'status' => 1], 'biz_name,type');
//判断门店是否存在
if (!$biz && $this->session['group_id'] < 4) {
$this->logout();
throw new Exception('门店不存在', API_CODE_FAIL);
}
$group_arr = $this->app_user_model->get_group();
$group_name = $group_arr[$user['group_id']] ? $group_arr[$user['group_id']] : '';
//获取拨打电话
$json = $this->session['jsondata'] ? json_decode($this->session['jsondata'], true) : array();
$tel = $this->session['mobile'];
if ($json && $json['licheb'] && $json['licheb']['tel']) {
$tel = $json['licheb']['tel'];
}
$data = array(
'uid' => $uid,
'uname' => $user['uname'],
'mobile' => $user['mobile'],
'headimg' => $user['headimg'],
'tel' => $tel,
'group_id' => $user['group_id'],
'group_name' => $group_name,
'biz_id' => $biz_id,
'biz_name' => $biz['biz_name'] ? $biz['biz_name'] : '',
'biz_type' => $biz['type'] ? $biz['type'] : '',
'show_sa' => $biz_id == 70 ? true : false,
);
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;
}
/**
* 统计数据
*/
protected function get_cal()
{
$uid = $this->session['uid'];
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
$group_id = $this->session['group_id'];
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->load->model('receiver/order/receiver_orders_model', 'orders_model');
$this->load->model('receiver/order/receiver_orders_v2_model');
$this->load->model('receiver/order/receiver_order_signs_model', 'order_signs_model');
$this->load->model('receiver/receiver_customers_visit_data_model', 'mdCustomerVisitData');
$where = [
'status>' => -1,
'is_top' => 1,
'biz_id' => $biz_id
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$gz_count = $this->customers_model->count($where);
$where = [
'status' => 0,
'biz_id' => $biz_id
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$sign_count = $this->orders_model->count($where);
$where = [
'status' => 1,
'biz_id' => $biz_id
];
$group_id == 1 && $where['admin_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$loan_count = $this->orders_model->count($where);
//客户代办事项
if ($group_id == 1) {
$customer_op_list = [];
} else {
//未派单客户
$where = ['admin_id' => 0, 'biz_id' => $biz_id, 'status>=' => 0];
if ($group_id == 4 && $biz_id != 1) {
$where['brand_id!='] = 3; //渠道经理过滤
}
$unuse_count = $this->customers_model->count($where);
$defeat_count = $this->customers_model->count(array('biz_id' => $biz_id, 'if_defeat' => 1, 'status>' => -1));
$customer_op_list = [
['title' => '待分配客户(人)', 'icon' => 'icon-daifenpei', 'total' => $unuse_count, 'page' => '/pages/customer/allot/index'],
['title' => '战败申请(人)', 'icon' => 'icon-statistics-custom-5', 'total' => $defeat_count, 'page' => '/pages/customer/optDefeat/index'],
];
}
$where = [
'status>=' => 0,
'payway' => 0,
'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0,
'id not in (select o_id from lc_receiver_order_status where pid_status=1 and status=2)' => null,
'biz_id' => $biz_id
];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$fq_total = $this->receiver_orders_v2_model->count($where);
$where = [
'status>=' => 0,
'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0,
'id not in (select o_id from lc_receiver_order_status where pid_status=2 and status=1)' => null,
'biz_id' => $biz_id
];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$pc_total = $this->receiver_orders_v2_model->count($where);
$where = [
'status>=' => 0,
'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0,
'id not in (select o_id from lc_receiver_order_status where pid_status=3 and status=1)' => null,
'id in (select o_id from lc_receiver_order_status where pid_status=0 and status=2)' => null,
'biz_id' => $biz_id
];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$kp_total = $this->receiver_orders_v2_model->count($where);
$where = [
'status>=' => 0,
'id>=' => Orders_v2_entity::V2_START_ID,
'brand_id>' => 0,
'id in (select o_id from lc_receiver_order_status where pid_status=3 and status=1)' => null,
'id not in (select o_id from lc_receiver_order_status where pid_status=4 and status=2)' => null,
'biz_id' => $biz_id
];
$group_id == 1 && $where['sale_id'] = $uid;
$group_id == 4 && $where['brand_id!='] = 3; //渠道经理过滤
$ck_total = $this->receiver_orders_v2_model->count($where);
$deallist = [
['title' => '分期办理 ', 'icon' => 'icon-banfenqi', 'total' => $fq_total, 'page' => '/pages/order/filterList/index2?type=fq&title=分期办理'],
['title' => '车辆匹配', 'icon' => 'icon-cheliangfenpei', 'total' => $pc_total, 'page' => '/pages/order/filterList/index2?type=pc&title=车辆分配'],
['title' => '发票开具', 'icon' => 'icon-kaipiao1', 'total' => $kp_total, 'page' => '/pages/order/filterList/index2?type=kp&title=发票开具'],
['title' => '交付确认', 'icon' => 'icon-jiaofu', 'total' => $ck_total, 'page' => '/pages/order/filterList/index2?type=jf&title=交付确认'],
];
$where_v = ['a.biz_id' => $biz_id, 'a.status in(0,1)' => null, 'b.t_day' => date('Y-m-d')];
$where_c = ['biz_id' => $biz_id, 'status in(0,1)' => null];
$group_id == 4 && $where_c['brand_id!='] = 3;
if ($group_id == 1) {
$where_v['b.sales_id'] = $uid;
$where_c['admin_id'] = $uid;
}
$h_num = $this->customers_model->count(array_merge($where_c, ['level' => 'H']));
$h_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'H', 'b.status<>' => 2]));
$h_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'H', 'b.status' => 2]));
$a_num = $this->customers_model->count(array_merge($where_c, ['level' => 'A']));
$a_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'A', 'b.status<>' => 2]));
$a_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'A', 'b.status' => 2]));
$b_num = $this->customers_model->count(array_merge($where_c, ['level' => 'B']));
$b_num_1 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'B', 'b.status<>' => 2]));
$b_num_2 = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['a.level' => 'B', 'b.status' => 2]));
$levelSt = [
['title' => 'H级客户', 'list' => [['title' => '总数 >', 'num' => $h_num, 'url' => '/pages/customer/filterList/index?level=H&status_tp=1&title=H级客户']
, ['title' => '今日需跟进 >', 'num' => $h_num_1, 'url' => '/pages/customer/filterList/index?level=H&status=1&visit=1&title=H级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $h_num_2, 'url' => '/pages/customer/filterList/index?level=H&status=2&visit=1&title=H级今日已跟进']]],
['title' => 'A级客户', 'list' => [['title' => '总数 >', 'num' => $a_num, 'url' => '/pages/customer/filterList/index?level=A&status_tp=1&title=A级客户']
, ['title' => '今日需跟进 >', 'num' => $a_num_1, 'url' => '/pages/customer/filterList/index?level=A&status=1&visit=1&title=A级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $a_num_2, 'url' => '/pages/customer/filterList/index?level=A&status=2&visit=1&title=A级今日已跟进']]],
['title' => 'B级客户', 'list' => [['title' => '总数 >', 'num' => $b_num, 'url' => '/pages/customer/filterList/index?level=B&status_tp=1&title=B级客户']
, ['title' => '今日需跟进 >', 'num' => $b_num_1, 'url' => '/pages/customer/filterList/index?level=B&status=1&visit=1&title=B级今日需跟进']
, ['title' => '今日已跟进 >', 'num' => $b_num_2, 'url' => '/pages/customer/filterList/index?level=B&status=2&visit=1&title=B级今日已跟进']]],
];
$wl_num = $this->mdCustomerVisitData->count_visit(array_merge($where_v, ['b.status<>' => 2]));
$data = [
'wl_count' => ['title' => '待跟进客户(人)', 'num' => $wl_num, 'url' => '/pages/customer/filterList/index?status=1&visit=1&title=待跟进客户'],
'gz_count' => ['title' => '特别关注客户(人)', 'num' => $gz_count, 'url' => '/pages/customer/filterList/index?istop=1&title=特别关注客户'],
'sign_count' => $sign_count,
'loan_count' => $loan_count,
'deallist' => $deallist,
'customer_op_list' => $customer_op_list,
'levelSt' => $levelSt,
];
return $data;
}
/**
* 更新联系手机号
* @return array
* @throws Hd_Exception
*/
protected function put_tel()
{
$tel = $this->input_param('tel');
if (!mobile_valid($tel)) {
throw new Hd_Exception('确认一下手机号是否正确', API_CODE_INVILD_PARAM);
}
$uid = $this->session['uid'];
$user = $this->app_user_model->get(array('id' => $uid));
$json = $user['jsondata'] ? json_decode($user['jsondata'], true) : array();
$json['licheb']['tel'] = $tel;
$upd = array('jsondata' => json_encode($json, JSON_UNESCAPED_UNICODE));
$ret = $this->app_user_model->update($upd, array('id' => $uid));
if (!$ret) {
throw new Hd_Exception('更新失败', API_CODE_FAIL);
}
$data = array('tel' => $tel);
return $data;
}
/**
* 获取门店信息
* @return array
*/
protected function get_bizs()
{
$city_id = $this->input_param('city_id');
$lists = [];
$biz_id_arr = explode(',', $this->session['biz_id']);
$fileds = 'id,biz_name,jsondata';
if ($this->session['biz_id'] && $biz_id_arr) {
$city_id && $o_where = ['city_id' => $city_id, 'type<>' => 4];
$bizs = $this->biz_model->get_by_id_arr($biz_id_arr, $o_where, $fileds);
} else {
$bizs = $this->biz_model->select(['status' => 1, 'city_id' => $city_id, 'type<>' => 4], 'id desc', '', '', $fileds);
}
if ($bizs) {
foreach ($bizs as $key => $val) {
$auto_brands = [];
$jsondata = json_decode($val['jsondata'], true);
$auto_brands_arr = $jsondata['auto_brands'];
$brand_ids = implode(',', $auto_brands_arr);
if ($auto_brands_arr && $brand_ids) {
$where = [
"id in ($brand_ids)" => null,
"id !=" => 3 //过滤狸车品牌
];
$brands = $this->auto_brand_model->select($where, '', 0, 0, 'id,name');
foreach ($brands as $key2 => $val2) {
$res_items = $this->mdItems->select(['brand_id' => $val2['id'], 'status<>' => 0, 'biz_id' => $val['id']
, 'bill_time' => '0000-00-00 00:00:00'], 'in_time asc', 0, 0, 'id,in_time,c_time');
$str_ids = $res_items ? implode(',', array_column($res_items, 'id')) : '';
$bg_color = '#999';
$name2 = '';
if ($str_ids) {
$bg_color = '#fff';
$in_time = $res_items[0]['in_time'] != '0000-00-00 00:00:00' ? strtotime($res_items[0]['in_time']) : $res_items[0]['c_time'];
$re_logs = $this->mdItemsOplogs->select(["item_id in({$str_ids})" => null, 'type' => 2, 'biz_id_to' => $val['id']
], 'com_time asc', 1, 1, 'item_id,com_time');//找出调拨最早
if ($re_logs && $re_logs[0]['item_id']) {
$this->load->model('items/items_transfer_model', 'mdTransfer');
$re_tran = $this->mdTransfer->get(['item_id' => $re_logs[0]['item_id'], 'status' => 1]);
if ($re_tran) {
$in_time = '';
$name2 = '(在途)';
} else {
$in_time = strtotime($re_logs[0]['com_time']);
}
}
if ($in_time) {//计算库存天数
$days = round((time() - $in_time) / 3600 / 24);
$days > 0 && $name2 = '(' . $days . '天)';
}
}
$auto_brands[] = ['name' => $val2['name'] . $name2, 'bg_color' => $bg_color];
}
}
$lists[] = [
'id' => $val['id'],
'name' => $val['biz_name'],
'auto_brands' => $auto_brands
];
}
}
$data = [
'list' => $lists,
];
return $data;
}
//获取门店管理员
protected function get_admins()
{
$biz_id = $this->input_param('biz_id');
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 10;
if (!$biz_id) {
$biz_id = $this->session['new_biz_id'] ? $this->session['new_biz_id'] : intval($this->session['biz_id']);
}
$where = [
'status' => 1,
'biz_id' => $biz_id
];
$lists = [];
$count = $this->app_user_model->count($where);
if ($count) {
$rows = $this->app_user_model->select($where, 'id desc', $page, $size, 'id,uname');
$lists = $rows;
}
$data = [
'list' => $lists,
'total' => $count
];
return $data;
}
/**
* 重新设置用户session里的biz_id
*/
protected function put_resetbiz()
{
$ukey = $this->input_param('ukey');
$biz_id = $this->input_param('biz_id');
if (!$biz_id) {
throw new Hd_Exception('参数错误', API_CODE_INVILD_PARAM);
}
$this->session['new_biz_id'] = $biz_id;
$this->app_redis->save($this->redis_login . $ukey, json_encode($this->session, JSON_UNESCAPED_UNICODE), 30 * 24 * 3600);
throw new Exception('保存成功', API_CODE_SUCCESS);
}
}