xhMerchantService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use linslin\yii2\curl;
  5. use common\components\dict;
  6. use common\components\stringUtil;
  7. use common\components\wxUtil;
  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 = dict::getDict('merchant', 'authAccount');
  21. return in_array($style, $authAccount);
  22. }
  23. /**
  24. * 保存公众号二维码
  25. * @return string
  26. */
  27. public static function qrCodeSave($url, $wxAppId)
  28. {
  29. return '';
  30. $directory = dict::getDict('imgSavePath', 'wxMpQrcode');
  31. $path = Yii::getAlias('@webroot') . '/../../images';
  32. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  33. if (!file_exists($path . $pre)) {
  34. mkdir($path . $pre, 0777, true);
  35. }
  36. $randString = stringUtil::buildOrderNo();
  37. $id = substr($wxAppId, -2);
  38. $name = $id . '_' . $randString . '.jpg';
  39. $fullFileName = $path . $pre . $name;
  40. $fileName = $pre . $name;
  41. $curl = new curl\Curl();
  42. $result = $curl->get($url);
  43. $fp2 = @fopen($fullFileName, 'a');//文件大小
  44. fwrite($fp2, $result);
  45. fclose($fp2);
  46. return $fileName;//默认大小 大
  47. }
  48. /**
  49. * 保存logo
  50. * @return string
  51. */
  52. public static function logoSave($url, $wxAppId)
  53. {
  54. return '';
  55. $directory = dict::getDict('imgSavePath', 'wxMpAvatar');
  56. $path = Yii::getAlias('@webroot') . '/../../images';
  57. $pre = '/' . $directory . '/' . date('Y') . '/' . date('m') . '/' . date("d") . '/';
  58. if (!file_exists($path . $pre)) {
  59. mkdir($path . $pre, 0777, true);
  60. }
  61. $randString = stringUtil::buildOrderNo();
  62. $id = substr($wxAppId, -2);
  63. $name = $id . '_' . $randString . '.jpg';
  64. $fullFileName = $path . $pre . $name;
  65. $fileName = $pre . $name;
  66. $curl = new curl\Curl();
  67. $result = $curl->get($url);
  68. $fp2 = @fopen($fullFileName, 'a');//文件大小
  69. fwrite($fp2, $result);
  70. fclose($fp2);
  71. $dstImg200 = $path . $pre . $id . '_' . $randString . '_200.jpg';//缩略宽 200px 中
  72. util::img2thumb($fullFileName, $dstImg200, 200, 0, 0, 0);
  73. $dstImg50 = $path . $pre . $id . '_' . $randString . '_50.jpg';//缩略成宽50px 小
  74. util::img2thumb($fullFileName, $dstImg50, 50, 0, 0, 0);
  75. return $fileName;//默认大小 大
  76. }
  77. /**
  78. * 传入最大的logo,输出三种logo:大 中 小
  79. */
  80. public static function getLogoList($largeImg)
  81. {
  82. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  83. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  84. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  85. }
  86. /**
  87. * 商家接入审核通过
  88. */
  89. public static function pass($admin)
  90. {
  91. $openId = $admin['openId'];
  92. $merchant = xhWxOpenService::getMerchant();
  93. $sjId = $merchant['id'];
  94. $allTM = xhTMessageService::getList($sjId);
  95. $shortTempId = 'OPENTM401915538';
  96. $tempId = $allTM[$shortTempId];
  97. $data = array(
  98. "touser" => $openId,
  99. "template_id" => $tempId,//模板id
  100. "url" => Yii::$app->params['adminUrl'] . "/main/approve?id={$merchant['id']}",
  101. "data" => array(
  102. "first" => array(
  103. "value" => "恭喜,您的公众号审核通过!",
  104. "color" => "#173177"
  105. ),
  106. "keyword1" => array(
  107. "value" => '审核通过',
  108. "color" => "#173177"
  109. ),
  110. "keyword2" => array(
  111. "value" => date("Y-m-d H:i:s"),
  112. "color" => "#173177"
  113. ),
  114. "remark" => array(
  115. "value" => '点此登陆后台查看详细',
  116. "color" => "#173177"
  117. )
  118. ),
  119. );
  120. wxUtil::sendTaskInform($data, $merchant);
  121. }
  122. public static function getById($id)
  123. {
  124. $merchant = xhMerchant::getByCondition(['id' => $id]);
  125. return $merchant;
  126. }
  127. public static function getByAccount($id)
  128. {
  129. $merchant = xhMerchant::getByCondition(['id' => $id]);
  130. return $merchant;
  131. }
  132. public static function getByAppId($appId)
  133. {
  134. $merchant = xhMerchant::getByCondition(['wxAppId' => $appId]);
  135. return $merchant;
  136. }
  137. public static function getByCondition($condition)
  138. {
  139. $merchant = xhMerchant::getByCondition($condition);
  140. return $merchant;
  141. }
  142. public static function getAllByCondition($condition)
  143. {
  144. return xhMerchant::getAllByCondition($condition);
  145. }
  146. public static function getOne()
  147. {
  148. $m = xhMerchant::find()->asArray()->one();
  149. return $m;
  150. }
  151. /**
  152. * 更新商家的缓存信息
  153. * @param int $id
  154. */
  155. public static function refreshMerchantInfo($id)
  156. {
  157. $merchant = xhMerchant::getById($id);
  158. $account = $merchant['id'];
  159. $appId = $merchant['wxAppId'];
  160. $accountKey = dict::getCacheKey('merchantAccount') . $account;
  161. $idKey = dict::getCacheKey('sjId') . $id;
  162. $appIdKey = dict::getCacheKey('merchantAppId') . $appId;
  163. Yii::$app->redis->executeCommand('DEL', [$accountKey]);
  164. Yii::$app->redis->executeCommand('DEL', [$idKey]);
  165. Yii::$app->redis->executeCommand('DEL', [$appIdKey]);
  166. self::getByAccount($account);
  167. self::getById($id);
  168. self::getByAppId($appId);
  169. }
  170. /**
  171. * 更新数据
  172. */
  173. public static function updateById($id, $post)
  174. {
  175. xhMerchant::updateById($id, $post);
  176. self::refreshMerchantInfo($id);
  177. }
  178. /**
  179. * 增加,返回数组
  180. */
  181. public static function add($data)
  182. {
  183. $merchant = xhMerchant::add($data);
  184. $id = $merchant['id'];
  185. self::refreshMerchantInfo($id);
  186. return $merchant;
  187. }
  188. /**
  189. * 传入最大的照片,输出三种照片:大 中 小
  190. */
  191. public static function getShopPhotoList($largeImg)
  192. {
  193. $extend = substr($largeImg, strrpos($largeImg, '.') + 1);
  194. $pre = substr($largeImg, 0, strrpos($largeImg, '.'));
  195. return ['small' => $pre . '_50.' . $extend, 'medium' => $pre . '_200.' . $extend, 'large' => $largeImg];
  196. }
  197. public static function getByName($name)
  198. {
  199. $merchant = xhMerchant::getByCondition(['name' => $name]);
  200. return $merchant;
  201. }
  202. public static function getAdmin($merchant)
  203. {
  204. $adminId = isset($merchant['adminId']) ? $merchant['adminId'] : 0;
  205. $admin = xhAdminService::getById($adminId);
  206. return $admin;
  207. }
  208. }