ShopAdminController.php 11 KB

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