| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace common\services;
- use Yii;
- use linslin\yii2\curl;
- use common\components\configDict;
- use common\components\stringUtil;
- use common\components\weixinUtil;
- use common\models\xhMerchant;
- use common\models\xhAdmin;
- use common\components\util;
- class xhMerchantService
- {
-
- /**
- * 是否认证的公众号,服务号
- */
- public static function isAuth($id)
- {
- $merchant = self::getById($id);
- $style = $merchant['accountStyle'];
- $authAccount = configDict::getConfig('merchant', 'authAccount');
- return in_array($style, $authAccount);
- }
-
- /**
- * 保存公众号二维码
- * @return string
- */
- public static function qrCodeSave($url, $wxAppId)
- {
- $directory = configDict::getConfig('imgSavePath', 'wxMpQrcode');
- $path = Yii::getAlias('@webroot') . '/../../images';
- $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($path . $pre)) {
- mkdir($path . $pre, 0777, true);
- }
- $randString = stringUtil::buildOrderNo();
- $id = substr($wxAppId, -2);
- $name = $id . '_' . $randString . '.jpg';
- $fullFileName = $path . $pre . $name;
- $fileName = $pre . $name;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $fp2 = @fopen($fullFileName, 'a');//文件大小
- fwrite($fp2, $result);
- fclose($fp2);
- return $fileName;//默认大小 大
- }
-
- /**
- * 保存logo
- * @return string
- */
- public static function logoSave($url, $wxAppId)
- {
- $directory = configDict::getConfig('imgSavePath', 'wxMpAvatar');
- $path = Yii::getAlias('@webroot') . '/../../images';
- $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
- if (!file_exists($path . $pre)) {
- mkdir($path . $pre, 0777, true);
- }
- $randString = stringUtil::buildOrderNo();
- $id = substr($wxAppId, -2);
- $name = $id . '_' . $randString . '.jpg';
- $fullFileName = $path . $pre . $name;
- $fileName = $pre . $name;
- $curl = new curl\Curl();
- $result = $curl->get($url);
- $fp2 = @fopen($fullFileName, 'a');//文件大小
- fwrite($fp2, $result);
- fclose($fp2);
-
- $dstImg200 = $path . $pre . $id . '_' . $randString . '_200.jpg';//缩略宽 200px 中
- util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
-
- $dstImg50 = $path . $pre . $id . '_' . $randString . '_50.jpg';//缩略成宽50px 小
- util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
- return $fileName;//默认大小 大
- }
-
- /**
- * 传入最大的logo,输出三种logo:大 中 小
- */
- public static function getLogoList($largeImg)
- {
- $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
- $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
- return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
- }
-
- /**
- * 申请时创建商家
- */
- public static function applyCreateMerchant($data)
- {
- //生成商家
- $date = date("Y-m-d H:i:s");
- $alipayAccount = $data['alipayAccount'];//支付宝帐号
- $alipayAccountName = isset($data['alipayAccountName']) ? $data['alipayAccountName'] : '';
- unset($data['alipayAccount']);
- $merchant = self::add($data);
- $merchantId = $merchant['id'];
- //生成商家资产
- $assetData = [
- 'balance' => 0,
- 'totalVisit' => 0,
- 'totalUser' => 0,
- 'totalMember' => 0,
- 'totalFans' => 0,
- 'totalWeixinFans' => 0,
- 'totalIntegral' => 0,
- 'totalPoint' => 0,
- 'totalGoods' => 0,
- 'totalRecharge' => 0,
- 'totalOrder' => 0,
- 'totalDeal' => 0,
- 'merchantId' => $merchantId,
- 'totalIncome' => 0,
- 'totalExpend' => 0,
- 'createTime' => $date,
- 'updateTime' => $date,
- ];
- xhMerchantAssetService::add($assetData);//商家资产表
-
- //生成商家拓展信息
- $extendData = [
- 'merchantId' => $merchantId,
- 'wxAppId' => isset($data['wxAppId']) ? $data['wxAppId'] : '',
- 'webTitle' => isset($data['merchantName']) ? $data['merchantName'] : '',
- 'webKeyword' => isset($data['merchantName']) ? $data['merchantName'] : '',
- 'webDescription' => isset($data['merchantName']) ? $data['merchantName'] : '',
- 'wxMenuInit' => 0,
- 'wxTMessageInit' => 0,
- 'payment' => 0,//支付功能关闭
- 'recharge' => 0,//充值功能关闭
- 'giveCoupon' => 1,//默认赠送代金劵
- 'giveCouponPrice' => 5,//代金劵金额5元
- 'couponMeetPrice' => 180,//满足金额
- 'couponValidity' => 30,//代金劵有效期30天
- 'alipayAccount' => $alipayAccount,//提现的支付宝帐号
- 'alipayAccountName' => $alipayAccountName,//提现支付宝帐号姓名
- 'gradeList' => '1_2000_9I2_5000_88I3_10000_85I4_20000_8',//1 级会员需要满 2000 积分 购物享9折;2 级会员需要满 5000 积分 购物享88折
- 'createTime' => $date,
- 'updateTime' => $date,
- ];
- xhMerchantExtendService::add($extendData);
- $setData = [
- 'merchantId' => $merchantId,
- 'riseType' => 0,
- 'riseAmount' => 0,
- 'addTime' => time(),
- ];
- xhGoodsSettingService::add($setData, $merchantId);
- //首页广告
- //xhRecommendService::add($recommendData, $merchantId);
- return $merchant;
- }
-
- /**
- * 商家接入审核通过
- */
- public static function pass($admin)
- {
- $openId = $admin['openId'];
- $merchant = xhWxOpenService::getMerchant();
- $merchantId = $merchant['id'];
- $allTM = xhTMessageService::getList($merchantId);
- $shortTempId = 'OPENTM401915538';
- $tempId = $allTM[$shortTempId];
- $data = array(
- "touser" => $openId,
- "template_id" => $tempId,//模板id
- "url" => Yii::$app->params['adminUrl'] . "/main/approve?id={$merchant['id']}",
- "data" => array(
- "first" => array(
- "value" => "恭喜,您的公众号审核通过!",
- "color" => "#173177"
- ),
- "keyword1" => array(
- "value" => '审核通过',
- "color" => "#173177"
- ),
- "keyword2" => array(
- "value" => date("Y-m-d H:i:s"),
- "color" => "#173177"
- ),
- "remark" => array(
- "value" => '点此登陆后台查看详细',
- "color" => "#173177"
- )
- ),
- );
- weixinUtil::sendTaskInform($data, $merchant);
- }
-
- public static function getById($id)
- {
- $merchant = xhMerchant::getByCondition(['id' => $id]);
- return $merchant;
- }
-
- public static function getByAccount($id)
- {
- $merchant = xhMerchant::getByCondition(['id' => $id]);
- return $merchant;
- }
-
- public static function getByAppId($appId)
- {
- $merchant = xhMerchant::getByCondition(['wxAppId' => $appId]);
- return $merchant;
- }
-
- public static function getByCondition($condition)
- {
- $merchant = xhMerchant::getByCondition($condition);
- return $merchant;
- }
-
- public static function getAllByCondition($condition)
- {
- return xhMerchant::getAllByCondition($condition);
- }
-
- public static function getOne()
- {
- $m = xhMerchant::find()->asArray()->one();
- return $m;
- }
-
- /**
- * 更新商家的缓存信息
- * @param int $id
- */
- public static function refreshMerchantInfo($id)
- {
- $merchant = xhMerchant::getById($id);
- $account = $merchant['id'];
- $appId = $merchant['wxAppId'];
- $accountKey = configDict::getCacheKey('merchantAccount') . $account;
- $idKey = configDict::getCacheKey('merchantId') . $id;
- $appIdKey = configDict::getCacheKey('merchantAppId') . $appId;
- Yii::$app->redis->executeCommand('DEL', [$accountKey]);
- Yii::$app->redis->executeCommand('DEL', [$idKey]);
- Yii::$app->redis->executeCommand('DEL', [$appIdKey]);
- self::getByAccount($account);
- self::getById($id);
- self::getByAppId($appId);
- }
-
- /**
- * 更新数据
- */
- public static function updateById($id, $post)
- {
- xhMerchant::updateById($id, $post);
- self::refreshMerchantInfo($id);
- }
-
- /**
- * 增加,返回数组
- */
- public static function add($data)
- {
- $merchant = xhMerchant::add($data);
- $id = $merchant['id'];
- self::refreshMerchantInfo($id);
- return $merchant;
- }
-
- /**
- * 传入最大的照片,输出三种照片:大 中 小
- */
- public static function getShopPhotoList($largeImg)
- {
- $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
- $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
- return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
- }
-
- public static function getByMerchantName($name)
- {
- $merchant = xhMerchant::getByCondition(['merchantName' => $name]);
- return $merchant;
- }
-
- public static function getAdmin($merchant)
- {
- $adminId = isset($merchant['adminId']) ? $merchant['adminId'] : 0;
- $admin = xhAdminService::getById($adminId);
- return $admin;
- }
-
- }
|