ShopAdminController.php 10 KB

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