business.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 = isset($info['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) == false) {
  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. $prefixImgUrl = imgUtil::getPrefix();
  53. foreach ($arr as $imgUrl) {
  54. $smallImg = self::getSmallImg($imgUrl);
  55. $small = $prefixImgUrl . $smallImg;
  56. $normal = $prefixImgUrl . $imgUrl;
  57. $imgList[] = $normal;
  58. $smallImgList[] = $small;
  59. }
  60. $info['shortImgList'] = $arr;
  61. $info['imgList'] = $imgList;
  62. $info['smallImgList'] = $smallImgList;
  63. return $info;
  64. }
  65. //对商品款式的转化 ssh 2019.12.4
  66. public static function formatGoodsStyleImg($info)
  67. {
  68. $imgUrl = isset($info['picture']) ? $info['picture'] : '';
  69. if (empty($imgUrl)) {
  70. $info['img'] = '';
  71. }
  72. $prefixImgUrl = imgUtil::getPrefix();
  73. $smallImg = self::getSmallImg($imgUrl);
  74. $small = $prefixImgUrl . $smallImg;
  75. //没有小图的生成小图(兼容旧系统) ssh 2020.6.4
  76. $imageRoot = dirUtil::getImgUploadDir();
  77. $preImg = $imageRoot . str_replace('/uploads', '', $imgUrl);
  78. if (file_exists($preImg)) {
  79. //没有小图则生成小图,兼容旧系统
  80. if (file_exists($imageRoot . $smallImg) == false) {
  81. $imgInfo = getimagesize($preImg);
  82. $width = isset($imgInfo[0]) ? $imgInfo[0] : 0;
  83. $height = isset($imgInfo[1]) ? $imgInfo[1] : 0;
  84. if ($width > 0) {
  85. //生成小图 150px
  86. $smallWidth = 150;
  87. $smallHeight = ceil((150 * $height) / $width);
  88. $smallFullPathFile = $imageRoot . str_replace('/uploads', '', $smallImg);
  89. Image::thumbnail($preImg, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  90. }
  91. }
  92. }
  93. $info['img'] = $small;
  94. return $info;
  95. }
  96. //图库图片的转化
  97. public static function formatPic($info)
  98. {
  99. $imgUrl = isset($info['img']) ? $info['img'] : '';
  100. if (empty($imgUrl)) {
  101. $info['imgUrl'] = '';
  102. }
  103. $prefixImgUrl = imgUtil::getPrefix();
  104. $smallImg = self::getSmallImg($imgUrl);
  105. $small = $prefixImgUrl . $smallImg;
  106. $info['imgUrl'] = $small;
  107. return $info;
  108. }
  109. //分类图的处理
  110. public static function formatCategoryImg($info)
  111. {
  112. $imgUrl = isset($info['img']) ? $info['img'] : '';
  113. if (empty($imgUrl)) {
  114. $info['imgUrl'] = '';
  115. }
  116. $prefixImgUrl = imgUtil::getPrefix();
  117. $smallImg = self::getSmallImg($imgUrl);
  118. $small = $prefixImgUrl . $smallImg;
  119. $info['imgUrl'] = $small;
  120. return $info;
  121. }
  122. //客户头像处理 ssh 2019.12.20
  123. public static function formatUserAvatar($info)
  124. {
  125. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  126. $prefixImgUrl = imgUtil::getPrefix();
  127. $dir = dirUtil::getImgDir();
  128. if (empty($imgUrl) || file_exists($dir . $imgUrl) == false) {
  129. $defaultImg = $prefixImgUrl . '/retail/default-img.png';
  130. $info['avatarUrl'] = $defaultImg;
  131. $info['smallAvatarUrl'] = $defaultImg;
  132. return $info;
  133. }
  134. $smallImg = self::getSmallImg($imgUrl);
  135. $small = $prefixImgUrl . $smallImg;
  136. $info['smallAvatarUrl'] = $small;
  137. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  138. $info['avatar'] = $prefixImgUrl . $imgUrl;
  139. return $info;
  140. }
  141. public static function formatOrderGoodsCover($info)
  142. {
  143. $imgUrl = isset($info['cover']) ? $info['cover'] : '';
  144. if (empty($imgUrl)) {
  145. $info['coverUrl'] = '';
  146. $info['smallCoverUrl'] = '';
  147. return $info;
  148. }
  149. $prefixImgUrl = imgUtil::getPrefix();
  150. $smallImg = self::getSmallImg($imgUrl);
  151. $small = $prefixImgUrl . $smallImg;
  152. $info['smallCoverUrl'] = $small;
  153. $info['coverUrl'] = $prefixImgUrl . $imgUrl;
  154. return $info;
  155. }
  156. //商家公众号logo处理 ssh 2019.12.20
  157. public static function formatMerchantLogo($info)
  158. {
  159. $imgUrl = isset($info['logo']) ? $info['logo'] : '';
  160. $prefixImgUrl = imgUtil::getPrefix();
  161. if (empty($imgUrl)) {
  162. $info['logoUrl'] = $prefixImgUrl . 'retail/default-img.png';
  163. $info['shortLogoUrl'] = 'retail/default-img.png';
  164. $info['smallLogoUrl'] = $prefixImgUrl . 'retail/default-img.png';
  165. $info['shortSmallLogoUrl'] = 'retail/default-img.png';
  166. $info['smallWxQrCodeUrl'] = $prefixImgUrl . 'retail/default-img.png';
  167. $info['wxQrCodeUrl'] = $prefixImgUrl . 'retail/default-img.png';
  168. return $info;
  169. }
  170. //logo
  171. $smallImg = self::getSmallImg($imgUrl);
  172. $small = $prefixImgUrl . $smallImg;
  173. $info['smallLogoUrl'] = $small;
  174. $info['shortSmallLogoUrl'] = $smallImg;
  175. $info['logoUrl'] = $prefixImgUrl . $imgUrl;
  176. $info['shortLogoUrl'] = $imgUrl;
  177. //二维码
  178. $smallImg = self::getSmallImg($info['wxQrCodeUrl']);
  179. $small = $prefixImgUrl . $smallImg;
  180. $info['smallWxQrCodeUrl'] = $small;
  181. $info['wxQrCodeUrl'] = $prefixImgUrl . $imgUrl;
  182. return $info;
  183. }
  184. //上传的图片进行转换 ssh 2019.12.28
  185. public static function formatUploadImg($imgUrl)
  186. {
  187. $prefix = imgUtil::getPrefix();
  188. $small = self::getSmallImg($imgUrl);
  189. $url = $prefix . $imgUrl;
  190. $smallUrl = $prefix . $small . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  191. return ['url' => $url, 'smallUrl' => $smallUrl];
  192. }
  193. //取小图,尺寸 200px ssh 2019.12.4
  194. public static function getSmallImg($imgUrl)
  195. {
  196. //2021.3.18 去除尺寸,之后可以用oss裁剪
  197. return $imgUrl;
  198. //如果没有小图需要生成小图
  199. $pos = strrpos($imgUrl, '.');
  200. $pre = substr($imgUrl, 0, $pos);
  201. $ext = substr($imgUrl, ($pos + 1));
  202. $smallImg = $pre . '_small.' . $ext;
  203. return $smallImg;
  204. }
  205. //平台员工头像处理 ssh 2020.1.5
  206. public static function formatStaffAvatar($info)
  207. {
  208. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  209. if (empty($imgUrl)) {
  210. $info['avatarUrl'] = '';
  211. $info['smallAvatarUrl'] = '';
  212. return $info;
  213. }
  214. $prefixImgUrl = imgUtil::getPrefix();
  215. $smallImg = self::getSmallImg($imgUrl);
  216. $small = $prefixImgUrl . $smallImg;
  217. $info['smallAvatarUrl'] = $small;
  218. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  219. return $info;
  220. }
  221. }