AdminClass.php 12 KB

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