* @version : HuoMp 1.0 */ namespace huomp\model\weixin; use huo\model\game\GameModel; use huolib\constant\CacheConst; use huolib\constant\CommonConst; use huolib\constant\MpConfConst; use huomp\model\common\CommonModel; use think\Cache; class MpConfModel extends CommonModel { protected $table = 'mp_mp_conf'; //protected $readonly = ['mp_id']; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $type = ['ext_info' => 'array']; protected $cache_key_prefix = CacheConst::CACHE_MP_CONF_PREFIX; /** * 添加数据 * * @param $data * * @return bool */ public function addData($data) { if (empty($data)) { return false; } $_data = $data; if (empty($_data['ext_info'])) { $_data['ext_info'] = []; } if (MpConfConst::MP_CONF_TYPE_8 == $_data['type']) { $_data['ext_info'] = array_merge( $_data['ext_info'], [ 'sslcert_path' => GLOBAL_CONF_PATH.'extra/pay/wxpay/cert/'.$_data['mp_id'].'/apiclient_cert.pem', 'sslkey_path' => GLOBAL_CONF_PATH.'extra/pay/wxpay/cert/'.$_data['mp_id'].'/apiclient_key.pem' ] ); } $_obj = self::create($_data, true); if ($_obj) { $_map['id'] = $_obj->id; $_data = $this->where($_map)->find(); if (false == $_data) { return false; } if (is_object($_data)) { $_data = $_data->toArray(); } $_cache_key = $this->cache_key_prefix.$_data['id']; $_cache_mp_key = $this->cache_key_prefix.$_data['mp_id']; Cache::set($_cache_key, $_data); Cache::set($_cache_mp_key, $_data); return $_data['id']; } return false; } /** * 更新数据 * * @param array $data 数据 * @param int $id ID * * @return bool */ public function updateData($data, $id) { $_map['id'] = $id; $_data = $this->getDataById($id); $_data = array_merge($_data, $data); $_rs = self::update($_data, $_map, true); if (false == $_rs) { return false; } $_cache_key = $this->cache_key_prefix.$_data['id']; $_cache_mp_key = $this->cache_key_prefix.$_data['mp_id']; $_cache_app_key = $this->cache_key_prefix.$_data['app_id'].$_data['type']; Cache::set($_cache_key, $_data); Cache::set($_cache_mp_key, $_data); Cache::set($_cache_app_key, $_data); return true; } /** * 根据Id获取mp_id * * @param $id * * @return mixed|string */ public function getMpIdById($id) { $_data = $this->getDataById($id); if (empty($_data)) { return ''; } return $_data['mp_id']; } /** * 根据Id获取数据 * * @param int $id * * @return array|bool */ public function getDataById($id) { if (empty($id)) { return false; } $_cache_key = $this->cache_key_prefix.$id; $_data = Cache::get($_cache_key); if (!empty($_data)) { //return $_data; } $_map['id'] = $id; $_data = $this->where($_map)->find(); if (false == $_data) { return false; } if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::set($_cache_key, $_data); } return $_data; } /** * 根据微信账号Id获取数据 * * @param string $mp_id 微信ID * * @return array|bool */ public function getDataByMpId($mp_id) { if (empty($mp_id)) { return false; } $_cache_mp_key = $this->cache_key_prefix.$mp_id; $_data = Cache::get($_cache_mp_key); if (!empty($_data)) { //return $_data; } $_map['mp_id'] = $mp_id; $_data = $this->where($_map)->find(); if (false == $_data) { return false; } if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::set($_cache_mp_key, $_data); } return $_data; } /** * /** * 根据appId获取数据 * * @param int $app_id * @param int $type * * @return array|bool */ public function getDataByAppId($app_id, $type) { $_cache_app_key = $this->cache_key_prefix.$app_id.$type; $_data = Cache::get($_cache_app_key); if (!empty($_data)) { //return $_data; } $_map = [ 'app_id' => $app_id, 'type' => $type ]; $_data = $this->where($_map)->find(); if (false == $_data) { return false; } if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::set($_cache_app_key, $_data); } return $_data; } /** * 根据配置ID获取游戏ID * * @param string $mp_id 微信ID * * @return bool|mixed */ public function getAppIdByMpId($mp_id) { $_data = $this->getDataByMpId($mp_id); if (empty($_data)) { return false; } return $_data['app_id']; } /** * 根据游戏ID获取配置ID * * @param int $app_id * @param int $type * * @return bool|mixed */ public function getIdByAppId($app_id, $type) { $_data = $this->getDataByAppId($app_id, $type); if (empty($_data)) { return false; } return $_data['id']; } /** * 根据微信Id获取Id * * @param string $mp_id * * @return string|false */ public function getIdByMpId($mp_id) { $_data = $this->getDataByMpId($mp_id); if (empty($_data)) { return false; } return $_data['id']; } /** * 根据微信Id获取秘钥 * * @param string $mp_id * * @return string|false */ public function getSecretByMpId($mp_id) { $_data = $this->getDataByMpId($mp_id); if (empty($_data)) { return false; } return $_data['app_secret']; } /** * 获取mp_id Name * * @param array $type 类型 * * @return array */ public function getMpIdName($type = []) { $_map = []; if (!empty($type)) { $_map['type'] = $type; } return self::where($_map)->column('wx_name', 'mp_id'); } /** * 获取ID Name * * @param array $type 类型 * * @return array */ public function getIdName($type = []) { $_map = []; if (!empty($type)) { $_map['type'] = $type; } return self::where($_map)->column('wx_name', 'id'); } /** * 更新数据 * * @param $app_id * @param $type * @param $data * * @return bool */ public function updateGameMpConf($app_id, $type, $data) { $_data = $this->getDataByAppId($app_id, $type); if (empty($_data)) { $_mp_data = $data; $_mp_data['app_id'] = $app_id; $_mp_data['type'] = MpConfConst::MP_CONF_TYPE_6; $_rs = $this->addData($_mp_data); } else { $_rs = $this->updateData($data, $_data['id']); } return $_rs; } /** * 删除信息 * * @param $mp_id * * @return bool */ public function deleteData($mp_id) { $_map['mp_id'] = $mp_id; $_data = $this->getDataByMpId($mp_id); $_data['is_delete'] = CommonConst::CONST_DELETED; $_data['delete_time'] = time(); $_rs = $this->updateData($_data, $_data['id']); if (false == $_rs) { return false; } $_cache_key = $this->cache_key_prefix.$_data['id']; $_cache_mp_key = $this->cache_key_prefix.$_data['mp_id']; $_cache_app_key = $this->cache_key_prefix.$_data['app_id'].$_data['type']; Cache::rm($_cache_key); Cache::rm($_cache_mp_key); Cache::rm($_cache_app_key); $_omp_model = new OaMpModel(); $_omc_model = new OaMchModel(); switch ($_data['type']) { case MpConfConst::MP_CONF_TYPE_7: /* 公众号删除关联小程序\关联商户 */ $_omp_model->where(['oa_id' => $mp_id])->delete(); $_omc_model->where(['oa_id' => $mp_id])->delete(); break; case MpConfConst::MP_CONF_TYPE_8: /* 商户删除关联公众号 */ $_omc_model->where(['mch_id' => $mp_id])->delete(); break; case MpConfConst::MP_CONF_TYPE_9: /* 开放平台删除关联账号 */ $ids = self::where(['parent' => $_data['id']])->column('id'); if (!empty($ids)) { foreach ($ids as $_V) { self::updateData(['parent' => 0], $_V); } } break; default: } return true; } public function ext() { return $this->hasOne(MpConfExtModel::class, 'conf_id', 'id'); } public function game() { return $this->belongsTo(GameModel::class, 'app_id', 'id'); } }