CustomController.php 14 KB

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