xhMerchantService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 = [
  140. 'merchantId' => $merchantId,
  141. 'riseType' => 0,
  142. 'riseAmount' => 0,
  143. 'addTime' => time(),
  144. ];
  145. xhGoodsSettingService::add($setData, $merchantId);
  146. //首页广告
  147. //xhRecommendService::add($recommendData, $merchantId);
  148. return $merchant;
  149. }
  150. /**
  151. * 商家接入审核通过
  152. */
  153. public static function pass($admin)
  154. {
  155. $openId = $admin['openId'];
  156. $merchant = xhWxOpenService::getMerchant();
  157. $merchantId = $merchant['id'];
  158. $allTM = xhTMessageService::getList($merchantId);
  159. $shortTempId = 'OPENTM401915538';
  160. $tempId = $allTM[$shortTempId];
  161. $data = array(
  162. "touser" => $openId,
  163. "template_id" => $tempId,//模板id
  164. "url" => Yii::$app->params['adminUrl'] . "/main/approve?id={$merchant['id']}",
  165. "data" => array(
  166. "first" => array(
  167. "value" => "恭喜,您的公众号审核通过!",
  168. "color" => "#173177"
  169. ),
  170. "keyword1" => array(
  171. "value" => '审核通过',
  172. "color" => "#173177"
  173. ),
  174. "keyword2" => array(
  175. "value" => date("Y-m-d H:i:s"),
  176. "color" => "#173177"
  177. ),
  178. "remark" => array(
  179. "value" => '点此登陆后台查看详细',
  180. "color" => "#173177"
  181. )
  182. ),
  183. );
  184. weixinUtil::sendTaskInform($data, $merchant);
  185. }
  186. public static function getById($id)
  187. {
  188. $merchant = xhMerchant::getByCondition(['id' => $id]);
  189. return $merchant;
  190. }
  191. public static function getByAccount($id)
  192. {
  193. $merchant = xhMerchant::getByCondition(['id' => $id]);
  194. return $merchant;
  195. }
  196. public static function getByAppId($appId)
  197. {
  198. $merchant = xhMerchant::getByCondition(['wxAppId' => $appId]);
  199. return $merchant;
  200. }
  201. public static function getByCondition($condition)
  202. {
  203. $merchant = xhMerchant::getByCondition($condition);
  204. return $merchant;
  205. }
  206. public static function getAllByCondition($condition)
  207. {
  208. return xhMerchant::getAllByCondition($condition);
  209. }
  210. public static function getOne()
  211. {
  212. $m = xhMerchant::find()->asArray()->one();
  213. return $m;
  214. }
  215. /**
  216. * 更新商家的缓存信息
  217. * @param int $id
  218. */
  219. public static function refreshMerchantInfo($id)
  220. {
  221. $merchant = xhMerchant::getById($id);
  222. $account = $merchant['id'];
  223. $appId = $merchant['wxAppId'];
  224. $accountKey = configDict::getCacheKey('merchantAccount') . $account;
  225. $idKey = configDict::getCacheKey('merchantId') . $id;
  226. $appIdKey = configDict::getCacheKey('merchantAppId') . $appId;
  227. Yii::$app->redis->executeCommand('DEL', [$accountKey]);
  228. Yii::$app->redis->executeCommand('DEL', [$idKey]);
  229. Yii::$app->redis->executeCommand('DEL', [$appIdKey]);
  230. self::getByAccount($account);
  231. self::getById($id);
  232. self::getByAppId($appId);
  233. }
  234. /**
  235. * 更新数据
  236. */
  237. public static function updateById($id, $post)
  238. {
  239. xhMerchant::updateById($id, $post);
  240. self::refreshMerchantInfo($id);
  241. }
  242. /**
  243. * 增加,返回数组
  244. */
  245. public static function add($data)
  246. {
  247. $merchant = xhMerchant::add($data);
  248. $id = $merchant['id'];
  249. self::refreshMerchantInfo($id);
  250. return $merchant;
  251. }
  252. /**
  253. * 传入最大的照片,输出三种照片:大 中 小
  254. */
  255. public static function getShopPhotoList($largeImg)
  256. {
  257. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  258. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  259. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  260. }
  261. public static function getByMerchantName($name)
  262. {
  263. $merchant = xhMerchant::getByCondition(['merchantName' => $name]);
  264. return $merchant;
  265. }
  266. public static function getAdmin($merchant)
  267. {
  268. $adminId = isset($merchant['adminId']) ? $merchant['adminId'] : 0;
  269. $admin = xhAdminService::getById($adminId);
  270. return $admin;
  271. }
  272. }