UserController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace business\controllers;
  3. use biz\admin\services\AdminService;
  4. use biz\merchant\services\MerchantAssetService;
  5. use biz\merchant\services\MerchantExtendService;
  6. use biz\merchant\services\MerchantRemarkService;
  7. use biz\user\services\UserAssetService;
  8. use biz\user\services\UserIntegralService;
  9. use biz\user\services\UserService;
  10. use common\components\wxUtil;
  11. use common\services\xhMerchantExtendService;
  12. use common\services\xhRechargeService;
  13. use common\services\xhTMessageService;
  14. use common\services\xhUserIntegralService;
  15. use Yii;
  16. use common\components\stringUtil;
  17. use common\components\util;
  18. class UserController extends BaseController
  19. {
  20. //客户列表 shish 2019.11.30
  21. public function actionList()
  22. {
  23. $get = Yii::$app->request->get();
  24. //组建where
  25. $type = isset($get['type']) ? $get['type'] : -1;
  26. $search = isset($get['search']) ? $get['search'] : '';
  27. $where = ['merchantId' => $this->merchantId];
  28. switch ($type) {
  29. case 1:
  30. //粉丝
  31. $where['subscribe'] = 1;
  32. break;
  33. case 2:
  34. //会员
  35. $where['member>'] = -1;
  36. break;
  37. default:
  38. }
  39. if (!empty($search)) {
  40. if (stringUtil::isMobile($search)) {
  41. $where['mobile'] = $search;
  42. } else {
  43. $where['userName'] = ['like', $search];
  44. }
  45. }
  46. $list = UserService::getUserList($where);
  47. $list['asset'] = MerchantAssetService::getByMerchantId($this->merchantId);
  48. util::success($list);
  49. }
  50. //获取客户基本和资产信息 shish 2019.11.30
  51. public function actionDetail()
  52. {
  53. $userId = Yii::$app->request->get('userId');
  54. $info = UserService::getUserInfo($userId);
  55. if (isset($info['merchantId']) == false || $this->merchantId != $info['merchantId']) {
  56. util::fail('没有权限');
  57. }
  58. util::success($info);
  59. }
  60. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  61. public function actionUserDetail()
  62. {
  63. $userId = Yii::$app->request->get('userId');
  64. $info = UserService::getFullUserInfo($userId);
  65. UserService::valid($info, $this->merchantId);
  66. $list = MerchantRemarkService::getRemarkList($userId);
  67. $info['remarkList'] = $list;
  68. util::success($info);
  69. }
  70. //添加客户,申请会员时会自动同步资产 shish 2019.11.30
  71. public function actionAdd()
  72. {
  73. $post = Yii::$app->request->post();
  74. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  75. util::fail('请输入手机号');
  76. }
  77. if ($post['mobile'] != $post['confirmMobile']) {
  78. util::fail('二次输入手机号不一样');
  79. }
  80. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  81. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  82. $mobile = $post['mobile'];
  83. $user = UserService::getByMobile($mobile, $this->merchantId);
  84. if (!empty($user)) {
  85. util::fail('手机号已使用,客户已存在');
  86. }
  87. $post['merchantId'] = $this->merchantId;
  88. $post['member'] = $post['memberLevel'];
  89. UserService::addUser($post);
  90. util::ok();
  91. }
  92. //充值 shish 2019.12.1
  93. public function actionRecharge()
  94. {
  95. $post = Yii::$app->request->post();
  96. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  97. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  98. $userId = isset($post['userId']) ? $post['userId'] : 0;
  99. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  100. $user = UserService::getById($userId);
  101. if (empty($user)) {
  102. util::fail('找不到客户');
  103. }
  104. if ($user['merchantId'] != $this->merchantId) {
  105. util::fail('您没有权限操作');
  106. }
  107. $userAsset = UserAssetService::getByUserId($userId);
  108. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  109. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  110. $upData['balance'] = $totalBalance;
  111. $upData['totalRecharge'] = $totalRecharge;
  112. $merchantAsset = MerchantAssetService::getByMerchantId($this->merchantId);
  113. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->merchant, $this->merchantExtend, $merchantAsset);
  114. if ($return['code'] == 'A0001') {
  115. util::ok();
  116. }
  117. util::fail();
  118. }
  119. //升级 shish 2019.12.1
  120. public function actionUpgrade()
  121. {
  122. $post = Yii::$app->request->post();
  123. $userId = isset($post['userId']) ? $post['userId'] : 0;
  124. $member = isset($post['member']) ? $post['member'] : 0;
  125. $user = UserService::getById($userId);
  126. if (empty($user)) {
  127. util::fail('客户不存在');
  128. }
  129. if ($user['merchantId'] != $this->merchantId) {
  130. util::fail('您没有权限操作');
  131. }
  132. $confirmPassword = $post['confirmPassword'];
  133. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  134. $grade = xhMerchantExtendService::getGradeList($this->merchantExtend, 'desc');
  135. $newLevelGrowth = $grade[$member]['growth'];//升级到当前级别需要的积分数
  136. $userAsset = UserAssetService::getByUserId($userId);//用户资产
  137. if ($userAsset['member'] != -1 && $userAsset['member'] >= $member) {
  138. util::fail('客户已经达到此级别了');
  139. }
  140. $now = time();
  141. $date = date("Y-m-d H:i", $now);
  142. $connection = Yii::$app->db;//事务处理
  143. $transaction = $connection->beginTransaction();
  144. try {
  145. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  146. //升级后的成长值
  147. $upData['growth'] = $newLevelGrowth;
  148. $upData['member'] = $member;
  149. UserAssetService::updateByUserId($userId, $upData);
  150. $integralData = [
  151. 'userId' => $userId,
  152. 'userName' => $user['userName'],
  153. 'merchantId' => $this->merchantId,
  154. 'relateId' => '',
  155. 'integral' => $newLevelGrowth,
  156. 'num' => $addGrowth,
  157. 'io' => 1,
  158. 'payWay' => 4,
  159. 'alipayId' => '',
  160. 'event' => '店长操作升级',
  161. 'operatorId' => $userId,
  162. 'createTime' => $date,
  163. 'capitalType' => 0,
  164. 'addTime' => $now,
  165. ];
  166. //用户兑换型积分流水增加
  167. //xhUserIntegralService::add($integralData);
  168. $transaction->commit();
  169. } catch (Exception $e) {
  170. $transaction->rollBack();
  171. util::fail();
  172. }
  173. $allTM = xhTMessageService::getList($this->merchantId);
  174. $openId = isset($user['openId']) ? $user['openId'] : '';
  175. if (empty($openId)) {
  176. util::ok();
  177. }
  178. $shortTempId = 'OPENTM205211943';//升级通知
  179. if (isset($allTM[$shortTempId]) == false) {
  180. util::ok();
  181. }
  182. $tempId = $allTM[$shortTempId];
  183. $data = [
  184. "touser" => $openId, "template_id" => $tempId, "url" => '',
  185. "data" => [
  186. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  187. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  188. "keyword2" => ["value" => $date, "color" => "#173177"],
  189. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  190. ]];
  191. wxUtil::sendTaskInform($data, $this->merchant);
  192. util::ok();
  193. }
  194. //重置客户密码
  195. public function actionResetPayPassword()
  196. {
  197. $post = Yii::$app->request->post();
  198. $userId = isset($post['userId']) ? $post['userId'] : 0;
  199. $password = isset($post['password']) ? $post['password'] : 0;
  200. $info = UserService::getById($userId);
  201. UserService::valid($info, $this->merchantId);
  202. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  203. util::ok();
  204. }
  205. }