UserController.php 7.3 KB

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