ShopAdminService.php 6.2 KB

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