OaMpLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * OaMpLogichp UTF-8
  4. * 公众号关联小程序逻辑
  5. *
  6. * @date : 2018/9/11 17:20
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HuoMP 1.0
  11. */
  12. namespace huomp\logic\game;
  13. use huolib\constant\CommonConst;
  14. use huolib\constant\MpConfConst;
  15. use huomp\model\common\CommonModel;
  16. use huomp\model\weixin\MpConfModel;
  17. use huomp\model\weixin\OaMpModel;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\exception\DbException;
  21. class OaMpLogic extends CommonModel {
  22. /**
  23. * 公众号列表条件
  24. *
  25. * @param array $param
  26. *
  27. * @return array
  28. */
  29. protected function getWxWhere($param = []) {
  30. $_map = [];
  31. if (!empty($param['start_time']) && !empty($param['start_time'])) {
  32. $_map['create_time'] = ['between', [strtotime($param['start_time']),
  33. CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])]];
  34. } else if (!empty($param['start_time'])) {
  35. $_map['create_time'] = ['gt', strtotime($param['start_time'])];
  36. } else if (!empty($param['end_time'])) {
  37. $_map['create_time'] = ['lt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  38. }
  39. if (!empty($param['mobile'])) {
  40. $_map['mobile'] = $param['mobile'];
  41. }
  42. if (!empty($param['master'])) {
  43. $_map['master'] = $param['master'];
  44. }
  45. if (!empty($param['company'])) {
  46. $_map['company'] = $param['company'];
  47. }
  48. if (!empty($param['mp_id'])) {
  49. $_map['mp_id'] = $param['mp_id'];
  50. }
  51. if (!empty($param['wx_id'])) {
  52. $_map['mp_id'] = $param['wx_id'];
  53. }
  54. if (!empty($param['mch_id'])) {
  55. $_map['mp_id'] = $param['mch_id'];
  56. }
  57. if (!empty($param['email'])) {
  58. $_map['email'] = $param['email'];
  59. }
  60. if (!empty($param['wx_email'])) {
  61. $_map['email'] = $param['wx_email'];
  62. }
  63. if (!empty($param['type'])) {
  64. $_map['type'] = $param['type'];
  65. }
  66. if (!empty($param['wx_name'])) {
  67. $_map['wx_name'] = $param['wx_name'];
  68. }
  69. if (!empty($param['wx_account'])) {
  70. $_map['wx_account'] = $param['wx_account'];
  71. }
  72. $_map['is_delete'] = CommonConst::CONST_NOT_DELETE;
  73. $_map['delete_time'] = 0;
  74. return $_map;
  75. }
  76. /**
  77. * 公众号列表字段
  78. *
  79. * @param int $type
  80. *
  81. * @return array
  82. */
  83. protected function getWxBaseField($type = MpConfConst::MP_CONF_TYPE_7) {
  84. $_field = [];
  85. switch ($type) {
  86. case MpConfConst::MP_CONF_TYPE_3: /* 3 移动应用 */
  87. break;
  88. case MpConfConst::MP_CONF_TYPE_5: /* 5 网站应用 */
  89. break;
  90. case MpConfConst::MP_CONF_TYPE_6: /* 6 小程序 */
  91. $_field = [
  92. 'wx_name' => 'wx_name',
  93. 'mp_id' => 'wx_id',
  94. 'wx_logo' => 'wx_logo',
  95. 'qr_code' => 'qr_code',
  96. ];
  97. break;
  98. case MpConfConst::MP_CONF_TYPE_8: /* 8 支付 */
  99. $_field = [
  100. 'mp_id' => 'mch_id',
  101. 'wx_name' => 'mch_name',
  102. 'ext_info' => 'ext_info',
  103. ];
  104. break;
  105. case MpConfConst::MP_CONF_TYPE_9: /* 9 开发平台 */
  106. $_field = [
  107. 'wx_name' => 'wx_name',
  108. 'mp_id' => 'wx_id',
  109. 'email' => 'wx_email'
  110. ];
  111. break;
  112. case MpConfConst::MP_CONF_TYPE_10: /* 10 第三方平台 */
  113. break;
  114. case MpConfConst::MP_CONF_TYPE_7: /* 7 公众号 */
  115. default:
  116. $_field = [
  117. 'wx_name' => 'wx_name',
  118. 'mp_id' => 'wx_id',
  119. 'wx_logo' => 'wx_logo',
  120. 'wx_account' => 'wx_account',
  121. 'qr_code' => 'qr_code',
  122. 'email' => 'wx_email'
  123. ];
  124. }
  125. $_field['id'] = 'id';
  126. $_field['app_id'] = 'app_id';
  127. $_field['master'] = 'linkman';
  128. $_field['mobile'] = 'mobile';
  129. $_field['create_time'] = 'create_time';
  130. $_field['update_time'] = 'update_time';
  131. $_field['parent_id'] = 'parent_id';
  132. return $_field;
  133. }
  134. /* 获取公众号列表 */
  135. public function getWxList($param, $page = '1,10', $order = '-create_time') {
  136. $_rdata = ['count' => 0, 'list' => []];
  137. $_map = $this->getWxWhere($param);
  138. $_wx_model = new MpConfModel();
  139. $_count = $_wx_model->where($_map)->count('id');
  140. if (empty($_count)) {
  141. return $_rdata;
  142. }
  143. $_field = $this->getWxBaseField($param['type']);
  144. $_order = $_wx_model->orderFilter($order);
  145. try {
  146. $_list = $_wx_model->with('ext,game')
  147. ->where($_map)
  148. ->field($_field)
  149. ->page($page)
  150. ->order($_order)
  151. ->select();
  152. } catch (DataNotFoundException $e) {
  153. return $_rdata;
  154. } catch (ModelNotFoundException $e) {
  155. return $_rdata;
  156. } catch (DbException $e) {
  157. return $_rdata;
  158. }
  159. if (is_object($_list)) {
  160. $_list = $_list->toArray();
  161. }
  162. if (empty($_list)) {
  163. return $_rdata;
  164. }
  165. switch ($param['type']) {
  166. case MpConfConst::MP_CONF_TYPE_3: /* 3 移动应用 */
  167. break;
  168. case MpConfConst::MP_CONF_TYPE_5: /* 5 网站应用 */
  169. break;
  170. case MpConfConst::MP_CONF_TYPE_6: /* 6 小程序 */
  171. foreach ($_list as $_k => $_v) {
  172. if (empty($_v['game']) || $_v['game']['is_delete'] == CommonConst::CONST_DELETED) {
  173. unset($_list[$_k]);
  174. }
  175. }
  176. break;
  177. case MpConfConst::MP_CONF_TYPE_8: /* 8 支付 */
  178. //获取关联的公众号
  179. foreach ($_list as $_k => $_v) {
  180. $_wx_data = (new OaMchLogic())->getWxByMchId($_v['mch_id']);
  181. $_list[$_k] = array_merge($_list[$_k], $_wx_data);
  182. /* 判断证书是否上传 */
  183. $_list[$_k]['ext_info']['sslcert_exist'] = file_exists($_v['ext_info']['sslcert_path']) ? '已上传'
  184. : '未上传';
  185. $_list[$_k]['ext_info']['sslkey_exist'] = file_exists($_v['ext_info']['sslkey_path']) ? '已上传'
  186. : '未上传';
  187. }
  188. break;
  189. case MpConfConst::MP_CONF_TYPE_9: /* 9 开发平台 */
  190. break;
  191. case MpConfConst::MP_CONF_TYPE_10: /* 10 第三方平台 */
  192. break;
  193. case MpConfConst::MP_CONF_TYPE_7: /* 7 公众号 */
  194. default:
  195. foreach ($_list as $_k => $_v) {
  196. $_list[$_k]['game_sum'] = (new OaMpModel())->getSumGame($_v['wx_id']);
  197. }
  198. }
  199. $_rdata['count'] = $_count;
  200. $_rdata['list'] = $_list;
  201. return $_rdata;
  202. }
  203. /**
  204. * 获取公众号关联游戏列表
  205. *
  206. * @param $wx_id
  207. * @param string $page
  208. * @param string $order
  209. *
  210. * @return array
  211. */
  212. public function getGames($wx_id, $page = '1,10', $order = '-id') {
  213. $_rdata = ['count' => 0, 'list' => []];
  214. $_map = ['oa_id' => $wx_id];
  215. $_model = new OaMpModel();
  216. $_count = $_model->where($_map)->count('id');
  217. if (empty($_count)) {
  218. return $_rdata;
  219. }
  220. $_order = $_model->orderFilter($order);
  221. try {
  222. $_datas = $_model->with('game,gmini')
  223. ->where($_map)
  224. ->page($page)
  225. ->order($_order)
  226. ->select();
  227. } catch (DataNotFoundException $e) {
  228. return $_rdata;
  229. } catch (ModelNotFoundException $e) {
  230. return $_rdata;
  231. } catch (DbException $e) {
  232. return $_rdata;
  233. }
  234. if (is_object($_datas)) {
  235. $_datas = $_datas->toArray();
  236. }
  237. if (empty($_datas)) {
  238. return $_rdata;
  239. }
  240. $_list = [];
  241. foreach ($_datas as $_k => $_v) {
  242. $_data = [];
  243. $_data['id'] = $_v['id'];
  244. $_data['app_id'] = $_v['app_id'];
  245. $_data['game'] = empty($_v['game']) ? '' : $_v['game']['name'];
  246. $_data['status'] = empty($_v['game']) ? '' : $_v['game']['status_label'];
  247. $_data['classify'] = empty($_v['game']) ? '' : $_v['game']['classify_label'];
  248. $_data['icon'] = empty($_v['game']) ? '' : $_v['game']['icon'];
  249. $_data['mini_app_id'] = empty($_v['gmini']) ? '' : $_v['gmini']['mini_app_id'];
  250. $_list[] = $_data;
  251. }
  252. $_rdata['count'] = $_count;
  253. $_rdata['list'] = $_list;
  254. return $_rdata;
  255. }
  256. /**
  257. * 游戏关联公众号条件
  258. *
  259. * @param array $param
  260. *
  261. * @return array
  262. */
  263. public function getGwWhere($param = []) {
  264. $_map = ['app_id' => $param['app_id']];
  265. $_where = $this->getWxWhere($param);
  266. $_where['type'] = MpConfConst::MP_CONF_TYPE_7; //只能是公众号
  267. if (!empty($param['wx_email']) || !empty($param['wx_name']) || !empty($param['linkman'])
  268. || !empty($param['mobile'])) {
  269. $_wxids = (new MpConfModel())->where($_where)->column('mp_id');
  270. $_map['oa_id'] = ['in', $_wxids];
  271. }
  272. return $_map;
  273. }
  274. /**
  275. * 获取游戏关联公众号列表
  276. *
  277. * @param $param
  278. * @param string $page
  279. * @param string $order
  280. *
  281. * @return array
  282. */
  283. public function getGameWxs($param, $page = '1,10', $order = '-id') {
  284. $_rdata = ['count' => 0, 'list' => []];
  285. $_map = $this->getGwWhere($param);
  286. $_model = new OaMpModel();
  287. $_count = $_model->where($_map)->count('id');
  288. if (empty($_count)) {
  289. return $_rdata;
  290. }
  291. $_order = $_model->orderFilter($order);
  292. try {
  293. $_datas = $_model->with('mc')
  294. ->where($_map)
  295. ->page($page)
  296. ->order($_order)
  297. ->select();
  298. } catch (DataNotFoundException $e) {
  299. return $_rdata;
  300. } catch (ModelNotFoundException $e) {
  301. return $_rdata;
  302. } catch (DbException $e) {
  303. return $_rdata;
  304. }
  305. if (is_object($_datas)) {
  306. $_datas = $_datas->toArray();
  307. }
  308. if (empty($_datas)) {
  309. return $_rdata;
  310. }
  311. $_list = [];
  312. foreach ($_datas as $_k => $_v) {
  313. $_data = [];
  314. $_data['id'] = $_v['id'];
  315. $_data['wx_id'] = empty($_v['mc']) ? '' : $_v['mc']['mp_id'];
  316. $_data['wx_name'] = empty($_v['mc']) ? '' : $_v['mc']['wx_name'];
  317. $_data['wx_logo'] = empty($_v['mc']) ? '' : $_v['mc']['wx_logo'];
  318. $_data['wx_account'] = empty($_v['mc']) ? '' : $_v['mc']['wx_account'];
  319. $_data['qr_code'] = empty($_v['mc']) ? '' : $_v['mc']['qr_code'];
  320. $_data['wx_email'] = empty($_v['mc']) ? '' : $_v['mc']['email'];
  321. $_data['linkman'] = empty($_v['mc']) ? '' : $_v['mc']['master'];
  322. $_data['mobile'] = empty($_v['mc']) ? '' : $_v['mc']['mobile'];
  323. $_data['is_default'] = $_v['is_default'];
  324. $_data['is_default_label'] = MpConfConst::MP_OA_MCH_DEFAULT_2 == $_v['is_default'] ? '默认' : '非默认';
  325. $_list[] = $_data;
  326. }
  327. $_rdata['count'] = $_count;
  328. $_rdata['list'] = $_list;
  329. return $_rdata;
  330. }
  331. /**
  332. * 获取公众号绑定的游戏id数组
  333. *
  334. * @param $oa_id
  335. *
  336. * @return array
  337. */
  338. public function getAppIdsByOaId($oa_id) {
  339. return (new OaMpModel())->where(['oa_id' => $oa_id])->column('app_id');
  340. }
  341. /**
  342. * 获取游戏绑定的公众号id数组
  343. *
  344. * @param $app_id
  345. *
  346. * @return array
  347. */
  348. public function getOaIdsByAppId($app_id) {
  349. return (new OaMpModel())->where(['app_id' => $app_id])->column('oa_id');
  350. }
  351. /**
  352. *设置默认
  353. *
  354. * @param $id
  355. * @param int $is_default
  356. *
  357. * @return bool
  358. */
  359. public function setDefault($id, $is_default = MpConfConst::MP_OA_MCH_DEFAULT_2) {
  360. $_model = new OaMpModel();
  361. $_data = $_model->getInfoById($id);
  362. $_map = ['app_id' => $_data['app_id']];
  363. /* 先将全部设置为非默认 */
  364. $_model->update(['is_default' => MpConfConst::MP_OA_MCH_DEFAULT_1], $_map);
  365. $_map = ['id' => $id];
  366. $_data = ['is_default' => $is_default];
  367. $_rs = $_model->update($_data, $_map);
  368. if (false == $_rs) {
  369. return false;
  370. }
  371. return true;
  372. }
  373. /**
  374. * 获取小程序默认公众号APPID
  375. *
  376. * @param $app_id
  377. *
  378. * @return mixed
  379. */
  380. public function getDefaultOaId($app_id) {
  381. $_map = ['app_id' => $app_id, 'is_default' => MpConfConst::MP_OA_MCH_DEFAULT_2];
  382. $_oa_id = (new OaMpModel())->where($_map)->value('oa_id');
  383. if (empty($_oa_id)) {
  384. return '';
  385. }
  386. return $_oa_id;
  387. }
  388. }