Files
2025-11-21 10:46:13 +08:00

95 lines
3.9 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auto_user_model extends HD_Model
{
const STATUS_DELETE = -1; //删除
const STATUS_DISABLE = 0; //禁用
const STATUS_NORMAL = 1; //正常
const C_FROM_APP = 'app';//来源app
private $table_name = 'lc_auto_user';
const MAP_USER_MODEL = [
'ain' => 'ain_users_model',
'fish' => 'fish_users_model'
];
public function __construct()
{
parent::__construct($this->table_name, 'default');
$this->load->model('agent/pingan/pingan_users_model');
$this->load->model('agent/organization/organization_model');
}
/**
* @param $openid
* @param string $mobile
* @param string $accountId 平安用户编码
* @param string $from ain fish
* @param array $otherInfo
* @return MyResponse
*/
public function initUser($mobile, $openid, $accountId, $from = '', $otherInfo = [])
{
try {
$userModel = self::MAP_USER_MODEL[$from];
if ($userModel) {
$this->load->model('agent/pingan/' . $userModel, 'pingAnUsers');
} else {
$this->load->model('agent/pingan/pingan_users_model', 'pingAnUsers');
}
if ($mobile) {
$user = $this->get(['mobile' => $mobile, 'status>=' => self::STATUS_NORMAL]);
} else {
$user = $this->get(['openid' => $openid, 'status>=' => self::STATUS_DISABLE]);
}
if (!$user) {
$user = [
'mobile' => $mobile,
'openid' => $openid,
'createTime' => date('Y-m-d H:i:s'),
];
if ($accountId) {
$this->organization_model->getTeamLevel($accountId);
$pingAnUsers = $this->pingAnUsers->get(['userCode' => $accountId, 'status' => Pingan_users_model::STATUS_NORMAL]);
$teamLevel = 0;
if ($pingAnUsers['orgTeamId']) {
$teamLevel = $this->organization_model->getTeamLevel($pingAnUsers['orgTeamId']);
}
if ($teamLevel >= 4) {
$user['teamId'] = $pingAnUsers['teamId'] ?: 0;
}
$user['depId'] = $pingAnUsers['depId'] ?: 0;
$user['areaId'] = $pingAnUsers['areaId'] ?: 0;
$user['pinganUserId'] = $pingAnUsers['id'] ?: 0;
$user['orgId'] = $pingAnUsers['orgId'] ?: 0;
$user['orgTeamId'] = $pingAnUsers['orgTeamId'] ?: 0;
}
$otherInfo['nickname'] && $user['nickname'] = $otherInfo['nickname'];
$otherInfo['headimg'] && $user['headimg'] = $otherInfo['headimg'];
$otherInfo['unionid'] && $user['unionid'] = $otherInfo['unionid'];
$otherInfo['cfrom'] && $user['cfrom'] = $otherInfo['cfrom'];
$userId = $this->add($user);
if (!$userId) {
throw new Exception('添加用户失败');
}
$user['id'] = $userId;
} else {
if ($user['status'] != self::STATUS_NORMAL) {
throw new Exception('账号已被禁用');
}
//更新用户信息
$updateData = [];
$otherInfo['nickname'] && $updateData['nickname'] = $otherInfo['nickname'];
$otherInfo['headimg'] && $updateData['headimg'] = $otherInfo['headimg'];
$updateData && $this->update($updateData, ['id' => $user['id']]);
}
return new MyResponse(EXIT_SUCCESS, 'success', $user);
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
}