123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932 |
- <?php
- /**
- * 企业绑定模版
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/11/29
- * Time: 15:28
- */
- namespace JinDouYun\Model\System;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- Use Mall\Framework\Factory;
- use JinDouYun\Cache\SystemCache;
- use JinDouYun\Model\Goods\MGoods;
- use JinDouYun\Model\Oem\MOem;
- use JinDouYun\Dao\System\DEnterpriseBindTemplate;
- use JinDouYun\Dao\System\DSystemTemplate;
- use JinDouYun\Dao\System\DTemplateModule;
- use JinDouYun\Dao\SystemSettings\DWxBindEnterprise;
- use Util\WeiXin\Oplatform;
- class MEnterpriseBindTemplate
- {
- private $objDEnterpriseBindTemplate;
- private $onlineUserId;
- private $onlineEnterpriseId;
- private $objDSystemTemplate;
- private $areaCode;
- public function __construct($onlineUserId, $enterpriseId, $areaCode = null)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $enterpriseId;
- $this->areaCode = $areaCode;
- $this->objDEnterpriseBindTemplate = new DEnterpriseBindTemplate('default');
- $this->objDSystemTemplate = new DSystemTemplate();
- }
- /**
- * 企业选择模版启用/停用
- * @param $params
- * @return ResultWrapper
- */
- public function bindTemplate($params, $token)
- {
- // 启用得微信模板id
- $weixinTemplateId = $params['weixinTemplateId'];
- unset($params['weixinTemplateId']);
- $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
- if (empty($oplatformConfigData)) {
- exit('配置错误');
- }
- // 获取当前登录企业的小程序配置
- $objSystemCache = new SystemCache();
- $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
- if (empty($miniprogramSetting)) {
- return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
- }
- $miniprogramSetting = json_decode($miniprogramSetting, true);
- // 企业设置的小程序id
- $authorizer_appid = $miniprogramSetting['appid'];
- $ext_json = [
- 'extEnable' => 'true',
- 'extAppid' => $miniprogramSetting['appid'],
- 'directCommit' => false,
- 'ext' => [
- 'token' => $token,
- ],
- ];
- $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
- // 1.上传小程序
- $result = $objOplatform->commit($authorizer_appid, $weixinTemplateId, $ext_json, '1.0', '切换模板');
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- // 2.获取已上传的代码的页面列表
- unset($result);
- $result = $objOplatform->getPage($authorizer_appid);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $pageList = $result->getData();
- // 3.获取的体验版二维码
- unset($result);
- $result = $objOplatform->getQrcode($authorizer_appid, $pageList[0]);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- if (!is_dir(UPLOAD_FILE_PATH)) {
- mkdir(UPLOAD_FILE_PATH, 0777, true);
- }
- if (!file_put_contents(UPLOAD_FILE_PATH . $authorizer_appid . '.jpeg', $result->getData())) {
- return ResultWrapper::fail('保存体验二维码失败', ErrorCode::$contentNotExists);
- }
- //关闭其他所有开启的模版
- $dbResult = $this->objDEnterpriseBindTemplate->update(['enableStatus' => StatusCode::$delete], ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- $params['auditStatus'] = StatusCode::$wxAuditStatus['inAudit'];
- $params['qrcodeImg'] = URL_UPLOAD . $authorizer_appid . '.jpeg';
- $dbResult = $this->objDEnterpriseBindTemplate->replace($params);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- //提交审核
- $dbResult = self::submitAudit($params);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success('上传代码成功');
- }
- /**
- * 获取企业正在使用的模版
- * @return ResultWrapper
- */
- public function getAllTemplate()
- {
- $objDSystemTemplate = new DSystemTemplate();
- $dbResult = $objDSystemTemplate->select(['deleteStatus' => StatusCode::$standard], 'id,title,weixinTemplateId,type,version');
- if ($dbResult === false) {
- return ResultWrapper::fail($objDSystemTemplate->error(), ErrorCode::$dberror);
- }
- $templateIData = $this->objDEnterpriseBindTemplate->get(['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard], 'auditId,version,auditStatus,reason,templateId,enterpriseId,status,qrcodeImg');
- if ($templateIData === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- foreach ($dbResult as &$template) {
- $template['isVersion'] = StatusCode::$standard;
- $template['enableStatus'] = StatusCode::$delete;
- $template['qrcodeImg'] = '';
- $template['auditStatus'] = StatusCode::$wxAuditStatus['beenRelease'];
- if (!empty($templateIData)) {
- if ($template['id'] == $templateIData['templateId']) {
- if ($templateIData['version'] != $template['version']) {
- $template['isVersion'] = StatusCode::$delete;//有新版本
- }
- $template['reason'] = isset($templateIData['reason']) && !empty($templateIData['reason']) ? json_decode($templateIData['reason'], true) : '';
- $template['auditId'] = isset($templateIData['auditId']) ? $templateIData['auditId'] : '';
- $template['auditStatus'] = isset($templateIData['auditStatus']) ? $templateIData['auditStatus'] : '';
- $template['enableStatus'] = StatusCode::$standard;
- //$template['status'] = $templateIData['status'];
- $template['qrcodeImg'] = $templateIData['qrcodeImg'];
- }
- }
- }
- $total = $this->objDEnterpriseBindTemplate->count(['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
- $return = [
- 'data' => $dbResult,
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 获取首页模版及数据
- * @return ResultWrapper
- * @throws \Exception
- * @deprecated
- */
- public function getTemplate()
- {
- $templateId = $this->objDEnterpriseBindTemplate->get_field('templateId', ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
- if ($templateId === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- if (empty($templateId)) {
- return ResultWrapper::fail('当前企业没有正在使用的模版', ErrorCode::$dberror);
- }
- $type = $this->objDSystemTemplate->get_field('type', ['id' => $templateId]);
- if ($type === false) {
- return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
- }
- if (empty($type)) {
- return ResultWrapper::fail('获取模版失败', ErrorCode::$paramError);
- }
- $allModuleData = [];
- switch ($type) {
- case StatusCode::$templateType['ordinary']:
- //普通模版
- //获取该模版平台设置的模块
- $objDTemplateModule = new DTemplateModule();
- $module = $objDTemplateModule->select(['templateId' => $templateId, 'enableStatus' => StatusCode::$standard]);
- if ($module === false) {
- return ResultWrapper::fail($objDTemplateModule->error(), ErrorCode::$dberror);
- }
- //获取该企业下显示的模块,模块下的数据
- $objMEnterpriseTemplateModule = new MEnterpriseTemplateModule($this->onlineUserId, $this->onlineEnterpriseId);
- $moduleData = $objMEnterpriseTemplateModule->getModuleByIds(['templateId' => $templateId, 'moduleId' => array_column($module, 'id')]);
- //验证商品有效性
- $moduleData = self::checkModule($moduleData);
- foreach ($module as $key => $val) {
- $allModuleData[$val['alias']] = [
- 'moduleAlias' => $val['alias'],
- 'enableStatus' => isset($moduleData[$val['alias']]['enableStatus']) ? $moduleData[$val['alias']]['enableStatus'] : StatusCode::$standard,
- 'moduleData' => isset($moduleData[$val['alias']]['moduleData']) ? json_decode($moduleData[$val['alias']]['moduleData'], true) : [],
- ];
- }
- break;
- case StatusCode::$templateType['universal']:
- //万能模版
- $objMPage = new MPage($this->onlineUserId, $this->onlineEnterpriseId, $this->areaCode);
- $dbResult = $objMPage->home();
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
- }
- $allModuleData = $dbResult->getData();
- break;
- }
- $allModuleData = array_values($allModuleData);
- $return = [
- 'data' => $allModuleData,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * @return ResultWrapper
- * @throws \Exception
- */
- public function homePage($pageType = 1,$shopId = null)
- {
- //根据企业id查找模板id
- $templateId = $this->objDEnterpriseBindTemplate->get_field('templateId', ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
- if ($templateId === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- if (empty($templateId)) {
- $templateId = 3; // 如果企业没设置模板,默认使用万能模板
- //return ResultWrapper::fail('当前企业没有正在使用的模版', ErrorCode::$dberror);
- }
- //根据模板id查找模板类型
- $type = $this->objDSystemTemplate->get_field('type', ['id' => $templateId]);
- if ($type === false) {
- return ResultWrapper::fail($this->objDSystemTemplate->error(), ErrorCode::$dberror);
- }
- // if (empty($type)) {
- // return ResultWrapper::fail('获取模版失败', ErrorCode::$contentNotExists);
- // }
- $allModuleData = [];
- if (!$type) $type = 2;
- switch ($type) {
- case StatusCode::$templateType['ordinary']: //普通模版
- //获取该模版平台设置的模块
- $objDTemplateModule = new DTemplateModule();
- $module = $objDTemplateModule->select(['templateId' => $templateId, 'enableStatus' => StatusCode::$standard]);
- if ($module === false) {
- return ResultWrapper::fail($objDTemplateModule->error(), ErrorCode::$dberror);
- }
- //获取该企业下显示的模块,模块下的数据
- $objMEnterpriseTemplateModule = new MEnterpriseTemplateModule($this->onlineUserId, $this->onlineEnterpriseId);
- $moduleData = $objMEnterpriseTemplateModule->getModuleByIds(['templateId' => $templateId, 'moduleId' => array_column($module, 'id')]);
- //验证商品有效性
- $moduleData = self::checkModule($moduleData);
- foreach ($module as $key => $val) {
- $allModuleData[$val['alias']] = [
- 'moduleAlias' => $val['alias'],
- 'enableStatus' => isset($moduleData[$val['alias']]['enableStatus']) ? $moduleData[$val['alias']]['enableStatus'] : StatusCode::$standard,
- 'moduleData' => isset($moduleData[$val['alias']]['moduleData']) ? json_decode($moduleData[$val['alias']]['moduleData'], true) : [],
- ];
- }
- break;
- case StatusCode::$templateType['universal']: //万能模版
- $objMPage = new MPage($this->onlineUserId, $this->onlineEnterpriseId, $this->areaCode);
- // 获取app首页
- $dbResult = $objMPage->home($pageType,$shopId);
- if (!$dbResult->isSuccess()) {
- return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror);
- }
- $allModuleData = $dbResult->getData();
- break;
- }
- $allModuleData = array_values($allModuleData);
- $all['type'] = $type;
- $all['data'] = $allModuleData;
- $return = [
- 'data' => $all,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 检测模块数据中的下架和删除数据 isInvalid=>4(已失效) isInvalid=>5(正常)
- * @param $data
- * @return mixed
- */
- private function checkModule($data)
- {
- if (empty($data)) return $data;
- //热销商品模块
- if (isset($data['hotGoods'])) {
- $hotGoods = empty($data['hotGoods']['moduleData']) ? [] : json_decode($data['hotGoods']['moduleData'], true);
- $goodsData = $hotGoods['goodsData'];
- $goodsId = array_unique(array_column($goodsData, 'id'));
- $objMGoods = new MGoods($this->onlineEnterpriseId, false, $this->onlineUserId);
- $pageParams = pageToOffset(1, 10);
- $selectParams = [
- 'goodsIds' => array_values($goodsId),
- 'limit' => $pageParams['limit'],
- 'offset' => $pageParams['offset'],
- 'userCenterId' => $this->onlineUserId,
- ];
- // 搜索商品
- $result = $objMGoods->search($selectParams);
- if (!$result->isSuccess()) {
- return $data;
- }
- $goodsData = $result->getData()['data'];
- foreach ($goodsData as $key => $val) {
- foreach ($val['skuData'] as $sku) {
- if ($sku['isMaster'] == StatusCode::$standard) {
- $goodsData[$key]['salePrice'] = $sku['salePrice'];
- $goodsData[$key]['inventory'] = $sku['inventory'];
- $goodsData[$key]['unitName'] = $sku['unitName'];
- }
- }
- }
- //$objMGoods->getGoodsNames($goodsId);
- foreach ($goodsData as $key => &$val) {
- $val['isInvalid'] = StatusCode::$standard;
- }
- $hotGoods['goodsData'] = $goodsData;
- $data['hotGoods']['moduleData'] = json_encode($hotGoods);
- unset($val);
- unset($goodsId);
- unset($result);
- }
- //首页商品分类模块
- if (isset($data['goodsCate'])) {
- $goodsCate = json_decode($data['goodsCate']['moduleData'], true);
- $cateData = $goodsCate['cateData'];
- $goodsId = [];
- foreach ($cateData as $key => $val) {
- foreach ($val['goodsData'] as $goods) {
- $goodsId[] = $goods['id'];
- }
- }
- $objMGoods = new MGoods($this->onlineEnterpriseId, false, $this->onlineUserId);
- $pageParams = pageToOffset(1, 100);
- $selectParams = [
- 'goodsIds' => array_values(array_unique($goodsId)),
- 'limit' => $pageParams['limit'],
- 'offset' => $pageParams['offset'],
- 'userCenterId' => $this->onlineUserId,
- ];
- $result = $objMGoods->search($selectParams);
- if (!$result->isSuccess()) {
- return $data;
- }
- $goodsData = $result->getData()['data'];
- $allGoodsData = [];
- foreach ($goodsData as $key => &$val) {
- foreach ($val['skuData'] as $sku) {
- if ($sku['isMaster'] == StatusCode::$standard) {
- $goodsData[$key]['salePrice'] = $sku['salePrice'];
- $goodsData[$key]['inventory'] = $sku['inventory'];
- $goodsData[$key]['unitName'] = $sku['unitName'];
- }
- }
- $allGoodsData[$val['id']] = $val;
- }
- //$result = $objMGoods->getGoodsNames(array_unique($goodsId));
- foreach ($cateData as $key => &$val) {
- $newGoods = [];
- foreach ($val['goodsData'] as &$goods) {
- if (isset($allGoodsData[$goods['id']])) {
- $newGoods[] = $allGoodsData[$goods['id']];
- }
- }
- $val['goodsData'] = $newGoods;
- }
- $goodsCate['cateData'] = $cateData;
- $data['goodsCate']['moduleData'] = json_encode($goodsCate);
- }
- return $data;
- }
- /**
- * 更新审核状态及信息
- * @param $callback
- * @param $params
- * @return ResultWrapper
- */
- public function getAuditStatus($params)
- {
- $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
- if (empty($oplatformConfigData)) {
- return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
- }
- // 获取当前登录企业的小程序配置
- $objSystemCache = new SystemCache();
- $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
- if (empty($miniprogramSetting)) {
- return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
- }
- $miniprogramSetting = json_decode($miniprogramSetting, true);
- // 企业设置的小程序id
- $authorizer_appid = $miniprogramSetting['appid'];
- $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
- $result = $objOplatform->getAuditstatus($authorizer_appid, $params['auditId']);//TODO
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
- }
- $callback = $result->getData();
- if ($callback['status'] == StatusCode::$wxAuditStatus['refuse']) {
- $reason = [
- 'reason' => isset($callback['reason']) ? $callback['reason'] : '',
- 'ScreenShot' => isset($callback['ScreenShot']) ? $callback['ScreenShot'] : '',
- ];
- }
- $dbResult = $this->objDEnterpriseBindTemplate->update([
- 'auditStatus' => isset($callback['status']) ? $callback['status'] : StatusCode::$wxAuditStatus['inAudit'],
- 'reason' => isset($reason) ? json_encode($reason) : '[]',
- ], [
- 'templateId' => $params['templateId'],
- 'enterpriseId' => $this->onlineEnterpriseId,
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- switch (isset($callback['status']) ? $callback['status'] : '') {
- case 0:
- $message = '审核成功';
- break;
- case 1:
- $message = '审核被拒绝';
- break;
- case 2:
- $message = '审核中';
- break;
- case 3:
- $message = '已撤回';
- break;
- case 4:
- $message = '审核延后';
- break;
- default:
- $message = '未查询到';
- break;
- }
- $return = [
- 'message' => $message,
- 'reason' => isset($reason) ? $reason : []
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 回调修改模版审核状态
- * @param $params
- * @return bool
- */
- public function updateAuditStatus($params)
- {
- $objDWxBindEnterprise = new DWxBindEnterprise();
- $dbResult = $objDWxBindEnterprise->get_field('enterpriseId', ['wxOriginId' => $params['ToUserName']]);
- if ($dbResult === false) {
- return false;
- }
- $dbResult = $this->objDEnterpriseBindTemplate->update(
- [
- 'auditStatus' => $params['auditStatus'],
- 'reason' => isset($params['reason']) ? json_encode($params['reason']) : '[]',
- ],
- ['enterpriseId' => $dbResult, 'enableStatus' => StatusCode::$standard]
- );
- if ($dbResult === false) {
- return false;
- }
- return true;
- }
- /**********************************************重写 start**********************************************************/
- /**
- * 1.提交代码
- *
- * @param $params
- * @param $token
- * @return ResultWrapper
- */
- public function submitCode($params, $token)
- {
- // 启用得微信模板id
- $weixinTemplateId = $params['weixinTemplateId'];
- unset($params['weixinTemplateId']);
- // 获取当前登录企业的小程序配置
- $objSystemCache = new SystemCache();
- $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
- if (empty($miniprogramSetting)) {
- return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
- }
- $miniprogramSetting = json_decode($miniprogramSetting, true);
- // 企业设置的小程序id
- $authorizer_appid = $miniprogramSetting['appid'];
- /*
- $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
- if (empty($oplatformConfigData)) {
- return ResultWrapper::fail('配置错误', ErrorCode::$paramError);
- }*/
- // 获取微信开放平台配置
- $objMOem = new MOem();
- // 默认作为SAAS用户后台操作方式获取配置
- $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
- $host = $refererUrl['host'];
- $result = $objMOem->getOemInfoByDomain($host);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- // 如果用企业id说明管理后台操作
- if(isset($params['enterpriseId']) && !empty($params['enterpriseId']) && empty($oemData)){
- // 先拿传过来的企业查询去查询
- $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- // 如果为空,用默认平台企业去操作
- if(empty($oemData)){
- $result = $objMOem->getOemInfoByenterpriseId(4);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- }
- if( empty($oemData) ){
- return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
- }
- $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
- if( empty($oplatformConfigData) ){
- return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
- }
- $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
- //设置服务器域名
- if( empty($oemData['apiDomain']) ){
- return ResultWrapper::fail('oem表中获取api接口域名配置为空', ErrorCode::$contentNotExists);
- }
- $setResult = $objOplatform->modifyDomain($authorizer_appid, $oemData['apiDomain']);
- if (!$setResult->isSuccess()){
- return ResultWrapper::fail($setResult->getData(), $setResult->getErrorCode());
- }
- $accountResult = $accountResult = $objOplatform->apiGetAuthorizerInfo($authorizer_appid);
- if (!$accountResult->isSuccess()){
- return ResultWrapper::fail($accountResult->getData(),$accountResult->getErrorCode());
- }
- $account = $accountResult->getData();
- //个人类型小程序不能设置业务域名
- if (isset($account['authorizer_info']['principal_name']) && $account['authorizer_info']['principal_name'] != '个人'){
- $setResult = $objOplatform->setwebviewdomain($authorizer_appid, $oemData['apiDomain']);
- if (!$setResult->isSuccess()){
- return ResultWrapper::fail($setResult->getData(), $setResult->getErrorCode());
- }
- }
- $ext_json = [
- 'extEnable' => 'true',
- 'extAppid' => $miniprogramSetting['appid'],
- 'directCommit' => false,
- 'ext' => [
- 'token' => $token,
- ],
- ];
- // 代理的从数据库表中获取模板id
- if( empty($oemData['weixinTemplateId']) ){
- return ResultWrapper::fail('oem表中获取微信开发平台模板id配置为空', ErrorCode::$contentNotExists);
- }
- $weixinTemplateId = $oemData['weixinTemplateId'];
- // 1.上传小程序
- $result = $objOplatform->commit($authorizer_appid, $weixinTemplateId, $ext_json, '1.0', '切换模板');
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- // 2.获取已上传的代码的页面列表
- unset($result);
- $result = $objOplatform->getPage($authorizer_appid);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $pageList = $result->getData();
- // 3.获取的体验版二维码
- unset($result);
- $result = $objOplatform->getQrcode($authorizer_appid, $pageList[0]);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- if (!is_dir(UPLOAD_FILE_PATH)) {
- mkdir(UPLOAD_FILE_PATH, 0777, true);
- }
- if (!file_put_contents(UPLOAD_FILE_PATH . $authorizer_appid . '.jpeg', $result->getData())) {
- return ResultWrapper::fail('保存体验二维码失败', ErrorCode::$contentNotExists);
- }
- //关闭其他所有开启的模版
- $dbResult = $this->objDEnterpriseBindTemplate->update(['enableStatus' => StatusCode::$delete, 'status' => StatusCode::$delete], ['enterpriseId' => $this->onlineEnterpriseId, 'enableStatus' => StatusCode::$standard]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- $params['auditStatus'] = StatusCode::$wxAuditStatus['noSubmit'];//代码提交成功,可以点击审核
- $params['qrcodeImg'] = URL_UPLOAD . $authorizer_appid . '.jpeg';//二维码地址
- $params['status'] = StatusCode::$standard;//已提交代码
- $dbResult = $this->objDEnterpriseBindTemplate->replace($params);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success('上传代码成功');
- }
- /**
- * 2.提交审核
- *
- * @param $params
- * @return ResultWrapper
- */
- public function submitAudit($params)
- {
- // 获取当前登录企业的小程序配置
- $objSystemCache = new SystemCache();
- $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
- if (empty($miniprogramSetting)) {
- return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
- }
- $miniprogramSetting = json_decode($miniprogramSetting, true);
- // 企业设置的小程序id
- $authorizer_appid = $miniprogramSetting['appid'];
- /*
- $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
- if (empty($oplatformConfigData)) {
- return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
- }*/
- // 如果用企业id说明管理后台操作 如果没有说明客户自己SAAS系统后台操作 获取微信开放平台配置
- $objMOem = new MOem();
- if(isset($params['enterpriseId']) && !empty($params['enterpriseId'])){
- // 先拿传过来的企业查询去查询
- $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- // 如果为空,用默认平台企业去操作
- if(empty($oemData)){
- $result = $objMOem->getOemInfoByenterpriseId(4);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- }else{
- $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
- $host = $refererUrl['host'];
- $result = $objMOem->getOemInfoByDomain($host);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- if( empty($oemData) ){
- return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
- }
- $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
- if( empty($oplatformConfigData) ){
- return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
- }
- $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
- $result = $objOplatform->submitAudit($authorizer_appid);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
- }
- $callback = $result->getData();
- $dbResult = $this->objDEnterpriseBindTemplate->update([
- 'auditId' => is_numeric($callback) ? $callback : '',
- 'auditStatus' => StatusCode::$wxAuditStatus['inAudit'],//提交审核成功,进入审核中
- ], [
- 'templateId' => $params['templateId'],
- 'enterpriseId' => $this->onlineEnterpriseId,
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 3.发布
- *
- * @param $params
- * @return ResultWrapper
- */
- public function release($params)
- {
- /*$oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
- if (empty($oplatformConfigData)) {
- return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
- }*/
- // 获取微信开放平台配置
- $objMOem = new MOem();
- // 如果用企业id说明管理后台操作 如果没有说明客户自己SAAS系统后台操作
- if(isset($params['enterpriseId']) && !empty($params['enterpriseId'])){
- // 先拿传过来的企业查询去查询
- $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- // 如果为空,用默认平台企业去操作
- if(empty($oemData)){
- $result = $objMOem->getOemInfoByenterpriseId(4);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- }else{
- $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
- $host = $refererUrl['host'];
- $result = $objMOem->getOemInfoByDomain($host);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- if( empty($oemData) ){
- return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
- }
- $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
- if( empty($oplatformConfigData) ){
- return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
- }
- $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
- // 获取当前登录企业的小程序配置
- $objSystemCache = new SystemCache();
- $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
- if (empty($miniprogramSetting)) {
- return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
- }
- $miniprogramSetting = json_decode($miniprogramSetting, true);
- $result = $objOplatform->release($miniprogramSetting['appid']);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), ErrorCode::$dberror);
- }
- //修改模版启用状态
- $dbResult = $this->objDEnterpriseBindTemplate->update([
- 'auditStatus' => StatusCode::$wxAuditStatus['beingUsed'],//审核成功,发布小程序成功进入正在使用状态
- ], [
- 'templateId' => $params['templateId'],
- 'enterpriseId' => $this->onlineEnterpriseId,
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($result->getData());
- }
- /**
- * 审核撤回
- * @param $params
- * @return ResultWrapper
- */
- public function revoke($params)
- {
- /*
- $oplatformConfigData = Factory::config()->getAppoint('weixin', 'oplatform');
- if (empty($oplatformConfigData)) {
- return ResultWrapper::fail('微信三方平台配置错误', ErrorCode::$paramError);
- }*/
- // 如果用企业id说明管理后台操作 如果没有说明客户自己SAAS系统后台操作 获取微信开放平台配置
- $objMOem = new MOem();
- if(isset($params['enterpriseId']) && !empty($params['enterpriseId'])){
- // 先拿传过来的企业查询去查询
- $result = $objMOem->getOemInfoByenterpriseId($params['enterpriseId']);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- // 如果为空,用默认平台企业去操作
- if(empty($oemData)){
- $result = $objMOem->getOemInfoByenterpriseId(4);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- }else{
- $refererUrl = parse_url($_SERVER['HTTP_REFERER']);
- $host = $refererUrl['host'];
- $result = $objMOem->getOemInfoByDomain($host);
- if( !$result->isSuccess() ){
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- $oemData = $result->getData();
- }
- if( empty($oemData) ){
- return ResultWrapper::fail('oem表中获取配置为空', ErrorCode::$contentNotExists);
- }
- $oplatformConfigData = json_decode($oemData['weixinOpen'], true);
- if( empty($oplatformConfigData) ){
- return ResultWrapper::fail('oem表中获取微信开放平台配置为空', ErrorCode::$contentNotExists);
- }
- $objOplatform = new Oplatform($oplatformConfigData['appid'], $oplatformConfigData['token'], $oplatformConfigData['encodingAesKey'], $oplatformConfigData['appSecret']);
- // 获取当前登录企业的小程序配置
- $objSystemCache = new SystemCache();
- $miniprogramSetting = $objSystemCache->getAppIdByEnterpriseId($this->onlineEnterpriseId);
- if (empty($miniprogramSetting)) {
- return ResultWrapper::fail('后台小程序配置项为空', ErrorCode::$configEroor);
- }
- $miniprogramSetting = json_decode($miniprogramSetting, true);
- $result = $objOplatform->undocodeaudit($miniprogramSetting['appid']);
- if (!$result->isSuccess()) {
- return ResultWrapper::fail($result->getData(), $result->getErrorCode());
- }
- //修改状态
- $dbResult = $this->objDEnterpriseBindTemplate->update([
- 'auditStatus' => StatusCode::$wxAuditStatus['beenRelease'],//撤回成功,初始状态
- ], [
- 'templateId' => $params['templateId'],
- 'enterpriseId' => $this->onlineEnterpriseId,
- ]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDEnterpriseBindTemplate->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($result->getData());
- }
- /**
- * 获取提交代码后小程序的状态
- *
- * @return ResultWrapper
- */
- public function getWxStatus()
- {
- $dbResult = $this->objDEnterpriseBindTemplate->get(
- [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'status' => StatusCode::$standard
- ],
- 'id,templateId,auditStatus'
- );
- if (empty($dbResult)) {
- $dbResult = [
- 'id' => null,
- 'templateId' => null,
- 'status' => null,
- ];
- }
- return ResultWrapper::success($dbResult);
- }
- }
|