business.php 5.7 KB

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