business.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * 通用业务
  4. * shish 2019.8.20
  5. */
  6. namespace common\components;
  7. use Yii;
  8. use yii\imagine\Image;
  9. class business
  10. {
  11. //支付方式对应资金要保存的字段 shish 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. //对商品图片转化 shish 2019.12.2
  33. public static function formatGoodsImg($info)
  34. {
  35. $shopImg = isset($info['shopImg']) ? $info['shopImg'] : [];
  36. unset($info['shopImg']);
  37. //没有保存图片返回空
  38. if (empty($shopImg)) {
  39. $info['shortImgList'] = [];
  40. $info['imgList'] = [];
  41. $info['smallImgList'] = [];
  42. return $info;
  43. }
  44. $arr = json_decode($shopImg, true);
  45. //图片内容转化失败返回空
  46. if (is_array($arr) == false) {
  47. $info['shortImgList'] = [];
  48. $info['imgList'] = [];
  49. $info['smallImgList'] = [];
  50. return $info;
  51. }
  52. $imgList = [];
  53. $smallImgList = [];
  54. $prefixImgUrl = Yii::$app->params['imgHost'];
  55. $imageRoot = dirUtil::getImgUploadDir();
  56. foreach ($arr as $imgUrl) {
  57. $smallImg = self::getSmallImg($imgUrl);
  58. $small = $prefixImgUrl . $smallImg;
  59. $normal = $prefixImgUrl . $imgUrl;
  60. /*
  61. $preImg = $imageRoot . str_replace('/uploads', '', $imgUrl);
  62. if (file_exists($preImg)) {
  63. //没有小图则生成小图,兼容旧系统
  64. if (file_exists($imageRoot . $smallImg) == false) {
  65. $imgInfo = getimagesize($preImg);
  66. $width = $imgInfo[0];
  67. $height = $imgInfo[1];
  68. //生成小图 200px
  69. $smallWidth = 200;
  70. $smallHeight = ceil((200 * $height) / $width);
  71. $smallFullPathFile = $imageRoot . str_replace('/uploads', '', $smallImg);
  72. Image::thumbnail($preImg, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
  73. }
  74. }
  75. */
  76. $imgList[] = $normal;
  77. $smallImgList[] = $small;
  78. }
  79. $info['shortImgList'] = $arr;
  80. $info['imgList'] = $imgList;
  81. $info['smallImgList'] = $smallImgList;
  82. return $info;
  83. }
  84. //对商品款式的转化 shish 2019.12.4
  85. public static function formatGoodsStyleImg($info)
  86. {
  87. $imgUrl = isset($info['picture']) ? $info['picture'] : '';
  88. if (empty($imgUrl)) {
  89. $info['img'] = '';
  90. }
  91. $prefixImgUrl = Yii::$app->params['imgHost'];
  92. $smallImg = self::getSmallImg($imgUrl);
  93. $small = $prefixImgUrl . $smallImg;
  94. $info['img'] = $small;
  95. return $info;
  96. }
  97. //图库图片的转化
  98. public static function formatPic($info)
  99. {
  100. $imgUrl = isset($info['img']) ? $info['img'] : '';
  101. if (empty($imgUrl)) {
  102. $info['imgUrl'] = '';
  103. }
  104. $prefixImgUrl = Yii::$app->params['imgHost'];
  105. $smallImg = self::getSmallImg($imgUrl);
  106. $small = $prefixImgUrl . $smallImg;
  107. $info['imgUrl'] = $small;
  108. return $info;
  109. }
  110. //分类图的处理
  111. public static function formatCategoryImg($info)
  112. {
  113. $imgUrl = isset($info['img']) ? $info['img'] : '';
  114. if (empty($imgUrl)) {
  115. $info['imgUrl'] = '';
  116. }
  117. $prefixImgUrl = Yii::$app->params['imgHost'];
  118. $smallImg = self::getSmallImg($imgUrl);
  119. $small = $prefixImgUrl . $smallImg;
  120. $info['imgUrl'] = $small;
  121. return $info;
  122. }
  123. //客户头像处理 shish 2019.12.20
  124. public static function formatUserAvatar($info)
  125. {
  126. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  127. if (empty($imgUrl)) {
  128. $info['avatarUrl'] = '';
  129. $info['smallAvatarUrl'] = '';
  130. return $info;
  131. }
  132. $prefixImgUrl = Yii::$app->params['imgHost'];
  133. $smallImg = self::getSmallImg($imgUrl);
  134. $small = $prefixImgUrl . $smallImg;
  135. $info['smallAvatarUrl'] = $small;
  136. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  137. return $info;
  138. }
  139. //
  140. public static function formatOrderGoodsCover($info)
  141. {
  142. $imgUrl = isset($info['cover']) ? $info['cover'] : '';
  143. if (empty($imgUrl)) {
  144. $info['coverUrl'] = '';
  145. $info['smallCoverUrl'] = '';
  146. return $info;
  147. }
  148. $prefixImgUrl = Yii::$app->params['imgHost'];
  149. $smallImg = self::getSmallImg($imgUrl);
  150. $small = $prefixImgUrl . $smallImg;
  151. $info['smallCoverUrl'] = $small;
  152. $info['coverUrl'] = $prefixImgUrl . $imgUrl;
  153. return $info;
  154. }
  155. //商家公众号logo处理 shish 2019.12.20
  156. public static function formatMerchantLogo($info)
  157. {
  158. $imgUrl = isset($info['logo']) ? $info['logo'] : '';
  159. if (empty($imgUrl)) {
  160. $info['logoUrl'] = '';
  161. $info['smallLogoUrl'] = '';
  162. return $info;
  163. }
  164. $prefixImgUrl = Yii::$app->params['imgHost'];
  165. $smallImg = self::getSmallImg($imgUrl);
  166. $small = $prefixImgUrl . $smallImg;
  167. $info['smallLogoUrl'] = $small;
  168. $info['logoUrl'] = $prefixImgUrl . $imgUrl;
  169. return $info;
  170. }
  171. //上传的图片进行转换 shish 2019.12.28
  172. public static function formatUploadImg($imgUrl)
  173. {
  174. $prefix = Yii::$app->params['imgHost'];
  175. $small = self::getSmallImg($imgUrl);
  176. $url = $prefix . $imgUrl;
  177. $smallUrl = $prefix . $small;
  178. return ['url' => $url, 'smallUrl' => $smallUrl];
  179. }
  180. //取小图,尺寸 200px shish 2019.12.4
  181. public static function getSmallImg($imgUrl)
  182. {
  183. //如果没有小图需要生成小图
  184. $pos = strrpos($imgUrl, '.');
  185. $pre = substr($imgUrl, 0, $pos);
  186. $ext = substr($imgUrl, ($pos + 1));
  187. $smallImg = $pre . '_small.' . $ext;
  188. return $smallImg;
  189. }
  190. //平台员工头像处理 shish 2020.1.5
  191. public static function formatStaffAvatar($info)
  192. {
  193. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  194. if (empty($imgUrl)) {
  195. $info['avatarUrl'] = '';
  196. $info['smallAvatarUrl'] = '';
  197. return $info;
  198. }
  199. $prefixImgUrl = Yii::$app->params['imgHost'];
  200. $smallImg = self::getSmallImg($imgUrl);
  201. $small = $prefixImgUrl . $smallImg;
  202. $info['smallAvatarUrl'] = $small;
  203. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  204. return $info;
  205. }
  206. }