UserController.php 7.3 KB

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