ShopAdminController.php 11 KB

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