MerchantService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace bizMall\merchant\services;
  3. use bizMall\admin\classes\AdminRoleClass;
  4. use bizMall\agent\classes\AgentAssetClass;
  5. use bizMall\auth\services\AuthService;
  6. use bizMall\base\services\BaseService;
  7. use bizMall\merchant\classes\AdminClass;
  8. use bizMall\merchant\classes\MerchantAssetClass;
  9. use bizMall\merchant\classes\MerchantClass;
  10. use bizMall\merchant\classes\MerchantExtendClass;
  11. use bizMall\merchant\classes\ShopClass;
  12. use bizMall\message\services\TMessageService;
  13. use bizMall\saas\services\SetAuthService;
  14. use bizMall\saas\services\SetMealService;
  15. use bizMall\user\services\UserService;
  16. use bizMall\wx\classes\WxOpenClass;
  17. use bizMall\wx\services\WxMenuService;
  18. use common\components\business;
  19. use common\components\util;
  20. use common\services\xhTMessageService;
  21. use common\services\xhWxMenuService;
  22. use Yii;
  23. class MerchantService extends BaseService
  24. {
  25. public static $baseFile = '\bizMall\merchant\classes\MerchantClass';
  26. //商家在业务端的权限 ssh 2019.11.25
  27. public static function getAuthIdList($sjId, $endId)
  28. {
  29. $merchant = self::getById($sjId);
  30. $setId = $merchant['setId'];
  31. if (empty($setId)) {
  32. util::fail('没有找到商家的套餐');
  33. }
  34. //业务端总共有哪些权限
  35. $idList = AuthService::getListId($endId);
  36. //套餐在业务端有哪些权限
  37. $setIdList = SetAuthService::getAuthIdList($setId, $endId);
  38. //有*号表示有所有的权限
  39. $commonIdList = in_array('*', $setIdList) ? $idList : array_intersect($idList, $setIdList);
  40. return $commonIdList;
  41. }
  42. //获取默认门店
  43. public static function getDefaultShopId($merchant)
  44. {
  45. return MerchantClass::getDefaultShopId($merchant);
  46. }
  47. //商家基本信息 ssh 2019.12.8
  48. public static function getInfo($merchant, $extend)
  49. {
  50. $merchant['extend'] = $extend;
  51. return $merchant;
  52. }
  53. //常用链接 ssh 2019.12.19
  54. public static function getCommonUrlList()
  55. {
  56. $list = MerchantClass::$commonUrl;
  57. return array_values($list);
  58. }
  59. //取指定常用链接 ssh 2019.12.28
  60. public static function getCommonUrl($id)
  61. {
  62. $list = MerchantClass::$commonUrl;
  63. return isset($list[$id]) ? $list[$id] : [];
  64. }
  65. //取平台下的商家 ssh 2019.12.20
  66. public static function getSaasMerchant($where)
  67. {
  68. $data = MerchantClass::getList('*', $where, 'addTime DESC');
  69. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  70. if (empty($list)) {
  71. return $data;
  72. }
  73. //管理员
  74. $adminIdList = array_column($list, 'adminId');
  75. $admin = AdminService::getAdminByIds($adminIdList, null, 'id');
  76. //商家资产
  77. $sjIdList = array_column($list, 'id');
  78. $asset = MerchantAssetService::getAssetBySjIds($sjIdList);
  79. //套餐
  80. $setIdList = array_column($list, 'setId');
  81. $set = SetMealService::getSetByIds($setIdList);
  82. //拓展
  83. $ext = MerchantExtendService::getExtendByMerchantIds($sjIdList);
  84. foreach ($list as $key => $val) {
  85. $adminId = $val['adminId'];
  86. $sjId = $val['id'];
  87. $setId = $val['setId'];
  88. $val['adminName'] = isset($admin[$adminId]['name']) ? $admin[$adminId]['name'] : '';
  89. $val['merchantAsset'] = isset($asset[$sjId]) ? $asset[$sjId] : [];
  90. $val['setMealName'] = isset($set[$setId]) ? $set[$setId]['name'] : '';
  91. $val['extend'] = isset($ext[$sjId]) ? $ext[$sjId] : [];
  92. $list[$key] = business::formatMerchantLogo($val);
  93. }
  94. $data['list'] = $list;
  95. return $data;
  96. }
  97. //根据id批量取商家 ssh 2019.12.20
  98. public static function getMerchantByIds($ids)
  99. {
  100. return MerchantClass::getMerchantByIds($ids);
  101. }
  102. //获取商家 ssh 2019.12.30
  103. public static function getMerchantById($id)
  104. {
  105. return MerchantClass::getMerchantById($id);
  106. }
  107. public static function getCurrentSetId($id)
  108. {
  109. return MerchantClass::getCurrentSetId($id);
  110. }
  111. //开店公众号授权之后初始化 ssh 2020.2.7
  112. public static function openShopInit($data)
  113. {
  114. $sjId = $data['sjId'];
  115. $merchant = MerchantClass::getMerchantById($sjId);
  116. if (empty($merchant)) {
  117. return false;
  118. }
  119. $extend = MerchantExtendClass::getBySjId($sjId);
  120. if (isset($extend['init']) && $extend['init'] == 1) {
  121. return true;
  122. }
  123. //事务处理
  124. $connection = Yii::$app->db;
  125. $transaction = $connection->beginTransaction();
  126. try {
  127. //初始化微信菜单
  128. $hasMenu = WxMenuService::getByCondition(['sjId' => $sjId]);
  129. if (empty($hasMenu)) {
  130. xhWxMenuService::applyInit($merchant);
  131. }
  132. //微信客户初始化
  133. $hasUser = UserService::getByCondition(['sjId' => $sjId]);
  134. if (empty($hasUser)) {
  135. $return = UserService::syncWxAllUser($sjId);
  136. $totalNum = isset($return['total']) ? $return['total'] : 0;
  137. MerchantAssetClass::updateByCondition(['sjId' => $sjId], ['totalFans' => $totalNum, 'totalUser' => $totalNum]);
  138. }
  139. //模板消息
  140. $hasMessage = TMessageService::getByCondition(['sjId' => $sjId]);
  141. if (empty($hasMessage)) {
  142. xhTMessageService::init($merchant);
  143. }
  144. //完成初始化标识
  145. MerchantExtendClass::updateByCondition(['sjId' => $sjId], ['init' => 1]);
  146. //代理资产
  147. AgentAssetClass::add(['sjId' => $sjId]);
  148. $transaction->commit();
  149. return true;
  150. } catch (Exception $e) {
  151. $transaction->rollBack();
  152. return false;
  153. }
  154. }
  155. //初始化平台的商家 ssh 2020.2.11
  156. public static function initOpenMerchant()
  157. {
  158. $open = WxOpenClass::getOpen();
  159. if (isset($open['wx_base_id']) && !empty($open['wx_base_id']) && isset($open['wx_mini_base_id']) && !empty($open['wx_mini_base_id'])) {
  160. util::stop('已初始化,无需重复操作');
  161. }
  162. $env = getenv('YII_ENV');
  163. switch ($env) {
  164. case 'local':
  165. $name = '花美灵';
  166. break;
  167. case 'dev':
  168. $name = '花美灵';
  169. break;
  170. case 'test':
  171. $name = '花掌柜';
  172. break;
  173. case 'production':
  174. $name = '花卉宝';
  175. break;
  176. default:
  177. $name = '花卉宝';
  178. }
  179. echo $name;
  180. }
  181. //获取商家的h5商城二维码 ssh 2020.4.30
  182. public static function getH5MallQrCode($id)
  183. {
  184. return MerchantClass::getH5MallQrCode($id);
  185. }
  186. }