123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- <?php
- //model 的重写基类
- namespace app\common\models;
- use app\models\Linkmenu;
- use app\models\ContentModel;
- use app\modules\admin\models\Sysconfig;
- use app\modules\admin\models\Role;
- use app\modules\ucenter\models\UserGroup;
- use app\modules\ad\models\AdInd;
- use app\modules\ad\models\AdSpace;
- use Yii;
- use yii\db\ActiveRecord;
- class EActiveRecord extends ActiveRecord
- {
- public static function shortTableName()
- {
- $table_name = self::tableName();
- return str_replace(array('{{%','}}'),"",$table_name);
- }
- /**
- * 获取模型验证的第一个错误
- */
- public function returnFirstError()
- {
- $errors = $this->getErrors();
- if(!empty($errors)){
- $firstError = array_splice($errors, 0,1);
- $attribute = key($firstError);
- return $this->getFirstError($attribute);
- }
- return null;
- }
- //获取最后插入ID
- public static function getLastInsertId()
- {
- return Yii::$app->db->getLastInsertID();
- }
- /**
- * @返回主键.
- */
- public static function modelPrimaryKey()
- {
- $pkInfo = parent::primaryKey();
- return $pkInfo[0];
- }
- //禁用状态
- public static function disableOptions($k=null)
- {
- $options = array('1'=>'禁用','0'=>'启用');
- if(is_numeric($k))return $options[$k];
- return $options;
- }
- //是否选择
- public static function boolOptions($k=null)
- {
- $options = array('1'=>'是','0'=>'否');
- if(is_numeric($k))return $options[$k];
- return $options;
- }
- //返回某个表(传入表名不含前缀)的主键
- public static function getPrikey($table=null)
- {
- if($table==null)$table = self::shortTableName();
- //查询表的主键
- $dsnInfo = explode(";",Yii::$app->db->dsn);
- $dbInfo = explode("=",$dsnInfo['1']);
- //$sql = "SELECT k.column_name,k.table_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING ( constraint_name, table_schema, table_name ) WHERE t.constraint_type = 'PRIMARY KEY' AND k.table_name = '".Yii::$app->db->tablePrefix.$table."' AND t.table_schema = '$dbInfo[1]'";
- $sql = "SELECT column_name FROM information_schema.key_column_usage WHERE table_name='".Yii::$app->db->tablePrefix.$table."' AND constraint_name LIKE '%_pkey'";
- $result = Yii::$app->db->createCommand($sql)->queryOne();
- return $result['column_name'];
- }
- //返回属性名称
- public static function getAttributeName($field)
- {
- if($field=='operate')
- {
- return Yii::t('attr',$field);
- }
- else
- {
- $table_name = self::shortTableName();
- return Yii::t('attr',$table_name.'.'.$field);
- }
- }
- //后台角色
- public static function roleOptions($k=0)
- {
- $resultList = Role::find()->where("disabled=0")->all();
- foreach($resultList as $result)
- {
- $options[$result->role_id] = $result->role_name;
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //联动菜单
- public static function linkmenuOptions($k=0)
- {
- $resultList = Linkmenu::find()->where("key_id=0")->all();
- foreach($resultList as $result)
- {
- $options[$result->menu_id] = $result->name;
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //系统参数选项
- public static function sysconfigOptions($name,$k=null)
- {
- $sysconfig = Sysconfig::find()->where("name='$name'")->one();
- $resultList = json_decode($sysconfig->value,true);
- if($sysconfig->type==1)
- {
- if(is_array($resultList))foreach($resultList as $id=>$name)
- {
- $options[$id] = $name;
- }
- if(!is_null($k))return $options[$k];
- }
- else
- {
- if(is_array($resultList))foreach($resultList as $name)
- {
- $options[] = $name;
- }
- if($k!='') return $k;
- }
- return $options;
- }
- //是否选择(字段管理页面)
- public static function fieldBoolOptions($k=null)
- {
- $options = array('0'=>'否','1'=>'是');
- if(is_numeric($k))return $options[$k];
- return $options;
- }
- //返回广告行业选项
- public function indOptions($k=NULL)
- {
- $resultList = AdInd::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
- if(is_array($resultList))foreach($resultList as $result)
- {
- $options[$result->ind_id] = $result->name;
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //返回广告位选项
- public function adspaceOptions($k=NULL)
- {
- $resultList = AdSpace::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
- if(is_array($resultList))foreach($resultList as $result)
- {
- $options[$result->space_id] = $result->name."(".$result->space_no.")";
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //返回用户组选项
- public static function userGroupOptions($k=NULL)
- {
- $resultList = UserGroup::find()->where("disabled=0")->orderBy(['list_order'=>SORT_ASC])->all();
- if(is_array($resultList))foreach($resultList as $result)
- {
- $options[$result->group_id] = $result->group_name;
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //返回VIP类型选择
- public static function viptypeOptions($k=null,$cat=null)
- {
- if($cat==null)
- {
- $vipTypeList = \app\modules\ucenter\models\UserVipType::find()->where("status=1")->orderBy(['vip_cat'=>SORT_ASC,'list_order'=>SORT_ASC])->all();
- }
- else
- {
- $vipTypeList = \app\modules\ucenter\models\UserVipType::find()->where("vip_cat='".$cat."' and status=1")->orderBy(['list_order'=>SORT_ASC])->all();
- }
- if(is_array($vipTypeList))foreach($vipTypeList as $vipType)
- {
- $options[$vipType->id] = $vipType->title;
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //返回用户等级选项
- public static function userGroupLevelOptions($group_id,$k=NULL)
- {
- $levelList = \app\modules\ucenter\models\UserGroupLevel::find()->where("user_group_id=".$group_id."")->orderBy(['id'=>SORT_ASC])->all();
- $options = [];
- if(is_array($levelList))foreach($levelList as $levelInfo)
- {
- $options[$levelInfo['id']] = $levelInfo['name'];
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //返回用户模型选项
- public static function userModelOptions($k=NULL)
- {
- $resultList = ContentModel::find()->where("type=2 and disabled=0")->all();
- if(is_array($resultList))foreach($resultList as $result)
- {
- $options[$result->model_id] = $result->name;
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //返回当前模型ID
- public static function getContentModelId()
- {
- $table_name = self::shortTableName();
- $contentModel = ContentModel::find()->where("table_name='".$table_name."'")->one();
- return $contentModel->model_id;
- }
- //返回标签链接
- public static function getTagLinksByModel($tags,$model_id)
- {
- if(empty($tags))return[];
- $tags = explode(",",$tags);
- if(is_array($tags))foreach($tags as $tag)
- {
- $tagModel = \app\models\Tag::find()->where("tag='".$tag."'")->one();
- if(!empty($tagModel))
- {
- $links[] = ['title'=>$tagModel->tag,'link'=>\app\common\components\SiteUrl::tagLink($tagModel->pinyin,$model_id)];
- }
- }
- return $links;
- }
- //返回内容首个标签
- public static function getFirstTag($tags)
- {
- $tags = explode(",",$tags);
- return $tags[0]?$tags[0]:'无';
- }
- //文本型选项
- public static function boxOptions($str,$k=null)
- {
- $typeList = explode("\n",$str);
- if(!empty($typeList))foreach($typeList as $_k) {
- $v = explode("|",$_k);
- if(!empty($v[0])){
- $options[trim($v[1])] = $v[0];
- }
- }
- if($k>0)return $options[$k];
- return $options;
- }
- //售后步骤
- public static function refundStepOptions($type,$k=null)
- {
- $type==1?$options = array('1'=>'申请退货退款','2'=>'卖家处理','3'=>'买家寄回商品','4'=>'买家收货','5'=>'退款完毕'):$options = array('1'=>'申请退款','2'=>'卖家处理','3'=>'退款完毕');
- if(is_numeric($k))return $options[$k];
- return $options;
- }
- }
|