| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- <?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, 'startRiseTime' => 0, 'endRiseTime', 'addTime' => time(), 'createTime' => date("Y-m-d H:i:s")];
- xhGoodsSettingService::add($setData, $merchantId);
-
- //生成分类
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '花束', 'showName' => '花束', 'description' => '花束', 'inTurn' => 0, 'classify' => 0];
- $bouquet = xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '花盒 ', 'showName' => '花盒', 'description' => '花盒', 'inTurn' => 1, 'classify' => 0];
- $box = xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '开业花篮 ', 'showName' => '开业花篮', 'description' => '开业花篮', 'inTurn' => 2, 'classify' => 0];
- $basket = xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '婚礼用花 ', 'showName' => '婚礼用花 ', 'description' => '婚礼用花 ', 'inTurn' => 3, 'classify' => 0];
- $wedding = xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '会议用花 ', 'showName' => '会议用花 ', 'description' => '会议用花', 'inTurn' => 4, 'classify' => 0];
- xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '情人节专区', 'showName' => '情人节专区', 'description' => '情人节专区', 'inTurn' => 5, 'classify' => 0];
- xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '母亲节专区', 'showName' => '母亲节专区', 'description' => '母亲节专区', 'inTurn' => 6, 'classify' => 0];
- xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '花艺在线', 'showName' => '花艺在线', 'description' => '花艺在线', 'inTurn' => 7, 'classify' => 1];
- xhCategoryService::addCategory($categoryData, 0, $merchantId);
- $goodsArr = [];
- $time = time();
- $maxNum = 8;
- $numList = [];
- for ($x = 0; $x < $maxNum; $x++) {
- $numList[] = $x;
- }
- shuffle($numList);
- //随机生成
- foreach ($numList as $val) {
- switch ($val) {
- case 0:
- //生成商品 1
- $goodsData1 = ['goodsName' => '一起走过的日子', 'price' => 190, 'cover' => '/goods/hs1_cover.jpg', 'shopImg' => json_encode(["/goods/hs1_img.jpg"]), 'content' => '图片来自我享花坊',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 0),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$bouquet['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData1);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $bouquet['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $bouquet['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 1:
- //生成商品 2
- $goodsData2 = ['goodsName' => '当爱在靠近', 'price' => 520, 'cover' => '/goods/hs2_cover.jpg', 'shopImg' => json_encode(['/goods/hs2_img.jpg']), 'content' => '图片来自我享花坊',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 1),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$bouquet['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData2);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $bouquet['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $bouquet['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 2:
- //生成商品 3
- $goodsData3 = ['goodsName' => '你的样子', 'price' => 230, 'cover' => '/goods/hs3_cover.jpg', 'shopImg' => json_encode(['/goods/hs3_img.jpg']), 'content' => '图片来自厦门景上添花',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 2),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$box['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData3);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $box['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $box['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 3:
- //生成商品 4
- $goodsData4 = ['goodsName' => '手捧花', 'price' => 390, 'cover' => '/goods/hs4_cover.jpg', 'shopImg' => json_encode(['/goods/hs4_img.jpg']), 'content' => '图片来自网络',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 3),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$wedding['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData4);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $wedding['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $wedding['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 4:
- //生成商品 5
- $goodsData5 = ['goodsName' => '蒸蒸日上', 'price' => 560, 'cover' => '/goods/ky1_cover.jpg', 'shopImg' => json_encode(['/goods/ky1_img.jpg']), 'content' => '图片来自沪花拾者',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 4),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$basket['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData5);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $basket['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $basket['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 5:
- //生成商品 6
- $goodsData6 = ['goodsName' => '我只在乎你', 'price' => 30, 'cover' => '/goods/one_cover.jpg', 'shopImg' => json_encode(['/goods/one1_img.jpg', '/goods/one2_img.jpg']), 'content' => '图片来自国兰花尚',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 5),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$bouquet['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData6);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $bouquet['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $bouquet['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 6:
- //生成商品 7
- $goodsData7 = ['goodsName' => '手捧花', 'price' => 380, 'cover' => '/goods/sph1_cover.jpg', 'shopImg' => json_encode(['/goods/sph1_img.jpg']), 'content' => '图片来自网络',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 6),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$wedding['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData7);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $wedding['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $wedding['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- case 7:
- //生成商品 8
- $goodsData8 = ['goodsName' => '大展鹏图', 'price' => 360, 'cover' => '/goods/ky2_cover.jpg', 'shopImg' => json_encode(['/goods/ky2_img.jpg']), 'content' => '图片来自沪花拾者',
- 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 7),
- 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$basket['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
- $goods = xhGoodsService::add($goodsData8);
- //生成商品的分类
- $goodsCategoryData = ['cId' => $basket['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
- xhGoodsCategoryService::add($goodsCategoryData, $basket['id'], $goods['id']);
- $goodsArr[] = $goods['id'];
- break;
- default:
- }
- }
- shuffle($goodsArr);
-
- $recommendData = [
- 'merchantId' => $merchantId,
- 'goods' => implode(',', $goodsArr),
- 'slide' => '[{"img":"/slide/banner1.jpg","link":""},{"img":"/slide/banner2.jpg","link":""},{"img":"/slide/banner3.jpg","link":""}]',
- 'createTime' => $date,
- 'updateTime' => $date,
- ];
- 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;
- }
-
- }
|