DiverseYouthNightSchool/application/common/model/dyqc/ManystoreShop.php

483 lines
14 KiB
PHP
Raw Normal View History

<?php
namespace app\common\model\dyqc;
use app\common\model\BaseModel;
use app\common\model\school\classes\ClassesLib;
use app\common\model\school\classes\Teacher;
use app\common\model\User;
use app\manystore\model\Manystore;
use app\manystore\model\ManystoreAuthGroup;
use app\manystore\model\ManystoreAuthGroupAccess;
use fast\Random;
use think\Cache;
use think\Model;
class ManystoreShop extends BaseModel
{
// 表名
protected $name = 'manystore_shop';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
'status_text',
'auth_time_text',
'create_time_text',
'update_time_text'
];
// 拼音对象
protected static $pinyin = null;
protected static function init()
{
self::$pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
}
/** 专家团队
* @return \think\model\relation\HasMany
*/
public function teachers()
{
return $this->hasMany(Teacher::class,'shop_id')->where("status","1")->order("weigh desc");
}
public function getLogoAttr($value, $data)
{
if (!empty($value)) return cdnurl($value, true);
}
public function getImageAttr($value, $data)
{
if (!empty($value)) return cdnurl($value, true);
}
public function getImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function getYyzzImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function getStatusList()
{
return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
}
public function getStatusTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
$list = $this->getStatusList();
return isset($list[$value]) ? $list[$value] : '';
}
public function getAuthTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['auth_time']) ? $data['auth_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getCreateTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getUpdateTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setAuthTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setCreateTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
protected function setUpdateTimeAttr($value)
{
return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
}
public function admin()
{
return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
/** 机构详情
* @param $id
* @throws \think\exception\DbException
*/
public function detail($id){
$self = $this->get($id,['teachers']);
//一对多隐藏字段方法
// foreach ($self['teachers'] as $k=>$v){
// $v->visible(['id','name','expert_image','expert_content']);
// }
// $self->visible(['id','name','expert_image','expert_content']);
$self["hot"] = $this->getHotClasses($id,$limit=5);
return $self;
}
public function getHotClasses($id,$limit=5){
$data = ClassesLib::where("shop_id",$id)
->where("status",'1')
->limit($limit)
->order("hot desc,weigh desc")
->field("title,headimage,price,hot,weigh")
->select();
return $data;
}
/**申请机构
* @param $type 申请类型 类型:1=个人,2=机构
* @param int $user_id 申请人
* @param $params 申请参数
* @param bool $check
* @param bool $trans
* @return bool
* @throws \Exception
*/
public function apply($type,$user_id=0,$params=[],$check=false,$trans=false){
if($check){
$user = User::get($user_id);
if(!$user)throw new \Exception("用户不存在");
//已经是机构
$shop = ManystoreShop::where( 'user_id',$user_id)->where("status","1")->find();
if($shop)throw new \Exception("已申请机构,请勿重复申请");
//验证参数
//$type 1=个人,2=机构
if(!in_array($type,['1','2']))throw new \Exception("类型参数错误");
//switch不同类型type做不同验证
//未传手机号则默认用户手机号
if(empty($params['tel'])) $params['tel'] = $user['mobile'];
switch ($type) {
case '1': //个人
$rule = [
'name' => 'require',
'tel' => 'require|number',
// 'desc' => 'require',
'front_idcard_image' => 'require',
'reverse_idcard_image' => 'require',
];
$rule_msg = [
'name.require' => '姓名必须填写',
'tel.require' => '服务电话必须填写',
'tel.number' => '服务电话必须是数字',
// 'desc.require' => '申请备注必须填写',
'front_idcard_image.require' => '身份证正面必须上传',
'reverse_idcard_image.require' => '身份证反面必须上传',
];
break;
case '2': //机构
$rule = [
'name' => 'require',
'tel' => 'require|number',
// 'desc' => 'require',
'front_idcard_image' => 'require',
'reverse_idcard_image' => 'require',
'images' => 'require',
'yyzz_images' => 'require',
];
$rule_msg = [
'name.require' => '机构名称必须填写',
'tel.require' => '服务电话必须填写',
'tel.number' => '服务电话必须是数字',
// 'desc.require' => '申请备注必须填写',
'front_idcard_image.require' => '法人身份证正面必须上传',
'reverse_idcard_image.require' => '法人身份证反面必须上传',
'images.require' => '机构环境照片必须上传',
'yyzz_images.require' => '营业执照照片必须上传',
];
break;
}
self::check($params,$rule,$rule_msg);
}
$params["type"] = $type;
// $order = self::getHaveCancelOrder($order_no);
//判断逻辑
if($trans){
self::beginTrans();
}
$res = true;
try{
//事务逻辑
//得到申请单(没有则创建)
$shop = ManystoreShop::where( 'user_id',$user_id)->find();
//如果存在申请单,则直接更新这个单的状态
//如果不存在则创建一个新单
if(!$shop){
//创建申请单
$shop = $this->creatShop($type,$user_id,$params);
}
//更新申请单状态为审核中
$shop['status']= '0';
//清空审核时间
$shop['reason']= '';
$shop['auth_time']= 0;
$shop["admin_id"] = 0;
$shop->save();
//调用订单事件
$data = ['shop' => $shop];
\think\Hook::listen('shop_apply_after', $data);
if($trans){
self::commitTrans();
}
}catch (\Exception $e){
if($trans){
self::rollbackTrans();
}
throw new \Exception($e->getMessage());
}
return $res;
}
/** 机构后台默认密码获取
* @param $type
* @param $user_id
* @param $params
*
* @throws \think\exception\DbException
*/
public function getDefaultPassword($type,$user_id,$params){
$user = User::get($user_id);
$defaultPassword = null;
switch ($type) {
case '1': //个人
//手机号 + 个人id生成的code
$defaultPassword = $user["mobile"].en_code($user['id']);
// $rule = [
// 'name' => 'require',
// 'tel' => 'require|number',
//// 'desc' => 'require',
// 'front_idcard_image' => 'require',
// 'reverse_idcard_image' => 'require',
// ];
// $rule_msg = [
// 'name.require' => '姓名必须填写',
// 'tel.require' => '服务电话必须填写',
// 'tel.number' => '服务电话必须是数字',
//// 'desc.require' => '申请备注必须填写',
// 'front_idcard_image.require' => '身份证正面必须上传',
// 'reverse_idcard_image.require' => '身份证反面必须上传',
// ];
break;
case '2': //机构
$defaultPassword = $user["mobile"].en_code($user['id']);
// $rule = [
// 'name' => 'require',
// 'tel' => 'require|number',
//// 'desc' => 'require',
// 'front_idcard_image' => 'require',
// 'reverse_idcard_image' => 'require',
// 'images' => 'require',
// 'yyzz_images' => 'require',
// ];
// $rule_msg = [
// 'name.require' => '机构名称必须填写',
// 'tel.require' => '服务电话必须填写',
// 'tel.number' => '服务电话必须是数字',
//// 'desc.require' => '申请备注必须填写',
// 'front_idcard_image.require' => '法人身份证正面必须上传',
// 'reverse_idcard_image.require' => '法人身份证反面必须上传',
// 'images.require' => '机构环境照片必须上传',
// 'yyzz_images.require' => '营业执照照片必须上传',
// ];
break;
}
return $defaultPassword;
}
public function creatShop($type,$user_id,$params){
//商家附表
$shop = (new self);
$shop_info = $shop->allowField(true)->save($params);
if($shop_info === false){
throw new \Exception($shop->getError());
}
//账号主表
$manystore_params = [
"nickname" => $params["name"],
];
$manystore_params["username"] = $params["tel"] ?: self::$pinyin->permalink($manystore_params["nickname"]);
$manystore_params["email"] = $manystore_params["username"] . "@xx.com";
$manystore_params['password'] = self::getDefaultPassword($type,$user_id,$params);
$manystore_params['shop_id'] = $shop->id;
$manystore_params['salt'] = Random::alnum();
$manystore_params['password'] = md5(md5($manystore_params['password']) . $manystore_params['salt']);
$manystore_params['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
$manystore_params['is_main'] = 1;
$model = new Manystore();
$result = $model->allowField(true)->save($manystore_params);
if ($result === false) {
throw new \Exception($model->getError());
}
$manystoreAuthGroupModel = new ManystoreAuthGroup();
$group = [];
switch ($type) {
case '1': //个人
$group['name'] = '个人认证类型';
$group['rules'] = '*';
break;
case '2': //机构
$group['name'] = '机构认证类型';
$group['rules'] = '*';
break;
}
$group['shop_id'] = $shop->id;
$group['createtime'] = time();
$group['updatetime'] = time();
$group_id = $manystoreAuthGroupModel->insertGetId($group);
if(!$group_id){
$this->error('添加失败');
}
$manystoreAuthGroupAccessModel = new ManystoreAuthGroupAccess();
$group_access = [];
$group_access['uid'] = $model->id;
$group_access['group_id'] = $group_id;
$manystoreAuthGroupAccessModel->insert($group_access);
//调用订单事件
$data = ['shop' => $shop];
\think\Hook::listen('shop_create_after', $data);
return $shop;
}
/** 机耕申请状态查询
*
*/
public static function getAuthInfo($user_id){
$auth_status = '-1'; //-1=未申请,0=待审核,1=审核通过,2=审核失败
$shop_id = 0;
$type = '1';
$reason =""; //失败原因
//得到申请单
$apply_info = self::where("user_id",$user_id)->where("status","1")->find();
if(!$apply_info)$apply_info = self::where("user_id",$user_id)->find();
//不存在说明未申请,直接返回
if(!$apply_info){
return compact('auth_status','shop_id','reason','apply_info',"type");
}
$type = $apply_info['type'];
//从申请单取到申请状态
$auth_status = $apply_info['status'];
//如果是审核失败,取失败原因
if($auth_status == '2'){
$reason = $apply_info['reason'];
}
//如果是申请成功取店铺id
if($auth_status == '1'){
$shop_id = $apply_info['shop_id'];
}
return compact('auth_status','shop_id','reason','apply_info',"type");
}
}