business.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * 通用业务
  4. * ssh 2019.8.20
  5. */
  6. namespace common\components;
  7. use Yii;
  8. use yii\imagine\Image;
  9. class business
  10. {
  11. //支付方式对应资金要保存的字段 ssh 2019.8.20
  12. public static function savePayWayAmount($object, $amount, $payWay)
  13. {
  14. switch ($payWay) {
  15. case 0:
  16. $object->weixin += $amount;
  17. break;
  18. case 1:
  19. $object->alipay += $amount;
  20. break;
  21. case 2:
  22. $object->balancePay += $amount;
  23. break;
  24. case 3:
  25. $object->cash += $amount;
  26. break;
  27. default:
  28. util::fail('付款方式不存在');
  29. }
  30. return $object;
  31. }
  32. //对商品图片转化 ssh 2019.12.2
  33. public static function formatGoodsImg($info)
  34. {
  35. $shopImg = $info['shopImg'] ?? [];
  36. unset($info['shopImg']);
  37. if (empty($shopImg)) {
  38. $info['shortImgList'] = ['/hhb_small.png'];
  39. $info['imgList'] = [imgUtil::groupImg('')];
  40. $info['smallImgList'] = [imgUtil::groupImg('')];
  41. return $info;
  42. }
  43. $arr = json_decode($shopImg, true);
  44. if (!is_array($arr)) {
  45. $info['shortImgList'] = ['/hhb_small.png'];
  46. $info['imgList'] = [imgUtil::groupImg('')];
  47. $info['smallImgList'] = [imgUtil::groupImg('')];
  48. return $info;
  49. }
  50. $imgList = [];
  51. $smallImgList = [];
  52. foreach ($arr as $imgUrl) {
  53. $small = imgUtil::groupImg($imgUrl) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  54. $normal = imgUtil::groupImg($imgUrl) . "?x-oss-process=image/resize,m_fill,h_900,w_900";
  55. $imgList[] = $normal;
  56. $smallImgList[] = $small;
  57. }
  58. $info['shortImgList'] = $arr;
  59. $info['imgList'] = $imgList;
  60. $info['smallImgList'] = $smallImgList;
  61. return $info;
  62. }
  63. //对商品款式的转化 ssh 2019.12.4
  64. public static function formatGoodsStyleImg($info)
  65. {
  66. $imgUrl = isset($info['picture']) ? $info['picture'] : '';
  67. if (empty($imgUrl)) {
  68. $info['img'] = '';
  69. }
  70. $prefixImgUrl = imgUtil::getPrefix();
  71. $smallImg = self::getSmallImg($imgUrl);
  72. $small = $prefixImgUrl . $smallImg;
  73. //没有小图的生成小图(兼容旧系统) ssh 2020.6.4
  74. $imageRoot = dirUtil::getImgUploadDir();
  75. $preImg = $imageRoot . str_replace('/uploads', '', $imgUrl);
  76. if (file_exists($preImg)) {
  77. //没有小图则生成小图,兼容旧系统
  78. if (file_exists($imageRoot . $smallImg) == false) {
  79. $imgInfo = getimagesize($preImg);
  80. $width = isset($imgInfo[0]) ? $imgInfo[0] : 0;
  81. $height = isset($imgInfo[1]) ? $imgInfo[1] : 0;
  82. if ($width > 0) {
  83. //生成小图 150px
  84. $smallWidth = 150;
  85. $smallHeight = ceil((150 * $height) / $width);
  86. $smallFullPathFile = $imageRoot . str_replace('/uploads', '', $smallImg);
  87. Image::thumbnail($preImg, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  88. }
  89. }
  90. }
  91. $info['img'] = $small;
  92. return $info;
  93. }
  94. //图库图片的转化
  95. public static function formatPic($info)
  96. {
  97. $imgUrl = isset($info['img']) ? $info['img'] : '';
  98. if (empty($imgUrl)) {
  99. $info['imgUrl'] = '';
  100. }
  101. $prefixImgUrl = imgUtil::getPrefix();
  102. $smallImg = self::getSmallImg($imgUrl);
  103. $small = $prefixImgUrl . $smallImg;
  104. $info['imgUrl'] = $small;
  105. return $info;
  106. }
  107. //分类图的处理
  108. public static function formatCategoryImg($info)
  109. {
  110. $imgUrl = isset($info['img']) ? $info['img'] : '';
  111. if (empty($imgUrl)) {
  112. $info['imgUrl'] = '';
  113. }
  114. $prefixImgUrl = imgUtil::getPrefix();
  115. $smallImg = self::getSmallImg($imgUrl);
  116. $small = $prefixImgUrl . $smallImg;
  117. $info['imgUrl'] = $small;
  118. return $info;
  119. }
  120. //客户头像处理 ssh 2019.12.20
  121. public static function formatUserAvatar($info)
  122. {
  123. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  124. $prefixImgUrl = imgUtil::getPrefix();
  125. $dir = dirUtil::getImgDir();
  126. if (empty($imgUrl) || file_exists($dir . $imgUrl) == false) {
  127. $defaultImg = $prefixImgUrl . '/retail/default-img.png';
  128. $info['avatarUrl'] = $defaultImg;
  129. $info['smallAvatarUrl'] = $defaultImg;
  130. return $info;
  131. }
  132. $smallImg = self::getSmallImg($imgUrl);
  133. $small = $prefixImgUrl . $smallImg;
  134. $info['smallAvatarUrl'] = $small;
  135. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  136. $info['avatar'] = $prefixImgUrl . $imgUrl;
  137. return $info;
  138. }
  139. public static function formatOrderGoodsCover($info)
  140. {
  141. $imgUrl = isset($info['cover']) ? $info['cover'] : '';
  142. if (empty($imgUrl)) {
  143. $info['coverUrl'] = '';
  144. $info['smallCoverUrl'] = '';
  145. return $info;
  146. }
  147. $prefixImgUrl = imgUtil::getPrefix();
  148. $smallImg = self::getSmallImg($imgUrl);
  149. $small = $prefixImgUrl . $smallImg;
  150. $info['smallCoverUrl'] = $small;
  151. $info['coverUrl'] = $prefixImgUrl . $imgUrl;
  152. return $info;
  153. }
  154. //商家公众号logo处理 ssh 2019.12.20
  155. public static function formatMerchantLogo($info)
  156. {
  157. $imgUrl = isset($info['logo']) ? $info['logo'] : '';
  158. $prefixImgUrl = imgUtil::getPrefix();
  159. if (empty($imgUrl)) {
  160. $info['logoUrl'] = $prefixImgUrl . 'retail/default-img.png';
  161. $info['shortLogoUrl'] = 'retail/default-img.png';
  162. $info['smallLogoUrl'] = $prefixImgUrl . 'retail/default-img.png';
  163. $info['shortSmallLogoUrl'] = 'retail/default-img.png';
  164. $info['smallWxQrCodeUrl'] = $prefixImgUrl . 'retail/default-img.png';
  165. $info['wxQrCodeUrl'] = $prefixImgUrl . 'retail/default-img.png';
  166. return $info;
  167. }
  168. //logo
  169. $smallImg = self::getSmallImg($imgUrl);
  170. $small = $prefixImgUrl . $smallImg;
  171. $info['smallLogoUrl'] = $small;
  172. $info['shortSmallLogoUrl'] = $smallImg;
  173. $info['logoUrl'] = $prefixImgUrl . $imgUrl;
  174. $info['shortLogoUrl'] = $imgUrl;
  175. //二维码
  176. $smallImg = self::getSmallImg($info['wxQrCodeUrl']);
  177. $small = $prefixImgUrl . $smallImg;
  178. $info['smallWxQrCodeUrl'] = $small;
  179. $info['wxQrCodeUrl'] = $prefixImgUrl . $imgUrl;
  180. return $info;
  181. }
  182. //上传的图片进行转换 ssh 2019.12.28
  183. public static function formatUploadImg($imgUrl)
  184. {
  185. $prefix = imgUtil::getPrefix();
  186. $small = self::getSmallImg($imgUrl);
  187. $url = $prefix . $imgUrl;
  188. $smallUrl = $prefix . $small . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  189. $bigUrl = $prefix . $small . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  190. return ['url' => $url, 'smallUrl' => $smallUrl, 'bigUrl' => $bigUrl];
  191. }
  192. //取小图,尺寸 200px ssh 2019.12.4
  193. public static function getSmallImg($imgUrl)
  194. {
  195. //2021.3.18 去除尺寸,之后可以用oss裁剪
  196. return $imgUrl;
  197. //如果没有小图需要生成小图
  198. $pos = strrpos($imgUrl, '.');
  199. $pre = substr($imgUrl, 0, $pos);
  200. $ext = substr($imgUrl, ($pos + 1));
  201. $smallImg = $pre . '_small.' . $ext;
  202. return $smallImg;
  203. }
  204. //平台员工头像处理 ssh 2020.1.5
  205. public static function formatStaffAvatar($info)
  206. {
  207. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  208. if (empty($imgUrl)) {
  209. $info['avatarUrl'] = '';
  210. $info['smallAvatarUrl'] = '';
  211. return $info;
  212. }
  213. $prefixImgUrl = imgUtil::getPrefix();
  214. $smallImg = self::getSmallImg($imgUrl);
  215. $small = $prefixImgUrl . $smallImg;
  216. $info['smallAvatarUrl'] = $small;
  217. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  218. return $info;
  219. }
  220. }