ShopAdminService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace bizHd\admin\services;
  3. use bizHd\admin\classes\AdminClass;
  4. use bizHd\admin\classes\AdminRoleClass;
  5. use bizHd\admin\classes\ShopAdminClass;
  6. use bizHd\auth\services\AuthService;
  7. use bizHd\base\services\BaseService;
  8. use bizHd\merchant\classes\ShopClass;
  9. use biz\sj\services\MerchantService;
  10. use bizHd\user\services\UserService;
  11. use common\components\error;
  12. use common\components\imgUtil;
  13. use common\components\miniUtil;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. use common\components\wxUtil;
  17. use common\services\xhTMessageService;
  18. use Yii;
  19. class ShopAdminService extends BaseService
  20. {
  21. public static $baseFile = '\bizHd\admin\classes\ShopAdminClass';
  22. ///创建管理员生成小程序 ssh 2020.4.17
  23. public static function prepareBindAdmin($data)
  24. {
  25. ShopAdminClass::prepareBindAdmin($data);
  26. $page = 'admin/shopAdmin/bind';
  27. $isOpen = 1;
  28. $adminId = $data['adminId'];
  29. $shopId = $data['shopId'];
  30. $scene = 'adminId=' . $adminId . '&shopId=' . $shopId;
  31. $merchant = $data['merchant'];
  32. $imgUrl = miniUtil::generateUnlimitedMiniCode($merchant, $page, $scene, $isOpen);
  33. return ['miniCode' => $imgUrl];
  34. }
  35. public static function generateAdmin($data, $admin)
  36. {
  37. $recommendAdminId = isset($data['recommendAdminId']) ? $data['recommendAdminId'] : 0;
  38. $mobile = isset($data['mobile']) ? $data['mobile'] : '';
  39. $shopId = isset($data['shopId']) ? $data['shopId'] : 0;
  40. $adminId = isset($data['adminId']) ? $data['adminId'] : 0;
  41. $name = $data['name'] ?? '未命名';
  42. $mainId = $data['mainId'] ?? 0;
  43. $bindData = ShopAdminClass::getBindAdminData($shopId, $recommendAdminId);
  44. if (empty($bindData)) {
  45. util::fail('小程序码已失效,请重新创建员工');
  46. }
  47. $avatar = $admin['avatar'] ?? '';
  48. $password = isset($bindData['password']) ? password_hash($bindData['password'], PASSWORD_BCRYPT) : '';
  49. $remark = isset($bindData['remark']) ? $bindData['remark'] : '';
  50. $remind = isset($bindData['remind']) ? $bindData['remind'] : 0;
  51. $status = 1;
  52. $roleId = $bindData['roleId'] ?? 0;
  53. $role = AdminRoleClass::getById($roleId);
  54. if (empty($role)) {
  55. util::fail('没有找到角色。');
  56. }
  57. $shop = ShopClass::getById($shopId);
  58. if (empty($shop)) {
  59. util::fail('没有找到门店52');
  60. }
  61. $sjId = $shop['sjId'] ?? 0;
  62. $shopAdmin = ShopAdminClass::getByCondition(['adminId' => $adminId, 'mainId' => $mainId], true);
  63. if (!empty($shopAdmin)) {
  64. //删除员工补回
  65. if ($shopAdmin->delStatus == 1) {
  66. $shopAdmin->delStatus = 0;
  67. $shopAdmin->roleId = $roleId;
  68. $shopAdmin->remind = $remind;
  69. $shopAdmin->status = $status;
  70. $shopAdmin->avatar = $avatar;
  71. $shopAdmin->remark = $remark;
  72. $shopAdmin->name = $name;
  73. $shopAdmin->save();
  74. $update = ['currentShopId' => $shopId];
  75. if (!empty($password)) {
  76. $update['password'] = $password;
  77. }
  78. AdminClass::updateById($adminId, $update);
  79. AdminRoleClass::counters(['num' => 1], ['id' => $roleId]);
  80. return $shopAdmin->attributes;
  81. }
  82. util::fail('您已经是员工');
  83. } else {
  84. if (isset($bindData['roleId']) == false || empty($bindData['roleId'])) {
  85. util::fail('选择角色');
  86. }
  87. $addData = [
  88. 'adminId' => $adminId,
  89. 'shopId' => $shopId,
  90. 'roleId' => $roleId,
  91. 'remind' => $remind,
  92. 'status' => $status,
  93. 'sjId' => $sjId,
  94. 'mainId' => $mainId,
  95. 'name' => $name,
  96. 'mobile' => $mobile,
  97. 'avatar' => $avatar,
  98. 'remark' => $remark,
  99. ];
  100. ShopAdminClass::delBindAdminData($shopId, $recommendAdminId);
  101. $result = ShopAdminClass::add($addData);
  102. $update = ['currentShopId' => $shopId];
  103. if (!empty($password)) {
  104. $update['password'] = $password;
  105. }
  106. AdminClass::updateById($adminId, $update);
  107. AdminRoleClass::counters(['num' => 1], ['id' => $roleId]);
  108. return $result;
  109. }
  110. }
  111. //获取管理员 ssh 2020.4.21
  112. public static function getAdminList($where)
  113. {
  114. $data = ShopAdminClass::getList('*', $where, 'addTime DESC');
  115. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  116. if (empty($list)) {
  117. return $data;
  118. }
  119. $roleIds = array_column($list, 'roleId');
  120. $roleIds = array_filter(array_unique($roleIds));
  121. $roleList = AdminRoleClass::getByIds($roleIds, null, 'id');
  122. foreach ($list as $key => $val) {
  123. $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
  124. $roleId = $val['roleId'];
  125. $roleName = isset($roleList[$roleId]['roleName']) ? $roleList[$roleId]['roleName'] : '';
  126. $list[$key]['roleName'] = $roleName;
  127. }
  128. $data['list'] = $list;
  129. return $data;
  130. }
  131. //获取要通知的管理员 ssh 2020.4.28
  132. public static function getNoticeAdminList($shopId)
  133. {
  134. $where = ['shopId' => $shopId];
  135. $respond = self::getAdminList($where);
  136. $list = isset($respond['list']) ? $respond['list'] : [];
  137. if (!empty($list)) {
  138. foreach ($list as $key => $val) {
  139. if (isset($val['remind']) && $val['remind'] == 0) {
  140. unset($list[$key]);
  141. }
  142. }
  143. }
  144. return $list;
  145. }
  146. public static function valid($shopAdmin, $mainId)
  147. {
  148. if (isset($shopAdmin['mainId']) == false || $shopAdmin['mainId'] != $mainId) {
  149. util::fail('不是您的员工');
  150. }
  151. }
  152. }