xhMerchantService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use linslin\yii2\curl;
  5. use common\components\configDict;
  6. use common\components\stringUtil;
  7. use common\components\weixinUtil;
  8. use common\models\xhMerchant;
  9. use common\models\xhAdmin;
  10. use common\components\util;
  11. class xhMerchantService
  12. {
  13. /**
  14. * 是否认证的公众号,服务号
  15. */
  16. public static function isAuth($id)
  17. {
  18. $merchant = self::getById($id);
  19. $style = $merchant['accountStyle'];
  20. $authAccount = configDict::getConfig('merchant', 'authAccount');
  21. return in_array($style, $authAccount);
  22. }
  23. /**
  24. * 保存公众号二维码
  25. * @return string
  26. */
  27. public static function qrCodeSave($url, $wxAppId)
  28. {
  29. $directory = configDict::getConfig('imgSavePath', 'wxMpQrcode');
  30. $path = Yii::getAlias('@webroot') . '/../../images';
  31. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  32. if (!file_exists($path . $pre)) {
  33. mkdir($path . $pre, 0777, true);
  34. }
  35. $randString = stringUtil::buildOrderNo();
  36. $id = substr($wxAppId, -2);
  37. $name = $id . '_' . $randString . '.jpg';
  38. $fullFileName = $path . $pre . $name;
  39. $fileName = $pre . $name;
  40. $curl = new curl\Curl();
  41. $result = $curl->get($url);
  42. $fp2 = @fopen($fullFileName, 'a');//文件大小
  43. fwrite($fp2, $result);
  44. fclose($fp2);
  45. return $fileName;//默认大小 大
  46. }
  47. /**
  48. * 保存logo
  49. * @return string
  50. */
  51. public static function logoSave($url, $wxAppId)
  52. {
  53. $directory = configDict::getConfig('imgSavePath', 'wxMpAvatar');
  54. $path = Yii::getAlias('@webroot') . '/../../images';
  55. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  56. if (!file_exists($path . $pre)) {
  57. mkdir($path . $pre, 0777, true);
  58. }
  59. $randString = stringUtil::buildOrderNo();
  60. $id = substr($wxAppId, -2);
  61. $name = $id . '_' . $randString . '.jpg';
  62. $fullFileName = $path . $pre . $name;
  63. $fileName = $pre . $name;
  64. $curl = new curl\Curl();
  65. $result = $curl->get($url);
  66. $fp2 = @fopen($fullFileName, 'a');//文件大小
  67. fwrite($fp2, $result);
  68. fclose($fp2);
  69. $dstImg200 = $path . $pre . $id . '_' . $randString . '_200.jpg';//缩略宽 200px 中
  70. util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
  71. $dstImg50 = $path . $pre . $id . '_' . $randString . '_50.jpg';//缩略成宽50px 小
  72. util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
  73. return $fileName;//默认大小 大
  74. }
  75. /**
  76. * 传入最大的logo,输出三种logo:大 中 小
  77. */
  78. public static function getLogoList($largeImg)
  79. {
  80. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  81. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  82. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  83. }
  84. /**
  85. * 申请时创建商家
  86. */
  87. public static function applyCreateMerchant($data)
  88. {
  89. //生成商家
  90. $date = date("Y-m-d H:i:s");
  91. $alipayAccount = $data['alipayAccount'];//支付宝帐号
  92. $alipayAccountName = isset($data['alipayAccountName']) ? $data['alipayAccountName'] : '';
  93. unset($data['alipayAccount']);
  94. $merchant = self::add($data);
  95. $merchantId = $merchant['id'];
  96. //生成商家资产
  97. $assetData = [
  98. 'balance' => 0,
  99. 'totalVisit' => 0,
  100. 'totalUser' => 0,
  101. 'totalMember' => 0,
  102. 'totalFans' => 0,
  103. 'totalWeixinFans' => 0,
  104. 'totalIntegral' => 0,
  105. 'totalPoint' => 0,
  106. 'totalGoods' => 0,
  107. 'totalRecharge' => 0,
  108. 'totalOrder' => 0,
  109. 'totalDeal' => 0,
  110. 'merchantId' => $merchantId,
  111. 'totalIncome' => 0,
  112. 'totalExpend' => 0,
  113. 'createTime' => $date,
  114. 'updateTime' => $date,
  115. ];
  116. xhMerchantAssetService::add($assetData);//商家资产表
  117. //生成商家拓展信息
  118. $extendData = [
  119. 'merchantId' => $merchantId,
  120. 'wxAppId' => isset($data['wxAppId']) ? $data['wxAppId'] : '',
  121. 'webTitle' => isset($data['merchantName']) ? $data['merchantName'] : '',
  122. 'webKeyword' => isset($data['merchantName']) ? $data['merchantName'] : '',
  123. 'webDescription' => isset($data['merchantName']) ? $data['merchantName'] : '',
  124. 'wxMenuInit' => 0,
  125. 'wxTMessageInit' => 0,
  126. 'payment' => 0,//支付功能关闭
  127. 'recharge' => 0,//充值功能关闭
  128. 'giveCoupon' => 1,//默认赠送代金劵
  129. 'giveCouponPrice' => 5,//代金劵金额5元
  130. 'couponMeetPrice' => 180,//满足金额
  131. 'couponValidity' => 30,//代金劵有效期30天
  132. 'alipayAccount' => $alipayAccount,//提现的支付宝帐号
  133. 'alipayAccountName' => $alipayAccountName,//提现支付宝帐号姓名
  134. 'gradeList' => '1_2000_9I2_5000_88I3_10000_85I4_20000_8',//1 级会员需要满 2000 积分 购物享9折;2 级会员需要满 5000 积分 购物享88折
  135. 'createTime' => $date,
  136. 'updateTime' => $date,
  137. ];
  138. xhMerchantExtendService::add($extendData);
  139. $setData = ['merchantId' => $merchantId, 'riseType' => 0, 'riseAmount' => 0, 'startRiseTime' => 0, 'endRiseTime', 'addTime' => time(), 'createTime' => date("Y-m-d H:i:s")];
  140. xhGoodsSettingService::add($setData, $merchantId);
  141. //生成分类
  142. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '花束', 'showName' => '花束', 'description' => '花束', 'inTurn' => 0, 'classify' => 0];
  143. $bouquet = xhCategoryService::addCategory($categoryData, 0, $merchantId);
  144. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '花盒 ', 'showName' => '花盒', 'description' => '花盒', 'inTurn' => 1, 'classify' => 0];
  145. $box = xhCategoryService::addCategory($categoryData, 0, $merchantId);
  146. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '开业花篮 ', 'showName' => '开业花篮', 'description' => '开业花篮', 'inTurn' => 2, 'classify' => 0];
  147. $basket = xhCategoryService::addCategory($categoryData, 0, $merchantId);
  148. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '婚礼用花 ', 'showName' => '婚礼用花 ', 'description' => '婚礼用花 ', 'inTurn' => 3, 'classify' => 0];
  149. $wedding = xhCategoryService::addCategory($categoryData, 0, $merchantId);
  150. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '会议用花 ', 'showName' => '会议用花 ', 'description' => '会议用花', 'inTurn' => 4, 'classify' => 0];
  151. xhCategoryService::addCategory($categoryData, 0, $merchantId);
  152. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '情人节专区', 'showName' => '情人节专区', 'description' => '情人节专区', 'inTurn' => 5, 'classify' => 0];
  153. xhCategoryService::addCategory($categoryData, 0, $merchantId);
  154. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '母亲节专区', 'showName' => '母亲节专区', 'description' => '母亲节专区', 'inTurn' => 6, 'classify' => 0];
  155. xhCategoryService::addCategory($categoryData, 0, $merchantId);
  156. $categoryData = ['merchantId' => $merchantId, 'parentId' => 0, 'categoryName' => '花艺在线', 'showName' => '花艺在线', 'description' => '花艺在线', 'inTurn' => 7, 'classify' => 1];
  157. xhCategoryService::addCategory($categoryData, 0, $merchantId);
  158. $goodsArr = [];
  159. $time = time();
  160. $maxNum = 8;
  161. $numList = [];
  162. for ($x = 0; $x < $maxNum; $x++) {
  163. $numList[] = $x;
  164. }
  165. shuffle($numList);
  166. //随机生成
  167. foreach ($numList as $val) {
  168. switch ($val) {
  169. case 0:
  170. //生成商品 1
  171. $goodsData1 = ['goodsName' => '一起走过的日子', 'price' => 190, 'cover' => '/goods/hs1_cover.jpg', 'shopImg' => json_encode(["/goods/hs1_img.jpg"]), 'content' => '图片来自我享花坊',
  172. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 0),
  173. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$bouquet['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  174. $goods = xhGoodsService::add($goodsData1);
  175. //生成商品的分类
  176. $goodsCategoryData = ['cId' => $bouquet['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  177. xhGoodsCategoryService::add($goodsCategoryData, $bouquet['id'], $goods['id']);
  178. $goodsArr[] = $goods['id'];
  179. break;
  180. case 1:
  181. //生成商品 2
  182. $goodsData2 = ['goodsName' => '当爱在靠近', 'price' => 520, 'cover' => '/goods/hs2_cover.jpg', 'shopImg' => json_encode(['/goods/hs2_img.jpg']), 'content' => '图片来自我享花坊',
  183. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 1),
  184. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$bouquet['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  185. $goods = xhGoodsService::add($goodsData2);
  186. //生成商品的分类
  187. $goodsCategoryData = ['cId' => $bouquet['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  188. xhGoodsCategoryService::add($goodsCategoryData, $bouquet['id'], $goods['id']);
  189. $goodsArr[] = $goods['id'];
  190. break;
  191. case 2:
  192. //生成商品 3
  193. $goodsData3 = ['goodsName' => '你的样子', 'price' => 230, 'cover' => '/goods/hs3_cover.jpg', 'shopImg' => json_encode(['/goods/hs3_img.jpg']), 'content' => '图片来自厦门景上添花',
  194. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 2),
  195. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$box['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  196. $goods = xhGoodsService::add($goodsData3);
  197. //生成商品的分类
  198. $goodsCategoryData = ['cId' => $box['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  199. xhGoodsCategoryService::add($goodsCategoryData, $box['id'], $goods['id']);
  200. $goodsArr[] = $goods['id'];
  201. break;
  202. case 3:
  203. //生成商品 4
  204. $goodsData4 = ['goodsName' => '手捧花', 'price' => 390, 'cover' => '/goods/hs4_cover.jpg', 'shopImg' => json_encode(['/goods/hs4_img.jpg']), 'content' => '图片来自网络',
  205. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 3),
  206. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$wedding['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  207. $goods = xhGoodsService::add($goodsData4);
  208. //生成商品的分类
  209. $goodsCategoryData = ['cId' => $wedding['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  210. xhGoodsCategoryService::add($goodsCategoryData, $wedding['id'], $goods['id']);
  211. $goodsArr[] = $goods['id'];
  212. break;
  213. case 4:
  214. //生成商品 5
  215. $goodsData5 = ['goodsName' => '蒸蒸日上', 'price' => 560, 'cover' => '/goods/ky1_cover.jpg', 'shopImg' => json_encode(['/goods/ky1_img.jpg']), 'content' => '图片来自沪花拾者',
  216. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 4),
  217. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$basket['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  218. $goods = xhGoodsService::add($goodsData5);
  219. //生成商品的分类
  220. $goodsCategoryData = ['cId' => $basket['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  221. xhGoodsCategoryService::add($goodsCategoryData, $basket['id'], $goods['id']);
  222. $goodsArr[] = $goods['id'];
  223. break;
  224. case 5:
  225. //生成商品 6
  226. $goodsData6 = ['goodsName' => '我只在乎你', 'price' => 30, 'cover' => '/goods/one_cover.jpg', 'shopImg' => json_encode(['/goods/one1_img.jpg', '/goods/one2_img.jpg']), 'content' => '图片来自国兰花尚',
  227. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 5),
  228. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$bouquet['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  229. $goods = xhGoodsService::add($goodsData6);
  230. //生成商品的分类
  231. $goodsCategoryData = ['cId' => $bouquet['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  232. xhGoodsCategoryService::add($goodsCategoryData, $bouquet['id'], $goods['id']);
  233. $goodsArr[] = $goods['id'];
  234. break;
  235. case 6:
  236. //生成商品 7
  237. $goodsData7 = ['goodsName' => '手捧花', 'price' => 380, 'cover' => '/goods/sph1_cover.jpg', 'shopImg' => json_encode(['/goods/sph1_img.jpg']), 'content' => '图片来自网络',
  238. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 6),
  239. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$wedding['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  240. $goods = xhGoodsService::add($goodsData7);
  241. //生成商品的分类
  242. $goodsCategoryData = ['cId' => $wedding['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  243. xhGoodsCategoryService::add($goodsCategoryData, $wedding['id'], $goods['id']);
  244. $goodsArr[] = $goods['id'];
  245. break;
  246. case 7:
  247. //生成商品 8
  248. $goodsData8 = ['goodsName' => '大展鹏图', 'price' => 360, 'cover' => '/goods/ky2_cover.jpg', 'shopImg' => json_encode(['/goods/ky2_img.jpg']), 'content' => '图片来自沪花拾者',
  249. 'inTurn' => 0, 'createTime' => $date, 'updateTime' => $date, 'sold' => rand(111, 999), 'stock' => rand(111, 999), 'itemNumber' => 'F' . $merchantId . ($time + 7),
  250. 'merchantId' => $merchantId, 'userId' => 0, 'categoryId' => (string)$basket['id'], 'createTime' => $date, 'flowerNum' => 0, 'fitPeople' => 0, 'fitFestival' => 0,];
  251. $goods = xhGoodsService::add($goodsData8);
  252. //生成商品的分类
  253. $goodsCategoryData = ['cId' => $basket['id'], 'gId' => $goods['id'], 'merchantId' => $merchantId];
  254. xhGoodsCategoryService::add($goodsCategoryData, $basket['id'], $goods['id']);
  255. $goodsArr[] = $goods['id'];
  256. break;
  257. default:
  258. }
  259. }
  260. shuffle($goodsArr);
  261. $recommendData = [
  262. 'merchantId' => $merchantId,
  263. 'goods' => implode(',', $goodsArr),
  264. 'slide' => '[{"img":"/slide/banner1.jpg","link":""},{"img":"/slide/banner2.jpg","link":""},{"img":"/slide/banner3.jpg","link":""}]',
  265. 'createTime' => $date,
  266. 'updateTime' => $date,
  267. ];
  268. xhRecommendService::add($recommendData, $merchantId);//首页推荐的商品
  269. return $merchant;
  270. }
  271. /**
  272. * 商家接入审核通过
  273. */
  274. public static function pass($admin)
  275. {
  276. $openId = $admin['openId'];
  277. $merchant = xhWxOpenService::getMerchant();
  278. $merchantId = $merchant['id'];
  279. $allTM = xhTMessageService::getList($merchantId);
  280. $shortTempId = 'OPENTM401915538';
  281. $tempId = $allTM[$shortTempId];
  282. $data = array(
  283. "touser" => $openId,
  284. "template_id" => $tempId,//模板id
  285. "url" => Yii::$app->params['adminUrl'] . "/main/approve?id={$merchant['id']}",
  286. "data" => array(
  287. "first" => array(
  288. "value" => "恭喜,您的公众号审核通过!",
  289. "color" => "#173177"
  290. ),
  291. "keyword1" => array(
  292. "value" => '审核通过',
  293. "color" => "#173177"
  294. ),
  295. "keyword2" => array(
  296. "value" => date("Y-m-d H:i:s"),
  297. "color" => "#173177"
  298. ),
  299. "remark" => array(
  300. "value" => '点此登陆后台查看详细',
  301. "color" => "#173177"
  302. )
  303. ),
  304. );
  305. weixinUtil::sendTaskInform($data, $merchant);
  306. }
  307. public static function getById($id)
  308. {
  309. $merchant = xhMerchant::getByCondition(['id' => $id]);
  310. return $merchant;
  311. }
  312. public static function getByAccount($id)
  313. {
  314. $merchant = xhMerchant::getByCondition(['id' => $id]);
  315. return $merchant;
  316. }
  317. public static function getByAppId($appId)
  318. {
  319. $merchant = xhMerchant::getByCondition(['wxAppId' => $appId]);
  320. return $merchant;
  321. }
  322. public static function getByCondition($condition)
  323. {
  324. $merchant = xhMerchant::getByCondition($condition);
  325. return $merchant;
  326. }
  327. public static function getAllByCondition($condition)
  328. {
  329. return xhMerchant::getAllByCondition($condition);
  330. }
  331. public static function getOne()
  332. {
  333. $m = xhMerchant::find()->asArray()->one();
  334. return $m;
  335. }
  336. /**
  337. * 更新商家的缓存信息
  338. * @param int $id
  339. */
  340. public static function refreshMerchantInfo($id)
  341. {
  342. $merchant = xhMerchant::getById($id);
  343. $account = $merchant['id'];
  344. $appId = $merchant['wxAppId'];
  345. $accountKey = configDict::getCacheKey('merchantAccount') . $account;
  346. $idKey = configDict::getCacheKey('merchantId') . $id;
  347. $appIdKey = configDict::getCacheKey('merchantAppId') . $appId;
  348. Yii::$app->redis->executeCommand('DEL', [$accountKey]);
  349. Yii::$app->redis->executeCommand('DEL', [$idKey]);
  350. Yii::$app->redis->executeCommand('DEL', [$appIdKey]);
  351. self::getByAccount($account);
  352. self::getById($id);
  353. self::getByAppId($appId);
  354. }
  355. /**
  356. * 更新数据
  357. */
  358. public static function updateById($id, $post)
  359. {
  360. xhMerchant::updateById($id, $post);
  361. self::refreshMerchantInfo($id);
  362. }
  363. /**
  364. * 增加,返回数组
  365. */
  366. public static function add($data)
  367. {
  368. $merchant = xhMerchant::add($data);
  369. $id = $merchant['id'];
  370. self::refreshMerchantInfo($id);
  371. return $merchant;
  372. }
  373. /**
  374. * 传入最大的照片,输出三种照片:大 中 小
  375. */
  376. public static function getShopPhotoList($largeImg)
  377. {
  378. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  379. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  380. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  381. }
  382. public static function getByMerchantName($name)
  383. {
  384. $merchant = xhMerchant::getByCondition(['merchantName' => $name]);
  385. return $merchant;
  386. }
  387. public static function getAdmin($merchant)
  388. {
  389. $adminId = isset($merchant['adminId']) ? $merchant['adminId'] : 0;
  390. $admin = xhAdminService::getById($adminId);
  391. return $admin;
  392. }
  393. }