ShopAdminController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizGhs\shop\classes\ShopAdminClass;
  7. use bizGhs\admin\services\AdminRoleService;
  8. use bizGhs\shop\services\ShopAdminService;
  9. use common\components\dict;
  10. use common\components\imgUtil;
  11. use common\components\sms;
  12. use common\components\stringUtil;
  13. use common\components\util;
  14. use Yii;
  15. class ShopAdminController extends BaseController
  16. {
  17. public $guestAccess = ['generate-admin'];
  18. //设置店长 ssh 20250711
  19. public function actionSetManager()
  20. {
  21. $get = Yii::$app->request->get();
  22. $id = $get['id'] ?? 0;
  23. $staff = ShopAdminClass::getById($id, true);
  24. if (empty($staff)) {
  25. util::fail('没有员工');
  26. }
  27. if ($staff->mainId != $this->mainId) {
  28. util::fail('不是你的');
  29. }
  30. $current = $this->shopAdmin;
  31. if ($current->mobile != 15280215347) {
  32. util::fail('无效操作');
  33. }
  34. $connection = Yii::$app->db;
  35. $transaction = $connection->beginTransaction();
  36. try {
  37. $staff->founder = 2;
  38. $staff->save();
  39. $newAdminId = $staff->adminId;
  40. $mainId = $staff->mainId;
  41. $shopList = ShopClass::getAllByCondition(['mainId' => $mainId], null, 'id,shopName,mainId,adminId', null, true);
  42. $adminId = 0;
  43. if (!empty($shopList)) {
  44. foreach ($shopList as $shop) {
  45. if (!empty($shop->adminId)) {
  46. $adminId = $shop->adminId;
  47. }
  48. $shop->adminId = $newAdminId;
  49. $shop->save();
  50. }
  51. if (!empty($adminId)) {
  52. ShopAdminClass::updateByCondition(['mainId' => $mainId, 'adminId' => $adminId], ['founder' => 1]);
  53. }
  54. }
  55. $transaction->commit();
  56. util::complete('操作成功');
  57. } catch (\Exception $e) {
  58. $transaction->rollBack();
  59. Yii::info("操作失败原因:" . $e->getMessage());
  60. util::fail('操作失败');
  61. }
  62. }
  63. //获取当前登录员工的信息 ssh 20230802
  64. public function actionGetStaffInfo()
  65. {
  66. $staff = $this->shopAdmin;
  67. util::success(['staff' => $staff]);
  68. }
  69. //获取所有在职员工 ssh 20220507
  70. public function actionGetAllStaff()
  71. {
  72. $where = ['mainId' => $this->mainId, 'delStatus' => 0, 'status' => 1, 'isPt' => 0,];
  73. $list = ShopAdminClass::getAllByCondition($where, 'inTurn DESC', 'id,name,mobile', null, true);
  74. util::success(['list' => $list]);
  75. }
  76. //获取添加员工的验证码 ssh 20210117
  77. public function actionGetStaffAuthCode()
  78. {
  79. $get = Yii::$app->request->get();
  80. $mobile = $get['mobile'] ?? '';
  81. if (stringUtil::isMobile($mobile) == false) {
  82. util::fail('请输入正确手机号');
  83. }
  84. $rand = rand(11111, 99999);
  85. $minute = 10;
  86. sms::send($mobile . ',' . $rand . ',' . $minute, '您的员工验证码:{$var},{$var}分钟内有效');
  87. $ptStyle = Yii::$app->params['ptStyle'];
  88. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  89. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 600, $rand]);
  90. util::complete();
  91. }
  92. //添加员工
  93. public function actionAddStaff()
  94. {
  95. $shopAdmin = $this->shopAdmin;
  96. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  97. util::fail('没有权限');
  98. }
  99. $adminId = $this->adminId;
  100. $shop = $this->shop;
  101. $connection = Yii::$app->db;
  102. $transaction = $connection->beginTransaction();
  103. try {
  104. $post = Yii::$app->request->post();
  105. $authCode = $post['authCode'] ?? '';
  106. $mobile = $post['mobile'] ?? '';
  107. $name = $post['name'] ?? '';
  108. $finance = $post['finance'] ?? 0;
  109. $identity = $post['identity'] ?? 0;
  110. $switchShop = $post['switchShop'] ?? 0;
  111. $ptSuperAdminId = AdminClass::getPtSuperAdminId();
  112. if ($shop->adminId != $adminId && $adminId != $ptSuperAdminId) {
  113. if ($finance == 1) {
  114. util::fail('超管才能开启员工财务权限');
  115. }
  116. if ($switchShop == 1) {
  117. util::fail('超管才能开启员工分店管理权限');
  118. }
  119. }
  120. util::checkRepeatCommit($mobile, 10);
  121. $ptStyle = Yii::$app->params['ptStyle'];
  122. //验证码取消验证,临时使用的方法
  123. if ($authCode != 19640123) {
  124. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  125. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  126. if (empty($saveAuthCode)) {
  127. util::fail('请填写验证码哦');
  128. }
  129. if (empty($authCode)) {
  130. util::fail('请填写验证码');
  131. }
  132. if ($saveAuthCode != $authCode) {
  133. util::fail('验证码错误');
  134. }
  135. }
  136. $ptStyle = Yii::$app->params['ptStyle'];
  137. $adminInfo = ['name' => $name, 'mobile' => $mobile, 'style' => $ptStyle, 'currentGhsShopId' => $this->shopId, 'identity' => $identity];
  138. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  139. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  140. $post['shopId'] = $this->shopId;
  141. $respond = ShopAdminClass::appGenerateStaff($post, $admin);
  142. $transaction->commit();
  143. util::success($respond);
  144. } catch (\Exception $exception) {
  145. $transaction->rollBack();
  146. Yii::info("失败原因:" . $exception->getMessage());
  147. util::fail('添加失败');
  148. }
  149. }
  150. //创建管理员数据准备 ssh 2020.4.17
  151. public function actionPrepareBind()
  152. {
  153. $post = Yii::$app->request->post();
  154. $shopAdmin = $this->shopAdmin->attributes;
  155. if ($shopAdmin['super'] != 1) {
  156. util::fail('超管才能操作');
  157. }
  158. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  159. $role = AdminRoleService::getById($roleId);
  160. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  161. util::fail('请选择角色!');
  162. }
  163. unset($post['userId']);
  164. unset($post['status']);
  165. $post['shopId'] = $this->shopId;
  166. $post['sjId'] = $this->sjId;
  167. $post['adminId'] = $this->adminId;
  168. $post['mainId'] = $this->mainId;
  169. $merchant = WxOpenClass::getGhsWxInfo();
  170. $post['merchant'] = $merchant;
  171. $respond = ShopAdminService::prepareBindAdmin($post);
  172. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  173. util::success(['miniCode' => $miniCode]);
  174. }
  175. //创建员工 ssh 2020.4.20
  176. public function actionGenerateAdmin()
  177. {
  178. $post = Yii::$app->request->post();
  179. $post['adminId'] = $this->adminId;
  180. $post['mainId'] = $this->mainId;
  181. $admin = $this->admin->attributes;
  182. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  183. if (!empty($currentShopId)) {
  184. $shop = ShopClass::getShopInfo($currentShopId);
  185. //一个人不能同时在二个商家门店下工作
  186. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  187. util::fail('您已经是别的商家员工');
  188. }
  189. }
  190. $openShop = $admin['openGhsShop'] ?? AdminClass::OPEN_SHOP_NO;
  191. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  192. util::fail('您已申请开过店,不能成为别人的员工');
  193. }
  194. $respond = ShopAdminService::generateAdmin($post, $admin);
  195. util::success($respond);
  196. }
  197. //收款通知 ssh 2021.1.14
  198. public function actionUpdateRemind()
  199. {
  200. $get = Yii::$app->request->get();
  201. $id = $get['id'] ?? 0;
  202. $remind = $get['remind'] ?? 0;
  203. $relation = ShopAdminClass::getById($id, true);
  204. if (empty($relation)) {
  205. util::fail('没有找到员工');
  206. }
  207. ShopAdminClass::valid($relation->attributes, $this->mainId);
  208. $relation->remind = $remind;
  209. $relation->save();
  210. util::complete();
  211. }
  212. //启用与停用 ssh 2021.1.14
  213. public function actionUpdateStatus()
  214. {
  215. $get = Yii::$app->request->get();
  216. $id = $get['id'] ?? 0;
  217. $status = $get['status'] ?? 0;
  218. $relation = ShopAdminClass::getById($id, true);
  219. if (empty($relation)) {
  220. util::fail('没有找到员工');
  221. }
  222. ShopAdminClass::valid($relation->attributes, $this->mainId);
  223. $relation->status = $status;
  224. $relation->save();
  225. util::complete();
  226. }
  227. //员工列表 ssh 2019.12.8
  228. public function actionList()
  229. {
  230. $where = [];
  231. $where['mainId'] = $this->mainId;
  232. $where['delStatus'] = 0;
  233. $where['isPt'] = 0;
  234. $list = ShopAdminService::getAdminList($where);
  235. util::success($list);
  236. }
  237. //获取员工信息 ssh 2021.1.13
  238. public function actionDetail()
  239. {
  240. $get = Yii::$app->request->get();
  241. $id = isset($get['id']) ? $get['id'] : 0;
  242. $respond = ShopAdminService::getAdminInfo($id);
  243. ShopAdminClass::valid($respond, $this->mainId);
  244. util::success($respond);
  245. }
  246. //修改员工信息
  247. public function actionUpdate()
  248. {
  249. $post = Yii::$app->request->post();
  250. $id = $post['id'] ?? 0;
  251. $shopAdmin = $this->shopAdmin;
  252. $adminId = $this->adminId;
  253. $shop = $this->shop;
  254. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  255. util::fail('没有权限');
  256. }
  257. if (empty($id)) {
  258. util::fail('请选择员工');
  259. }
  260. $relation = ShopAdminClass::getById($id, true);
  261. if (empty($relation)) {
  262. util::fail('没有找到员工');
  263. }
  264. if ($relation->mainId != $this->mainId) {
  265. util::fail('不能修改');
  266. }
  267. $roleId = $post['roleId'] ?? 0;
  268. $remind = $post['remind'] ?? 0;
  269. $status = $post['status'] ?? 1;
  270. $super = $post['super'] ?? 0;
  271. $name = $post['name'] ?? '';
  272. $finance = $post['finance'] ?? 0;
  273. $switchShop = $post['switchShop'] ?? 0;
  274. $identity = $post['identity'] ?? 0;
  275. $founder = $relation->founder ?? 1;
  276. $ptSuperAdminId = AdminClass::getPtSuperAdminId();
  277. if ($shop->adminId != $adminId && $adminId != $ptSuperAdminId) {
  278. if ($finance == 1) {
  279. util::fail('超管才能修改财务权限');
  280. }
  281. if ($switchShop == 1) {
  282. util::fail('超管才能修改管理分店权限');
  283. }
  284. }
  285. if ($founder == 2) {
  286. if ($super == 0) {
  287. util::fail('超管不能关闭改库存改价权限');
  288. }
  289. }
  290. if (!empty($name)) {
  291. $relation->name = $name;
  292. }
  293. $relation->roleId = $roleId;
  294. $relation->remind = $remind;
  295. $relation->status = $status;
  296. $relation->super = $super;
  297. $relation->finance = $finance;
  298. $relation->switchShop = $switchShop;
  299. $relation->identity = $identity;
  300. $relation->save();
  301. //需要更改原名,下单环节的下单人取的是人家原名的名称
  302. $currentId = $relation->adminId ?? 0;
  303. $current = AdminClass::getById($currentId, true);
  304. if (!empty($current)) {
  305. $current->name = $name;
  306. $current->save();
  307. }
  308. util::complete('修改成功');
  309. }
  310. //删除员工关系 ssh 2021.1.14
  311. public function actionDelete()
  312. {
  313. $id = Yii::$app->request->get('id');
  314. $shopAdmin = $this->shopAdmin;
  315. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  316. util::fail('没有权限');
  317. }
  318. $connection = Yii::$app->db;
  319. $transaction = $connection->beginTransaction();
  320. try {
  321. $relation = ShopAdminClass::getById($id, true);
  322. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->mainId);
  323. $transaction->commit();
  324. util::complete();
  325. } catch (\Exception $exception) {
  326. $transaction->rollBack();
  327. Yii::info("失败原因:" . $exception->getMessage());
  328. util::fail('删除失败');
  329. }
  330. }
  331. }