business.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. $prefixImgUrl = Yii::$app->params['imgHost'];
  128. if (empty($imgUrl)) {
  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. return $info;
  139. }
  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 = Yii::$app->params['imgHost'];
  150. $smallImg = self::getSmallImg($imgUrl);
  151. $small = $prefixImgUrl . $smallImg;
  152. $info['smallCoverUrl'] = $small;
  153. $info['coverUrl'] = $prefixImgUrl . $imgUrl;
  154. return $info;
  155. }
  156. //商家公众号logo处理 shish 2019.12.20
  157. public static function formatMerchantLogo($info)
  158. {
  159. $imgUrl = isset($info['logo']) ? $info['logo'] : '';
  160. if (empty($imgUrl)) {
  161. $info['logoUrl'] = '';
  162. $info['smallLogoUrl'] = '';
  163. $info['smallWxQrCodeUrl'] = '';
  164. $info['wxQrCodeUrl'] = '';
  165. return $info;
  166. }
  167. $prefixImgUrl = Yii::$app->params['imgHost'];
  168. //logo
  169. $smallImg = self::getSmallImg($imgUrl);
  170. $small = $prefixImgUrl . $smallImg;
  171. $info['smallLogoUrl'] = $small;
  172. $info['logoUrl'] = $prefixImgUrl . $imgUrl;
  173. //二维码
  174. $smallImg = self::getSmallImg($info['wxQrCodeUrl']);
  175. $small = $prefixImgUrl . $smallImg;
  176. $info['smallWxQrCodeUrl'] = $small;
  177. $info['wxQrCodeUrl'] = $prefixImgUrl . $imgUrl;
  178. return $info;
  179. }
  180. //上传的图片进行转换 shish 2019.12.28
  181. public static function formatUploadImg($imgUrl)
  182. {
  183. $prefix = Yii::$app->params['imgHost'];
  184. $small = self::getSmallImg($imgUrl);
  185. $url = $prefix . $imgUrl;
  186. $smallUrl = $prefix . $small;
  187. return ['url' => $url, 'smallUrl' => $smallUrl];
  188. }
  189. //取小图,尺寸 200px shish 2019.12.4
  190. public static function getSmallImg($imgUrl)
  191. {
  192. //如果没有小图需要生成小图
  193. $pos = strrpos($imgUrl, '.');
  194. $pre = substr($imgUrl, 0, $pos);
  195. $ext = substr($imgUrl, ($pos + 1));
  196. $smallImg = $pre . '_small.' . $ext;
  197. return $smallImg;
  198. }
  199. //平台员工头像处理 shish 2020.1.5
  200. public static function formatStaffAvatar($info)
  201. {
  202. $imgUrl = isset($info['avatar']) ? $info['avatar'] : '';
  203. if (empty($imgUrl)) {
  204. $info['avatarUrl'] = '';
  205. $info['smallAvatarUrl'] = '';
  206. return $info;
  207. }
  208. $prefixImgUrl = Yii::$app->params['imgHost'];
  209. $smallImg = self::getSmallImg($imgUrl);
  210. $small = $prefixImgUrl . $smallImg;
  211. $info['smallAvatarUrl'] = $small;
  212. $info['avatarUrl'] = $prefixImgUrl . $imgUrl;
  213. return $info;
  214. }
  215. }