ShopClass.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace bizGhs\merchant\classes;
  3. use bizGhs\cp\classes\CpClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use bizGhs\shop\classes\MainClass;
  6. use bizGhs\shop\classes\ShopAdminClass;
  7. use bizHd\ad\classes\AdClass;
  8. use bizHd\goods\classes\CategoryClass;
  9. use bizHd\goods\classes\GoodsSettingClass;
  10. use bizHd\merchant\services\SjService;
  11. use bizHd\saas\classes\ApplyClass;
  12. use common\components\dict;
  13. use common\components\httpUtil;
  14. use common\components\qrCodeUtil;
  15. use common\components\stringUtil;
  16. use common\components\util;
  17. use Yii;
  18. use bizHd\base\classes\BaseClass;
  19. use common\components\imgUtil;
  20. class ShopClass extends BaseClass
  21. {
  22. public static $baseFile = '\bizGhs\merchant\models\Shop';
  23. public static function addMainShop($data, $oldGhsShop, $sj)
  24. {
  25. if (isset($oldGhsShop->lsShopId) == false || empty($oldGhsShop->lsShopId)) {
  26. util::fail('本店请先绑定零售店');
  27. }
  28. $mobile = $data['mobile'] ?? 0;
  29. if (empty($mobile) || stringUtil::isMobile($mobile) == false) {
  30. util::fail('请输入正确手机号');
  31. }
  32. $hasShop = ShopClass::getByCondition(['mobile' => $mobile], true);
  33. if (!empty($hasShop)) {
  34. util::fail('手机号已经使用');
  35. }
  36. $hasApply = ApplyClass::getByCondition(['mobile' => $mobile], true);
  37. if (!empty($hasApply)) {
  38. util::fail('手机号已注册过');
  39. }
  40. $defaultShopId = $sj['defaultShopId'] ?? 0;
  41. $defaultShop = ShopClass::getById($defaultShopId, true);
  42. if (empty($defaultShop)) {
  43. util::fail('没有找到门店76');
  44. }
  45. $oldMainId = $defaultShop->mainId ?? 0;
  46. if (empty($oldMainId)) {
  47. util::fail('没有找到main信息');
  48. }
  49. //创建main和复制花材
  50. $main = MainClass::addMainCopyItem($oldMainId);
  51. $mainId = $main->id ?? 0;
  52. //复制产品
  53. // $copyKey = 'copy_old_cp_to_new_cp';
  54. // $copyValue = json_encode(['oldMainId' => $oldMainId, 'newMainId' => $mainId]);
  55. // Yii::$app->redis->executeCommand('LPUSH', [$copyKey, $copyValue]);
  56. $data['mainId'] = $mainId;
  57. $newGhsShop = \bizGhs\shop\classes\ShopClass::addShop($data);
  58. $lsOldShop = ShopClass::getById($oldGhsShop->lsShopId, true);
  59. $data['sjId'] = $lsOldShop->sjId ?? 0;
  60. $data['ptStyle'] = dict::getDict('ptStyle', 'hd');
  61. $newLsShop = \bizHd\shop\classes\ShopClass::addShop($data);
  62. $newLsShop->pfShopId = $newGhsShop->id ?? 0;
  63. $newLsShop->save();
  64. $newGhsShop->lsShopId = $newLsShop->id ?? 0;
  65. $newGhsShop->save();
  66. return $newGhsShop;
  67. }
  68. //组装门店基本信息 ssh 2019.12.2
  69. public static function groupBaseInfo($list)
  70. {
  71. foreach ($list as $key => $val) {
  72. $shortAvatar = $val['avatar'] ?? '';
  73. $list[$key]['avatar'] = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  74. if (empty($val['img'])) {
  75. $list[$key]['img'] = [];
  76. $list[$key]['imgUrl'] = [];
  77. continue;
  78. }
  79. $list[$key]['img'] = json_decode($val['img'], true);
  80. if (isset($list[$key]['img']) && is_array($list[$key]['img'])) {
  81. foreach ($list[$key]['img'] as $img) {
  82. $list[$key]['imgUrl'][] = imgUtil::groupImg($img);
  83. }
  84. }
  85. }
  86. return $list;
  87. }
  88. public static function getShopList($where)
  89. {
  90. $list = self::getList('*', $where, 'addTime DESC');
  91. $list['list'] = self::groupBaseInfo($list['list']);
  92. return $list;
  93. }
  94. //获取商家默认的门店ID ssh 2020.1.19
  95. public static function getDefaultShopId($merchant)
  96. {
  97. return isset($merchant['defaultShopId']) ? $merchant['defaultShopId'] : 0;
  98. }
  99. //取商家所有门店 ssh 2020.2.29
  100. public static function getAllShop($sjId)
  101. {
  102. return self::getAllByCondition(['sjId' => $sjId], null, 'id,shopName,avatar');
  103. }
  104. //验证权限 ssh 2021.1.14
  105. public static function valid($shop, $sjId)
  106. {
  107. if (isset($shop['sjId']) && $shop['sjId'] == $sjId) {
  108. return true;
  109. }
  110. util::fail('您无法访问此店铺');
  111. }
  112. //删除门店 ssh 2020.3.1
  113. public static function deleteShop($shop)
  114. {
  115. if (isset($shop['default']) && $shop['default'] == 1) {
  116. util::fail('默认门店不允许删除');
  117. }
  118. $id = $shop['id'];
  119. self::updateById($id, ['delStatus' => 1]);
  120. }
  121. //生成收款码 ssh 2021.1.14
  122. public static function generateGatheringCode($sjId, $shopId)
  123. {
  124. $url = Yii::$app->params['ghsDomain'] . "/#/pages/pay/index?account={$sjId}&shopId=" . $shopId;
  125. //强制转成https
  126. $url = httpUtil::becomeHttps($url);
  127. $gatheringCode = qrCodeUtil::generateGatheringQrCode($url, $sjId, $shopId, '', 10);
  128. return $gatheringCode;
  129. }
  130. //绑定零售店 ssh 20220430
  131. public static function bindLsShopByCustomId($ghsShop, $id, $adminId)
  132. {
  133. $ghsShopId = $ghsShop->id ?? 0;
  134. $ghsMainId = $ghsShop->mainId ?? 0;
  135. if (empty($ghsMainId)) {
  136. util::fail('没有找到你的main信息');
  137. }
  138. $ghsMain = MainClass::getById($ghsMainId, true);
  139. if (empty($ghsMain)) {
  140. util::fail('没有找到main信息');
  141. }
  142. $ghsMobile = $ghsShop->mobile ?? 0;
  143. if (empty($ghsMobile)) {
  144. util::fail('手机号有问题?');
  145. }
  146. $customMobile = 0;
  147. $customShop = null;
  148. $customShopId = 0;
  149. if (!empty($id)) {
  150. $custom = CustomClass::getById($id, true);
  151. if (empty($custom)) {
  152. util::fail('门店信息没有找到');
  153. }
  154. $customShopId = $custom->shopId ?? 0;
  155. $customShop = self::getById($customShopId, true);
  156. if (isset($customShop->pfShopId) && !empty($customShop->pfShopId)) {
  157. util::fail('这个零售店已经绑定了批发店');
  158. }
  159. $customMobile = $customShop->mobile ?? 0;
  160. }
  161. if ($ghsMobile != $customMobile) {
  162. //如果绑定的零售店跟批发店手机号不一样,则先查批发店这个手机号有没创建过零售店,没有则自动给创建,有则提醒选的零售店不对
  163. $has = ShopClass::getByCondition(['mobile' => $ghsMobile, 'ptStyle' => dict::getDict('ptStyle', 'hd')], true);
  164. if (!empty($has) && !empty($id)) {
  165. util::fail('请确认此零售店开通时用的手机号与本批发店一致?');
  166. } else {
  167. //创建零售店
  168. $hdInitData = $ghsShop->attributes ?? [];
  169. $hdInitData['adminId'] = $adminId;
  170. $hdInitData['name'] = $hdInitData['merchantName'] ?? '';
  171. $respond = SjService::initHdBaseInfo($hdInitData, $ghsMain, true);
  172. $customShop = $respond['shop'] ?? [];
  173. if (empty($customShop)) {
  174. util::fail('没有生成门店');
  175. }
  176. $customShopId = $customShop->id ?? 0;
  177. }
  178. } else {
  179. //商品和分类初始化
  180. CategoryClass::initCategoryGoods($ghsMainId);
  181. //商城和门店广告初始化
  182. AdClass::initAdd($ghsMainId);
  183. //商家商品配置信息
  184. $setData = ['mainId' => $ghsMainId, 'riseType' => 0, 'riseAmount' => 0];
  185. GoodsSettingClass::add($setData);
  186. }
  187. $customShop->mainId = $ghsMainId;
  188. $customShop->pfShopId = $ghsShopId;
  189. $customShop->save();
  190. $ghsShop->lsShopId = $customShopId;
  191. $ghsShop->save();
  192. //允许员工登录零售店
  193. $staffList = ShopAdminClass::getAllByCondition(['mainId' => $ghsMainId], null, '*', null, true);
  194. if (!empty($staffList)) {
  195. foreach ($staffList as $staff) {
  196. $adminId = $staff->adminId ?? 0;
  197. $admin = AdminClass::getById($adminId, true);
  198. if (!empty($admin)) {
  199. $admin->currentShopId = $customShopId;
  200. $admin->save();
  201. }
  202. }
  203. }
  204. }
  205. }