CustomController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\admin\services\AdminService;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\custom\services\CustomService;
  6. use biz\sj\services\MerchantAssetService;
  7. use biz\sj\services\MerchantRemarkService;
  8. use bizHd\user\services\UserAssetService;
  9. use bizHd\user\services\UserGrowthService;
  10. use bizHd\user\services\UserService;
  11. use bizHd\user\services\UserUpgradeService;
  12. use common\components\wxUtil;
  13. use common\services\xhMerchantExtendService;
  14. use common\services\xhRechargeService;
  15. use common\services\xhTMessageService;
  16. use common\services\xhUserIntegralService;
  17. use Yii;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use biz\sj\services\MerchantExtendService;
  21. use bizHd\message\services\SmsService;
  22. use bizHd\user\classes\UserClass;
  23. use yii\helpers\Json;
  24. class CustomController extends BaseController
  25. {
  26. public $guestAccess = ['list'];
  27. //客户列表 ssh 2019.11.30
  28. public function actionList()
  29. {
  30. $get = Yii::$app->request->get();
  31. $type = isset($get['type']) ? $get['type'] : 0;
  32. $name = isset($get['name']) ? $get['name'] : '';
  33. $where = ['shopId' => $this->shopId];
  34. if ($type == 1) {
  35. //消费排行
  36. $sort = 'buyAmount DESC';
  37. } else if ($type == 2) {
  38. //欠款客户
  39. $where['isDebt'] = 1;
  40. $sort = 'debtAmount DESC';
  41. } else {
  42. $sort = 'visitTime DESC';
  43. }
  44. if (!empty($name)) {
  45. if (stringUtil::isLetter($name)) {
  46. $where['py'] = ['like', $name];
  47. } else {
  48. $where['name'] = ['like', $name];
  49. }
  50. }
  51. $list = CustomService::getCustomList($where, $sort);
  52. util::success($list);
  53. }
  54. //获取客户基本和资产信息 ssh 2019.11.30
  55. public function actionDetail()
  56. {
  57. $userId = Yii::$app->request->get('userId');
  58. $info = CustomClass::getCustomInfo($userId);
  59. CustomClass::valid($info, $this->shopId);
  60. util::success($info);
  61. }
  62. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  63. public function actionUserDetail()
  64. {
  65. $userId = Yii::$app->request->get('userId');
  66. $info = UserService::getFullUserInfo($userId);
  67. UserService::valid($info, $this->sjId);
  68. $list = MerchantRemarkService::getRemarkList($userId);
  69. $info['remarkList'] = $list;
  70. util::success($info);
  71. }
  72. //添加客户,申请会员时会自动同步资产 ssh 2019.11.30
  73. public function actionAdd()
  74. {
  75. $post = Yii::$app->request->post();
  76. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  77. util::fail('请输入手机号');
  78. }
  79. if ($post['mobile'] != $post['confirmMobile']) {
  80. util::fail('二次输入手机号不一样');
  81. }
  82. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  83. AdminService::verifyPayPassword($this->adminId, $payPassword);
  84. $mobile = $post['mobile'];
  85. $user = UserService::getByMobile($mobile, $this->sjId);
  86. if (!empty($user)) {
  87. util::fail('手机号已使用');
  88. }
  89. $post['sjId'] = $this->sjId;
  90. $post['member'] = $post['memberLevel'];
  91. UserService::addUser($post);
  92. util::complete();
  93. }
  94. //充值 ssh 2019.12.1
  95. public function actionRecharge()
  96. {
  97. $post = Yii::$app->request->post();
  98. $payPassword = isset($post['payPassword']) ? $post['payPassword'] : '';
  99. AdminService::verifyPayPassword($this->adminId, $payPassword);
  100. $userId = isset($post['userId']) ? $post['userId'] : 0;
  101. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  102. $user = UserService::getById($userId);
  103. if (empty($user)) {
  104. util::fail('找不到客户');
  105. }
  106. if ($user['sjId'] != $this->sjId) {
  107. util::fail('您没有权限操作');
  108. }
  109. $userAsset = UserAssetService::getByUserId($userId);
  110. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  111. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  112. $upData['balance'] = $totalBalance;
  113. $upData['totalRecharge'] = $totalRecharge;
  114. $merchantAsset = MerchantAssetService::getBySjId($this->sjId);
  115. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj->attributes, $this->sjExtend->attributes, $merchantAsset);
  116. if ($return['code'] == 'A0001') {
  117. util::complete();
  118. }
  119. util::fail();
  120. }
  121. //升级 ssh 2019.12.1
  122. public function actionUpgrade()
  123. {
  124. $post = Yii::$app->request->post();
  125. $userId = isset($post['userId']) ? $post['userId'] : 0;
  126. $member = isset($post['member']) ? $post['member'] : 0;
  127. $user = UserService::getById($userId);
  128. if (empty($user)) {
  129. util::fail('客户不存在');
  130. }
  131. //验证是否商家客户
  132. UserService::valid($user, $this->sjId);
  133. //验证确认密码
  134. $payPassword = $post['payPassword'];
  135. AdminService::verifyPayPassword($this->adminId, $payPassword);
  136. $grade = xhMerchantExtendService::getGradeList($this->sjExtend->attributes, 'desc');
  137. //升级后的成长值
  138. $newLevelGrowth = $grade[$member]['growth'];
  139. $newDiscount = $grade[$member]['discount'];
  140. $userAsset = UserAssetService::getByUserId($userId);
  141. if ($userAsset['member'] >= $member) {
  142. util::fail('客户已经达到此级别了');
  143. }
  144. $now = time();
  145. $date = date("Y-m-d H:i", $now);
  146. $connection = Yii::$app->db;//事务处理
  147. $transaction = $connection->beginTransaction();
  148. try {
  149. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  150. $upData['growth'] = $newLevelGrowth;
  151. $upData['member'] = $member;
  152. $upData['discount'] = $newDiscount;
  153. UserAssetService::updateByUserId($userId, $upData);
  154. UserService::updateById($userId, ['member' => $member]);
  155. $upgradeData = [
  156. 'member' => $member,
  157. 'prevMember' => $userAsset['member'],
  158. 'sjId' => $this->sjId,
  159. 'gainType' => 0,
  160. 'addTime' => $now,
  161. 'userId' => $userId,
  162. ];
  163. $upgradeRespond = UserUpgradeService::add($upgradeData);
  164. $growthData = [
  165. 'userId' => $userId,
  166. 'userName' => $user['userName'],
  167. 'sjId' => $this->sjId,
  168. 'relateId' => $upgradeRespond['id'],
  169. 'growth' => $newLevelGrowth,
  170. 'num' => $addGrowth,
  171. 'io' => 1,
  172. 'payWay' => 4,
  173. 'alipayId' => '',
  174. 'event' => '管理员操作升到' . $member . '级增加成长值',
  175. 'operatorId' => $userId,
  176. 'createTime' => $date,
  177. 'gainType' => 0,
  178. 'addTime' => $now,
  179. ];
  180. UserGrowthService::add($growthData);
  181. $transaction->commit();
  182. } catch (Exception $e) {
  183. $transaction->rollBack();
  184. util::fail();
  185. }
  186. $allTM = xhTMessageService::getList($this->sjId);
  187. $openId = isset($user['openId']) ? $user['openId'] : '';
  188. if (empty($openId)) {
  189. util::complete();
  190. }
  191. $shortTempId = 'OPENTM205211943';//升级通知
  192. if (isset($allTM[$shortTempId]) == false) {
  193. util::complete();
  194. }
  195. $tempId = $allTM[$shortTempId];
  196. $data = [
  197. "touser" => $openId, "template_id" => $tempId, "url" => '',
  198. "data" => [
  199. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  200. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  201. "keyword2" => ["value" => $date, "color" => "#173177"],
  202. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  203. ]];
  204. wxUtil::sendTaskInform($data, $this->sj);
  205. util::complete();
  206. }
  207. //重置客户密码
  208. public function actionResetPayPassword()
  209. {
  210. $post = Yii::$app->request->post();
  211. $userId = isset($post['userId']) ? $post['userId'] : 0;
  212. $password = isset($post['password']) ? $post['password'] : 0;
  213. $info = UserService::getById($userId);
  214. UserService::valid($info, $this->sjId);
  215. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  216. util::complete();
  217. }
  218. //获取登陆客户的资产 ssh 2019.11.22
  219. public function actionAsset()
  220. {
  221. $asset = UserAssetService::getByUserId($this->adminId);
  222. util::success($asset);
  223. }
  224. //获取登陆客户的优惠情况 ssh 2019.12.3
  225. public function actionDiscount()
  226. {
  227. $discount = UserAssetService::getDiscount($this->adminId);
  228. util::success($discount);
  229. }
  230. //小程序用户完整信息 ssh 2019.12.12
  231. public function actionMiniFullInfo()
  232. {
  233. $post = Yii::$app->request->post();
  234. $iv = isset($post['iv']) ? $post['iv'] : '';
  235. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  236. $appId = $this->sj['miniAppId'];
  237. $sjId = $this->sjId;
  238. $miniOpenId = $this->user['miniOpenId'];
  239. if (empty($miniOpenId)) {
  240. util::fail('mini_open_id empty');
  241. }
  242. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  243. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  244. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  245. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  246. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  247. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  248. if ($errCode != 0) {
  249. Yii::info($result . ' ' . $errCode);
  250. util::fail('获取用户信息失败');
  251. }
  252. Yii::info('小程序获取用户信息:' . $result);
  253. $originalInfo = Json::decode($result);
  254. $source = UserClass::$userSourceId['mini']['name'];
  255. $user = UserService::replaceUser($originalInfo, $source, $sjId);
  256. $userId = $user['id'];
  257. $userInfo = UserService::getUserInfo($userId);
  258. util::success($userInfo);
  259. }
  260. //小程序用户手机号
  261. public function actionMiniMobile()
  262. {
  263. $post = Yii::$app->request->post();
  264. $iv = $post['iv'];
  265. $encryptedData = $post['encryptedData'];
  266. $sjId = $this->sjId;
  267. $merchant = $this->sj->attributes;
  268. $appId = $merchant['miniAppId'];
  269. $miniOpenId = $this->user['miniOpenId'];
  270. $user = $this->user;
  271. if (!empty($user) && !empty($user['mobile'])) {
  272. $userId = $user['id'];
  273. util::success(['mobile' => $user['mobile']]);
  274. }
  275. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  276. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  277. if (empty($sessionKey)) {
  278. util::fail('系统错误,sesstion empty');
  279. }
  280. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  281. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  282. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  283. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  284. if ($errCode != 0) {
  285. util::fail('解密失败');
  286. }
  287. $arr = Json::decode($result);
  288. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  289. if (empty($mobile)) {
  290. util::fail('没有获取手机号,请重试');
  291. }
  292. $userId = $user['id'];
  293. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  294. util::success(['mobile' => $mobile]);
  295. }
  296. //密码修改
  297. public function actionPassword()
  298. {
  299. $post = Yii::$app->request->post();
  300. $old = isset($post['old']) ? $post['old'] : '';
  301. $new = isset($post['new']) ? $post['new'] : '';
  302. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  303. if (empty($new)) {
  304. util::fail('请输入新密码');
  305. }
  306. if ($new != $confirm) {
  307. util::fail('二次密码不一致');
  308. }
  309. $userId = $this->adminId;
  310. $user = UserService::getUserInfo($userId);
  311. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  312. if (empty($old)) {
  313. util::fail('请填写旧密码');
  314. }
  315. if (password_verify($old, $user['payPassword']) == false) {
  316. util::fail('旧密码错误');
  317. }
  318. }
  319. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  320. util::complete('修改成功');
  321. }
  322. //申请会员 ssh 2019.12.18
  323. public function actionApplyMember()
  324. {
  325. $post = Yii::$app->request->post();
  326. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  327. if (stringUtil::isMobile($mobile) == false) {
  328. util::fail('请输入手机号');
  329. }
  330. $userId = $this->adminId;
  331. $asset = UserAssetService::getByUserId($userId);
  332. if (isset($asset['member']) && $asset['member'] != -1) {
  333. util::fail('已经是会员了');
  334. }
  335. $code = isset($post['code']) ? $post['code'] : '';
  336. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  337. if (empty($cacheCode) || $cacheCode != $code) {
  338. util::fail('验证码错误');
  339. }
  340. $update = [];
  341. if (isset($post['realName']) && !empty($post['realName'])) {
  342. $update['realName'] = $post['realName'];
  343. }
  344. if (isset($post['birthday']) && !empty($post['birthday'])) {
  345. $update['birthday'] = $post['birthday'];
  346. }
  347. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  348. if ($userId == $user['id']) {
  349. util::fail('手机号已验证');
  350. }
  351. if (!empty($user)) {
  352. util::fail('手机号已经注册过了');
  353. }
  354. $update['mobile'] = $mobile;
  355. UserService::becomeMember($this->user, $this->sj->attributes, $update);
  356. util::complete('注册成功');
  357. }
  358. //用户基本信息和资产 ssh 2019.12.19
  359. public function actionMyProfile()
  360. {
  361. $custom = $this->custom;
  362. $user = UserService::getUserInfo($this->adminId);
  363. $growth = $custom['growth'] ?? 0;
  364. $extend = $this->sjExtend->attributes;
  365. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  366. //下个等级的积分
  367. $nextLevelGrowth = 0;
  368. foreach ($grade as $val) {
  369. $currentGrowth = $val['growth'];
  370. if ($currentGrowth > $growth) {
  371. $nextLevelGrowth = $currentGrowth;
  372. break;
  373. }
  374. }
  375. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  376. util::success($user);
  377. }
  378. //登陆客户的信息 ssh 2020.3.12
  379. public function actionLoginDetail()
  380. {
  381. $user = $this->user;
  382. util::success($user);
  383. }
  384. }