MerchantService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace biz\merchant\services;
  3. use biz\admin\classes\AdminRoleClass;
  4. use biz\agent\classes\AgentAssetClass;
  5. use biz\auth\services\AuthService;
  6. use biz\base\services\BaseService;
  7. use biz\merchant\classes\AdminClass;
  8. use biz\merchant\classes\MerchantAssetClass;
  9. use biz\merchant\classes\MerchantClass;
  10. use biz\merchant\classes\MerchantExtendClass;
  11. use biz\merchant\classes\ShopClass;
  12. use biz\message\services\TMessageService;
  13. use biz\saas\services\SetAuthService;
  14. use biz\saas\services\SetMealService;
  15. use biz\user\services\UserService;
  16. use biz\wx\classes\WxOpenClass;
  17. use biz\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 = '\biz\merchant\classes\MerchantClass';
  26. //商家在业务端的权限 shish 2019.11.25
  27. public static function getAuthIdList($merchantId, $endId)
  28. {
  29. $merchant = self::getById($merchantId);
  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. $defaultShopId = isset($merchant['defaultShopId']) ? $merchant['defaultShopId'] : 0;
  46. return $defaultShopId;
  47. }
  48. //商家基本信息,$sensitive true 除敏感信息 shish 2019.12.8
  49. public static function getInfo($merchant, $extend, $sensitive = true)
  50. {
  51. if ($sensitive) {
  52. $delete = ['miniAppId', 'miniAppSecret', 'wxAppId', 'wxAppSecret', 'openAppId', 'wxToken', 'wxJsApiTicket', 'wxAccessToken', 'wxRefreshToken'];
  53. foreach ($delete as $del) {
  54. unset($merchant[$del]);
  55. }
  56. $deleteExtend = ['miniAppId', 'miniAccessToken', 'miniRefreshToken', 'wxPayKey', 'wxPayMerchantId', 'wxAppId'];
  57. foreach ($deleteExtend as $delExtend) {
  58. unset($extend[$delExtend]);
  59. }
  60. }
  61. $merchant['extend'] = $extend;
  62. return $merchant;
  63. }
  64. //常用链接 shish 2019.12.19
  65. public static function getCommonUrlList()
  66. {
  67. $list = MerchantClass::$commonUrl;
  68. return array_values($list);
  69. }
  70. //取指定常用链接 shish 2019.12.28
  71. public static function getCommonUrl($id)
  72. {
  73. $list = MerchantClass::$commonUrl;
  74. return isset($list[$id]) ? $list[$id] : [];
  75. }
  76. //取平台下的商家 shish 2019.12.20
  77. public static function getSaasMerchant($where)
  78. {
  79. $data = MerchantClass::getList('*', $where, 'addTime DESC');
  80. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  81. if (empty($list)) {
  82. return $data;
  83. }
  84. //管理员
  85. $adminIdList = array_column($list, 'adminId');
  86. $admin = AdminService::getAdminByIds($adminIdList, null, 'id');
  87. //商家资产
  88. $merchantIdList = array_column($list, 'id');
  89. $asset = MerchantAssetService::getAssetByMerchantIds($merchantIdList);
  90. //套餐
  91. $setIdList = array_column($list, 'setId');
  92. $set = SetMealService::getSetByIds($setIdList);
  93. //拓展
  94. $ext = MerchantExtendService::getExtendByMerchantIds($merchantIdList);
  95. foreach ($list as $key => $val) {
  96. $adminId = $val['adminId'];
  97. $merchantId = $val['id'];
  98. $setId = $val['setId'];
  99. $val['adminName'] = isset($admin[$adminId]['adminName']) ? $admin[$adminId]['adminName'] : '';
  100. $val['merchantAsset'] = isset($asset[$merchantId]) ? $asset[$merchantId] : [];
  101. $val['setMealName'] = isset($set[$setId]) ? $set[$setId]['name'] : '';
  102. $val['extend'] = isset($ext[$merchantId]) ? $ext[$merchantId] : [];
  103. $list[$key] = business::formatMerchantLogo($val);
  104. }
  105. $data['list'] = $list;
  106. return $data;
  107. }
  108. //根据id批量取商家 shish 2019.12.20
  109. public static function getMerchantByIds($ids)
  110. {
  111. return MerchantClass::getMerchantByIds($ids);
  112. }
  113. //获取商家 shish 2019.12.30
  114. public static function getMerchantById($id)
  115. {
  116. return MerchantClass::getMerchantById($id);
  117. }
  118. public static function getCurrentSetId($id)
  119. {
  120. return MerchantClass::getCurrentSetId($id);
  121. }
  122. //开店公众号授权之后初始化 shish 2020.2.7
  123. public static function openShopInit($data)
  124. {
  125. $merchantId = $data['merchantId'];
  126. $merchant = MerchantClass::getMerchantById($merchantId);
  127. if (empty($merchant)) {
  128. return false;
  129. }
  130. $extend = MerchantExtendClass::getByMerchantId($merchantId);
  131. if (isset($extend['init']) && $extend['init'] == 1) {
  132. return true;
  133. }
  134. //事务处理
  135. $connection = Yii::$app->db;
  136. $transaction = $connection->beginTransaction();
  137. try {
  138. //初始化微信菜单
  139. $hasMenu = WxMenuService::getByCondition(['merchantId' => $merchantId]);
  140. if (empty($hasMenu)) {
  141. xhWxMenuService::applyInit($merchant);
  142. }
  143. //微信客户初始化
  144. $hasUser = UserService::getByCondition(['merchantId' => $merchantId]);
  145. if (empty($hasUser)) {
  146. $return = UserService::syncWxAllUser($merchantId);
  147. $totalNum = isset($return['total']) ? $return['total'] : 0;
  148. MerchantAssetClass::updateByCondition(['merchantId' => $merchantId], ['totalFans' => $totalNum, 'totalUser' => $totalNum]);
  149. }
  150. //模板消息
  151. $hasMessage = TMessageService::getByCondition(['merchantId' => $merchantId]);
  152. if (empty($hasMessage)) {
  153. xhTMessageService::init($merchant);
  154. }
  155. //完成初始化标识
  156. MerchantExtendClass::updateByCondition(['merchantId' => $merchantId], ['init' => 1]);
  157. //代理资产
  158. AgentAssetClass::add(['merchantId' => $merchantId]);
  159. $transaction->commit();
  160. return true;
  161. } catch (Exception $e) {
  162. $transaction->rollBack();
  163. return false;
  164. }
  165. }
  166. //初始化平台的商家 shish 2020.2.11
  167. public static function initOpenMerchant()
  168. {
  169. $open = WxOpenClass::getOpen();
  170. if (isset($open['wx_base_id']) && !empty($open['wx_base_id']) && isset($open['wx_mini_base_id']) && !empty($open['wx_mini_base_id'])) {
  171. util::stop('已初始化,无需重复操作');
  172. }
  173. $env = getenv('YII_ENV');
  174. switch ($env) {
  175. case 'local':
  176. $merchantName = '花美灵';
  177. break;
  178. case 'dev':
  179. $merchantName = '花美灵';
  180. break;
  181. case 'test':
  182. $merchantName = '花掌柜';
  183. break;
  184. case 'production':
  185. $merchantName = '花卉宝';
  186. break;
  187. default:
  188. $merchantName = '花卉宝';
  189. }
  190. echo $merchantName;
  191. }
  192. //获取商家的h5商城二维码 shish 2020.4.30
  193. public static function getH5MallQrCode($id)
  194. {
  195. return MerchantClass::getH5MallQrCode($id);
  196. }
  197. }