ShopAdminController.php 9.6 KB

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