business.php 5.5 KB

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