business.php 5.6 KB

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