ShopAdminController.php 9.7 KB

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