MpConfModel.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /**
  3. * MpConfModel.php UTF-8
  4. *
  5. *
  6. * @date : 2018/9/14 20:31
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huomp\model\weixin;
  13. use huo\model\game\GameModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use huolib\constant\MpConfConst;
  17. use huomp\model\common\CommonModel;
  18. use think\Cache;
  19. class MpConfModel extends CommonModel {
  20. protected $table = 'mp_mp_conf';
  21. //protected $readonly = ['mp_id'];
  22. // 开启自动写入时间戳字段
  23. protected $autoWriteTimestamp = true;
  24. protected $type = ['ext_info' => 'array'];
  25. protected $cache_key_prefix = CacheConst::CACHE_MP_CONF_PREFIX;
  26. /**
  27. * 添加数据
  28. *
  29. * @param $data
  30. *
  31. * @return bool
  32. */
  33. public function addData($data) {
  34. if (empty($data)) {
  35. return false;
  36. }
  37. $_data = $data;
  38. if (empty($_data['ext_info'])) {
  39. $_data['ext_info'] = [];
  40. }
  41. if (MpConfConst::MP_CONF_TYPE_8 == $_data['type']) {
  42. $_data['ext_info'] = array_merge(
  43. $_data['ext_info'],
  44. [
  45. 'sslcert_path' => GLOBAL_CONF_PATH.'extra/pay/wxpay/cert/'.$_data['mp_id'].'/apiclient_cert.pem',
  46. 'sslkey_path' => GLOBAL_CONF_PATH.'extra/pay/wxpay/cert/'.$_data['mp_id'].'/apiclient_key.pem'
  47. ]
  48. );
  49. }
  50. $_obj = self::create($_data, true);
  51. if ($_obj) {
  52. $_map['id'] = $_obj->id;
  53. $_data = $this->where($_map)->find();
  54. if (false == $_data) {
  55. return false;
  56. }
  57. if (is_object($_data)) {
  58. $_data = $_data->toArray();
  59. }
  60. $_cache_key = $this->cache_key_prefix.$_data['id'];
  61. $_cache_mp_key = $this->cache_key_prefix.$_data['mp_id'];
  62. Cache::set($_cache_key, $_data);
  63. Cache::set($_cache_mp_key, $_data);
  64. return $_data['id'];
  65. }
  66. return false;
  67. }
  68. /**
  69. * 更新数据
  70. *
  71. * @param array $data 数据
  72. * @param int $id ID
  73. *
  74. * @return bool
  75. */
  76. public function updateData($data, $id) {
  77. $_map['id'] = $id;
  78. $_data = $this->getDataById($id);
  79. $_data = array_merge($_data, $data);
  80. $_rs = self::update($_data, $_map, true);
  81. if (false == $_rs) {
  82. return false;
  83. }
  84. $_cache_key = $this->cache_key_prefix.$_data['id'];
  85. $_cache_mp_key = $this->cache_key_prefix.$_data['mp_id'];
  86. $_cache_app_key = $this->cache_key_prefix.$_data['app_id'].$_data['type'];
  87. Cache::set($_cache_key, $_data);
  88. Cache::set($_cache_mp_key, $_data);
  89. Cache::set($_cache_app_key, $_data);
  90. return true;
  91. }
  92. /**
  93. * 根据Id获取mp_id
  94. *
  95. * @param $id
  96. *
  97. * @return mixed|string
  98. */
  99. public function getMpIdById($id) {
  100. $_data = $this->getDataById($id);
  101. if (empty($_data)) {
  102. return '';
  103. }
  104. return $_data['mp_id'];
  105. }
  106. /**
  107. * 根据Id获取数据
  108. *
  109. * @param int $id
  110. *
  111. * @return array|bool
  112. */
  113. public function getDataById($id) {
  114. if (empty($id)) {
  115. return false;
  116. }
  117. $_cache_key = $this->cache_key_prefix.$id;
  118. $_data = Cache::get($_cache_key);
  119. if (!empty($_data)) {
  120. //return $_data;
  121. }
  122. $_map['id'] = $id;
  123. $_data = $this->where($_map)->find();
  124. if (false == $_data) {
  125. return false;
  126. }
  127. if (is_object($_data)) {
  128. $_data = $_data->toArray();
  129. }
  130. if (!empty($_data)) {
  131. Cache::set($_cache_key, $_data);
  132. }
  133. return $_data;
  134. }
  135. /**
  136. * 根据微信账号Id获取数据
  137. *
  138. * @param string $mp_id 微信ID
  139. *
  140. * @return array|bool
  141. */
  142. public function getDataByMpId($mp_id) {
  143. if (empty($mp_id)) {
  144. return false;
  145. }
  146. $_cache_mp_key = $this->cache_key_prefix.$mp_id;
  147. $_data = Cache::get($_cache_mp_key);
  148. if (!empty($_data)) {
  149. //return $_data;
  150. }
  151. $_map['mp_id'] = $mp_id;
  152. $_data = $this->where($_map)->find();
  153. if (false == $_data) {
  154. return false;
  155. }
  156. if (is_object($_data)) {
  157. $_data = $_data->toArray();
  158. }
  159. if (!empty($_data)) {
  160. Cache::set($_cache_mp_key, $_data);
  161. }
  162. return $_data;
  163. }
  164. /**
  165. * /**
  166. * 根据appId获取数据
  167. *
  168. * @param int $app_id
  169. * @param int $type
  170. *
  171. * @return array|bool
  172. */
  173. public function getDataByAppId($app_id, $type) {
  174. $_cache_app_key = $this->cache_key_prefix.$app_id.$type;
  175. $_data = Cache::get($_cache_app_key);
  176. if (!empty($_data)) {
  177. //return $_data;
  178. }
  179. $_map = [
  180. 'app_id' => $app_id,
  181. 'type' => $type
  182. ];
  183. $_data = $this->where($_map)->find();
  184. if (false == $_data) {
  185. return false;
  186. }
  187. if (is_object($_data)) {
  188. $_data = $_data->toArray();
  189. }
  190. if (!empty($_data)) {
  191. Cache::set($_cache_app_key, $_data);
  192. }
  193. return $_data;
  194. }
  195. /**
  196. * 根据配置ID获取游戏ID
  197. *
  198. * @param string $mp_id 微信ID
  199. *
  200. * @return bool|mixed
  201. */
  202. public function getAppIdByMpId($mp_id) {
  203. $_data = $this->getDataByMpId($mp_id);
  204. if (empty($_data)) {
  205. return false;
  206. }
  207. return $_data['app_id'];
  208. }
  209. /**
  210. * 根据游戏ID获取配置ID
  211. *
  212. * @param int $app_id
  213. * @param int $type
  214. *
  215. * @return bool|mixed
  216. */
  217. public function getIdByAppId($app_id, $type) {
  218. $_data = $this->getDataByAppId($app_id, $type);
  219. if (empty($_data)) {
  220. return false;
  221. }
  222. return $_data['id'];
  223. }
  224. /**
  225. * 根据微信Id获取Id
  226. *
  227. * @param string $mp_id
  228. *
  229. * @return string|false
  230. */
  231. public function getIdByMpId($mp_id) {
  232. $_data = $this->getDataByMpId($mp_id);
  233. if (empty($_data)) {
  234. return false;
  235. }
  236. return $_data['id'];
  237. }
  238. /**
  239. * 根据微信Id获取秘钥
  240. *
  241. * @param string $mp_id
  242. *
  243. * @return string|false
  244. */
  245. public function getSecretByMpId($mp_id) {
  246. $_data = $this->getDataByMpId($mp_id);
  247. if (empty($_data)) {
  248. return false;
  249. }
  250. return $_data['app_secret'];
  251. }
  252. /**
  253. * 获取mp_id Name
  254. *
  255. * @param array $type 类型
  256. *
  257. * @return array
  258. */
  259. public function getMpIdName($type = []) {
  260. $_map = [];
  261. if (!empty($type)) {
  262. $_map['type'] = $type;
  263. }
  264. return self::where($_map)->column('wx_name', 'mp_id');
  265. }
  266. /**
  267. * 获取ID Name
  268. *
  269. * @param array $type 类型
  270. *
  271. * @return array
  272. */
  273. public function getIdName($type = []) {
  274. $_map = [];
  275. if (!empty($type)) {
  276. $_map['type'] = $type;
  277. }
  278. return self::where($_map)->column('wx_name', 'id');
  279. }
  280. /**
  281. * 更新数据
  282. *
  283. * @param $app_id
  284. * @param $type
  285. * @param $data
  286. *
  287. * @return bool
  288. */
  289. public function updateGameMpConf($app_id, $type, $data) {
  290. $_data = $this->getDataByAppId($app_id, $type);
  291. if (empty($_data)) {
  292. $_mp_data = $data;
  293. $_mp_data['app_id'] = $app_id;
  294. $_mp_data['type'] = MpConfConst::MP_CONF_TYPE_6;
  295. $_rs = $this->addData($_mp_data);
  296. } else {
  297. $_rs = $this->updateData($data, $_data['id']);
  298. }
  299. return $_rs;
  300. }
  301. /**
  302. * 删除信息
  303. *
  304. * @param $mp_id
  305. *
  306. * @return bool
  307. */
  308. public function deleteData($mp_id) {
  309. $_map['mp_id'] = $mp_id;
  310. $_data = $this->getDataByMpId($mp_id);
  311. $_data['is_delete'] = CommonConst::CONST_DELETED;
  312. $_data['delete_time'] = time();
  313. $_rs = $this->updateData($_data, $_data['id']);
  314. if (false == $_rs) {
  315. return false;
  316. }
  317. $_cache_key = $this->cache_key_prefix.$_data['id'];
  318. $_cache_mp_key = $this->cache_key_prefix.$_data['mp_id'];
  319. $_cache_app_key = $this->cache_key_prefix.$_data['app_id'].$_data['type'];
  320. Cache::rm($_cache_key);
  321. Cache::rm($_cache_mp_key);
  322. Cache::rm($_cache_app_key);
  323. $_omp_model = new OaMpModel();
  324. $_omc_model = new OaMchModel();
  325. switch ($_data['type']) {
  326. case MpConfConst::MP_CONF_TYPE_7: /* 公众号删除关联小程序\关联商户 */
  327. $_omp_model->where(['oa_id' => $mp_id])->delete();
  328. $_omc_model->where(['oa_id' => $mp_id])->delete();
  329. break;
  330. case MpConfConst::MP_CONF_TYPE_8: /* 商户删除关联公众号 */
  331. $_omc_model->where(['mch_id' => $mp_id])->delete();
  332. break;
  333. case MpConfConst::MP_CONF_TYPE_9: /* 开放平台删除关联账号 */
  334. $ids = self::where(['parent' => $_data['id']])->column('id');
  335. if (!empty($ids)) {
  336. foreach ($ids as $_V) {
  337. self::updateData(['parent' => 0], $_V);
  338. }
  339. }
  340. break;
  341. default:
  342. }
  343. return true;
  344. }
  345. public function ext() {
  346. return $this->hasOne(MpConfExtModel::class, 'conf_id', 'id');
  347. }
  348. public function game() {
  349. return $this->belongsTo(GameModel::class, 'app_id', 'id');
  350. }
  351. }