MWxTemplate.Class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: XiaoMing
  5. * Date: 2020/5/8
  6. * Time: 10:09
  7. */
  8. namespace JinDouYun\Model\Manage;
  9. use Jindouyun\Cache\EnterpriseCache;
  10. use JinDouYun\Dao\Enterprise\DEnterprise;
  11. use JinDouYun\Dao\Enterprise\DEnterpriseCategory;
  12. use JinDouYun\Dao\System\DEnterpriseBindTemplate;
  13. use JinDouYun\Dao\System\DSystemTemplate;
  14. use JinDouYun\Model\System\MEnterpriseBindTemplate;
  15. use Mall\Framework\Core\ErrorCode;
  16. use Mall\Framework\Core\ResultWrapper;
  17. use Mall\Framework\Core\StatusCode;
  18. /**
  19. * 微信模版
  20. * Class MWxTemplate
  21. * @package JinDouYun\Model\Manage
  22. */
  23. class MWxTemplate
  24. {
  25. private $objDEnterpriseBindTemplate;
  26. private $objDEnterprise;
  27. private $objDSystemTemplate;
  28. private $objEnterpriseCache;
  29. /**
  30. * MWxTemplate constructor.
  31. * @throws \Exception
  32. */
  33. public function __construct()
  34. {
  35. $this->objDEnterpriseBindTemplate = new DEnterpriseBindTemplate();
  36. $this->objDEnterprise = new DEnterprise('default');
  37. $this->objDEnterprise->setTable($this->objDEnterprise->get_Table() . '_1');
  38. $this->objDSystemTemplate = new DSystemTemplate();
  39. $this->objEnterpriseCache = new EnterpriseCache();
  40. }
  41. /**
  42. * 获取小程序模版
  43. * @param $selectParams
  44. * @return ResultWrapper
  45. */
  46. public function getAll($selectParams)
  47. {
  48. $limit = $selectParams['limit'];
  49. unset($selectParams['limit']);
  50. $offset = $selectParams['offset'];
  51. unset($selectParams['offset']);
  52. $whereSql = '';
  53. !empty($selectParams['enterpriseName']) && $whereSql .= ' and e.enterpriseName like "%'.$selectParams["enterpriseName"].'%"';
  54. !empty($selectParams['isWeChatStatus']) && $whereSql .= ' and b.auditStatus = '.$selectParams["isWeChatStatus"];
  55. !empty($selectParams['oemId']) && $whereSql .= ' and e.oemId = '.$selectParams["oemId"];
  56. $countField = ' COUNT(b.id) AS total ';
  57. $fields = 'b.id,b.templateId,b.enterpriseId,b.enableStatus,b.qrcodeImg,b.createTime,b.auditId,b.auditStatus,b.reason';
  58. $Sql = 'SELECT ' . $fields. ' FROM qianniao_enterprise_bind_template AS b
  59. LEFT JOIN qianniao_enterprise_1 AS e ON b.enterpriseId = e.id
  60. WHERE b.enableStatus = ' . StatusCode::$standard . $whereSql.' ORDER BY ID DESC LIMIT '. $offset . ',' . $limit ;
  61. $countSql = 'SELECT ' . $countField. ' FROM qianniao_enterprise_bind_template AS b
  62. LEFT JOIN qianniao_enterprise_1 AS e ON b.enterpriseId = e.id
  63. WHERE b.enableStatus = ' . StatusCode::$standard . $whereSql ;
  64. $dbResult = $this->objDEnterpriseBindTemplate->query($Sql);
  65. if ($dbResult === false) {
  66. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  67. }
  68. $total = $this->objDEnterpriseBindTemplate->query($countSql);
  69. $return = [
  70. 'data' => self::formatMapping($dbResult),
  71. 'total' => isset($total[0]['total']) ? $total[0]['total'] : 0,
  72. ];
  73. return ResultWrapper::success($return);
  74. }
  75. /**
  76. * @param $data
  77. * @return mixed
  78. */
  79. private function formatMapping($data)
  80. {
  81. if (empty($data)) return $data;
  82. //获取企业
  83. $dbResult = $this->objDEnterprise->select(['id' => array_values(array_unique(array_column($data, 'enterpriseId')))], 'id,enterpriseName,token');
  84. if ($dbResult === false) {
  85. $enterprise = [];
  86. } else {
  87. $mapping = [];
  88. foreach ($dbResult as $val) {
  89. $mapping[$val['id']] = $val;
  90. }
  91. $enterprise = $mapping;
  92. }
  93. unset($dbResult);
  94. //获取模版
  95. $dbResult = $this->objDSystemTemplate->select(['id' => array_values(array_unique(array_column($data, 'templateId')))], 'id,title,weixinTemplateId,type,version');
  96. if ($dbResult === false) {
  97. $template = [];
  98. } else {
  99. $mapping = [];
  100. foreach ($dbResult as $val) {
  101. $mapping[$val['id']] = $val;
  102. }
  103. $template = $mapping;
  104. }
  105. //mapping
  106. foreach ($data as &$value) {
  107. $value['enterpriseName'] = isset($enterprise[$value['enterpriseId']]['enterpriseName']) ? $enterprise[$value['enterpriseId']]['enterpriseName'] : '';//企业名称
  108. $value['token'] = isset($enterprise[$value['enterpriseId']]['token']) ? $enterprise[$value['enterpriseId']]['token'] : '';//企业token
  109. $value['title'] = isset($template[$value['templateId']]['title']) ? $template[$value['templateId']]['title'] : '';//模版名称名称
  110. $value['type'] = isset($template[$value['templateId']]['type']) ? $template[$value['templateId']]['type'] : 1;//模版类型
  111. $value['typeName'] = $value['type'] == 1 ? '系统模版' : '万能模版';//模版类型名
  112. $value['weixinTemplateId'] = isset($template[$value['templateId']]['weixinTemplateId']) ? $template[$value['templateId']]['weixinTemplateId'] : 0;//微信模版id
  113. $value['auditMsg'] = self::auditStatusAttr($value['auditStatus']);//状态描述
  114. $value['version'] = isset($template[$value['templateId']]['version']) ? $template[$value['templateId']]['version'] : '';//模版版本
  115. if(!empty($value['reason'])){
  116. $value['reason'] = json_decode($value['reason'],true);
  117. }else{
  118. $value['reason'] = (object)[];
  119. }
  120. }
  121. return $data;
  122. }
  123. /**
  124. * @param $value
  125. * @return mixed|string
  126. */
  127. public static function auditStatusAttr($value)
  128. {
  129. $auditStatus = [
  130. '0' => '审核成功',
  131. '1' => '审核拒绝',
  132. '2' => '审核中',
  133. '3' => '已撤回',
  134. '4' => '延后',
  135. '5' => '提交审核',
  136. '6' => '提交代码',
  137. '7' => '正在使用'
  138. ];
  139. return isset($auditStatus[$value]) ? $auditStatus[$value] : '未知';
  140. }
  141. /**
  142. * 提交代码&&提交审核
  143. * @param $params
  144. * @param $token
  145. * @return ResultWrapper
  146. */
  147. public function submitCodeAndAudit($params)
  148. {
  149. foreach ($params as $value) {
  150. $objMEnterpriseBindTemplate = new MEnterpriseBindTemplate(null, $value['enterpriseId']);
  151. //提交代码
  152. $dbResult = $objMEnterpriseBindTemplate->submitCode(
  153. [
  154. 'weixinTemplateId' => $value['weixinTemplateId'],//微信模版id
  155. 'templateId' => $value['templateId'],//模版id
  156. 'enableStatus' => StatusCode::$standard,//启用
  157. 'enterpriseId' => $value['enterpriseId'],//企业id
  158. 'version' => $value['version'],//版本号
  159. ],
  160. $value['token']);
  161. if (!$dbResult->isSuccess()) {
  162. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  163. }
  164. //缓存
  165. $this->objEnterpriseCache->EnterpriseDataCache($value['enterpriseId'], 'weixinTemplateId', $value['enterpriseId']);
  166. $this->objEnterpriseCache->EnterpriseDataCache($value['enterpriseId'], 'templateId', $value['templateId']);
  167. //提交审核
  168. $dbResult = $objMEnterpriseBindTemplate->submitAudit([
  169. 'templateId' => $value['templateId'],//模版id,
  170. 'enterpriseId' => $value['enterpriseId'],//企业id
  171. ]);
  172. if (!$dbResult->isSuccess()) {
  173. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  174. }
  175. }
  176. return ResultWrapper::success('已提交审核');
  177. }
  178. /**
  179. * 发布已审核小程序
  180. * @param $params
  181. * @return ResultWrapper
  182. */
  183. public function release($params)
  184. {
  185. foreach ($params as $value){
  186. $objMEnterpriseBindTemplate = new MEnterpriseBindTemplate(null, $value['enterpriseId']);
  187. $dbResult = $objMEnterpriseBindTemplate->release([
  188. 'templateId' => $value['templateId'],
  189. 'enterpriseId' => $value['enterpriseId'],
  190. ]);
  191. if (!$dbResult->isSuccess()) {
  192. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  193. }
  194. }
  195. return ResultWrapper::success('发布成功');
  196. }
  197. /**
  198. * 撤销审核
  199. * @param $params
  200. * @return ResultWrapper
  201. */
  202. public function revoke($params)
  203. {
  204. foreach ($params as $value){
  205. $objMEnterpriseBindTemplate = new MEnterpriseBindTemplate(null, $value['enterpriseId']);
  206. $dbResult = $objMEnterpriseBindTemplate->revoke([
  207. 'templateId' => $value['templateId'],
  208. 'enterpriseId' => $value['enterpriseId'],
  209. ]);
  210. if (!$dbResult->isSuccess()) {
  211. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  212. }
  213. }
  214. return ResultWrapper::success('撤销成功');
  215. }
  216. }