123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?php
- /**
- * MpConfModel.php UTF-8
- *
- *
- * @date : 2018/9/14 20:31
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : wuyonghong <wyh@huosdk.com>
- * @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');
- }
- }
|