MSystem.Class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * 系统设置Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/28
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Model\SystemSettings;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use JinDouYun\Cache\SystemCache;
  13. use JinDouYun\Model\Oem\MOem;
  14. use JinDouYun\Dao\SystemSettings\DSystem;
  15. use JinDouYun\Dao\SystemSettings\DWxBindEnterprise;
  16. use Util\WeiXin\Oplatform;
  17. class MSystem
  18. {
  19. // 授权回调地址
  20. private $redirect_uri = 'http://'.DOMAIN_WWW.'/AuthorizePage';
  21. private $objDSystem;
  22. private $userCenterId;
  23. private $enterpriseId;
  24. private $objDWxBindEnterprise;
  25. public function __construct($enterpriseId, $userCenterId = false)
  26. {
  27. $this->enterpriseId = $enterpriseId;
  28. $this->userCenterId = $userCenterId;
  29. $this->objDSystem = new DSystem('default');
  30. $this->objDWxBindEnterprise = new DWxBindEnterprise('default');
  31. }
  32. /**
  33. * type = 1
  34. * 修改系统设置
  35. * @param $params
  36. * @param $wxOriginId
  37. * @return ResultWrapper
  38. */
  39. public function updateSystemSettings($params, $wxOriginId)
  40. {
  41. $this->objDSystem->beginTransaction();
  42. if (isset($params['id']) && !empty($params['id'])) {
  43. $dbData = $this->objDSystem->get(['id' => $params['id']]);
  44. if ($dbData === false) {
  45. return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
  46. }
  47. if (empty($dbData)) return ResultWrapper::fail('查询数据为空', ErrorCode::$dberror);
  48. $params['createTime'] = $dbData['createTime'];
  49. }
  50. $dbResult = $this->objDWxBindEnterprise->replace(['enterpriseId' => $this->enterpriseId, 'wxOriginId' => $wxOriginId]);
  51. if ($dbResult === false) {
  52. $this->objDSystem->rollBack();
  53. return ResultWrapper::fail($this->objDWxBindEnterprise->error(), ErrorCode::$dberror);
  54. }
  55. $dbResult = $this->objDSystem->replace($params);
  56. if ($dbResult === false) {
  57. $this->objDSystem->rollBack();
  58. return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
  59. }
  60. $this->objDSystem->commit();
  61. $objSystemCache = new SystemCache();
  62. $objSystemCache->cacheEnterpriseRelationWxAppId($params['content'], $this->enterpriseId);
  63. return ResultWrapper::success($dbResult);
  64. }
  65. /**
  66. * 系统设置详情
  67. * @param $params
  68. * @return ResultWrapper
  69. */
  70. public function getSystemSettingsInfo($params)
  71. {
  72. $dbResult = $this->objDSystem->get(['type' => $params,'enterpriseId' => $this->enterpriseId]);
  73. if ($dbResult === false) {
  74. return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
  75. }
  76. $dbResult && $dbResult['content'] = json_decode($dbResult['content'], true);
  77. return ResultWrapper::success($dbResult);
  78. }
  79. /**
  80. * 获取预授权码(pre_auth_code)
  81. * 官网文档地址: https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Authorization_Process_Technical_Description.html
  82. * @return ResultWrapper
  83. */
  84. public function preAuthCode()
  85. {
  86. // $content = $this->objDSystem->get(['enterpriseId' => $this->enterpriseId], 'content');
  87. // if ($content === false) {
  88. // return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
  89. // }
  90. // if (empty($content)) return ResultWrapper::fail('请先填写基本信息', ErrorCode::$dberror);
  91. //
  92. // $oplatformConfigData = json_decode($content['content'],true);
  93. //获取微信配置
  94. /*
  95. $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  96. if (empty($oplatformConfigData)) {
  97. return ResultWrapper::fail('配置错误', ErrorCode::$dberror);
  98. }*/
  99. $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
  100. $host = $refererUrl['host'];
  101. $objMOem = new MOem();
  102. $result = $objMOem->getOemInfoByDomain($host);
  103. if( !$result->isSuccess() ){
  104. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  105. }
  106. $oemData = $result->getData();
  107. if( empty($oemData) ){
  108. return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
  109. }
  110. $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
  111. if( empty($oplatformConfigData) ){
  112. return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
  113. }
  114. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  115. $result = $objOplatform->preAuthCode();
  116. if (!$result->isSuccess()) {
  117. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  118. }
  119. $pre_auth_code = $result->getData();
  120. $this->redirect_uri = 'http://'.$host.'/AuthorizePage';
  121. $auth_url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=' . $oplatformConfigData['appid'] . '&pre_auth_code=' . $pre_auth_code . '&redirect_uri=' . $this->redirect_uri . '&auth_type=2';
  122. return ResultWrapper::success($auth_url);
  123. }
  124. /**
  125. * type = 4
  126. * @param $params
  127. * @return ResultWrapper
  128. */
  129. public function saveByteDanceSetting($params)
  130. {
  131. $dbResult = $this->objDSystem->replace($params);
  132. if ($dbResult === false) {
  133. return ResultWrapper::fail($this->objDSystem->error(), ErrorCode::$dberror);
  134. }
  135. //缓存字节小程序配置
  136. $objSystemCache = new SystemCache();
  137. $objSystemCache->cacheByteDanceSetting($params['content'], $this->enterpriseId);
  138. return ResultWrapper::success($dbResult);
  139. }
  140. }