ShopAdminController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. //获取添加员工的验证码 shish 20210117
  20. public function actionGetStaffAuthCode()
  21. {
  22. $get = Yii::$app->request->get();
  23. $mobile = $get['mobile'] ?? '';
  24. if (stringUtil::isMobile($mobile) == false) {
  25. util::fail('请输入正确手机号');
  26. }
  27. $rand = rand(11111, 99999);
  28. $minute = 10;
  29. sms::send($mobile . ',' . $rand . ',' . $minute, '您的员工验证码:{$var},{$var}分钟内有效');
  30. $cacheKey = 'add_staff_auth_code_' . $mobile;
  31. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 600, $rand]);
  32. util::complete();
  33. }
  34. //添加员工
  35. public function actionAddStaff()
  36. {
  37. $post = Yii::$app->request->post();
  38. $authCode = $post['authCode'] ?? '';
  39. $mobile = $post['mobile'] ?? '';
  40. $name = $post['name'] ?? '';
  41. //避免重复提交
  42. util::checkRepeatCommit($mobile, 10);
  43. $cacheKey = 'add_staff_auth_code_' . $mobile;
  44. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  45. if (empty($saveAuthCode)) {
  46. util::fail('请填写验证码哦');
  47. }
  48. if (empty($authCode)) {
  49. util::fail('请填写验证码');
  50. }
  51. if ($saveAuthCode != $authCode) {
  52. util::fail('验证码错误');
  53. }
  54. $ptStyle = Yii::$app->params['ptStyle'];
  55. $adminInfo = ['name' => $name, 'mobile' => $mobile, 'style' => $ptStyle,];
  56. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  57. $admin = AdminService::replaceAdmin($adminInfo, $fromApp);
  58. $respond = ShopAdminService::appGenerateStaff($post, $admin);
  59. util::success($respond);
  60. }
  61. //创建管理员数据准备 ssh 2020.4.17
  62. public function actionPrepareBind()
  63. {
  64. $post = Yii::$app->request->post();
  65. $shopAdmin = $this->shopAdmin->attributes;
  66. if ($shopAdmin['super'] != 1) {
  67. util::fail('超管才能操作');
  68. }
  69. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  70. $role = AdminRoleService::getById($roleId);
  71. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  72. util::fail('请选择角色!');
  73. }
  74. unset($post['userId']);
  75. unset($post['status']);
  76. $post['shopId'] = $this->shopId;
  77. $post['sjId'] = $this->sjId;
  78. $post['adminId'] = $this->adminId;
  79. $merchant = WxOpenClass::getGhsWxInfo();
  80. $post['merchant'] = $merchant;
  81. $respond = ShopAdminService::prepareBindAdmin($post);
  82. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  83. util::success(['miniCode' => $miniCode]);
  84. }
  85. //创建员工 ssh 2020.4.20
  86. public function actionGenerateAdmin()
  87. {
  88. $post = Yii::$app->request->post();
  89. $post['adminId'] = $this->adminId;
  90. $admin = $this->admin->attributes;
  91. $currentShopId = $admin['currentShopId'] ?? 0;
  92. if (!empty($currentShopId)) {
  93. $shop = ShopClass::getShopInfo($currentShopId);
  94. //一个人不能同时在二个商家门店下工作
  95. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  96. util::fail('您已经是别的商家员工');
  97. }
  98. }
  99. $openShop = $admin['openShop'] ?? AdminClass::OPEN_SHOP_NO;
  100. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  101. util::fail('您已申请开过店,不能成为别人的员工');
  102. }
  103. $respond = ShopAdminService::generateAdmin($post, $admin);
  104. util::success($respond);
  105. }
  106. //收款通知 ssh 2021.1.14
  107. public function actionUpdateRemind()
  108. {
  109. $get = Yii::$app->request->get();
  110. $id = $get['id'] ?? 0;
  111. $remind = $get['remind'] ?? 0;
  112. $relation = ShopAdminClass::getById($id, true);
  113. if (empty($relation)) {
  114. util::fail('没有找到员工');
  115. }
  116. ShopAdminClass::valid($relation->attributes, $this->shopId);
  117. $relation->remind = $remind;
  118. $relation->save();
  119. util::complete();
  120. }
  121. //启用与停用 ssh 2021.1.14
  122. public function actionUpdateStatus()
  123. {
  124. $get = Yii::$app->request->get();
  125. $id = $get['id'] ?? 0;
  126. $status = $get['status'] ?? 0;
  127. $relation = ShopAdminClass::getById($id, true);
  128. if (empty($relation)) {
  129. util::fail('没有找到员工');
  130. }
  131. ShopAdminClass::valid($relation->attributes, $this->shopId);
  132. $relation->status = $status;
  133. $relation->save();
  134. util::complete();
  135. }
  136. //员工列表 ssh 2019.12.8
  137. public function actionList()
  138. {
  139. $where = [];
  140. $where['shopId'] = $this->shopId;
  141. $where['delStatus'] = 0;
  142. $where['isPt'] = 0;
  143. $list = ShopAdminService::getAdminList($where);
  144. util::success($list);
  145. }
  146. //获取员工信息 ssh 2021.1.13
  147. public function actionDetail()
  148. {
  149. $get = Yii::$app->request->get();
  150. $id = isset($get['id']) ? $get['id'] : 0;
  151. $respond = ShopAdminService::getAdminInfo($id);
  152. ShopAdminClass::valid($respond, $this->shopId);
  153. util::success($respond);
  154. }
  155. //修改员工信息
  156. public function actionUpdate()
  157. {
  158. $post = Yii::$app->request->post();
  159. $id = $post['id'] ?? 0;
  160. $shopAdmin = $this->shopAdmin->attributes;
  161. if ($shopAdmin['super'] != 1) {
  162. util::fail('超管才能操作');
  163. }
  164. if (empty($id)) {
  165. util::fail('请选择员工');
  166. }
  167. $relation = ShopAdminClass::getById($id, true);
  168. if (empty($relation)) {
  169. util::fail('没有找到员工');
  170. }
  171. ShopAdminClass::valid($relation, $this->shopId);
  172. unset($post['id']);
  173. $roleId = $post['roleId'] ?? 0;
  174. $remind = $post['remind'] ?? 0;
  175. $status = $post['status'] ?? 1;
  176. $relation->roleId = $roleId;
  177. $relation->remind = $remind;
  178. $relation->status = $status;
  179. if (isset($post['name']) && !empty($post['name'])) {
  180. $relation->name = $post['name'];
  181. }
  182. $relation->save();
  183. //若有传密码,则修改密码
  184. $password = $post['password'] ?? '';
  185. if (!empty($password)) {
  186. if (ctype_alnum($password)) {
  187. util::fail('密码必须是字母、数字和符号的组合');
  188. }
  189. $adminId = $relation->adminId;
  190. $password = password_hash($password, PASSWORD_BCRYPT);
  191. AdminClass::updateById($adminId, ['password' => $password]);
  192. }
  193. util::complete('修改成功');
  194. }
  195. //删除员工关系 ssh 2021.1.14
  196. public function actionDelete()
  197. {
  198. $id = Yii::$app->request->get('id');
  199. $shopAdmin = $this->shopAdmin->attributes;
  200. if ($shopAdmin['super'] != 1) {
  201. util::fail('超管才能操作');
  202. }
  203. $relation = ShopAdminClass::getById($id, true);
  204. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->shopId);
  205. util::complete();
  206. }
  207. }