MEnterpriseBindTemplate.Class.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <?php
  2. /**
  3. * 企业绑定模版
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/29
  7. * Time: 15:28
  8. */
  9. namespace JinDouYun\Model\System;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Core\StatusCode;
  13. Use Mall\Framework\Factory;
  14. use JinDouYun\Cache\SystemCache;
  15. use JinDouYun\Model\Goods\MGoods;
  16. use JinDouYun\Model\Oem\MOem;
  17. use JinDouYun\Dao\System\DEnterpriseBindTemplate;
  18. use JinDouYun\Dao\System\DSystemTemplate;
  19. use JinDouYun\Dao\System\DTemplateModule;
  20. use JinDouYun\Dao\SystemSettings\DWxBindEnterprise;
  21. use Util\WeiXin\Oplatform;
  22. class MEnterpriseBindTemplate
  23. {
  24. private $objDEnterpriseBindTemplate;
  25. private $onlineUserId;
  26. private $onlineEnterpriseId;
  27. private $objDSystemTemplate;
  28. private $areaCode;
  29. public function __construct($onlineUserId, $enterpriseId, $areaCode = null)
  30. {
  31. $this->onlineUserId = $onlineUserId;
  32. $this->onlineEnterpriseId = $enterpriseId;
  33. $this->areaCode = $areaCode;
  34. $this->objDEnterpriseBindTemplate = new DEnterpriseBindTemplate('default');
  35. $this->objDSystemTemplate = new DSystemTemplate();
  36. }
  37. /**
  38. * 企业选择模版启用/停用
  39. * @param $params
  40. * @return ResultWrapper
  41. */
  42. public function bindTemplate($params, $token)
  43. {
  44. // 启用得微信模板id
  45. $weixinTemplateId = $params['weixinTemplateId'];
  46. unset($params['weixinTemplateId']);
  47. $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  48. if (empty($oplatformConfigData)) {
  49. exit('配置错误');
  50. }
  51. // 获取当前登录企业的小程序配置
  52. $objSystemCache = new SystemCache();
  53. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  54. if (empty($miniprogramSetting)) {
  55. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  56. }
  57. $miniprogramSetting = json_decode($miniprogramSetting, true);
  58. // 企业设置的小程序id
  59. $authorizer_appid = $miniprogramSetting['appid'];
  60. $ext_json = [
  61. 'extEnable' => 'true',
  62. 'extAppid' => $miniprogramSetting['appid'],
  63. 'directCommit' => false,
  64. 'ext' => [
  65. 'token' => $token,
  66. ],
  67. ];
  68. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  69. // 1.上传小程序
  70. $result = $objOplatform->commit($authorizer_appid, $weixinTemplateId, $ext_json, '1.0', '切换模板');
  71. if (!$result->isSuccess()) {
  72. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  73. }
  74. // 2.获取已上传的代码的页面列表
  75. unset($result);
  76. $result = $objOplatform->getPage($authorizer_appid);
  77. if (!$result->isSuccess()) {
  78. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  79. }
  80. $pageList = $result->getData();
  81. // 3.获取的体验版二维码
  82. unset($result);
  83. $result = $objOplatform->getQrcode($authorizer_appid, $pageList[0]);
  84. if (!$result->isSuccess()) {
  85. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  86. }
  87. if (!is_dir(UPLOAD_FILE_PATH)) {
  88. mkdir(UPLOAD_FILE_PATH, 0777, true);
  89. }
  90. if (!file_put_contents(UPLOAD_FILE_PATH . $authorizer_appid . '.jpeg', $result->getData())) {
  91. return ResultWrapper::fail('保存体验二维码失败', ErrorCode::$contentNotExists);
  92. }
  93. //关闭其他所有开启的模版
  94. $dbResult = $this->objDEnterpriseBindTemplate->update(['enableStatus' => StatusCode::$delete], ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
  95. if ($dbResult === false) {
  96. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  97. }
  98. $params['auditStatus'] = StatusCode::$wxAuditStatus['inAudit'];
  99. $params['qrcodeImg'] = URL_UPLOAD . $authorizer_appid . '.jpeg';
  100. $dbResult = $this->objDEnterpriseBindTemplate->replace($params);
  101. if ($dbResult === false) {
  102. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  103. }
  104. //提交审核
  105. $dbResult = self::submitAudit($params);
  106. if (!$dbResult->isSuccess()) {
  107. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  108. }
  109. return ResultWrapper::success('上传代码成功');
  110. }
  111. /**
  112. * 获取企业正在使用的模版
  113. * @return ResultWrapper
  114. */
  115. public function getAllTemplate()
  116. {
  117. $objDSystemTemplate = new DSystemTemplate();
  118. $dbResult = $objDSystemTemplate->select(['deleteStatus' => StatusCode::$standard], 'id,title,weixinTemplateId,type,version');
  119. if ($dbResult === false) {
  120. return ResultWrapper::fail($objDSystemTemplate->error(), ErrorCode::$dberror);
  121. }
  122. $templateIData = $this->objDEnterpriseBindTemplate->get(['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard], 'auditId,version,auditStatus,reason,templateId,enterpriseId,status,qrcodeImg');
  123. if ($templateIData === false) {
  124. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  125. }
  126. foreach ($dbResult as &$template) {
  127. $template['isVersion'] = StatusCode::$standard;
  128. $template['enableStatus'] = StatusCode::$delete;
  129. $template['qrcodeImg'] = '';
  130. $template['auditStatus'] = StatusCode::$wxAuditStatus['beenRelease'];
  131. if (!empty($templateIData)) {
  132. if ($template['id'] == $templateIData['templateId']) {
  133. if ($templateIData['version'] != $template['version']) {
  134. $template['isVersion'] = StatusCode::$delete;//有新版本
  135. }
  136. $template['reason'] = isset($templateIData['reason']) && !empty($templateIData['reason']) ? json_decode($templateIData['reason'], true) : '';
  137. $template['auditId'] = isset($templateIData['auditId']) ? $templateIData['auditId'] : '';
  138. $template['auditStatus'] = isset($templateIData['auditStatus']) ? $templateIData['auditStatus'] : '';
  139. $template['enableStatus'] = StatusCode::$standard;
  140. //$template['status'] = $templateIData['status'];
  141. $template['qrcodeImg'] = $templateIData['qrcodeImg'];
  142. }
  143. }
  144. }
  145. $total = $this->objDEnterpriseBindTemplate->count(['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
  146. $return = [
  147. 'data' => $dbResult,
  148. 'total' => ($total) ? intval($total) : 0,
  149. ];
  150. return ResultWrapper::success($return);
  151. }
  152. /**
  153. * 获取首页模版及数据
  154. * @return ResultWrapper
  155. * @throws \Exception
  156. * @deprecated
  157. */
  158. public function getTemplate()
  159. {
  160. $templateId = $this->objDEnterpriseBindTemplate->get_field('templateId', ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
  161. if ($templateId === false) {
  162. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  163. }
  164. if (empty($templateId)) {
  165. return ResultWrapper::fail('当前企业没有正在使用的模版', ErrorCode::$dberror);
  166. }
  167. $type = $this->objDSystemTemplate->get_field('type', ['id' => $templateId]);
  168. if ($type === false) {
  169. return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
  170. }
  171. if (empty($type)) {
  172. return ResultWrapper::fail('获取模版失败', ErrorCode::$paramError);
  173. }
  174. $allModuleData = [];
  175. switch ($type) {
  176. case StatusCode::$templateType['ordinary']:
  177. //普通模版
  178. //获取该模版平台设置的模块
  179. $objDTemplateModule = new DTemplateModule();
  180. $module = $objDTemplateModule->select(['templateId' => $templateId, 'enableStatus' => StatusCode::$standard]);
  181. if ($module === false) {
  182. return ResultWrapper::fail($objDTemplateModule->error(), ErrorCode::$dberror);
  183. }
  184. //获取该企业下显示的模块,模块下的数据
  185. $objMEnterpriseTemplateModule = new MEnterpriseTemplateModule($this->onlineUserId, $this->onlineEnterpriseId);
  186. $moduleData = $objMEnterpriseTemplateModule->getModuleByIds(['templateId' => $templateId, 'moduleId' => array_column($module, 'id')]);
  187. //验证商品有效性
  188. $moduleData = self::checkModule($moduleData);
  189. foreach ($module as $key => $val) {
  190. $allModuleData[$val['alias']] = [
  191. 'moduleAlias' => $val['alias'],
  192. 'enableStatus' => isset($moduleData[$val['alias']]['enableStatus']) ? $moduleData[$val['alias']]['enableStatus'] : StatusCode::$standard,
  193. 'moduleData' => isset($moduleData[$val['alias']]['moduleData']) ? json_decode($moduleData[$val['alias']]['moduleData'], true) : [],
  194. ];
  195. }
  196. break;
  197. case StatusCode::$templateType['universal']:
  198. //万能模版
  199. $objMPage = new MPage($this->onlineUserId, $this->onlineEnterpriseId, $this->areaCode);
  200. $dbResult = $objMPage->home();
  201. if (!$dbResult->isSuccess()) {
  202. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  203. }
  204. $allModuleData = $dbResult->getData();
  205. break;
  206. }
  207. $allModuleData = array_values($allModuleData);
  208. $return = [
  209. 'data' => $allModuleData,
  210. ];
  211. return ResultWrapper::success($return);
  212. }
  213. /**
  214. * @return ResultWrapper
  215. * @throws \Exception
  216. */
  217. public function homePage($pageType = 1,$shopId = null)
  218. {
  219. //根据企业id查找模板id
  220. $templateId = $this->objDEnterpriseBindTemplate->get_field('templateId', ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
  221. if ($templateId === false) {
  222. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  223. }
  224. if (empty($templateId)) {
  225. $templateId = 3; // 如果企业没设置模板,默认使用万能模板
  226. //return ResultWrapper::fail('当前企业没有正在使用的模版', ErrorCode::$dberror);
  227. }
  228. //根据模板id查找模板类型
  229. $type = $this->objDSystemTemplate->get_field('type', ['id' => $templateId]);
  230. if ($type === false) {
  231. return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
  232. }
  233. // if (empty($type)) {
  234. // return ResultWrapper::fail('获取模版失败', ErrorCode::$contentNotExists);
  235. // }
  236. $allModuleData = [];
  237. if (!$type) $type = 2;
  238. switch ($type) {
  239. case StatusCode::$templateType['ordinary']: //普通模版
  240. //获取该模版平台设置的模块
  241. $objDTemplateModule = new DTemplateModule();
  242. $module = $objDTemplateModule->select(['templateId' => $templateId, 'enableStatus' => StatusCode::$standard]);
  243. if ($module === false) {
  244. return ResultWrapper::fail($objDTemplateModule->error(), ErrorCode::$dberror);
  245. }
  246. //获取该企业下显示的模块,模块下的数据
  247. $objMEnterpriseTemplateModule = new MEnterpriseTemplateModule($this->onlineUserId, $this->onlineEnterpriseId);
  248. $moduleData = $objMEnterpriseTemplateModule->getModuleByIds(['templateId' => $templateId, 'moduleId' => array_column($module, 'id')]);
  249. //验证商品有效性
  250. $moduleData = self::checkModule($moduleData);
  251. foreach ($module as $key => $val) {
  252. $allModuleData[$val['alias']] = [
  253. 'moduleAlias' => $val['alias'],
  254. 'enableStatus' => isset($moduleData[$val['alias']]['enableStatus']) ? $moduleData[$val['alias']]['enableStatus'] : StatusCode::$standard,
  255. 'moduleData' => isset($moduleData[$val['alias']]['moduleData']) ? json_decode($moduleData[$val['alias']]['moduleData'], true) : [],
  256. ];
  257. }
  258. break;
  259. case StatusCode::$templateType['universal']: //万能模版
  260. $objMPage = new MPage($this->onlineUserId, $this->onlineEnterpriseId, $this->areaCode);
  261. // 获取app首页
  262. $dbResult = $objMPage->home($pageType,$shopId);
  263. if (!$dbResult->isSuccess()) {
  264. return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
  265. }
  266. $allModuleData = $dbResult->getData();
  267. break;
  268. }
  269. $allModuleData = array_values($allModuleData);
  270. $all['type'] = $type;
  271. $all['data'] = $allModuleData;
  272. $return = [
  273. 'data' => $all,
  274. ];
  275. return ResultWrapper::success($return);
  276. }
  277. /**
  278. * 检测模块数据中的下架和删除数据 isInvalid=>4(已失效) isInvalid=>5(正常)
  279. * @param $data
  280. * @return mixed
  281. */
  282. private function checkModule($data)
  283. {
  284. if (empty($data)) return $data;
  285. //热销商品模块
  286. if (isset($data['hotGoods'])) {
  287. $hotGoods = empty($data['hotGoods']['moduleData']) ? [] : json_decode($data['hotGoods']['moduleData'], true);
  288. $goodsData = $hotGoods['goodsData'];
  289. $goodsId = array_unique(array_column($goodsData, 'id'));
  290. $objMGoods = new MGoods($this->onlineEnterpriseId, false, $this->onlineUserId);
  291. $pageParams = pageToOffset(1, 10);
  292. $selectParams = [
  293. 'goodsIds' => array_values($goodsId),
  294. 'limit' => $pageParams['limit'],
  295. 'offset' => $pageParams['offset'],
  296. 'userCenterId' => $this->onlineUserId,
  297. ];
  298. // 搜索商品
  299. $result = $objMGoods->search($selectParams);
  300. if (!$result->isSuccess()) {
  301. return $data;
  302. }
  303. $goodsData = $result->getData()['data'];
  304. foreach ($goodsData as $key => $val) {
  305. foreach ($val['skuData'] as $sku) {
  306. if ($sku['isMaster'] == StatusCode::$standard) {
  307. $goodsData[$key]['salePrice'] = $sku['salePrice'];
  308. $goodsData[$key]['inventory'] = $sku['inventory'];
  309. $goodsData[$key]['unitName'] = $sku['unitName'];
  310. }
  311. }
  312. }
  313. //$objMGoods->getGoodsNames($goodsId);
  314. foreach ($goodsData as $key => &$val) {
  315. $val['isInvalid'] = StatusCode::$standard;
  316. }
  317. $hotGoods['goodsData'] = $goodsData;
  318. $data['hotGoods']['moduleData'] = json_encode($hotGoods);
  319. unset($val);
  320. unset($goodsId);
  321. unset($result);
  322. }
  323. //首页商品分类模块
  324. if (isset($data['goodsCate'])) {
  325. $goodsCate = json_decode($data['goodsCate']['moduleData'], true);
  326. $cateData = $goodsCate['cateData'];
  327. $goodsId = [];
  328. foreach ($cateData as $key => $val) {
  329. foreach ($val['goodsData'] as $goods) {
  330. $goodsId[] = $goods['id'];
  331. }
  332. }
  333. $objMGoods = new MGoods($this->onlineEnterpriseId, false, $this->onlineUserId);
  334. $pageParams = pageToOffset(1, 100);
  335. $selectParams = [
  336. 'goodsIds' => array_values(array_unique($goodsId)),
  337. 'limit' => $pageParams['limit'],
  338. 'offset' => $pageParams['offset'],
  339. 'userCenterId' => $this->onlineUserId,
  340. ];
  341. $result = $objMGoods->search($selectParams);
  342. if (!$result->isSuccess()) {
  343. return $data;
  344. }
  345. $goodsData = $result->getData()['data'];
  346. $allGoodsData = [];
  347. foreach ($goodsData as $key => &$val) {
  348. foreach ($val['skuData'] as $sku) {
  349. if ($sku['isMaster'] == StatusCode::$standard) {
  350. $goodsData[$key]['salePrice'] = $sku['salePrice'];
  351. $goodsData[$key]['inventory'] = $sku['inventory'];
  352. $goodsData[$key]['unitName'] = $sku['unitName'];
  353. }
  354. }
  355. $allGoodsData[$val['id']] = $val;
  356. }
  357. //$result = $objMGoods->getGoodsNames(array_unique($goodsId));
  358. foreach ($cateData as $key => &$val) {
  359. $newGoods = [];
  360. foreach ($val['goodsData'] as &$goods) {
  361. if (isset($allGoodsData[$goods['id']])) {
  362. $newGoods[] = $allGoodsData[$goods['id']];
  363. }
  364. }
  365. $val['goodsData'] = $newGoods;
  366. }
  367. $goodsCate['cateData'] = $cateData;
  368. $data['goodsCate']['moduleData'] = json_encode($goodsCate);
  369. }
  370. return $data;
  371. }
  372. /**
  373. * 更新审核状态及信息
  374. * @param $callback
  375. * @param $params
  376. * @return ResultWrapper
  377. */
  378. public function getAuditStatus($params)
  379. {
  380. $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  381. if (empty($oplatformConfigData)) {
  382. return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
  383. }
  384. // 获取当前登录企业的小程序配置
  385. $objSystemCache = new SystemCache();
  386. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  387. if (empty($miniprogramSetting)) {
  388. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  389. }
  390. $miniprogramSetting = json_decode($miniprogramSetting, true);
  391. // 企业设置的小程序id
  392. $authorizer_appid = $miniprogramSetting['appid'];
  393. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  394. $result = $objOplatform->getAuditstatus($authorizer_appid, $params['auditId']);//TODO
  395. if (!$result->isSuccess()) {
  396. return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
  397. }
  398. $callback = $result->getData();
  399. if ($callback['status'] == StatusCode::$wxAuditStatus['refuse']) {
  400. $reason = [
  401. 'reason' => isset($callback['reason']) ? $callback['reason'] : '',
  402. 'ScreenShot' => isset($callback['ScreenShot']) ? $callback['ScreenShot'] : '',
  403. ];
  404. }
  405. $dbResult = $this->objDEnterpriseBindTemplate->update([
  406. 'auditStatus' => isset($callback['status']) ? $callback['status'] : StatusCode::$wxAuditStatus['inAudit'],
  407. 'reason' => isset($reason) ? json_encode($reason) : '[]',
  408. ], [
  409. 'templateId' => $params['templateId'],
  410. 'enterpriseId' => $this->onlineEnterpriseId,
  411. ]);
  412. if ($dbResult === false) {
  413. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  414. }
  415. switch (isset($callback['status']) ? $callback['status'] : '') {
  416. case 0:
  417. $message = '审核成功';
  418. break;
  419. case 1:
  420. $message = '审核被拒绝';
  421. break;
  422. case 2:
  423. $message = '审核中';
  424. break;
  425. case 3:
  426. $message = '已撤回';
  427. break;
  428. case 4:
  429. $message = '审核延后';
  430. break;
  431. default:
  432. $message = '未查询到';
  433. break;
  434. }
  435. $return = [
  436. 'message' => $message,
  437. 'reason' => isset($reason) ? $reason : []
  438. ];
  439. return ResultWrapper::success($return);
  440. }
  441. /**
  442. * 回调修改模版审核状态
  443. * @param $params
  444. * @return bool
  445. */
  446. public function updateAuditStatus($params)
  447. {
  448. $objDWxBindEnterprise = new DWxBindEnterprise();
  449. $dbResult = $objDWxBindEnterprise->get_field('enterpriseId', ['wxOriginId' => $params['ToUserName']]);
  450. if ($dbResult === false) {
  451. return false;
  452. }
  453. $dbResult = $this->objDEnterpriseBindTemplate->update(
  454. [
  455. 'auditStatus' => $params['auditStatus'],
  456. 'reason' => isset($params['reason']) ? json_encode($params['reason']) : '[]',
  457. ],
  458. ['enterpriseId' => $dbResult, 'enableStatus' => StatusCode::$standard]
  459. );
  460. if ($dbResult === false) {
  461. return false;
  462. }
  463. return true;
  464. }
  465. /**********************************************重写 start**********************************************************/
  466. /**
  467. * 1.提交代码
  468. *
  469. * @param $params
  470. * @param $token
  471. * @return ResultWrapper
  472. */
  473. public function submitCode($params, $token)
  474. {
  475. // 启用得微信模板id
  476. $weixinTemplateId = $params['weixinTemplateId'];
  477. unset($params['weixinTemplateId']);
  478. // 获取当前登录企业的小程序配置
  479. $objSystemCache = new SystemCache();
  480. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  481. if (empty($miniprogramSetting)) {
  482. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  483. }
  484. $miniprogramSetting = json_decode($miniprogramSetting, true);
  485. // 企业设置的小程序id
  486. $authorizer_appid = $miniprogramSetting['appid'];
  487. /*
  488. $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  489. if (empty($oplatformConfigData)) {
  490. return ResultWrapper::fail('配置错误', ErrorCode::$paramError);
  491. }*/
  492. // 获取微信开放平台配置
  493. $objMOem = new MOem();
  494. // 默认作为SAAS用户后台操作方式获取配置
  495. $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
  496. $host = $refererUrl['host'];
  497. $result = $objMOem->getOemInfoByDomain($host);
  498. if( !$result->isSuccess() ){
  499. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  500. }
  501. $oemData = $result->getData();
  502. // 如果用企业id说明管理后台操作
  503. if(isset($params['enterpriseId']) && !empty($params['enterpriseId']) && empty($oemData)){
  504. // 先拿传过来的企业查询去查询
  505. $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
  506. if( !$result->isSuccess() ){
  507. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  508. }
  509. $oemData = $result->getData();
  510. // 如果为空,用默认平台企业去操作
  511. if(empty($oemData)){
  512. $result = $objMOem->getOemInfoByenterpriseId(4);
  513. if( !$result->isSuccess() ){
  514. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  515. }
  516. $oemData = $result->getData();
  517. }
  518. }
  519. if( empty($oemData) ){
  520. return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
  521. }
  522. $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
  523. if( empty($oplatformConfigData) ){
  524. return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
  525. }
  526. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  527. //设置服务器域名
  528. if( empty($oemData['apiDomain']) ){
  529. return ResultWrapper::fail('oem表中获取api接口域名配置为空', ErrorCode::$contentNotExists);
  530. }
  531. $setResult = $objOplatform->modifyDomain($authorizer_appid, $oemData['apiDomain']);
  532. if (!$setResult->isSuccess()){
  533. return ResultWrapper::fail($setResult->getData(), $setResult->getErrorCode());
  534. }
  535. $accountResult = $accountResult = $objOplatform->apiGetAuthorizerInfo($authorizer_appid);
  536. if (!$accountResult->isSuccess()){
  537. return ResultWrapper::fail($accountResult->getData(),$accountResult->getErrorCode());
  538. }
  539. $account = $accountResult->getData();
  540. //个人类型小程序不能设置业务域名
  541. if (isset($account['authorizer_info']['principal_name']) && $account['authorizer_info']['principal_name'] != '个人'){
  542. $setResult = $objOplatform->setwebviewdomain($authorizer_appid, $oemData['apiDomain']);
  543. if (!$setResult->isSuccess()){
  544. return ResultWrapper::fail($setResult->getData(), $setResult->getErrorCode());
  545. }
  546. }
  547. $ext_json = [
  548. 'extEnable' => 'true',
  549. 'extAppid' => $miniprogramSetting['appid'],
  550. 'directCommit' => false,
  551. 'ext' => [
  552. 'token' => $token,
  553. ],
  554. ];
  555. // 代理的从数据库表中获取模板id
  556. if( empty($oemData['weixinTemplateId']) ){
  557. return ResultWrapper::fail('oem表中获取微信开发平台模板id配置为空', ErrorCode::$contentNotExists);
  558. }
  559. $weixinTemplateId = $oemData['weixinTemplateId'];
  560. // 1.上传小程序
  561. $result = $objOplatform->commit($authorizer_appid, $weixinTemplateId, $ext_json, '1.0', '切换模板');
  562. if (!$result->isSuccess()) {
  563. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  564. }
  565. // 2.获取已上传的代码的页面列表
  566. unset($result);
  567. $result = $objOplatform->getPage($authorizer_appid);
  568. if (!$result->isSuccess()) {
  569. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  570. }
  571. $pageList = $result->getData();
  572. // 3.获取的体验版二维码
  573. unset($result);
  574. $result = $objOplatform->getQrcode($authorizer_appid, $pageList[0]);
  575. if (!$result->isSuccess()) {
  576. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  577. }
  578. if (!is_dir(UPLOAD_FILE_PATH)) {
  579. mkdir(UPLOAD_FILE_PATH, 0777, true);
  580. }
  581. if (!file_put_contents(UPLOAD_FILE_PATH . $authorizer_appid . '.jpeg', $result->getData())) {
  582. return ResultWrapper::fail('保存体验二维码失败', ErrorCode::$contentNotExists);
  583. }
  584. //关闭其他所有开启的模版
  585. $dbResult = $this->objDEnterpriseBindTemplate->update(['enableStatus' => StatusCode::$delete, 'status' => StatusCode::$delete], ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
  586. if ($dbResult === false) {
  587. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  588. }
  589. $params['auditStatus'] = StatusCode::$wxAuditStatus['noSubmit'];//代码提交成功,可以点击审核
  590. $params['qrcodeImg'] = URL_UPLOAD . $authorizer_appid . '.jpeg';//二维码地址
  591. $params['status'] = StatusCode::$standard;//已提交代码
  592. $dbResult = $this->objDEnterpriseBindTemplate->replace($params);
  593. if ($dbResult === false) {
  594. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  595. }
  596. return ResultWrapper::success('上传代码成功');
  597. }
  598. /**
  599. * 2.提交审核
  600. *
  601. * @param $params
  602. * @return ResultWrapper
  603. */
  604. public function submitAudit($params)
  605. {
  606. // 获取当前登录企业的小程序配置
  607. $objSystemCache = new SystemCache();
  608. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  609. if (empty($miniprogramSetting)) {
  610. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  611. }
  612. $miniprogramSetting = json_decode($miniprogramSetting, true);
  613. // 企业设置的小程序id
  614. $authorizer_appid = $miniprogramSetting['appid'];
  615. /*
  616. $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  617. if (empty($oplatformConfigData)) {
  618. return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
  619. }*/
  620. // 如果用企业id说明管理后台操作 如果没有说明客户自己SAAS系统后台操作 获取微信开放平台配置
  621. $objMOem = new MOem();
  622. if(isset($params['enterpriseId']) && !empty($params['enterpriseId'])){
  623. // 先拿传过来的企业查询去查询
  624. $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
  625. if( !$result->isSuccess() ){
  626. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  627. }
  628. $oemData = $result->getData();
  629. // 如果为空,用默认平台企业去操作
  630. if(empty($oemData)){
  631. $result = $objMOem->getOemInfoByenterpriseId(4);
  632. if( !$result->isSuccess() ){
  633. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  634. }
  635. $oemData = $result->getData();
  636. }
  637. }else{
  638. $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
  639. $host = $refererUrl['host'];
  640. $result = $objMOem->getOemInfoByDomain($host);
  641. if( !$result->isSuccess() ){
  642. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  643. }
  644. $oemData = $result->getData();
  645. }
  646. if( empty($oemData) ){
  647. return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
  648. }
  649. $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
  650. if( empty($oplatformConfigData) ){
  651. return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
  652. }
  653. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  654. $result = $objOplatform->submitAudit($authorizer_appid);
  655. if (!$result->isSuccess()) {
  656. return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
  657. }
  658. $callback = $result->getData();
  659. $dbResult = $this->objDEnterpriseBindTemplate->update([
  660. 'auditId' => is_numeric($callback) ? $callback : '',
  661. 'auditStatus' => StatusCode::$wxAuditStatus['inAudit'],//提交审核成功,进入审核中
  662. ], [
  663. 'templateId' => $params['templateId'],
  664. 'enterpriseId' => $this->onlineEnterpriseId,
  665. ]);
  666. if ($dbResult === false) {
  667. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  668. }
  669. return ResultWrapper::success($dbResult);
  670. }
  671. /**
  672. * 3.发布
  673. *
  674. * @param $params
  675. * @return ResultWrapper
  676. */
  677. public function release($params)
  678. {
  679. /*$oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  680. if (empty($oplatformConfigData)) {
  681. return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
  682. }*/
  683. // 获取微信开放平台配置
  684. $objMOem = new MOem();
  685. // 如果用企业id说明管理后台操作 如果没有说明客户自己SAAS系统后台操作
  686. if(isset($params['enterpriseId']) && !empty($params['enterpriseId'])){
  687. // 先拿传过来的企业查询去查询
  688. $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
  689. if( !$result->isSuccess() ){
  690. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  691. }
  692. $oemData = $result->getData();
  693. // 如果为空,用默认平台企业去操作
  694. if(empty($oemData)){
  695. $result = $objMOem->getOemInfoByenterpriseId(4);
  696. if( !$result->isSuccess() ){
  697. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  698. }
  699. $oemData = $result->getData();
  700. }
  701. }else{
  702. $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
  703. $host = $refererUrl['host'];
  704. $result = $objMOem->getOemInfoByDomain($host);
  705. if( !$result->isSuccess() ){
  706. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  707. }
  708. $oemData = $result->getData();
  709. }
  710. if( empty($oemData) ){
  711. return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
  712. }
  713. $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
  714. if( empty($oplatformConfigData) ){
  715. return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
  716. }
  717. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  718. // 获取当前登录企业的小程序配置
  719. $objSystemCache = new SystemCache();
  720. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  721. if (empty($miniprogramSetting)) {
  722. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  723. }
  724. $miniprogramSetting = json_decode($miniprogramSetting, true);
  725. $result = $objOplatform->release($miniprogramSetting['appid']);
  726. if (!$result->isSuccess()) {
  727. return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
  728. }
  729. //修改模版启用状态
  730. $dbResult = $this->objDEnterpriseBindTemplate->update([
  731. 'auditStatus' => StatusCode::$wxAuditStatus['beingUsed'],//审核成功,发布小程序成功进入正在使用状态
  732. ], [
  733. 'templateId' => $params['templateId'],
  734. 'enterpriseId' => $this->onlineEnterpriseId,
  735. ]);
  736. if ($dbResult === false) {
  737. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  738. }
  739. return ResultWrapper::success($result->getData());
  740. }
  741. /**
  742. * 审核撤回
  743. * @param $params
  744. * @return ResultWrapper
  745. */
  746. public function revoke($params)
  747. {
  748. /*
  749. $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
  750. if (empty($oplatformConfigData)) {
  751. return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
  752. }*/
  753. // 如果用企业id说明管理后台操作 如果没有说明客户自己SAAS系统后台操作 获取微信开放平台配置
  754. $objMOem = new MOem();
  755. if(isset($params['enterpriseId']) && !empty($params['enterpriseId'])){
  756. // 先拿传过来的企业查询去查询
  757. $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
  758. if( !$result->isSuccess() ){
  759. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  760. }
  761. $oemData = $result->getData();
  762. // 如果为空,用默认平台企业去操作
  763. if(empty($oemData)){
  764. $result = $objMOem->getOemInfoByenterpriseId(4);
  765. if( !$result->isSuccess() ){
  766. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  767. }
  768. $oemData = $result->getData();
  769. }
  770. }else{
  771. $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
  772. $host = $refererUrl['host'];
  773. $result = $objMOem->getOemInfoByDomain($host);
  774. if( !$result->isSuccess() ){
  775. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  776. }
  777. $oemData = $result->getData();
  778. }
  779. if( empty($oemData) ){
  780. return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
  781. }
  782. $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
  783. if( empty($oplatformConfigData) ){
  784. return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
  785. }
  786. $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
  787. // 获取当前登录企业的小程序配置
  788. $objSystemCache = new SystemCache();
  789. $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
  790. if (empty($miniprogramSetting)) {
  791. return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
  792. }
  793. $miniprogramSetting = json_decode($miniprogramSetting, true);
  794. $result = $objOplatform->undocodeaudit($miniprogramSetting['appid']);
  795. if (!$result->isSuccess()) {
  796. return ResultWrapper::fail($result->getData(), $result->getErrorCode());
  797. }
  798. //修改状态
  799. $dbResult = $this->objDEnterpriseBindTemplate->update([
  800. 'auditStatus' => StatusCode::$wxAuditStatus['beenRelease'],//撤回成功,初始状态
  801. ], [
  802. 'templateId' => $params['templateId'],
  803. 'enterpriseId' => $this->onlineEnterpriseId,
  804. ]);
  805. if ($dbResult === false) {
  806. return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
  807. }
  808. return ResultWrapper::success($result->getData());
  809. }
  810. /**
  811. * 获取提交代码后小程序的状态
  812. *
  813. * @return ResultWrapper
  814. */
  815. public function getWxStatus()
  816. {
  817. $dbResult = $this->objDEnterpriseBindTemplate->get(
  818. [
  819. 'enterpriseId' => $this->onlineEnterpriseId,
  820. 'status' => StatusCode::$standard
  821. ],
  822. 'id,templateId,auditStatus'
  823. );
  824. if (empty($dbResult)) {
  825. $dbResult = [
  826. 'id' => null,
  827. 'templateId' => null,
  828. 'status' => null,
  829. ];
  830. }
  831. return ResultWrapper::success($dbResult);
  832. }
  833. }