ShopAdminController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. $post['shopId'] = $this->shopId;
  59. $respond = ShopAdminService::appGenerateStaff($post, $admin);
  60. util::success($respond);
  61. }
  62. //创建管理员数据准备 ssh 2020.4.17
  63. public function actionPrepareBind()
  64. {
  65. $post = Yii::$app->request->post();
  66. $shopAdmin = $this->shopAdmin->attributes;
  67. if ($shopAdmin['super'] != 1) {
  68. util::fail('超管才能操作');
  69. }
  70. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  71. $role = AdminRoleService::getById($roleId);
  72. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  73. util::fail('请选择角色!');
  74. }
  75. unset($post['userId']);
  76. unset($post['status']);
  77. $post['shopId'] = $this->shopId;
  78. $post['sjId'] = $this->sjId;
  79. $post['adminId'] = $this->adminId;
  80. $merchant = WxOpenClass::getGhsWxInfo();
  81. $post['merchant'] = $merchant;
  82. $respond = ShopAdminService::prepareBindAdmin($post);
  83. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  84. util::success(['miniCode' => $miniCode]);
  85. }
  86. //创建员工 ssh 2020.4.20
  87. public function actionGenerateAdmin()
  88. {
  89. $post = Yii::$app->request->post();
  90. $post['adminId'] = $this->adminId;
  91. $admin = $this->admin->attributes;
  92. $currentShopId = $admin['currentShopId'] ?? 0;
  93. if (!empty($currentShopId)) {
  94. $shop = ShopClass::getShopInfo($currentShopId);
  95. //一个人不能同时在二个商家门店下工作
  96. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  97. util::fail('您已经是别的商家员工');
  98. }
  99. }
  100. $openShop = $admin['openShop'] ?? AdminClass::OPEN_SHOP_NO;
  101. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  102. util::fail('您已申请开过店,不能成为别人的员工');
  103. }
  104. $respond = ShopAdminService::generateAdmin($post, $admin);
  105. util::success($respond);
  106. }
  107. //收款通知 ssh 2021.1.14
  108. public function actionUpdateRemind()
  109. {
  110. $get = Yii::$app->request->get();
  111. $id = $get['id'] ?? 0;
  112. $remind = $get['remind'] ?? 0;
  113. $relation = ShopAdminClass::getById($id, true);
  114. if (empty($relation)) {
  115. util::fail('没有找到员工');
  116. }
  117. ShopAdminClass::valid($relation->attributes, $this->shopId);
  118. $relation->remind = $remind;
  119. $relation->save();
  120. util::complete();
  121. }
  122. //启用与停用 ssh 2021.1.14
  123. public function actionUpdateStatus()
  124. {
  125. $get = Yii::$app->request->get();
  126. $id = $get['id'] ?? 0;
  127. $status = $get['status'] ?? 0;
  128. $relation = ShopAdminClass::getById($id, true);
  129. if (empty($relation)) {
  130. util::fail('没有找到员工');
  131. }
  132. ShopAdminClass::valid($relation->attributes, $this->shopId);
  133. $relation->status = $status;
  134. $relation->save();
  135. util::complete();
  136. }
  137. //员工列表 ssh 2019.12.8
  138. public function actionList()
  139. {
  140. $where = [];
  141. $where['shopId'] = $this->shopId;
  142. $where['delStatus'] = 0;
  143. $where['isPt'] = 0;
  144. $list = ShopAdminService::getAdminList($where);
  145. util::success($list);
  146. }
  147. //获取员工信息 ssh 2021.1.13
  148. public function actionDetail()
  149. {
  150. $get = Yii::$app->request->get();
  151. $id = isset($get['id']) ? $get['id'] : 0;
  152. $respond = ShopAdminService::getAdminInfo($id);
  153. ShopAdminClass::valid($respond, $this->shopId);
  154. util::success($respond);
  155. }
  156. //修改员工信息
  157. public function actionUpdate()
  158. {
  159. $post = Yii::$app->request->post();
  160. $id = $post['id'] ?? 0;
  161. $shopAdmin = $this->shopAdmin->attributes;
  162. if ($shopAdmin['super'] != 1) {
  163. util::fail('超管才能操作');
  164. }
  165. if (empty($id)) {
  166. util::fail('请选择员工');
  167. }
  168. $relation = ShopAdminClass::getById($id, true);
  169. if (empty($relation)) {
  170. util::fail('没有找到员工');
  171. }
  172. ShopAdminClass::valid($relation, $this->shopId);
  173. unset($post['id']);
  174. $roleId = $post['roleId'] ?? 0;
  175. $remind = $post['remind'] ?? 0;
  176. $status = $post['status'] ?? 1;
  177. $relation->roleId = $roleId;
  178. $relation->remind = $remind;
  179. $relation->status = $status;
  180. if (isset($post['name']) && !empty($post['name'])) {
  181. $relation->name = $post['name'];
  182. }
  183. $relation->save();
  184. //若有传密码,则修改密码
  185. $password = $post['password'] ?? '';
  186. if (!empty($password)) {
  187. if (ctype_alnum($password)) {
  188. util::fail('密码必须是字母、数字和符号的组合');
  189. }
  190. $adminId = $relation->adminId;
  191. $password = password_hash($password, PASSWORD_BCRYPT);
  192. AdminClass::updateById($adminId, ['password' => $password]);
  193. }
  194. util::complete('修改成功');
  195. }
  196. //删除员工关系 ssh 2021.1.14
  197. public function actionDelete()
  198. {
  199. $id = Yii::$app->request->get('id');
  200. $shopAdmin = $this->shopAdmin->attributes;
  201. if ($shopAdmin['super'] != 1) {
  202. util::fail('超管才能操作');
  203. }
  204. $relation = ShopAdminClass::getById($id, true);
  205. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->shopId);
  206. util::complete();
  207. }
  208. }