AdminClass.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. namespace bizHd\admin\classes;
  3. use bizHd\user\classes\UserClass;
  4. use bizGhs\custom\classes\CustomClass;
  5. use biz\sj\classes\MerchantClass;
  6. use common\components\imgUtil;
  7. use common\components\noticeUtil;
  8. use biz\shop\classes\ShopClass;
  9. use common\components\util;
  10. use common\services\ImageService;
  11. use Yii;
  12. use bizHd\base\classes\BaseClass;
  13. class AdminClass extends BaseClass
  14. {
  15. const STYLE_GHS = 2;
  16. const STYLE_HD = 1;
  17. public static $baseFile = '\bizHd\admin\models\Admin';
  18. //收益通知人
  19. public static $noticeAdmin = [
  20. //国兰
  21. 'wx193341a4a80f6ea3' => ['o4c8Vs54PQh32nkgG8R3m4VuGTvw', 'o4c8VszacQJyrzu651CAu0sCsfPc'],
  22. //花美灵
  23. 'wx84d95afcb7b594eb' => ['oTFbYvsjwgVwXggwtlM2ESAKwuN8', 'oTFbYvqP6qwdCCkdFB9GYnlzDIUg'],
  24. ];
  25. //切换门店 lqh 2021.1.31
  26. public static function toggleShop($admin, $newShop, $sjId, $originShopAdmin)
  27. {
  28. $right = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($originShopAdmin);
  29. if (empty($right)) {
  30. util::fail("没有权限");
  31. }
  32. // 判断 $newShop 是否本账号下的门店
  33. $exit = ShopAdminClass::exists(['adminId'=>$admin->id, 'mainId'=>$newShop->mainId]);
  34. if (!$exit) { // 不存在说明不是本账号下的门店,则直接返回
  35. util::fail("切换的不是您的门店");
  36. }
  37. $newShopId = $newShop->id ?? 0;
  38. $pfShopId = $newShop->pfShopId ?? 0;
  39. $shopAdmin = \biz\shop\classes\ShopAdminClass::changeShopAddRelate($originShopAdmin, $newShop);
  40. $admin->currentShopId = $newShopId;
  41. $admin->currentGhsShopId = $pfShopId;
  42. $admin->save();
  43. return $shopAdmin;
  44. }
  45. //开店时初始化员工和角色
  46. public static function initAdmin($initData, $mainId, $shop)
  47. {
  48. $shopId = $shop->id ?? 0;
  49. $mobile = $initData['mobile'] ?? 0;
  50. $adminId = $initData['adminId'] ?? 0;
  51. $hasRole = AdminRoleClass::getByCondition(['mainId' => $mainId], true);
  52. if (empty($hasRole)) {
  53. AdminRoleClass::addAdminRole(['roleName' => '员工', 'mainId' => $mainId, 'auth' => '']);
  54. $roleReturn = AdminRoleClass::addAdminRole(['roleName' => '店长', 'num' => 1, 'mainId' => $mainId, 'auth' => '*']);
  55. $roleId = $roleReturn['id'];
  56. } else {
  57. $roleId = $hasRole->id ?? 0;
  58. }
  59. $admin = \biz\admin\classes\AdminClass::getById($adminId, true);
  60. if (empty($admin)) {
  61. util::fail('没有找到管理员信息');
  62. }
  63. $admin->openShop = 3;
  64. $admin->currentShopId = $shopId;
  65. $admin->save();
  66. //批发端已经生成员工,避免出现重复的员工
  67. $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
  68. if (empty($staff)) {
  69. $avatar = $admin->avatar;
  70. $name = $admin->name;
  71. $shopData = [
  72. 'adminId' => $adminId,
  73. 'mainId' => $mainId,
  74. 'remind' => 1,
  75. 'roleId' => $roleId,
  76. 'default' => 1,
  77. 'avatar' => $avatar,
  78. 'name' => $name,
  79. 'super' => 1,
  80. 'finance' => 1,
  81. 'switchShop' => 1,
  82. 'founder' => 2,
  83. 'mobile' => $mobile,
  84. ];
  85. ShopAdminClass::add($shopData);
  86. }
  87. }
  88. //组合员工基本信息 ssh 2019.12.9
  89. public static function groupAdminBaseInfo($list)
  90. {
  91. foreach ($list as $key => $val) {
  92. $list[$key]['roleName'] = '';
  93. $list[$key]['avatarUrl'] = imgUtil::groupImg($val['avatar']);
  94. $list[$key]['addDate'] = date("Y-m-d H:i", $val['addTime']);
  95. $list[$key]['lastLoginTime'] = date("Y-m-d H:i", $val['loginTime']);
  96. }
  97. return $list;
  98. }
  99. //获取员工详情 ssh 2019.12.9
  100. public static function getDetail($id)
  101. {
  102. $detail = self::getById($id);
  103. if (empty($detail)) {
  104. return [];
  105. }
  106. $list = self::groupAdminBaseInfo([$detail]);
  107. return current($list);
  108. }
  109. //获取员工详情 ssh 2019.12.9
  110. public static function getAdminById($id)
  111. {
  112. $detail = AdminClass::getById($id);
  113. if (empty($detail)) {
  114. util::fail('没有找到员工信息!');
  115. }
  116. $list = self::groupAdminBaseInfo([$detail]);
  117. return current($list);
  118. }
  119. //添加员工 ssh 2019.12.9
  120. public static function addAdmin($data)
  121. {
  122. $sjId = $data['sjId'];
  123. //验证角色
  124. $roleId = $data['roleId'];
  125. $role = AdminRoleClass::getById($roleId);
  126. AdminRoleClass::valid($role, $sjId);
  127. //验证关联的微信
  128. $adminId = $data['admin'];
  129. $admin = self::getById($adminId);
  130. if (empty($admin)) {
  131. util::fail('没有找到管理员信息');
  132. }
  133. $data['password'] = password_hash($data['password'], PASSWORD_BCRYPT);
  134. $respond = self::add($data);
  135. return $respond;
  136. }
  137. //获取商家的所有员工 ssh 2019.12.9
  138. public static function getAdminList($sjId)
  139. {
  140. return self::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0, 'status' => 1], null, '*');
  141. }
  142. //获取商家所有员工的名字 ssh 2019.12.9
  143. public static function getAdminNameList($sjId)
  144. {
  145. $list = self::getAdminList($sjId);
  146. return !empty($list) ? array_column($list, 'name') : [];
  147. }
  148. //获取商家所有员工已经关联微信的userId
  149. public static function getShopAdminUserId($sjId)
  150. {
  151. $list = self::getAdminList($sjId);
  152. return !empty($list) ? array_column($list, 'userId') : [];
  153. }
  154. //验证权限
  155. public static function valid($admin, $sjId)
  156. {
  157. if (empty($admin)) {
  158. util::fail('员工无效');
  159. }
  160. if ($admin['sjId'] != $sjId) {
  161. util::fail('您无法操作此员工');
  162. }
  163. }
  164. //删除员工,需要考虑权限 ssh 2019.12.9
  165. public static function deleteAdmin($admin, $adminId)
  166. {
  167. $id = $admin['id'];
  168. if ($admin['super'] == 1) {
  169. util::fail('超级管理员不能删除');
  170. }
  171. if ($id == $adminId) {
  172. util::fail('不能删除自己');
  173. }
  174. self::updateById($id, ['delStatus' => 1]);
  175. }
  176. public static function getByMiniOpenId($miniOpenId)
  177. {
  178. return self::getByCondition(['miniOpenId' => $miniOpenId]);
  179. }
  180. public static function getByUnionId($unionId)
  181. {
  182. return self::getByCondition(['unionId' => $unionId]);
  183. }
  184. //更新员工信息 ssh 2020.5.30
  185. public static function updateAdmin($originalAdmin, $getAdminInfo)
  186. {
  187. $data = [];
  188. $adminId = $originalAdmin['id'];
  189. //替换掉微信不支持的图片
  190. $nickName = isset($getAdminInfo['nickName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getAdminInfo['nickName']) : '';
  191. if (!empty($nickName)) {
  192. $data['name'] = $nickName;
  193. }
  194. if (isset($originalAdmin['name']) && !empty($originalAdmin['name'])) {
  195. $data['name'] = $originalAdmin['name'];
  196. }
  197. if (isset($getAdminInfo['openId']) && !empty($getAdminInfo['openId'])) {
  198. $openId = $getAdminInfo['openId'];
  199. $data['openId'] = $openId;
  200. }
  201. if (isset($getAdminInfo['miniOpenId']) && !empty($getAdminInfo['miniOpenId'])) {
  202. $data['miniOpenId'] = $getAdminInfo['miniOpenId'];
  203. }
  204. if (isset($getAdminInfo['unionId']) && !empty($getAdminInfo['unionId'])) {
  205. $data['unionId'] = $getAdminInfo['unionId'];
  206. }
  207. if (isset($getAdminInfo['sex'])) {
  208. $data['sex'] = $getAdminInfo['sex'];
  209. }
  210. if (isset($getAdminInfo['subscribe'])) {
  211. $data['subscribe'] = $getAdminInfo['subscribe'];
  212. }
  213. if (isset($getAdminInfo['subscribeTime'])) {
  214. $data['subscribeTime'] = $getAdminInfo['subscribeTime'];
  215. }
  216. if (isset($getAdminInfo['isFull'])) {
  217. $data['isFull'] = $getAdminInfo['isFull'];
  218. }
  219. if (isset($getAdminInfo['headImgUrl']) && !empty($getAdminInfo['headImgUrl'])) {
  220. $data['isFull'] = 1;
  221. }
  222. //更新头像
  223. $sjId = 0;
  224. if (isset($originalAdmin['avatar']) == false || empty($originalAdmin['avatar'])) {
  225. if (isset($getAdminInfo['headImgUrl']) && !empty($getAdminInfo['headImgUrl'])) {
  226. $imgUrl = $getAdminInfo['headImgUrl'];
  227. $name = ImageService::generateAvatar($sjId, $imgUrl);
  228. if (!empty($name)) {
  229. $data['avatar'] = $name;
  230. }
  231. }
  232. }
  233. self::updateById($adminId, $data);
  234. $admin = self::getById($adminId);
  235. return $admin;
  236. }
  237. public static function generateAdmin($getAdminInfo)
  238. {
  239. $time = time();
  240. //替换掉微信名称不支持的图片
  241. $nickName = isset($getAdminInfo['nickName']) ? preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $getAdminInfo['nickName']) : '';
  242. $getAdminInfo['name'] = $nickName;
  243. $subscribe = isset($getAdminInfo['subscribe']) ? $getAdminInfo['subscribe'] : 0;
  244. $getAdminInfo['subscribe'] = $subscribe;
  245. $getAdminInfo['avatar'] = '';
  246. $getAdminInfo['password'] = '';
  247. $getAdminInfo['payPassword'] = '';
  248. $getAdminInfo['mobile'] = isset($getAdminInfo['mobile']) ? $getAdminInfo['mobile'] : '';
  249. $getAdminInfo['addTime'] = $time;
  250. $getAdminInfo['source'] = isset($getAdminInfo['source']) ? $getAdminInfo['source'] : 0;
  251. $getAdminInfo['hasSubscribe'] = 0;
  252. $getAdminInfo['openShop'] = 1;
  253. if (isset($getAdminInfo['headImgUrl']) && !empty($getAdminInfo['headImgUrl'])) {
  254. $imgUrl = $getAdminInfo['headImgUrl'];
  255. $name = ImageService::generateAvatar(0, $imgUrl);
  256. if (!empty($name)) {
  257. $getAdminInfo['avatar'] = $name;
  258. }
  259. }
  260. $admin = self::add($getAdminInfo);
  261. return $admin;
  262. }
  263. //找出相同unionId的用户
  264. public static function getSameUnionId($unionId)
  265. {
  266. return self::getAllByCondition(['unionId' => $unionId], 'addTime desc', '*');
  267. }
  268. public static function getByOpenId($openId)
  269. {
  270. return self::getByCondition(['openId' => $openId]);
  271. }
  272. //将微信和小程序获取的用户信息转成可以保存的用户信息
  273. public static function switchAdminInfo($info, $source)
  274. {
  275. $openId = '';
  276. if (isset($info['openid']) && !empty($info['openid'])) {
  277. $openId = $info['openid'];
  278. unset($info['openId']);
  279. }
  280. if (isset($info['openId']) && !empty($info['openId'])) {
  281. $openId = $info['openId'];
  282. unset($info['openId']);
  283. }
  284. if (!empty($openId)) {
  285. $adminSource = UserClass::$userSourceId;
  286. switch ($source) {
  287. case $adminSource['mini']['name']:
  288. $info['miniOpenId'] = $openId;
  289. break;
  290. case $adminSource['official']['name']:
  291. $info['openId'] = $openId;
  292. break;
  293. default:
  294. }
  295. }
  296. if (isset($info['nickName'])) {
  297. $info['name'] = $info['nickName'];
  298. }
  299. if (isset($info['unionid'])) {
  300. $info['unionId'] = $info['unionid'];
  301. }
  302. if (isset($info['unionId'])) {
  303. $info['unionId'] = $info['unionId'];
  304. }
  305. if (isset($info['headimgurl'])) {
  306. $info['headImgUrl'] = $info['headimgurl'];
  307. }
  308. if (isset($info['avatarUrl'])) {
  309. $info['headImgUrl'] = $info['avatarUrl'];
  310. }
  311. if (isset($info['subscribe_time'])) {
  312. $info['subscribeTime'] = $info['subscribe_time'];
  313. }
  314. if (isset($info['subscribe'])) {
  315. $info['subscribe'] = $info['subscribe'];
  316. }
  317. return $info;
  318. }
  319. //取消关注 ssh 2020.4.19
  320. public static function unFocus($adminId)
  321. {
  322. self::updateById($adminId, ['subscribe' => 0, 'hasSubscribe' => 1]);
  323. }
  324. }