520 lines
21 KiB
PHP
520 lines
21 KiB
PHP
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\api\BaseController;
|
||
use app\api\middleware\Auth;
|
||
use Firebase\JWT\JWT;
|
||
use think\Exception;
|
||
use think\facade\Db;
|
||
use think\exception\ValidateException;
|
||
use think\facade\Request;
|
||
use app\admin\model\ShopUser;
|
||
|
||
/**
|
||
* 用户信息相关接口.
|
||
*/
|
||
class Userinfo extends BaseController
|
||
{
|
||
/**
|
||
* 控制器中间件 [不需要鉴权]
|
||
* @var array
|
||
*/
|
||
protected $middleware = [
|
||
Auth::class => ['except' => ['isBinding'] ]
|
||
];
|
||
/**
|
||
* 微信是否绑定用户
|
||
*
|
||
* @ApiTitle (微信是否绑定用户)
|
||
* @ApiSummary (微信是否绑定用户)
|
||
* @ApiMethod (POST)
|
||
* @ApiRoute (/api/Userinfo/isBinding)
|
||
* @ApiParams (name="openid", type="integer", required=fasle, description="微信openID")
|
||
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
||
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
||
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
|
||
* @ApiReturn ({
|
||
* 'code':'1',
|
||
* 'msg':'返回成功'
|
||
* })
|
||
*/
|
||
public function isBinding()
|
||
{
|
||
$post = get_params();
|
||
// if(!$post['openid']){
|
||
// $this->apiError('缺少参数');
|
||
// }
|
||
// $where['openid'] = $post['openid'];
|
||
// $user = Db::table('fa_user')->where($where)->find();
|
||
// if ($user) {
|
||
//// 调登录接口返回信息
|
||
// $ret = $this->auth->login($user['mobile'], '123456');
|
||
// if ($ret) {
|
||
// $Userinfo = $this->auth->getUserinfo();
|
||
// $map['user_id'] = $user['id'];
|
||
// $is_wgy = Db::table('fa_szxc_information_usermsg')->where($map)->value('is_wgy');
|
||
// $Userinfo['is_wgy'] = $is_wgy;
|
||
// $userinfo_data['userinfo'] = $Userinfo;
|
||
// $this->apiSuccess('已完善,登录成功', $userinfo_data,1);
|
||
// } else {
|
||
// $this->apiError($this->auth->getError());
|
||
// }
|
||
// }else{
|
||
// $this->apiError('未完善信息');
|
||
// }
|
||
}
|
||
|
||
/**
|
||
* 授权登录后完善用户信息
|
||
*
|
||
* @ApiTitle (授权登录后完善用户信息)
|
||
* @ApiSummary (授权登录后完善用户信息)
|
||
* @ApiMethod (POST)
|
||
* @ApiRoute (/api/Userinfo/Binding)
|
||
* @ApiParams (name="openID", type="integer", required=fasle, description="微信openID")
|
||
* @ApiParams (name="phone", type="integer", required=fasle, description="电话")
|
||
* @ApiParams (name="nickname", type="integer", required=fasle, description="微信昵称")
|
||
* @ApiParams (name="avatar", type="integer", required=fasle, description="微信头像")
|
||
* @ApiParams (name="name", type="integer", required=fasle, description="姓名")
|
||
* @ApiParams (name="marital_status", type="integer", required=fasle, description="婚姻状态")
|
||
* @ApiParams (name="idcard", type="integer", required=fasle, description="身份证号")
|
||
* @ApiParams (name="address_name", type="string", required=true, description="地区中文")
|
||
* @ApiParams (name="area_id", type="string", required=true, description="区id")
|
||
* @ApiParams (name="street_id", type="string", required=true, description="镇id")
|
||
* @ApiParams (name="village_id", type="string", required=true, description="村id")
|
||
* @ApiParams (name="brigade_id", type="string", required=true, description="大队id")
|
||
* @ApiReturnParams (name="code", type="integer", required=true, sample="0")
|
||
* @ApiReturnParams (name="msg", type="string", required=true, sample="返回成功")
|
||
* @ApiReturnParams (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
|
||
* @ApiReturn ({
|
||
* 'code':'1',
|
||
* 'msg':'返回成功'
|
||
* })
|
||
*/
|
||
public function Binding()
|
||
{
|
||
$post = get_params();
|
||
if(!$post['idcard'] || !$post['area_id'] || !$post['street_id'] || !$post['village_id'] || !$post['name']){
|
||
$this->apiError('缺少参数');
|
||
}
|
||
if($post['phone'] == 'undefined'){
|
||
$this->apiError('手机号错误');
|
||
}
|
||
|
||
// $where['id'] = $this->request->uid; // 废弃
|
||
$post['user_id'] = $this->request->uid;
|
||
|
||
$user = ShopUser::where('phone', $post['phone'])->find(); // 通过唯一手机号获取商城用户信息
|
||
// $user = Db::table('fa_user')->where($where)->field('id,nickname,group_id,mobile,avatar,username,createtime,score')->find(); // 农科user表废弃
|
||
|
||
// 已绑定
|
||
if ($user) { // 如果用户存在
|
||
|
||
Db::startTrans(); // 开启事务
|
||
|
||
try {
|
||
|
||
//判断身份证号是否已被使用
|
||
$InformationUsermsg = Db::table('fa_szxc_information_usermsg')
|
||
->where('idcard',$post['idcard']) // 身份证号
|
||
->field('idcard')
|
||
->find();
|
||
|
||
// 假如身份证号被使用,则报错
|
||
if ($InformationUsermsg) {
|
||
$this->apiError('身份证号已被使用');
|
||
}
|
||
|
||
// 获取用户user_id
|
||
$userid = $user['uid'];
|
||
// $userid = $post['user_id']; // 原user_id 废弃
|
||
|
||
// 组装地址名
|
||
if ($post['area_id'] != '') {
|
||
$area_name = Db::table('fa_geo_area')->where('area_code', $post['area_id'])->value('area_name');
|
||
}
|
||
if ($post['street_id'] != '') {
|
||
$street_name = Db::table('fa_geo_street')->where('street_code', $post['street_id'])->value('street_name');
|
||
}
|
||
if ($post['village_id'] != '') {
|
||
$village = Db::table('fa_geo_village')->where('village_code', $post['village_id'])->value('village_name');
|
||
}
|
||
if ($post['brigade_id'] != '') {
|
||
$brigade_name = Db::table('fa_geo_brigade')->where('id', $post['brigade_id'])->value('brigade_name');
|
||
}
|
||
|
||
$address_name = $area_name.$street_name.$village.$brigade_name;
|
||
|
||
// 更新用户表
|
||
$u_up['nickname'] = $post['name']; // 真实姓名-- 待处理
|
||
|
||
// Db::table('fa_user')->where($where)->update($u_up); // 废弃的农科user表
|
||
|
||
//写入用户信息表
|
||
$data['user_id'] = $userid;
|
||
$data['area_id'] = $post['area_id'];
|
||
$data['name'] = $post['name'];
|
||
$data['gender'] = $this->get_sex($post['idcard']);
|
||
$data['age'] = $this->getAgeFromIdNo($post['idcard']);
|
||
$data['marital_status'] = $post['marital_status'];
|
||
$data['phone'] = $post['phone']??'';
|
||
$data['idcard'] = $post['idcard'];
|
||
$data['address_name'] = $address_name;
|
||
$data['createtime'] = time();
|
||
$data['street_id'] = $post['street_id'];
|
||
$data['village_id'] = $post['village_id_id'];
|
||
$data['brigade_id'] = $post['brigade_id'];
|
||
|
||
// 写入用户关联信息表
|
||
$msgExists = Db::table('fa_szxc_information_usermsg')
|
||
->where('user_id', $userid)
|
||
->find();
|
||
|
||
// 录入信息
|
||
// 判断写入状态
|
||
if (!$msgExists){
|
||
// 假如没写如果,则增加一条记录
|
||
Db::table('fa_szxc_information_usermsg')
|
||
->strict(false)
|
||
->insert($data);
|
||
}
|
||
|
||
//写入用户地区表
|
||
$address_data['user_id'] = $userid;
|
||
$address_data['area_id'] = $post['area_id'];
|
||
$address_data['street_id'] = $post['street_id'];
|
||
$address_data['village_id'] = $post['village_id_id'];
|
||
$address_data['village_code'] = $post['village_id'];
|
||
$address_data['brigade_id'] = $post['brigade_id'];
|
||
$address_data['createtime'] = date('Y-m-d H:i:s');
|
||
$add = Db::table('fa_szxc_information_useraddress')
|
||
->where('user_id', $userid)
|
||
->find();
|
||
|
||
// 新增被邀请的家庭成员注册,直接加入该家庭
|
||
if(isset($post['hz_id']) && !empty($post['hz_id'])){
|
||
// 写入家庭成员表
|
||
$my_user_id = $userid;
|
||
$hz_id = $post['hz_id']; //户主id
|
||
$time = time();
|
||
$f_where[] = ['user_ids','find in set',$hz_id];
|
||
$family_arr = Db::table('fa_szxc_family')->where($f_where)->find();
|
||
if($family_arr){
|
||
$u_data['user_ids'] = \think\facade\Db::raw("concat(user_ids ,"."',".$my_user_id."')");
|
||
$u_data['updatetime'] = $time;
|
||
Db::table('fa_szxc_family')->where('id',$family_arr['id'])->update($u_data);
|
||
|
||
$h_data['householder_id'] = $hz_id;
|
||
Db::table('fa_szxc_information_usermsg')->where('user_id',$my_user_id)->update($h_data);
|
||
|
||
}else{
|
||
$szxc_family['user_ids'] = $hz_id.','.$my_user_id;
|
||
$szxc_family['householder_id'] = $hz_id;
|
||
$szxc_family['createtime'] = $time;
|
||
Db::table('fa_szxc_family')->strict(false)->insert($szxc_family);
|
||
}
|
||
}
|
||
|
||
// 判断用户地区写入状态
|
||
if (!$add){
|
||
|
||
Db::table('fa_szxc_information_useraddress')->strict(false)->insert($address_data);
|
||
|
||
// 商城农科user表,被废弃
|
||
// $eb_nk_user = Db::connect('shop')
|
||
// ->table('eb_nk_user')
|
||
// ->where('n_user_id',$post['user_id'])
|
||
// ->find();
|
||
|
||
// if ($eb_nk_user){
|
||
|
||
// 废弃同步更新商城用户表
|
||
// Db::connect('shop')->table('eb_nk_user')->where('id',$eb_nk_user['id'])
|
||
// ->update(['area_id'=>$post['area_id'],'street_id'=>$post['street_id'],'village_id'=>$post['village_id_id'],'village_code'=>$post['village_id']]);
|
||
|
||
// 无需更新用户昵称 废弃
|
||
// if ($fa_user['nickname']){
|
||
// Db::connect('shop')->table('eb_user')->where('uid',$eb_nk_user['user_id'])->update(['nickname'=>$fa_user['nickname']]);
|
||
// }
|
||
// }
|
||
}
|
||
|
||
// 新增修改用户推广员id spread_uid
|
||
// $spread_uid = $userid;
|
||
|
||
// 先获取当前村推官员
|
||
$s_where[] = ['b.village_id', '=', $post['village_id_id']];
|
||
$s_where[] = ['a.is_promoter', '=', 1];
|
||
|
||
// 废弃复杂查询uid,上一步已获取到
|
||
$spread_uid = Db::connect('shop')
|
||
->table('eb_user')
|
||
->alias('a')
|
||
->join('eb_nk_user b','a.uid = b.user_id')
|
||
->where($s_where)
|
||
->value('a.uid');
|
||
|
||
// 推广员uid
|
||
if($spread_uid){
|
||
|
||
// 更新本用户推广人UID
|
||
ShopUser::where('uid', $spread_uid)
|
||
->update([
|
||
'spread_uid' => $spread_uid,
|
||
'promoter_time'=>date('Y-m-d H:i:s')
|
||
]);
|
||
|
||
// 推广员邀请人数增加
|
||
ShopUser::where('uid', $spread_uid)
|
||
->inc('spread_count','1')
|
||
->update();
|
||
|
||
// -- 废弃复杂更新推荐用户uid
|
||
// if ($eb_nk_user){
|
||
// Db::connect('shop')->table('eb_user')->where('uid',$eb_nk_user['user_id'])->update(['spread_uid'=>$spread_uid,'promoter_time'=>date('Y-m-d H:i:s')]);
|
||
// Db::connect('shop')->table('eb_user')->where('uid',$spread_uid)->inc('spread_count','1')->update();
|
||
// }
|
||
|
||
}
|
||
|
||
$Userinfo = $user;
|
||
$Userinfo['user_id'] = $userid;
|
||
// $Userinfo['user_id'] = $post['user_id']; // 临时废弃
|
||
$userinfo_data['userinfo'] = $Userinfo;
|
||
|
||
$find = Db::table('fa_szxc_information_usermsg')
|
||
->where('user_id', $userid)
|
||
->find();
|
||
|
||
// $group = Db::connect('shop')->table('eb_user_group')
|
||
// ->where('id',$userinfo_data['userinfo']['group_id'])
|
||
// ->find();
|
||
|
||
// $userinfo_data['userinfo']['group_name'] = $group['name'];
|
||
|
||
if ($find){
|
||
$userinfo_data['userinfo']['name'] = $find['name'];
|
||
$userinfo_data['userinfo']['no_update'] = 0;
|
||
$userinfo_data['userinfo']['address_name'] = $find['address_name'];
|
||
}else{
|
||
$userinfo_data['userinfo']['no_update'] = 1;
|
||
$userinfo_data['userinfo']['address_name'] = '';
|
||
}
|
||
|
||
Db::commit();
|
||
$this->apiSuccess('已完善,登录成功', $userinfo_data);
|
||
|
||
// } else {
|
||
// $this->apiError($this->auth->getError());
|
||
// }
|
||
|
||
} catch (ValidateException | PDOException | Exception $e) {
|
||
|
||
Db::rollback();
|
||
$this->apiError($e->getMessage());
|
||
|
||
}
|
||
} else {
|
||
|
||
$this->apiError('未注册不能绑定');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param $user_id
|
||
* @return string
|
||
*/
|
||
public function getToken($user_id){
|
||
$time = time(); //当前时间
|
||
$conf = $this->jwt_conf;
|
||
$token = [
|
||
'iss' => $conf['iss'], //签发者 可选
|
||
'aud' => $conf['aud'], //接收该JWT的一方,可选
|
||
'iat' => $time, //签发时间
|
||
'nbf' => $time-1 , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用
|
||
'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时
|
||
'data' => [
|
||
//自定义信息,不要定义敏感信息
|
||
'userid' =>$user_id,
|
||
]
|
||
];
|
||
return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token 默认'HS256'
|
||
}
|
||
|
||
/**
|
||
|
||
* 根据身份证号码获取性别
|
||
|
||
* author:xiaochuan
|
||
|
||
* @param string $idcard 身份证号码
|
||
|
||
* @return int $sex 性别 1男 2女 0未知
|
||
|
||
*/
|
||
|
||
function get_sex($idcard) {
|
||
|
||
if(empty($idcard)) return null;
|
||
|
||
$sexint = (int) substr($idcard, 16, 1);
|
||
|
||
return $sexint % 2 === 0 ? '2' : '1';
|
||
|
||
}
|
||
|
||
|
||
// 计算年龄
|
||
function getAgeFromIdNo($idno=''){
|
||
|
||
$btime = strtotime(substr($idno, 6, 8));//idno是身份证号 截取日期并转为时间戳
|
||
$byear =date('Y',$btime );
|
||
$bmonth =date('m',$btime );
|
||
$bday =date('d',$btime );
|
||
$curYear=date('Y');
|
||
$curMoth = date('m');
|
||
$curDay = date('d');
|
||
$age = $curYear - $byear;
|
||
if( $curMoth < $bmonth || ($curMoth ==$bmonth && $curDay < $bday)){
|
||
$age--;
|
||
}
|
||
return $age ??0;
|
||
|
||
}
|
||
|
||
//验证省份证格式是否正确
|
||
|
||
/**
|
||
* 获取密码加密后的字符串
|
||
* @param string $password 密码
|
||
* @param string $salt 密码盐
|
||
* @return string
|
||
*/
|
||
private function getEncryptPassword($password, $salt = '')
|
||
{
|
||
return md5(md5($password) . $salt);
|
||
}
|
||
|
||
/**
|
||
* 身份证号搜索户主
|
||
* @return string
|
||
*/
|
||
public function getUserByIdcard()
|
||
{
|
||
//身份证号
|
||
$idcard = get_params('idcard');
|
||
if (empty($idcard)) {
|
||
$this->apiError('缺少参数');
|
||
}
|
||
$where[] = ['idcard','like','%'.$idcard.'%'];
|
||
$where[] = ['status','=','1'];
|
||
$where[] = ['is_hz','=','1'];
|
||
$hz_arr = Db::table('fa_szxc_information_usermsg')->where($where)->field('user_id,name,idcard,gender,age,phone,address_name')->select()->toArray();
|
||
if ($hz_arr) {
|
||
$this->apiSuccess('获取成功', $hz_arr);
|
||
} else {
|
||
$this->apiError('暂无数据');
|
||
}
|
||
}
|
||
|
||
public function validateIDCard($idcard) {
|
||
if(empty($idcard)){
|
||
return false;
|
||
}else{
|
||
$idcard = strtoupper($idcard); # 如果是小写x,转化为大写X
|
||
if(strlen($idcard) != 18 && strlen($idcard) != 15){
|
||
return false;
|
||
}
|
||
# 如果是15位身份证,则转化为18位
|
||
if(strlen($idcard) == 15){
|
||
# 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
|
||
if (array_search(substr($idcard, 12, 3), array('996', '997', '998', '999')) !== false) {
|
||
$idcard = substr($idcard, 0, 6) . '18' . substr($idcard, 6, 9);
|
||
} else {
|
||
$idcard = substr($idcard, 0, 6) . '19' . substr($idcard, 6, 9);
|
||
}
|
||
# 加权因子
|
||
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
|
||
# 校验码对应值
|
||
$code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
|
||
$checksum = 0;
|
||
for ($i = 0; $i < strlen($idcard); $i++) {
|
||
$checksum += substr($idcard, $i, 1) * $factor[$i];
|
||
}
|
||
$idcard = $idcard . $code[$checksum % 11];
|
||
}
|
||
# 验证身份证开始
|
||
$IDCardBody = substr($idcard, 0, 17); # 身份证主体
|
||
$IDCardCode = strtoupper(substr($idcard, 17, 1)); # 身份证最后一位的验证码
|
||
|
||
# 加权因子
|
||
$factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
|
||
# 校验码对应值
|
||
$code = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
|
||
$checksum = 0;
|
||
for ($i = 0; $i < strlen($IDCardBody); $i++) {
|
||
$checksum += substr($IDCardBody, $i, 1) * $factor[$i];
|
||
}
|
||
$validateIdcard = $code[$checksum % 11]; # 判断身份证是否合理
|
||
if($validateIdcard != $IDCardCode){
|
||
return false;
|
||
}else{
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
|
||
//市场销量趋势折线图
|
||
public function orderNumGroup()
|
||
{
|
||
$date = 'lately7';
|
||
$list = Db::connect('shop')->table('eb_product_order_log')->when($date, function ($query, $date) {
|
||
getModelTime($query, $date, 'create_time');
|
||
})->field(Db::raw('from_unixtime(unix_timestamp(create_time),\'%m-%d\') as time, count(id) as new'))
|
||
->group('time')->order('time ASC')->select()->toArray();
|
||
$newUserList = array_combine(array_column($list, 'time'), array_column($list, 'new'));
|
||
$time = getDatesBetweenTwoDays(getStartModelTime($date), date('Y-m-d'));
|
||
$orderList = [];
|
||
$time_arr = [];
|
||
$value_arr = [];
|
||
foreach ($time as $item) {
|
||
$new = $newUserList[$item] ?? 0;
|
||
$time_arr[] = $item;
|
||
$value_arr[] = $new;
|
||
}
|
||
$return['day'] = $time_arr;
|
||
$return['value'] = $value_arr;
|
||
$this->apiSuccess('获取成功', $return);
|
||
}
|
||
|
||
//产品销量前十
|
||
public function get_ranking($area_id=0,$street_id=0,$village_id=0){
|
||
$where = [];
|
||
if ($area_id){
|
||
$where[] = ['area_id', '=', $area_id];
|
||
}
|
||
if ($street_id){
|
||
$where[] = ['street_id', '=', $street_id];
|
||
}
|
||
if ($village_id){
|
||
$where[] = ['village_id', '=', $village_id];
|
||
}
|
||
// 商品销量前十
|
||
$return['data'] = Db::connect('shop')->table('eb_store_product')
|
||
->alias('a')
|
||
->join('eb_product_order_log b','a.product_id = b.product_id')
|
||
->join('eb_store_order c','b.order_id = c.order_id')
|
||
->field('a.store_name as name,sum(c.total_num) as value,a.image,a.price')
|
||
->where($where)
|
||
->limit(10)
|
||
->group('a.product_id')
|
||
->order('value desc')
|
||
->select();
|
||
|
||
$this->apiSuccess('获取成功',$return);
|
||
}
|
||
|
||
|
||
}
|